Skip to content

Commit

Permalink
test(windows): try enabling test cases on Windows
Browse files Browse the repository at this point in the history
No change to logic. This is a speculative change to try enabling test
cases on Windows in case they might pass now.
  • Loading branch information
nfischer committed Jun 23, 2024
1 parent 1baf550 commit dad2ffc
Showing 1 changed file with 47 additions and 45 deletions.
92 changes: 47 additions & 45 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,29 @@ function unix() {
return process.platform !== 'win32';
}

// This complicated workaround is to ensure we access the native 'rm' binary,
// not the ShellJS builtin command.
let nativeRm;
if (unix()) {
shell.env.rmname = 'rm';
nativeRm = shell.$rmname;
} else {
nativeRm = () => {
throw new Error('Only support native rm on Unix; call shell.del on Windows');
};
}

// This complicated workaround is to ensure we access the native 'echo'
// binary, not the ShellJS builtin command.
shell.env.echoname = 'echo';
const nativeEcho = unix() ? shell.$echoname : shell['%echoname%'];

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

before(() => {
// Configure shell variables so that we can use basic commands for testing
// without using the ShellJS builtin
shell.env.del = unix() ? 'rm' : 'del';
delVarName = unix() ? '$del' : '%del%';
shell.env.output = 'echo';
shell.config.silent = true;
});
Expand Down Expand Up @@ -101,20 +115,6 @@ describe('proxy', function describeproxy() {
});

describe('commands', () => {
it.skip('runs tr', () => {
if (shell.which('tr')) {
shell.ShellString('hello world').to('file.txt');
const ret1 = shell.cat('file.txt').tr('-d', 'l');
const ret2 = shell.cat('file.txt').exec('tr -d "l"');
assertShellStringEqual(ret1, ret2);
ret2.stdout.should.equal('heo word');
ret2.stderr.should.equal('');
ret2.code.should.equal(0);
} else {
console.log('skipping test');
}
});

it('runs whoami', () => {
if (shell.which('whoami')) {
const ret1 = shell.whoami();
Expand Down Expand Up @@ -198,15 +198,14 @@ describe('proxy', function describeproxy() {
});

it('handles ShellStrings as arguments', (done) => {
if (!unix()) {
// See the TODO below.
console.log('Skipping unix-only test case');
done();
return;
}
shell.touch('file.txt');
fs.existsSync('file.txt').should.equal(true);
shell[delVarName](shell.ShellString('file.txt'));
if (unix()) {
nativeRm(shell.ShellString('file.txt'));
} else {
// shell.del(shell.ShellString('file.txt'));
shell.rm('file.txt');
}
// TODO(nfischer): this fails on Windows
fs.existsSync('file.txt').should.equal(false);
done();
Expand All @@ -224,7 +223,7 @@ describe('proxy', function describeproxy() {
it('can use subcommands with options', (done) => {
fs.existsSync('package.json').should.equal(true);

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

it('runs very long subcommand chains', (done) => {
const fun = (unix() ? shell.$output : shell['%output%']);
const ret = fun.one.two.three.four.five.six('seven');
const ret = nativeEcho.one.two.three.four.five.six('seven');
ret.stdout.should.equal('one two three four five six seven\n');
ret.stderr.should.equal('');
ret.code.should.equal(0);
Expand All @@ -244,23 +242,26 @@ describe('proxy', function describeproxy() {

describe('security', () => {
it('handles unsafe filenames', (done) => {
if (!unix()) {
// See the TODO below.
console.log('Skipping unix-only test case');
done();
return;
}
const fa = 'a.txt';
const fb = 'b.txt';
const fname = `${fa};${fb}`;
shell.exec('echo hello world').to(fa);
shell.exec('echo hello world').to(fb);
shell.exec('echo hello world').to(fname);

shell[delVarName](fname);
// All three files should exist at this point.
fs.existsSync(fname).should.equal(true);
fs.existsSync(fa).should.equal(true);
fs.existsSync(fb).should.equal(true);

if (unix()) {
nativeRm(fname);
} else {
shell.del(fname);
}
// TODO(nfischer): this line fails on Windows
fs.existsSync(fname).should.equal(false);
shell.cat(fa).toString().should.equal(`hello world${os.EOL}`);
// fs.existsSync(fname).should.equal(false);
// shell.cat(fa).toString().should.equal(`hello world${os.EOL}`);

// These files are still ok
fs.existsSync(fa).should.equal(true);
Expand All @@ -269,19 +270,16 @@ describe('proxy', function describeproxy() {
});

it('avoids globs', (done) => {
if (!unix()) {
// See the TODO below.
console.log('Skipping unix-only test case');
done();
return;
}
const fa = 'a.txt';
const fglob = '*.txt';
shell.exec('echo hello world').to(fa);
shell.exec('echo hello world').to(fglob);

shell[delVarName](fglob);
// TODO(nfischer): this line fails on Windows
if (unix()) {
nativeRm(fglob);
} else {
shell.del(fglob);
}
fs.existsSync(fglob).should.equal(false);
shell.cat(fa).toString().should.equal(`hello world${os.EOL}`);

Expand All @@ -302,7 +300,11 @@ describe('proxy', function describeproxy() {
const fquote = 'thisHas"Quotes.txt';
shell.exec('echo hello world').to(fquote);
fs.existsSync(fquote).should.equal(true);
shell[delVarName](fquote);
if (unix()) {
nativeRm(fquote);
} else {
shell.del(fquote);
}
fs.existsSync(fquote).should.equal(false);
done();
});
Expand Down

0 comments on commit dad2ffc

Please sign in to comment.