-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add tests to ensure there are no regressions
Adds a test suite that just verifies every model solution.
- Loading branch information
1 parent
b70aeac
commit 5d7bac8
Showing
10 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`)) | ||
} | ||
}) | ||
}) | ||
}) | ||
} | ||
}) |