Skip to content

Commit

Permalink
chore: add tests to ensure there are no regressions
Browse files Browse the repository at this point in the history
Adds a test suite that just verifies every model solution.
  • Loading branch information
achingbrain committed Nov 10, 2024
1 parent b70aeac commit 5d7bac8
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 1 deletion.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
},
"scripts": {
"lint": "standard",
"postinstall": "patch-package"
"postinstall": "patch-package",
"test": "mocha test/*.spec.js"
},
"dependencies": {
"@chainsafe/libp2p-yamux": "^7.0.1",
Expand All @@ -37,6 +38,7 @@
"workshopper-wrappedexec": "^0.1.1"
},
"devDependencies": {
"mocha": "^10.8.2",
"pre-commit": "^1.2.2",
"standard": "^17.1.0"
},
Expand Down
17 changes: 17 additions & 0 deletions patches/workshopper-adventure+7.0.0.patch
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
diff --git a/node_modules/workshopper-adventure/index.js b/node_modules/workshopper-adventure/index.js
index 7e85fe3..0538dbe 100644
--- a/node_modules/workshopper-adventure/index.js
+++ b/node_modules/workshopper-adventure/index.js
@@ -79,8 +79,10 @@ function WA (options) {

this.options = options

- var globalStorage = storage(storage.userDir, '.config', 'workshopper')
- this.appStorage = storage(storage.userDir, '.config', options.name)
+ const storageDir = process.env.WORKSHOPPER_ADVENTURE_STORAGE_DIR ?? storage.userDir
+
+ var globalStorage = storage(storageDir, '.config', 'workshopper')
+ this.appStorage = storage(storageDir, '.config', options.name)

this.exercises = []
this._meta = {}
diff --git a/node_modules/workshopper-adventure/lib/msee.js b/node_modules/workshopper-adventure/lib/msee.js
index 1fcbf99..605c566 100644
--- a/node_modules/workshopper-adventure/lib/msee.js
Expand Down
85 changes: 85 additions & 0 deletions test/exercises.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/* eslint-env mocha */

const { spawn } = require('node:child_process')
const path = require('node:path')
const os = require('node:os')
const fs = require('node:fs')
const menu = require('../exercises/menu.json')

function makeConfigDir () {
const dir = path.join(os.tmpdir(), 'protocol-adventure-test', `dir-${Math.random()}`)
fs.mkdirSync(dir, {
recursive: true
})

return dir
}

function selectLanguage (selected, dir) {
const containingDir = path.join(dir, '.config', 'workshopper')
fs.mkdirSync(containingDir, {
recursive: true
})
fs.writeFileSync(path.join(containingDir, 'lang.json'), `${JSON.stringify({ selected }, 0, 2)}\n`)
}

function selectExercise (exercise, dir) {
const containingDir = path.join(dir, '.config', '@libp2p/protocol-adventure')
fs.mkdirSync(containingDir, {
recursive: true
})
fs.writeFileSync(path.join(containingDir, 'current.json'), `${JSON.stringify(exercise)}\n`)
}

describe('protocol-adventure', () => {
let configDir

before(() => {
configDir = makeConfigDir()
selectLanguage('en', configDir)
})

beforeEach(() => {

})

for (const exercise of menu) {
it(`should pass ${exercise}`, async () => {
selectExercise(exercise, configDir)

const verify = spawn('protocol-adventure', ['verify', `exercises/${exercise}/solution.mjs`], {
env: {
...process.env,
WORKSHOPPER_ADVENTURE_STORAGE_DIR: configDir
}
})

let stdout = ''
let stderr = ''

verify.stdout.on('data', (data) => {
stdout += data.toString()
})

verify.stderr.on('data', (data) => {
stderr += data.toString()
})

await new Promise((resolve, reject) => {
verify.on('close', (code) => {
if (code === 0) {
resolve()
} else {
console.error('--- STDOUT ---')
console.error(stdout)

console.error('--- STDERR ---')
console.error(stderr)

reject(new Error(`Failed to verify solution to ${exercise} - child process exited with code ${code}`))
}
})
})
})
}
})

0 comments on commit 5d7bac8

Please sign in to comment.