shelljs/test/pipe.js
Nate Fischer 29eba085f7 refactor: prefer promises over test.cb
This is a followup to PR #1211 to use promises everywhere and migrate
away from `test.cb()`. The `test.cb()` function is removed in a future
version of ava.

I made an attempt to convert the skipped shell.cmd() async test cases,
but async functionality isn't implemented so it's possible the test
conversion is not totally correct.
2025-04-07 23:52:01 -07:00

81 lines
2.3 KiB
JavaScript

const test = require('ava');
const shell = require('..');
shell.config.silent = true;
//
// Invalids
//
test('commands like `rm` cannot be on the right side of pipes', t => {
t.is(shell.ls('.').rm, undefined);
t.is(shell.cat('test/resources/file1.txt').rm, undefined);
});
//
// Valids
//
test('piping to cat() should return roughly the same thing', t => {
t.is(
shell.cat('test/resources/file1.txt').cat().toString(),
shell.cat('test/resources/file1.txt').toString()
);
});
test('piping ls() into cat() converts to a string-like object', t => {
t.is(shell.ls('test/resources/').cat().toString(), shell.ls('test/resources/').stdout);
});
test('grep works in a pipe', t => {
const result = shell.ls('test/resources/').grep('file1');
t.is(result.toString(), 'file1\nfile1.js\nfile1.txt\n');
});
test('multiple pipes work', t => {
const result = shell.ls('test/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('test/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('test/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('test/resources/uniq/pipe').uniq('-c').sort('-n');
t.falsy(shell.error());
t.is(result.toString(), shell.cat('test/resources/uniq/pipeSorted').toString());
});
test('Synchronous exec', t => {
const result = shell.cat('test/resources/grep/file').exec('shx grep "alpha*beta"');
t.falsy(shell.error());
t.is(result.toString(), 'alphaaaaaaabeta\nalphbeta\n');
});
test('Asynchronous exec', async t => {
const promise = new Promise(resolve => {
shell.cat('test/resources/grep/file')
.exec('shx grep "alpha*beta"', (code, stdout, stderr) => {
resolve({ code, stdout, stderr });
});
});
const result = await promise;
t.is(result.code, 0);
t.is(result.stdout, 'alphaaaaaaabeta\nalphbeta\n');
t.is(result.stderr, '');
});