Skip to content

Commit b6b2cd8

Browse files
nfischerfreitagbr
authored andcommitted
fix: convert error output to be consistent cross-platform (shelljs#684)
* fix: convert error output to be consistent cross-platform The error output produced by `shell.error()` or `result.stderr` should not be inconsistent between platforms. This ensures that path separators are always printed by ShellJS as `/` instead of as `\` on Windows. This should allow scripts using ShellJS to be more consistent cross-platform. We were not previously relying on error output to always be consistent-- only checking its truthiness. Since this was not part of our tested API, it should be reasonable to change this and release in a patch. Fixes shelljs#681 * Fix broken pushd test case * Fix TODO in a test case
1 parent fb09c6a commit b6b2cd8

4 files changed

Lines changed: 55 additions & 4 deletions

File tree

src/common.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ function log() {
8080
}
8181
exports.log = log;
8282

83+
// Converts strings to be equivalent across all platforms. Primarily responsible
84+
// for making sure we use '/' instead of '\' as path separators, but this may be
85+
// expanded in the future if necessary
86+
function convertErrorOutput(msg) {
87+
if (typeof msg !== 'string') {
88+
throw new TypeError('input must be a string');
89+
}
90+
return msg.replace(/\\/g, '/');
91+
}
92+
exports.convertErrorOutput = convertErrorOutput;
93+
8394
// Shows error message. Throws if config.fatal is true
8495
function error(msg, _code, options) {
8596
// Validate input
@@ -105,7 +116,7 @@ function error(msg, _code, options) {
105116

106117
if (!state.errorCode) state.errorCode = options.code;
107118

108-
var logEntry = options.prefix + msg;
119+
var logEntry = convertErrorOutput(options.prefix + msg);
109120
state.error = state.error ? state.error + '\n' : '';
110121
state.error += logEntry;
111122

test/common.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,50 @@ test('parseOptions (invalid type)', t => {
4747
});
4848
});
4949

50+
test('convertErrorOutput: no args', t => {
51+
t.throws(() => {
52+
common.convertErrorOutput();
53+
}, TypeError);
54+
});
55+
56+
test('convertErrorOutput: input must be a vanilla string', t => {
57+
t.throws(() => {
58+
common.convertErrorOutput(3);
59+
}, TypeError);
60+
61+
t.throws(() => {
62+
common.convertErrorOutput({});
63+
}, TypeError);
64+
});
65+
5066
//
5167
// Valids
5268
//
5369

70+
//
71+
// common.convertErrorOutput()
72+
//
73+
test('convertErrorOutput: nothing to convert', t => {
74+
const input = 'hello world';
75+
const result = common.convertErrorOutput(input);
76+
t.is(result, input);
77+
});
78+
79+
test('convertErrorOutput: does not change forward slash', t => {
80+
const input = 'dir/sub/file.txt';
81+
const result = common.convertErrorOutput(input);
82+
t.is(result, input);
83+
});
84+
85+
test('convertErrorOutput: changes backslashes to forward slashes', t => {
86+
const input = 'dir\\sub\\file.txt';
87+
const result = common.convertErrorOutput(input);
88+
t.is(result, 'dir/sub/file.txt');
89+
});
90+
91+
//
92+
// common.expand()
93+
//
5494
test('single file, array syntax', t => {
5595
const result = common.expand(['resources/file1.txt']);
5696
t.falsy(shell.error());

test/mv.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ test('-n option with a directory as the destination', t => {
9696
const result = shell.mv('-n', 'file1', 'cp');
9797
t.truthy(shell.error());
9898
t.is(result.code, 1);
99-
// TODO(nate): make this an equals comparison once issue #681 is resolved
100-
t.regex(result.stderr, /mv: dest file already exists: cp.file1/);
99+
t.is(result.stderr, 'mv: dest file already exists: cp/file1');
101100
});
102101

103102
test('-f is the default behavior', t => {

test/pushd.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ test('Push invalid directory', t => {
273273
shell.pushd('does/not/exist');
274274
t.is(
275275
shell.error(),
276-
'pushd: no such file or directory: ' + path.resolve('.', 'does/not/exist')
276+
`pushd: no such file or directory: ${path.resolve('.', 'does/not/exist')
277+
.replace(/\\/g, '/')}`
277278
);
278279
t.is(process.cwd(), oldCwd);
279280
});

0 commit comments

Comments
 (0)