Skip to content

Commit 8c23689

Browse files
authored
Make tests run on non-English Windows (#110)
1 parent 5aab750 commit 8c23689

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

test/helpers/assert.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {spawnSync} from 'node:child_process';
12
import {nonExistentCommand, nodeHangingCommand, nodeEvalCommandStart} from './commands.js';
23

34
export const assertSubprocessErrorName = (t, name) => {
@@ -23,13 +24,24 @@ export const assertNonExistent = (t, {name, exitCode, signalName, command, messa
2324
assertDurationMs(t, durationMs);
2425
};
2526

27+
// Support localized error messages on non-English Windows.
28+
let nonExistentCommandOutput;
29+
const toNonExistentCommandOutput = command => {
30+
if (typeof nonExistentCommandOutput !== 'string') {
31+
const {stderr} = spawnSync(nonExistentCommand, {shell: true, encoding: 'utf8'});
32+
nonExistentCommandOutput = stderr.replace(/\r?\n$/, '');
33+
}
34+
35+
return nonExistentCommandOutput.replaceAll(nonExistentCommand, command.split(/[ /]/)[0]);
36+
};
37+
2638
export const assertWindowsNonExistent = (t, {name, exitCode, signalName, command, message, stderr, cause, durationMs}, expectedCommand = nonExistentCommand) => {
2739
assertSubprocessErrorName(t, name);
2840
t.is(exitCode, 1);
2941
t.is(signalName, undefined);
3042
t.is(command, expectedCommand);
3143
t.is(message, `Command failed with exit code 1: ${expectedCommand}`);
32-
t.true(stderr.includes('not recognized as an internal or external command'));
44+
t.is(stderr, toNonExistentCommandOutput(expectedCommand));
3345
t.is(cause, undefined);
3446
assertDurationMs(t, durationMs);
3547
};

test/helpers/setup.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@ const updateCodePage = codePage => {
1212
assert(getCodePage() === codePage);
1313
};
1414

15-
// On Windows in simplified chinese, the default code page is 936
16-
// Run `chcp 65001` to change it to UTF8
15+
// On Windows, run `chcp 65001` to change the current code page to UTF8.
16+
// This is necessary to correctly parse non-English cmd.exe error output.
1717
// https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/chcp
18+
// and https://learn.microsoft.com/en-us/windows/win32/intl/code-page-identifiers
1819
export default function setup() {
1920
if (process.platform !== 'win32') {
2021
return;
2122
}
2223

2324
const originalCodePage = getCodePage();
24-
if (originalCodePage === '936') {
25+
if (originalCodePage !== '65001') {
2526
updateCodePage('65001');
2627

2728
process.on('exit', () => {

0 commit comments

Comments
 (0)