shelljs/test/grep.js
Nate Fischer 2ee83ebf74 refactor(test): update AVA and refactor tests (#760)
This updates tests for `AVA` 19.0.0+.

`AVA` 0.18.0 has a breaking change which changes the current working directory
for tests. As a result, we need to change 'resources' -> 'test/resources' (and
 similar path changes).

`AVA` 0.19.0 has a breaking change that all tests must run at least one assert.
This breaking change was already resolved by #746, so no change was necessary in
this PR.

This updates to `AVA` 0.21.0, since there are no other breaking changes.
2017-08-11 11:03:13 -07:00

139 lines
3.9 KiB
JavaScript

import fs from 'fs';
import test from 'ava';
import shell from '..';
import utils from './utils/utils';
test.beforeEach(t => {
t.context.tmp = utils.getTempDir();
shell.config.resetForTesting();
shell.cp('-r', 'test/resources', t.context.tmp);
});
test.afterEach.always(t => {
shell.rm('-rf', t.context.tmp);
});
//
// Invalids
//
test('no args', t => {
const result = shell.grep();
t.truthy(shell.error());
t.is(result.code, 2);
});
test('too few args', t => {
const result = shell.grep(/asdf/g); // too few args
t.truthy(shell.error());
t.is(result.code, 2);
});
test('no such file', t => {
t.falsy(fs.existsSync('/asdfasdf')); // sanity check
const result = shell.grep(/asdf/g, '/asdfasdf'); // no such file
t.truthy(shell.error());
t.is(result.stderr, 'grep: no such file or directory: /asdfasdf');
t.is(result.code, 2);
});
test('if at least one file is missing, this should be an error', t => {
t.falsy(fs.existsSync('asdfasdf')); // sanity check
t.truthy(fs.existsSync(`${t.context.tmp}/file1`)); // sanity check
const result = shell.grep(/asdf/g, `${t.context.tmp}/file1`, 'asdfasdf');
t.truthy(shell.error());
t.is(result.stderr, 'grep: no such file or directory: asdfasdf');
t.is(result.code, 2);
});
//
// Valids
//
test('basic', t => {
const result = shell.grep('line', 'test/resources/a.txt');
t.falsy(shell.error());
t.is(result.split('\n').length - 1, 4);
});
test('-v option', t => {
const result = shell.grep('-v', 'line', 'test/resources/a.txt');
t.falsy(shell.error());
t.is(result.split('\n').length - 1, 8);
});
test('matches one line', t => {
const result = shell.grep('line one', 'test/resources/a.txt');
t.falsy(shell.error());
t.is(result.toString(), 'This is line one\n');
});
test('multiple files', t => {
const result = shell.grep(/test/, 'test/resources/file1.txt',
'test/resources/file2.txt');
t.falsy(shell.error());
t.is(result.toString(), 'test1\ntest2\n');
});
test('multiple files, array syntax', t => {
const result = shell.grep(/test/, ['test/resources/file1.txt',
'test/resources/file2.txt']);
t.falsy(shell.error());
t.is(result.toString(), 'test1\ntest2\n');
});
test('multiple files, glob syntax, * for file name', t => {
const result = shell.grep(/test/, 'test/resources/file*.txt');
t.falsy(shell.error());
t.truthy(result.toString(), 'test1\ntest2\n');
});
test('multiple files, glob syntax, * for directory name', t => {
const result = shell.grep(/test/, 'test/r*/file*.txt');
t.falsy(shell.error());
t.is(result.toString(), 'test1\ntest2\n');
});
test('multiple files, double-star glob', t => {
const result = shell.grep(/test/, 'test/resources/**/file*.js');
t.falsy(shell.error());
t.is(result.toString(), 'test\ntest\ntest\ntest\n');
});
test('one file, * in regex', t => {
const result = shell.grep(/alpha*beta/, 'test/resources/grep/file');
t.falsy(shell.error());
t.is(result.toString(), 'alphaaaaaaabeta\nalphbeta\n');
});
test('one file, * in string-regex', t => {
const result = shell.grep('alpha*beta', 'test/resources/grep/file');
t.falsy(shell.error());
t.is(result.toString(), 'alphaaaaaaabeta\nalphbeta\n');
});
test('one file, * in regex, make sure * is not globbed', t => {
const result = shell.grep(/l*\.js/, 'test/resources/grep/file');
t.falsy(shell.error());
t.is(result.toString(), 'this line ends in.js\nlllllllllllllllll.js\n');
});
test('one file, * in string-regex, make sure * is not globbed', t => {
const result = shell.grep('l*\\.js', 'test/resources/grep/file');
t.falsy(shell.error());
t.is(result.toString(), 'this line ends in.js\nlllllllllllllllll.js\n');
});
test('-l option', t => {
const result = shell.grep('-l', 'test1', 'test/resources/file1', 'test/resources/file2',
'test/resources/file1.txt');
t.falsy(shell.error());
t.truthy(result.match(/file1(\n|$)/));
t.truthy(result.match(/file1.txt/));
t.falsy(result.match(/file2.txt/));
t.is(result.split('\n').length - 1, 2);
});