Make reference dumping easier during testing

This commit is contained in:
Bán Dénes 2022-02-27 18:19:50 +01:00
parent b928cbd35d
commit e13927d050

View file

@ -8,7 +8,9 @@ const ergogen = require('../src/ergogen')
require('./helpers/mock_footprints').inject(ergogen) require('./helpers/mock_footprints').inject(ergogen)
let what = process.env.npm_config_what let what = process.env.npm_config_what
let dump = process.env.npm_config_dump const dump = process.env.npm_config_dump
// Unit tests // Unit tests
// the --what switch supports each unit individually // the --what switch supports each unit individually
@ -21,12 +23,12 @@ for (const unit of glob.sync(path.join(__dirname, 'unit', '*.js'))) {
require(`./unit/${base}.js`) require(`./unit/${base}.js`)
} }
// Integration tests // Integration tests
// the --what switch supports categories (like `points` and `outlines`) // the --what switch supports categories (like `points` and `outlines`)
// as well as individual tests using slash-notation (like `points/000`) // as well as individual tests using slash-notation (like `points/000`)
// the --dump switch can output actual results for easier reference creation // the --dump switch can output the new results, overriding the old reference
// by default, json output is generated of the whole `actual`, but a raw,
// type-specific representation can be written if a deep path is specified
const cap = s => s.charAt(0).toUpperCase() + s.slice(1) const cap = s => s.charAt(0).toUpperCase() + s.slice(1)
@ -36,33 +38,9 @@ const test = function(input_path) {
title = path.basename(input_path, '.yaml').split('_').join(' ') title = path.basename(input_path, '.yaml').split('_').join(' ')
it(title, async function() { it(title, async function() {
const input = yaml.load(fs.readFileSync(input_path).toString()) const input = yaml.load(fs.readFileSync(input_path).toString())
const actual = await ergogen.process(input, true) const output = await ergogen.process(input, true)
// if we're just creating the reference, we can dump the current output // compare output vs. reference
if (dump) {
// whole dump
if (dump === true || dump === 'true') {
const out = path.join(
path.dirname(input_path),
path.basename(input_path, '.yaml') + '___ref_candidate.json'
)
fs.writeJSONSync(out, actual, {spaces: 4})
// partial, type-specific dump
} else {
const part = u.deep(actual, dump)
const out = path.join(
path.dirname(input_path),
path.basename(input_path, '.yaml') + '___' + dump.split('.').join('_')
)
if (a.type(part)() == 'string') {
fs.writeFileSync(out + '.txt', part)
} else {
fs.writeJSONSync(out + '.json', part, {spaces: 4})
}
}
}
// compare actual vs. reference
const base = path.join(path.dirname(input_path), path.basename(input_path, '.yaml')) const base = path.join(path.dirname(input_path), path.basename(input_path, '.yaml'))
for (const expected_path of glob.sync(base + '___*')) { for (const expected_path of glob.sync(base + '___*')) {
let expected = fs.readFileSync(expected_path).toString() let expected = fs.readFileSync(expected_path).toString()
@ -70,7 +48,15 @@ const test = function(input_path) {
expected = JSON.parse(expected) expected = JSON.parse(expected)
} }
const comp_path = expected_path.split('___')[1].split('.')[0].split('_').join('.') const comp_path = expected_path.split('___')[1].split('.')[0].split('_').join('.')
u.deep(actual, comp_path).should.deep.equal(expected) const output_part = u.deep(output, comp_path)
if (dump) {
if (a.type(output_part)() == 'string') {
fs.writeFileSync(expected_path, output_part)
} else {
fs.writeJSONSync(expected_path, output_part, {spaces: 4})
}
}
output_part.should.deep.equal(expected)
} }
}) })
} }
@ -102,9 +88,11 @@ if (what) {
} }
} }
// End-to-end tests to actually drive the CLI as well // End-to-end tests to actually drive the CLI as well
// --what filter is the same as above ('cli', or 'cli/prefix') // --what is the same as above ('cli', or 'cli/prefix')
// --dump saves the log, as well as prevents the output from being deleted // --dump automatically overrides the old reference
const read = (d, p) => fs.readFileSync(path.join(d, p)).toString() const read = (d, p) => fs.readFileSync(path.join(d, p)).toString()
const exists = (d, p) => fs.existsSync(path.join(d, p)) const exists = (d, p) => fs.existsSync(path.join(d, p))
@ -132,18 +120,18 @@ for (let w of cli_what) {
if (dump) { if (dump) {
fs.writeFileSync(path.join(t, 'log'), actual_log) fs.writeFileSync(path.join(t, 'log'), actual_log)
} }
actual_log.should.equal(ref_log)
const comp_res = dircompare.compareSync(output_path, path.join(t, 'reference'), { const comp_res = dircompare.compareSync(output_path, path.join(t, 'reference'), {
compareContent: true compareContent: true
}) })
if (dump) { if (dump) {
fs.moveSync(output_path, path.join(t, 'output'), {overwrite: true}) fs.moveSync(output_path, path.join(t, 'reference'), {overwrite: true})
} else { } else {
fs.removeSync(output_path) fs.removeSync(output_path)
} }
actual_log.should.equal(ref_log)
comp_res.same.should.be.true comp_res.same.should.be.true
} else { } else {
const ref_error = read(t, 'error').replace(version_regex, '<version>') const ref_error = read(t, 'error')
try { try {
execSync(command, {stdio: 'pipe'}) execSync(command, {stdio: 'pipe'})
throw 'should_have_thrown' throw 'should_have_thrown'
@ -151,7 +139,7 @@ for (let w of cli_what) {
if (ex === 'should_have_thrown') { if (ex === 'should_have_thrown') {
throw new Error('This command should have thrown!') throw new Error('This command should have thrown!')
} }
const actual_error = ex.stderr.toString().replace(version_regex, '<version>') const actual_error = ex.stderr.toString().split('\n')[0]
if (dump) { if (dump) {
fs.writeFileSync(path.join(t, 'error'), actual_error) fs.writeFileSync(path.join(t, 'error'), actual_error)
} }