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 6018205
Showing 1 changed file with 47 additions and 30 deletions.
77 changes: 47 additions & 30 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 @@ -198,15 +212,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 {
// TODO(nfischer): revert this back to ShellString
shell.del('file.txt');
}
// TODO(nfischer): this fails on Windows
fs.existsSync('file.txt').should.equal(false);
done();
Expand All @@ -224,7 +237,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 +246,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 +256,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,18 +284,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);
if (unix()) {
nativeRm(fglob);
} else {
shell.del(fglob);
}
// TODO(nfischer): this line fails on Windows
fs.existsSync(fglob).should.equal(false);
shell.cat(fa).toString().should.equal(`hello world${os.EOL}`);
Expand All @@ -302,7 +315,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 6018205

Please sign in to comment.