From cf649c720855501a8a6b7921abc1c1ca61e12025 Mon Sep 17 00:00:00 2001 From: Chris Deacy Date: Fri, 10 Jan 2025 10:18:46 -0800 Subject: [PATCH] Fix broken tests on main (#165) --- .vscode/settings.json | 7 ++ tests/fixtures/batch-test-group/.bazelrc | 3 + tests/fixtures/batch-test-group/.bazelversion | 1 + .../fixtures/bazel-dependent-builds/.bazelrc | 3 + .../bazel-dependent-builds/.bazelversion | 1 + .../fixtures/bazel-dependent-failure/.bazelrc | 3 + .../bazel-dependent-failure/.bazelversion | 1 + tests/fixtures/bazel-rules/.bazelrc | 3 + tests/fixtures/bazel-rules/.bazelversion | 1 + tests/fixtures/bin/.bazelrc | 3 + tests/fixtures/bin/.bazelversion | 1 + tests/fixtures/commands/.bazelrc | 3 + tests/fixtures/commands/.bazelversion | 1 + .../find-changed-targets/bazel/.bazelrc | 3 + .../find-changed-targets/bazel/.bazelversion | 1 + .../report-mismatched-top-level-deps/.bazelrc | 3 + .../.bazelversion | 1 + tests/fixtures/scaffold/.bazelrc | 3 + tests/fixtures/scaffold/.bazelversion | 1 + tests/fixtures/script/.bazelrc | 3 + tests/fixtures/script/.bazelversion | 1 + tests/fixtures/yarn-pnp/.bazelrc | 3 + tests/fixtures/yarn-pnp/.bazelversion | 1 + tests/index.js | 77 ++++++++++++++++--- 24 files changed, 118 insertions(+), 10 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 tests/fixtures/batch-test-group/.bazelrc create mode 100644 tests/fixtures/batch-test-group/.bazelversion create mode 100644 tests/fixtures/bazel-dependent-builds/.bazelrc create mode 100644 tests/fixtures/bazel-dependent-builds/.bazelversion create mode 100644 tests/fixtures/bazel-dependent-failure/.bazelrc create mode 100644 tests/fixtures/bazel-dependent-failure/.bazelversion create mode 100644 tests/fixtures/bazel-rules/.bazelrc create mode 100644 tests/fixtures/bazel-rules/.bazelversion create mode 100644 tests/fixtures/bin/.bazelrc create mode 100644 tests/fixtures/bin/.bazelversion create mode 100644 tests/fixtures/commands/.bazelrc create mode 100644 tests/fixtures/commands/.bazelversion create mode 100644 tests/fixtures/find-changed-targets/bazel/.bazelrc create mode 100644 tests/fixtures/find-changed-targets/bazel/.bazelversion create mode 100644 tests/fixtures/report-mismatched-top-level-deps/.bazelrc create mode 100644 tests/fixtures/report-mismatched-top-level-deps/.bazelversion create mode 100644 tests/fixtures/scaffold/.bazelrc create mode 100644 tests/fixtures/scaffold/.bazelversion create mode 100644 tests/fixtures/script/.bazelrc create mode 100644 tests/fixtures/script/.bazelversion create mode 100644 tests/fixtures/yarn-pnp/.bazelrc create mode 100644 tests/fixtures/yarn-pnp/.bazelversion diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..937325a --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "javascript.validate.enable": true, + "editor.codeActionsOnSave": { + "source.fixAll.eslint": "explicit" + }, + "editor.formatOnSave": true +} diff --git a/tests/fixtures/batch-test-group/.bazelrc b/tests/fixtures/batch-test-group/.bazelrc new file mode 100644 index 0000000..f794280 --- /dev/null +++ b/tests/fixtures/batch-test-group/.bazelrc @@ -0,0 +1,3 @@ +# Disable Bzlmod +common --noenable_bzlmod +common --enable_workspace diff --git a/tests/fixtures/batch-test-group/.bazelversion b/tests/fixtures/batch-test-group/.bazelversion new file mode 100644 index 0000000..a8a1887 --- /dev/null +++ b/tests/fixtures/batch-test-group/.bazelversion @@ -0,0 +1 @@ +7.1.2 diff --git a/tests/fixtures/bazel-dependent-builds/.bazelrc b/tests/fixtures/bazel-dependent-builds/.bazelrc new file mode 100644 index 0000000..f794280 --- /dev/null +++ b/tests/fixtures/bazel-dependent-builds/.bazelrc @@ -0,0 +1,3 @@ +# Disable Bzlmod +common --noenable_bzlmod +common --enable_workspace diff --git a/tests/fixtures/bazel-dependent-builds/.bazelversion b/tests/fixtures/bazel-dependent-builds/.bazelversion new file mode 100644 index 0000000..a8a1887 --- /dev/null +++ b/tests/fixtures/bazel-dependent-builds/.bazelversion @@ -0,0 +1 @@ +7.1.2 diff --git a/tests/fixtures/bazel-dependent-failure/.bazelrc b/tests/fixtures/bazel-dependent-failure/.bazelrc new file mode 100644 index 0000000..f794280 --- /dev/null +++ b/tests/fixtures/bazel-dependent-failure/.bazelrc @@ -0,0 +1,3 @@ +# Disable Bzlmod +common --noenable_bzlmod +common --enable_workspace diff --git a/tests/fixtures/bazel-dependent-failure/.bazelversion b/tests/fixtures/bazel-dependent-failure/.bazelversion new file mode 100644 index 0000000..a8a1887 --- /dev/null +++ b/tests/fixtures/bazel-dependent-failure/.bazelversion @@ -0,0 +1 @@ +7.1.2 diff --git a/tests/fixtures/bazel-rules/.bazelrc b/tests/fixtures/bazel-rules/.bazelrc new file mode 100644 index 0000000..f794280 --- /dev/null +++ b/tests/fixtures/bazel-rules/.bazelrc @@ -0,0 +1,3 @@ +# Disable Bzlmod +common --noenable_bzlmod +common --enable_workspace diff --git a/tests/fixtures/bazel-rules/.bazelversion b/tests/fixtures/bazel-rules/.bazelversion new file mode 100644 index 0000000..a8a1887 --- /dev/null +++ b/tests/fixtures/bazel-rules/.bazelversion @@ -0,0 +1 @@ +7.1.2 diff --git a/tests/fixtures/bin/.bazelrc b/tests/fixtures/bin/.bazelrc new file mode 100644 index 0000000..f794280 --- /dev/null +++ b/tests/fixtures/bin/.bazelrc @@ -0,0 +1,3 @@ +# Disable Bzlmod +common --noenable_bzlmod +common --enable_workspace diff --git a/tests/fixtures/bin/.bazelversion b/tests/fixtures/bin/.bazelversion new file mode 100644 index 0000000..a8a1887 --- /dev/null +++ b/tests/fixtures/bin/.bazelversion @@ -0,0 +1 @@ +7.1.2 diff --git a/tests/fixtures/commands/.bazelrc b/tests/fixtures/commands/.bazelrc new file mode 100644 index 0000000..f794280 --- /dev/null +++ b/tests/fixtures/commands/.bazelrc @@ -0,0 +1,3 @@ +# Disable Bzlmod +common --noenable_bzlmod +common --enable_workspace diff --git a/tests/fixtures/commands/.bazelversion b/tests/fixtures/commands/.bazelversion new file mode 100644 index 0000000..a8a1887 --- /dev/null +++ b/tests/fixtures/commands/.bazelversion @@ -0,0 +1 @@ +7.1.2 diff --git a/tests/fixtures/find-changed-targets/bazel/.bazelrc b/tests/fixtures/find-changed-targets/bazel/.bazelrc new file mode 100644 index 0000000..f794280 --- /dev/null +++ b/tests/fixtures/find-changed-targets/bazel/.bazelrc @@ -0,0 +1,3 @@ +# Disable Bzlmod +common --noenable_bzlmod +common --enable_workspace diff --git a/tests/fixtures/find-changed-targets/bazel/.bazelversion b/tests/fixtures/find-changed-targets/bazel/.bazelversion new file mode 100644 index 0000000..a8a1887 --- /dev/null +++ b/tests/fixtures/find-changed-targets/bazel/.bazelversion @@ -0,0 +1 @@ +7.1.2 diff --git a/tests/fixtures/report-mismatched-top-level-deps/.bazelrc b/tests/fixtures/report-mismatched-top-level-deps/.bazelrc new file mode 100644 index 0000000..f794280 --- /dev/null +++ b/tests/fixtures/report-mismatched-top-level-deps/.bazelrc @@ -0,0 +1,3 @@ +# Disable Bzlmod +common --noenable_bzlmod +common --enable_workspace diff --git a/tests/fixtures/report-mismatched-top-level-deps/.bazelversion b/tests/fixtures/report-mismatched-top-level-deps/.bazelversion new file mode 100644 index 0000000..a8a1887 --- /dev/null +++ b/tests/fixtures/report-mismatched-top-level-deps/.bazelversion @@ -0,0 +1 @@ +7.1.2 diff --git a/tests/fixtures/scaffold/.bazelrc b/tests/fixtures/scaffold/.bazelrc new file mode 100644 index 0000000..f794280 --- /dev/null +++ b/tests/fixtures/scaffold/.bazelrc @@ -0,0 +1,3 @@ +# Disable Bzlmod +common --noenable_bzlmod +common --enable_workspace diff --git a/tests/fixtures/scaffold/.bazelversion b/tests/fixtures/scaffold/.bazelversion new file mode 100644 index 0000000..a8a1887 --- /dev/null +++ b/tests/fixtures/scaffold/.bazelversion @@ -0,0 +1 @@ +7.1.2 diff --git a/tests/fixtures/script/.bazelrc b/tests/fixtures/script/.bazelrc new file mode 100644 index 0000000..f794280 --- /dev/null +++ b/tests/fixtures/script/.bazelrc @@ -0,0 +1,3 @@ +# Disable Bzlmod +common --noenable_bzlmod +common --enable_workspace diff --git a/tests/fixtures/script/.bazelversion b/tests/fixtures/script/.bazelversion new file mode 100644 index 0000000..a8a1887 --- /dev/null +++ b/tests/fixtures/script/.bazelversion @@ -0,0 +1 @@ +7.1.2 diff --git a/tests/fixtures/yarn-pnp/.bazelrc b/tests/fixtures/yarn-pnp/.bazelrc new file mode 100644 index 0000000..f794280 --- /dev/null +++ b/tests/fixtures/yarn-pnp/.bazelrc @@ -0,0 +1,3 @@ +# Disable Bzlmod +common --noenable_bzlmod +common --enable_workspace diff --git a/tests/fixtures/yarn-pnp/.bazelversion b/tests/fixtures/yarn-pnp/.bazelversion new file mode 100644 index 0000000..a8a1887 --- /dev/null +++ b/tests/fixtures/yarn-pnp/.bazelversion @@ -0,0 +1 @@ +7.1.2 diff --git a/tests/index.js b/tests/index.js index f703969..e28684d 100644 --- a/tests/index.js +++ b/tests/index.js @@ -67,9 +67,10 @@ const {shouldSync, getVersion} = require('../utils/version-onboarding.js'); const yarnCmds = require('../utils/yarn-commands.js'); const {sortPackageJson} = require('../utils/sort-package-json'); +const originalProcessExit = process.exit; process.on('unhandledRejection', e => { console.error(e.stack); - process.exit(1); + originalProcessExit(1); }); // $FlowFixMe flow can't handle statics of async function @@ -80,32 +81,88 @@ const tmp = tmpdir(); runTests(); async function t(test) { + const testName = test.name; const match = (process.argv[2] || '').toLowerCase(); - if (test.name.toLowerCase().indexOf(match) > -1) { + + if (testName.toLowerCase().indexOf(match) > -1) { if (match) console.log(`Testing ${test.name}`); try { await test(); + console.log(`✅ ${testName}`); } catch (e) { - console.error(`\n\nTest "${test.name}" failed with error:`); + console.log(`❌ ${testName}`); throw e; } } } +/** + * Uses an error stack trace to determine which test from this + * file caused a given function to be called. + */ +function getCallingTestFn() { + const error = new Error(); + if (!error.stack) return null; + + const errorStackLines = error.stack.split('\n'); + for (const line of errorStackLines) { + const trimmedLine = line.split('at ')[1]; + if (!trimmedLine) continue; + + const [, fnName, filePath] = + trimmedLine.match( + /^(?:async )?(?:(.+) \()?(?:file:\/\/)?([^:]+):(\d+):(\d+)\)?$/ + ) || []; + + if ( + filePath === __filename && + fnName.startsWith('test') && + fnName.length > 4 + ) { + return fnName; + } + } + + return null; +} + +const testsExpectingExit /*: Map */ = new Map(); +// $FlowFixMe +process.exit = (code = 0) => { + const callingTest = getCallingTestFn(); + if (callingTest && testsExpectingExit.has(callingTest)) { + testsExpectingExit.set(callingTest, code); + return; + } + + // $FlowFixMe: bad libdef + assert.fail(`'process.exit' was unexpectedly called with code '${code}'`); +}; + async function expectProcessExit(expectedExitCode, run) { + const callingTest = getCallingTestFn() || ''; + const shouldUseFallbackExitMock = !callingTest; + let exitCode = 0; - // $FlowFixMe const originalExit = process.exit; - // $FlowFixMe - process.exit = (code = 0) => { - exitCode = code; - }; + if (shouldUseFallbackExitMock) { + // $FlowFixMe + process.exit = (code = 0) => { + exitCode = code; + }; + } else { + testsExpectingExit.set(callingTest, 0); + } try { await run(); } finally { - // $FlowFixMe - process.exit = originalExit; + if (shouldUseFallbackExitMock) { + // $FlowFixMe + process.exit = originalExit; + } else { + exitCode = testsExpectingExit.get(callingTest) || 0; + } assert.strictEqual( exitCode,