Skip to content

Commit 3dee128

Browse files
committed
test: cleanup cross-platform behavior in tests
This exposes `shell.__native`, which can be used to bypass ShellJS builtins and call native commands directly. e.g., `shell.__native.rm()` will call the native platform 'rm' binary. This should not be used for most usecases. I'm primarily creating this just for testing this module. This refactors tests to use `shell.__native` where it improves readability and also adds some helpful assertions.
1 parent 322dedd commit 3dee128

File tree

2 files changed

+40
-28
lines changed

2 files changed

+40
-28
lines changed

index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,12 @@ const proxyifyCmd = (t, ...cmdStart) => {
6666
// origShell.ShellString = ShellStringProxy;
6767

6868
// export the modified shell
69-
module.exports = proxyifyCmd(origShell);
69+
const proxifiedShell = proxyifyCmd(origShell);
70+
71+
// Allow access to native commands, bypassing ShellJS builtins. Useful for
72+
// testing, but most usecases should prefer calling the proxifiedShell directly
73+
// which prefers ShellJS builtins when available. Store this under an unusual
74+
// name to limit the risk of name conflicts with real commands.
75+
// eslint-disable-next-line no-underscore-dangle
76+
proxifiedShell.__native = proxyifyCmd({});
77+
module.exports = proxifiedShell;

test/test.js

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ const fs = require('fs');
55
const os = require('os');
66
const shell = require('../index');
77
const { cmdArrayAttr } = require('../common');
8+
9+
/* eslint-disable no-underscore-dangle */
10+
// Disable lint rule for '__native'.
11+
812
require('should');
913

1014
function assertShellStringEqual(a, b) {
@@ -20,14 +24,8 @@ function unix() {
2024

2125
describe('proxy', function describeproxy() {
2226
this.timeout(10000); // shell.exec() is slow
23-
let delVarName;
2427

2528
before(() => {
26-
// Configure shell variables so that we can use basic commands for testing
27-
// without using the ShellJS builtin
28-
shell.env.del = unix() ? 'rm' : 'del';
29-
delVarName = unix() ? '$del' : '%del%';
30-
shell.env.output = 'echo';
3129
shell.config.silent = true;
3230
});
3331

@@ -107,20 +105,6 @@ describe('proxy', function describeproxy() {
107105
});
108106

109107
describe('commands', () => {
110-
it.skip('runs tr', () => {
111-
if (shell.which('tr')) {
112-
shell.ShellString('hello world').to('file.txt');
113-
const ret1 = shell.cat('file.txt').tr('-d', 'l');
114-
const ret2 = shell.cat('file.txt').exec('tr -d "l"');
115-
assertShellStringEqual(ret1, ret2);
116-
ret2.stdout.should.equal('heo word');
117-
ret2.stderr.should.equal('');
118-
ret2.code.should.equal(0);
119-
} else {
120-
console.log('skipping test');
121-
}
122-
});
123-
124108
it('runs whoami', () => {
125109
if (shell.which('whoami')) {
126110
const ret1 = shell.whoami();
@@ -212,7 +196,11 @@ describe('proxy', function describeproxy() {
212196
}
213197
shell.touch('file.txt');
214198
fs.existsSync('file.txt').should.equal(true);
215-
shell[delVarName](shell.ShellString('file.txt'));
199+
if (unix()) {
200+
shell.__native.rm(shell.ShellString('file.txt'));
201+
} else {
202+
shell.del(shell.ShellString('file.txt'));
203+
}
216204
// TODO(nfischer): this fails on Windows
217205
fs.existsSync('file.txt').should.equal(false);
218206
done();
@@ -230,7 +218,7 @@ describe('proxy', function describeproxy() {
230218
it('can use subcommands with options', (done) => {
231219
fs.existsSync('../package.json').should.equal(true);
232220

233-
// dont' actually remove this file, but do a dry run
221+
// don't actually remove this file, but do a dry run
234222
const ret = shell.git.rm('-qrnf', '../package.json');
235223
ret.code.should.equal(0);
236224
ret.stdout.should.equal('');
@@ -239,8 +227,7 @@ describe('proxy', function describeproxy() {
239227
});
240228

241229
it('runs very long subcommand chains', (done) => {
242-
const fun = (unix() ? shell.$output : shell['%output%']);
243-
const ret = fun.one.two.three.four.five.six('seven');
230+
const ret = shell.__native.echo.one.two.three.four.five.six('seven');
244231
ret.stdout.should.equal('one two three four five six seven\n');
245232
ret.stderr.should.equal('');
246233
ret.code.should.equal(0);
@@ -263,7 +250,16 @@ describe('proxy', function describeproxy() {
263250
shell.exec('echo hello world').to(fb);
264251
shell.exec('echo hello world').to(fname);
265252

266-
shell[delVarName](fname);
253+
// All three files should exist at this point.
254+
fs.existsSync(fname).should.equal(true);
255+
fs.existsSync(fa).should.equal(true);
256+
fs.existsSync(fb).should.equal(true);
257+
258+
if (unix()) {
259+
shell.__native.rm(fname);
260+
} else {
261+
shell.del(fname);
262+
}
267263
// TODO(nfischer): this line fails on Windows
268264
fs.existsSync(fname).should.equal(false);
269265
shell.cat(fa).toString().should.equal(`hello world${os.EOL}`);
@@ -286,7 +282,11 @@ describe('proxy', function describeproxy() {
286282
shell.exec('echo hello world').to(fa);
287283
shell.exec('echo hello world').to(fglob);
288284

289-
shell[delVarName](fglob);
285+
if (unix()) {
286+
shell.__native.rm(fglob);
287+
} else {
288+
shell.del(fglob);
289+
}
290290
// TODO(nfischer): this line fails on Windows
291291
fs.existsSync(fglob).should.equal(false);
292292
shell.cat(fa).toString().should.equal(`hello world${os.EOL}`);
@@ -308,7 +308,11 @@ describe('proxy', function describeproxy() {
308308
const fquote = 'thisHas"Quotes.txt';
309309
shell.exec('echo hello world').to(fquote);
310310
fs.existsSync(fquote).should.equal(true);
311-
shell[delVarName](fquote);
311+
if (unix()) {
312+
shell.__native.rm(fquote);
313+
} else {
314+
shell.del(fquote);
315+
}
312316
fs.existsSync(fquote).should.equal(false);
313317
done();
314318
});

0 commit comments

Comments
 (0)