Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 52 additions & 31 deletions bin/ota-validate.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#! /usr/bin/env node
import './env.js';

import path from 'path';
import { fileURLToPath } from 'url';

Expand All @@ -9,49 +8,71 @@ import Mocha from 'mocha';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const VALIDATE_TEST_FILEPATH = '../scripts/declarations/validate/index.mocha.js';
const VALIDATE_PATH = path.resolve(__dirname, VALIDATE_TEST_FILEPATH);
export function createMocha({ delay = false, reporter = 'spec' } = {}) {
return new Mocha({
delay,
failZero: true,
reporter,
});
}

export async function runMochaTests(mocha, testPath) {
try {
mocha.addFile(testPath); // With `delay` option, this statement will not load the file directly, `loadFilesAsync` is required.
await mocha.loadFilesAsync(); // Load files previously added to the Mocha cache with `addFile`.

return new Promise(resolve => {
let hasFailedTests = false;

mocha.run()
.on('fail', () => { hasFailedTests = true; })
.on('end', () => { resolve(hasFailedTests ? 1 : 0); });
});
} catch (error) {
console.error('Error running tests:', error);

return 2;
}
}

// Mocha catches unhandled rejection from the user code and re-emits them to the process (see https://github.com/mochajs/mocha/blob/master/lib/runner.js#L198)
process.on('unhandledRejection', reason => {
// Re-throw them so that the validation command fails in these cases (for example, if there is a syntax error when parsing JSON declaration files)
throw reason;
process.on('unhandledRejection', reason => { // Mocha catches unhandled rejection from the user code and re-emits them to the process (see https://github.com/mochajs/mocha/blob/master/lib/runner.js#L198)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The line number #L198 referred to here does not agree with the version of Mocha used and there is no guarantee that this link will work over time.

throw reason; // Re-throw them so that the validation command fails in these cases (for example, if there is a syntax error when parsing JSON declaration files)
});

program
.name('ota validate')
.description('Validate terms declarations and metadata files');

program.command('declarations')
.description('Run a series of tests to check the validity of terms declarations')
.option('-s, --services [serviceId...]', 'service IDs of services to validate')
.option('-t, --types [termsType...]', 'terms types to validate')
.option('-m, --modified', 'target only services modified in the current git branch')
.option('-o, --schema-only', 'much faster check of declarations, but does not check that the documents are actually accessible');
.option('-o, --schema-only', 'much faster check of declarations, but does not check that the documents are actually accessible')
.action(async options => {
const VALIDATE_TEST_FILEPATH = '../scripts/declarations/validate/index.mocha.js';
const VALIDATE_PATH = path.resolve(__dirname, VALIDATE_TEST_FILEPATH);

const mocha = new Mocha({
delay: true, // as the validation script performs an asynchronous load before running the tests, the execution of the tests are delayed until run() is called
failZero: true, // consider that being called with no service to validate is a failure
});
const mocha = createMocha({ delay: true }); // as the validation script performs an asynchronous load before running the tests, the execution of the tests are delayed until run() is called
const generateValidationTestSuite = (await import(VALIDATE_TEST_FILEPATH)).default;

(async () => {
mocha.addFile(VALIDATE_PATH); // As `delay` has been called, this statement will not load the file directly, `loadFilesAsync` is required.
await mocha.loadFilesAsync() // Load files previously added to the Mocha cache with `addFile`.
.catch(error => {
console.error(error);
process.exit(2);
});
generateValidationTestSuite(options);

let hasFailedTests = false;
const exitCode = await runMochaTests(mocha, VALIDATE_PATH);

const generateValidationTestSuite = (await import(VALIDATE_TEST_FILEPATH)).default;
process.exit(exitCode);
});

generateValidationTestSuite(program.parse().opts());
program.command('metadata')
.description('Validate the metadata file structure')
.action(async () => {
const VALIDATE_TEST_FILEPATH = '../scripts/metadata/index.mocha.js';
const VALIDATE_PATH = path.resolve(__dirname, VALIDATE_TEST_FILEPATH);

mocha.run()
.on('fail', () => { hasFailedTests = true; })
.on('end', () => {
if (hasFailedTests) {
process.exit(1);
}
const mocha = createMocha();
const exitCode = await runMochaTests(mocha, VALIDATE_PATH);

process.exit(0);
});
})();
process.exit(exitCode);
});

program.parse();