Skip to content

Commit

Permalink
Abort sl root call if output resembles a steam locomotive (#15053)
Browse files Browse the repository at this point in the history
Jest detects whether a repository is a sapling repo by calling the `sl`
binary, and getting the output. If `sl` (steam locomotive) is installed,
the output of `sl root` 1) takes forever to get and 2) is not the root,
but a moving image of a steam locomotive. This change monitors the
stdout stream, and aborts the `sl` call if the first character is an
escape character, which indicates that the terminal is being cleared to
make way for a train to come through.

See also: #14046
  • Loading branch information
rmartine-ias authored May 12, 2024
1 parent 85bab0e commit 7bffeb5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
- `[babel-plugin-jest-hoist]` Use `denylist` instead of the deprecated `blacklist` for Babel 8 support ([#14109](https://github.com/jestjs/jest/pull/14109))
- `[expect]` Check error instance type for `toThrow/toThrowError` ([#14576](https://github.com/jestjs/jest/pull/14576))
- `[jest-changed-files]` Print underlying errors when VCS commands fail ([#15052](https://github.com/jestjs/jest/pull/15052))
- `[jest-changed-files]` Abort `sl root` call if output resembles a steam locomotive ([#15053](https://github.com/jestjs/jest/pull/15053))
- `[jest-circus]` [**BREAKING**] Prevent false test failures caused by promise rejections handled asynchronously ([#14315](https://github.com/jestjs/jest/pull/14315))
- `[jest-circus]` Replace recursive `makeTestResults` implementation with iterative one ([#14760](https://github.com/jestjs/jest/pull/14760))
- `[jest-circus]` Omit `expect.hasAssertions()` errors if a test already has errors ([#14866](https://github.com/jestjs/jest/pull/14866))
Expand Down
26 changes: 25 additions & 1 deletion packages/jest-changed-files/src/sl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import type {SCMAdapter} from './types';
*/
const env = {...process.env, HGPLAIN: '1'};

// Whether `sl` is a steam locomotive or not
let isSteamLocomotive = false;

const adapter: SCMAdapter = {
findChangedFiles: async (cwd, options) => {
const includePaths = options.includePaths ?? [];
Expand All @@ -42,8 +45,29 @@ const adapter: SCMAdapter = {
},

getRoot: async cwd => {
if (isSteamLocomotive) {
return null;
}

try {
const result = await execa('sl', ['root'], {cwd, env});
const subprocess = execa('sl', ['root'], {cwd, env});

// Check if we're calling sl (steam locomotive) instead of sl (sapling)
// by looking for the escape character in the first chunk of data.
if (subprocess.stdout) {
subprocess.stdout.once('data', (data: Buffer | string) => {
data = Buffer.isBuffer(data) ? data.toString() : data;
if (data.codePointAt(0) === 27) {
subprocess.cancel();
isSteamLocomotive = true;
}
});
}

const result = await subprocess;
if (result.killed && isSteamLocomotive) {
return null;
}

return result.stdout;
} catch {
Expand Down

0 comments on commit 7bffeb5

Please sign in to comment.