(inputRoot: string, outputRoot: string, filter: (name: string) => boolean = () => true)
| 22 | } |
| 23 | |
| 24 | function runTests(inputRoot: string, outputRoot: string, filter: (name: string) => boolean = () => true) { |
| 25 | for (const name of readdirSync(inputRoot)) { |
| 26 | if (!isJsFile(inputRoot, name) || !filter(name)) continue; |
| 27 | const only = name.startsWith("only."); |
| 28 | const skip = name.startsWith("skip."); |
| 29 | const outname = only || skip ? name.slice(5) : name; |
| 30 | const path = join(inputRoot, name); |
| 31 | (only ? it.only : skip ? it.skip : it)(path, async () => { |
| 32 | const outfile = resolve(outputRoot, `${outname}.json`); |
| 33 | const diffile = resolve(outputRoot, `${outname}-changed.json`); |
| 34 | const input = await readFile(path, "utf8"); |
| 35 | let actual: string; |
| 36 | let expected: string; |
| 37 | |
| 38 | try { |
| 39 | actual = JSON.stringify(redactJavaScript(parseJavaScript(input, {path: name})), stringify, 2); |
| 40 | } catch (error) { |
| 41 | if (!(error instanceof SyntaxError)) throw error; |
| 42 | actual = JSON.stringify({error: error.message}, null, 2); |
| 43 | } |
| 44 | |
| 45 | try { |
| 46 | expected = await readFile(outfile, "utf8"); |
| 47 | } catch (error) { |
| 48 | if (!isEnoent(error) || process.env.CI === "true") throw error; |
| 49 | console.warn(`! generating ${outfile}`); |
| 50 | await mkdir(outputRoot, {recursive: true}); |
| 51 | await writeFile(outfile, actual, "utf8"); |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | const equal = expected === actual; |
| 56 | |
| 57 | if (equal) { |
| 58 | if (process.env.CI !== "true") { |
| 59 | try { |
| 60 | await unlink(diffile); |
| 61 | console.warn(`! deleted ${diffile}`); |
| 62 | } catch (error) { |
| 63 | if (!isEnoent(error)) throw error; |
| 64 | } |
| 65 | } |
| 66 | } else { |
| 67 | console.warn(`! generating ${diffile}`); |
| 68 | await writeFile(diffile, actual, "utf8"); |
| 69 | } |
| 70 | |
| 71 | assert.ok(equal, `${name} must match snapshot`); |
| 72 | }); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | describe("parseJavaScript(input, options)", () => { |
| 77 | runTests("test/input", "test/output"); |
no test coverage detected