Smarter dump switch for the test runner, reference adjustments

This commit is contained in:
Bán Dénes 2021-07-14 19:35:47 +02:00
parent 42a3e2de55
commit d09b3fdf38
3 changed files with 321 additions and 513 deletions

View file

@ -3,15 +3,16 @@ const path = require('path')
const yaml = require('js-yaml')
const glob = require('glob')
const u = require('../src/utils')
const a = require('../src/assert')
const ergogen = require('../src/ergogen')
require('./helpers/mock_footprints').inject(ergogen)
let what = process.env.npm_config_what
let first = process.env.npm_config_first
let dump = process.env.npm_config_dump
// Unit tests
// the --what switch supports each unit individually
// the --first switch does nothing here
// the --dump switch does nothing here
what = what ? what.split(',') : false
for (const unit of glob.sync(path.join(__dirname, 'unit', '*.js'))) {
@ -23,7 +24,9 @@ for (const unit of glob.sync(path.join(__dirname, 'unit', '*.js'))) {
// Integration tests
// the --what switch supports categories (like `points` and `outlines`)
// as well as individual tests using slash-notation (like `points/000`)
// the --first switch outputs the actual results for easier reference creation
// the --dump switch can output actual results for easier reference creation
// 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)
@ -33,13 +36,34 @@ const test = function(input_path) {
it(title, function() {
const input = yaml.load(fs.readFileSync(input_path).toString())
const actual = ergogen.process(input, true)
if (first) {
const out = path.join(path.dirname(input_path), path.basename(input_path, '.yaml') + '___actual.json')
fs.writeJSONSync(out, actual, {spaces: 4})
// if we're just creating the reference, we can dump the current output
if (dump) {
const out = path.join(
path.dirname(input_path),
path.basename(input_path, '.yaml') + '___ref_candidate'
)
// whole dump
if (dump === true) {
fs.writeJSONSync(out + '.json', actual, {spaces: 4})
// partial, type-specific dump
} else {
const part = u.deep(actual, dump)
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'))
for (const expected_path of glob.sync(base + '___*')) {
const expected = JSON.parse(fs.readFileSync(expected_path).toString())
let expected = fs.readFileSync(expected_path).toString()
if (expected_path.endsWith('.json')) {
expected = JSON.parse(expected)
}
const comp_path = expected_path.split('___')[1].split('.')[0].split('_').join('.')
u.deep(actual, comp_path).should.deep.equal(expected)
}