Skip to content

Commit 37c33f4

Browse files
committed
Use semver ranges
1 parent 04a5245 commit 37c33f4

27 files changed

+147
-245
lines changed

Gulpfile.js

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@ const baselineAccept = require("./scripts/build/baselineAccept");
2424
const cmdLineOptions = require("./scripts/build/options");
2525
const exec = require("./scripts/build/exec");
2626
const browserify = require("./scripts/build/browserify");
27-
const debounce = require("./scripts/build/debounce");
2827
const prepend = require("./scripts/build/prepend");
2928
const { removeSourceMaps } = require("./scripts/build/sourcemaps");
30-
const { CancelSource, CancelError } = require("./scripts/build/cancellation");
29+
const { CancellationTokenSource, CancelError, delay, Semaphore } = require("prex");
3130
const { libraryTargets, generateLibs } = require("./scripts/build/lib");
3231
const { runConsoleTests, cleanTestDirs, writeTestConfigFile, refBaseline, localBaseline, refRwcBaseline, localRwcBaseline } = require("./scripts/build/tests");
3332

@@ -556,35 +555,61 @@ gulp.task(
556555
"Watches for changes to the build inputs for built/local/run.js, then executes runtests-parallel.",
557556
["build-rules", "watch-runner", "watch-services", "watch-lssl"],
558557
() => {
559-
/** @type {CancelSource | undefined} */
560-
let runTestsSource;
561-
562-
const fn = debounce(() => {
563-
runTests().catch(error => {
564-
if (error instanceof CancelError) {
558+
const runTestsSemaphore = new Semaphore(1);
559+
const fn = async () => {
560+
try {
561+
// Ensure only one instance of the test runner is running at any given time.
562+
if (runTestsSemaphore.count > 0) {
563+
await runTestsSemaphore.wait();
564+
try {
565+
// Wait for any concurrent recompilations to complete...
566+
try {
567+
await delay(100);
568+
while (project.hasRemainingWork()) {
569+
await project.waitForWorkToComplete();
570+
await delay(500);
571+
}
572+
}
573+
catch (e) {
574+
if (e instanceof CancelError) return;
575+
throw e;
576+
}
577+
578+
// cancel any pending or active test run if a new recompilation is triggered
579+
const runTestsSource = new CancellationTokenSource();
580+
project.waitForWorkToStart().then(() => {
581+
runTestsSource.cancel();
582+
});
583+
584+
if (cmdLineOptions.tests || cmdLineOptions.failed) {
585+
await runConsoleTests(runJs, "mocha-fivemat-progress-reporter", /*runInParallel*/ false, /*watchMode*/ true, runTestsSource.token);
586+
}
587+
else {
588+
await runConsoleTests(runJs, "min", /*runInParallel*/ true, /*watchMode*/ true, runTestsSource.token);
589+
}
590+
}
591+
finally {
592+
runTestsSemaphore.release();
593+
}
594+
}
595+
}
596+
catch (e) {
597+
if (e instanceof CancelError) {
565598
log.warn("Operation was canceled");
566599
}
567600
else {
568-
log.error(error);
601+
log.error(e);
569602
}
570-
});
571-
}, /*timeout*/ 100, { max: 500 });
603+
}
604+
};
572605

573-
gulp.watch(watchPatterns, () => project.wait(runTestsSource && runTestsSource.token).then(fn));
606+
gulp.watch(watchPatterns, (e) => fn());
574607

575608
// NOTE: gulp.watch is far too slow when watching tests/cases/**/* as it first enumerates *every* file
576609
const testFilePattern = /(\.ts|[\\/]tsconfig\.json)$/;
577610
fs.watch("tests/cases", { recursive: true }, (_, file) => {
578-
if (testFilePattern.test(file)) project.wait(runTestsSource && runTestsSource.token).then(fn);
611+
if (testFilePattern.test(file)) fn();
579612
});
580-
581-
function runTests() {
582-
if (runTestsSource) runTestsSource.cancel();
583-
runTestsSource = new CancelSource();
584-
return cmdLineOptions.tests || cmdLineOptions.failed
585-
? runConsoleTests(runJs, "mocha-fivemat-progress-reporter", /*runInParallel*/ false, /*watchMode*/ true, runTestsSource.token)
586-
: runConsoleTests(runJs, "min", /*runInParallel*/ true, /*watchMode*/ true, runTestsSource.token);
587-
}
588613
});
589614

590615
gulp.task("clean-built", /*help*/ false, [`clean:${diagnosticInformationMapTs}`], () => del(["built"]));

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
"mocha": "latest",
8282
"mocha-fivemat-progress-reporter": "latest",
8383
"plugin-error": "latest",
84+
"prex": "^0.4.3",
8485
"q": "latest",
8586
"remove-internal": "^2.9.2",
8687
"run-sequence": "latest",

scripts/build/cancellation.js

Lines changed: 0 additions & 71 deletions
This file was deleted.

scripts/build/countdown.js

Lines changed: 0 additions & 62 deletions
This file was deleted.

scripts/build/exec.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const cp = require("child_process");
33
const log = require("fancy-log"); // was `require("gulp-util").log (see https://github.com/gulpjs/gulp-util)
44
const isWin = /^win/.test(process.platform);
55
const chalk = require("./chalk");
6-
const { CancelToken, CancelError } = require("./cancellation");
6+
const { CancelError } = require("prex");
77

88
module.exports = exec;
99

@@ -15,24 +15,28 @@ module.exports = exec;
1515
*
1616
* @typedef ExecOptions
1717
* @property {boolean} [ignoreExitCode]
18-
* @property {CancelToken} [cancelToken]
18+
* @property {import("prex").CancellationToken} [cancelToken]
1919
*/
2020
function exec(cmd, args, options = {}) {
2121
return /**@type {Promise<{exitCode: number}>}*/(new Promise((resolve, reject) => {
22+
if (options.cancelToken) {
23+
options.cancelToken.throwIfCancellationRequested();
24+
}
25+
2226
log(`> ${chalk.green(cmd)} ${args.join(" ")}`);
2327
// TODO (weswig): Update child_process types to add windowsVerbatimArguments to the type definition
2428
const subshellFlag = isWin ? "/c" : "-c";
2529
const command = isWin ? [possiblyQuote(cmd), ...args] : [`${cmd} ${args.join(" ")}`];
2630
const ex = cp.spawn(isWin ? "cmd" : "/bin/sh", [subshellFlag, ...command], { stdio: "inherit", windowsVerbatimArguments: true });
27-
const subscription = options.cancelToken && options.cancelToken.subscribe(() => {
31+
const subscription = options.cancelToken && options.cancelToken.register(() => {
2832
log(`${chalk.red("killing")} '${chalk.green(cmd)} ${args.join(" ")}'...`);
2933
ex.kill("SIGINT");
3034
ex.kill("SIGTERM");
3135
ex.kill();
3236
reject(new CancelError());
3337
});
3438
ex.on("exit", exitCode => {
35-
subscription && subscription.unsubscribe();
39+
if (subscription) subscription.unregister();
3640
if (exitCode === 0 || options.ignoreExitCode) {
3741
resolve({ exitCode });
3842
}
@@ -41,7 +45,7 @@ function exec(cmd, args, options = {}) {
4145
}
4246
});
4347
ex.on("error", error => {
44-
subscription && subscription.unsubscribe();
48+
if (subscription) subscription.unregister();
4549
reject(error);
4650
});
4751
}));

0 commit comments

Comments
 (0)