diff --git a/ava-test/pipe.js b/ava-test/pipe.js new file mode 100644 index 0000000..dbb2cc5 --- /dev/null +++ b/ava-test/pipe.js @@ -0,0 +1,74 @@ +import test from 'ava'; + +import shell from '..'; + +shell.config.silent = true; + +// +// Invalids +// + +test('commands like `rm` cannot be on the right side of pipes', t => { + t.is(typeof shell.ls('.').rm, 'undefined'); + t.is(typeof shell.cat('resources/file1.txt').rm, 'undefined'); +}); + +// +// Valids +// + +test('piping to cat() should return roughly the same thing', t => { + t.is( + shell.cat('resources/file1.txt').cat().toString(), + shell.cat('resources/file1.txt').toString() + ); +}); + +test('piping ls() into cat() converts to a string-like object', t => { + t.is(shell.ls('resources/').cat().toString(), shell.ls('resources/').stdout); +}); + +test('grep works in a pipe', t => { + const result = shell.ls('resources/').grep('file1'); + t.is(result.toString(), 'file1\nfile1.js\nfile1.txt\n'); +}); + +test('multiple pipes work', t => { + const result = shell.ls('resources/').cat().grep('file1'); + t.is(result.toString(), 'file1\nfile1.js\nfile1.txt\n'); +}); + +test('Equivalent to a simple grep() test case', t => { + const result = shell.cat('resources/grep/file').grep(/alpha*beta/); + t.falsy(shell.error()); + t.is(result.toString(), 'alphaaaaaaabeta\nalphbeta\n'); +}); + +test('Equivalent to a simple sed() test case', t => { + const result = shell.cat('resources/grep/file').sed(/l*\.js/, ''); + t.falsy(shell.error()); + t.is( + result.toString(), + 'alphaaaaaaabeta\nhowareyou\nalphbeta\nthis line ends in\n\n' + ); +}); + +test('Sort a file by frequency of each line', t => { + const result = shell.sort('resources/uniq/pipe').uniq('-c').sort('-n'); + t.falsy(shell.error()); + t.is(result.toString(), shell.cat('resources/uniq/pipeSorted').toString()); +}); + +test('Synchronous exec', t => { + const result = shell.cat('resources/grep/file').exec('shx grep "alpha*beta"'); + t.falsy(shell.error()); + t.is(result.toString(), 'alphaaaaaaabeta\nalphbeta\n'); +}); + +test.cb('Asynchronous exec', t => { + shell.cat('resources/grep/file').exec('shx grep "alpha*beta"', (code, stdout) => { + t.is(code, 0); + t.is(stdout, 'alphaaaaaaabeta\nalphbeta\n'); + t.end(); + }); +}); diff --git a/test/pipe.js b/test/pipe.js deleted file mode 100644 index 9a0560e..0000000 --- a/test/pipe.js +++ /dev/null @@ -1,64 +0,0 @@ -var shell = require('..'); - -var assert = require('assert'); - -shell.config.silent = true; - -shell.rm('-rf', 'tmp'); -shell.mkdir('tmp'); - -// -// Invalids -// - -// commands like `rm` can't be on the right side of pipes -assert.equal(typeof shell.ls('.').rm, 'undefined'); -assert.equal(typeof shell.cat('resources/file1.txt').rm, 'undefined'); - -// -// Valids -// - -// piping to cat() should return roughly the same thing -assert.strictEqual(shell.cat('resources/file1.txt').cat().toString(), - shell.cat('resources/file1.txt').toString()); - -// piping ls() into cat() converts to a string -assert.strictEqual(shell.ls('resources/').cat().toString(), - shell.ls('resources/').stdout); - -var result; -result = shell.ls('resources/').grep('file1'); -assert.equal(result + '', 'file1\nfile1.js\nfile1.txt\n'); - -result = shell.ls('resources/').cat().grep('file1'); -assert.equal(result + '', 'file1\nfile1.js\nfile1.txt\n'); - -// Equivalent to a simple grep() test case -result = shell.cat('resources/grep/file').grep(/alpha*beta/); -assert.equal(shell.error(), null); -assert.equal(result.toString(), 'alphaaaaaaabeta\nalphbeta\n'); - -// Equivalent to a simple sed() test case -result = shell.cat('resources/grep/file').sed(/l*\.js/, ''); -assert.ok(!shell.error()); -assert.equal(result.toString(), 'alphaaaaaaabeta\nhowareyou\nalphbeta\nthis line ends in\n\n'); - -// Sort a file by frequency of each line -result = shell.sort('resources/uniq/pipe').uniq('-c').sort('-n'); -assert.equal(shell.error(), null); -assert.equal(result.toString(), shell.cat('resources/uniq/pipeSorted').toString()); - -// Synchronous exec. To support Windows, the arguments must be passed -// using double quotes because node, following win32 convention, -// passes single quotes through to process.argv verbatim. -result = shell.cat('resources/grep/file').exec('shx grep "alpha*beta"'); -assert.ok(!shell.error()); -assert.equal(result, 'alphaaaaaaabeta\nalphbeta\n'); - -// Async exec -shell.cat('resources/grep/file').exec('shx grep "alpha*beta"', function (code, stdout) { - assert.equal(code, 0); - assert.equal(stdout, 'alphaaaaaaabeta\nalphbeta\n'); - shell.exit(123); -});