diff --git a/ava-test/sed.js b/ava-test/sed.js new file mode 100644 index 0000000..14f0f58 --- /dev/null +++ b/ava-test/sed.js @@ -0,0 +1,176 @@ +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.silent = true; + shell.cp('-r', 'resources', t.context.tmp); +}); + +test.afterEach.always(t => { + shell.rm('-rf', t.context.tmp); +}); + + +// +// Invalids +// + +test('no arguments', t => { + const result = shell.sed(); + t.truthy(shell.error()); + t.is(result.code, 1); + t.truthy(result.stderr); +}); + +test('only one argument', t => { + const result = shell.sed(/asdf/g); + t.truthy(shell.error()); + t.is(result.code, 1); +}); + +test('only two arguments', t => { + const result = shell.sed(/asdf/g, 'nada'); + t.truthy(shell.error()); + t.is(result.code, 1); +}); + +test('no such file', t => { + t.falsy(fs.existsSync('asdfasdf')); // sanity check + const result = shell.sed(/asdf/g, 'nada', 'asdfasdf'); + t.truthy(shell.error()); + t.is(result.code, 2); + t.is(result.stderr, 'sed: no such file or directory: asdfasdf'); +}); + +// TODO(nate): flaky test +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.sed(/asdf/g, 'nada', `${t.context.tmp}/file1`, 'asdfasdf'); + t.truthy(shell.error()); + t.is(result.code, 2); + t.is(result.stderr, 'sed: no such file or directory: asdfasdf'); +}); + +// +// Valids +// + +test('search with a string', t => { + const result = shell.sed('test1', 'hello', `${t.context.tmp}/file1`); + t.falsy(shell.error()); + t.is(result.code, 0); + t.is(result.toString(), 'hello'); +}); + +test('search with a regex', t => { + const result = shell.sed(/test1/, 'hello', `${t.context.tmp}/file1`); + t.falsy(shell.error()); + t.is(result.code, 0); + t.is(result.toString(), 'hello'); +}); + +test('replace with a number instead of a string', t => { + const result = shell.sed(/test1/, 1234, `${t.context.tmp}/file1`); + t.falsy(shell.error()); + t.is(result.code, 0); + t.is(result.toString(), '1234'); +}); + +test('replace using a function', t => { + const replaceFun = match => match.toUpperCase() + match; + const result = shell.sed(/test1/, replaceFun, `${t.context.tmp}/file1`); + t.falsy(shell.error()); + t.is(result.code, 0); + t.is(result.toString(), 'TEST1test1'); +}); + +test('-i option', t => { + const result = shell.sed('-i', /test1/, 'hello', `${t.context.tmp}/file1`); + t.falsy(shell.error()); + t.is(result.code, 0); + t.is(result.toString(), 'hello'); + t.is(shell.cat(`${t.context.tmp}/file1`).toString(), 'hello'); +}); + +test('make sure * in regex is not globbed', t => { + const result = shell.sed(/alpha*beta/, 'hello', 'resources/grep/file'); + t.falsy(shell.error()); + t.is(result.code, 0); + t.is( + result.toString(), + 'hello\nhowareyou\nhello\nthis line ends in.js\nlllllllllllllllll.js\n' + ); +}); + +test('make sure * in string-regex is not globbed', t => { + const result = shell.sed('alpha*beta', 'hello', 'resources/grep/file'); + t.falsy(shell.error()); + t.is(result.code, 0); + t.is( + result.toString(), + 'hello\nhowareyou\nhello\nthis line ends in.js\nlllllllllllllllll.js\n' + ); +}); + +test('make sure * in regex is not globbed (matches something)', t => { + const result = shell.sed(/l*\.js/, '', 'resources/grep/file'); + t.falsy(shell.error()); + t.is(result.code, 0); + t.is( + result.toString(), + 'alphaaaaaaabeta\nhowareyou\nalphbeta\nthis line ends in\n\n' + ); +}); + +test('make sure * in string-regex is not globbed (matches something)', t => { + const result = shell.sed('l*\\.js', '', 'resources/grep/file'); + t.falsy(shell.error()); + t.is(result.code, 0); + t.is( + result.toString(), + 'alphaaaaaaabeta\nhowareyou\nalphbeta\nthis line ends in\n\n' + ); +}); + +test('multiple file names', t => { + const result = shell.sed('test', 'hello', `${t.context.tmp}/file1`, + `${t.context.tmp}/file2`); + t.falsy(shell.error()); + t.is(result.code, 0); + t.is(result.toString(), 'hello1\nhello2'); +}); + +test('array of file names (and try it out with a simple regex)', t => { + const result = shell.sed(/t.*st/, 'hello', [`${t.context.tmp}/file1`, + `${t.context.tmp}/file2`]); + t.falsy(shell.error()); + t.is(result.code, 0); + t.is(result.toString(), 'hello1\nhello2'); +}); + +test('multiple file names, with in-place-replacement', t => { + const result = shell.sed('-i', 'test', 'hello', [`${t.context.tmp}/file1`, + `${t.context.tmp}/file2`]); + t.falsy(shell.error()); + t.is(result.code, 0); + t.is(result.toString(), 'hello1\nhello2'); + t.is(shell.cat(`${t.context.tmp}/file1`).toString(), 'hello1'); + t.is(shell.cat(`${t.context.tmp}/file2`).toString(), 'hello2'); +}); + +test('glob file names, with in-place-replacement', t => { + t.is(shell.cat(`${t.context.tmp}/file1.txt`).toString(), 'test1\n'); + t.is(shell.cat(`${t.context.tmp}/file2.txt`).toString(), 'test2\n'); + const result = shell.sed('-i', 'test', 'hello', `${t.context.tmp}/file*.txt`); + t.falsy(shell.error()); + t.is(result.code, 0); + t.is(result.toString(), 'hello1\n\nhello2\n'); // TODO: fix sed's behavior + t.is(shell.cat(`${t.context.tmp}/file1.txt`).toString(), 'hello1\n'); + t.is(shell.cat(`${t.context.tmp}/file2.txt`).toString(), 'hello2\n'); +}); diff --git a/test/sed.js b/test/sed.js deleted file mode 100644 index 2105dcc..0000000 --- a/test/sed.js +++ /dev/null @@ -1,137 +0,0 @@ -var shell = require('..'); - -var assert = require('assert'); -var fs = require('fs'); - -shell.config.silent = true; - -shell.rm('-rf', 'tmp'); -shell.mkdir('tmp'); - -var result; - -// -// Invalids -// - -result = shell.sed(); -assert.ok(shell.error()); -assert.equal(result.code, 1); -assert.ok(result.stderr); - -result = shell.sed(/asdf/g); // too few args -assert.ok(shell.error()); -assert.equal(result.code, 1); - -result = shell.sed(/asdf/g, 'nada'); // too few args -assert.ok(shell.error()); -assert.equal(result.code, 1); - -assert.equal(fs.existsSync('asdfasdf'), false); // sanity check -result = shell.sed(/asdf/g, 'nada', 'asdfasdf'); // no such file -assert.ok(shell.error()); -assert.equal(result.code, 2); -assert.equal(result.stderr, 'sed: no such file or directory: asdfasdf'); - -// if at least one file is missing, this should be an error -shell.cp('-f', 'resources/file1', 'tmp/file1'); -assert.equal(fs.existsSync('asdfasdf'), false); // sanity check -assert.equal(fs.existsSync('tmp/file1'), true); // sanity check -result = shell.sed(/asdf/g, 'nada', 'tmp/file1', 'asdfasdf'); -assert.ok(shell.error()); -assert.equal(result.code, 2); -assert.equal(result.stderr, 'sed: no such file or directory: asdfasdf'); - -// -// Valids -// - -shell.cp('-f', 'resources/file1', 'tmp/file1'); -result = shell.sed('test1', 'hello', 'tmp/file1'); // search string -assert.equal(shell.error(), null); -assert.equal(result.code, 0); -assert.equal(result.toString(), 'hello'); - -result = shell.sed(/test1/, 'hello', 'tmp/file1'); // search regex -assert.equal(shell.error(), null); -assert.equal(result.code, 0); -assert.equal(result.toString(), 'hello'); - -result = shell.sed(/test1/, 1234, 'tmp/file1'); // numeric replacement -assert.equal(shell.error(), null); -assert.equal(result.code, 0); -assert.equal(result.toString(), '1234'); - -var replaceFun = function (match) { - return match.toUpperCase() + match; -}; -result = shell.sed(/test1/, replaceFun, 'tmp/file1'); // replacement function -assert.equal(shell.error(), null); -assert.equal(result.code, 0); -assert.equal(result.toString(), 'TEST1test1'); - -result = shell.sed('-i', /test1/, 'hello', 'tmp/file1'); -assert.equal(shell.error(), null); -assert.equal(result.code, 0); -assert.equal(result.toString(), 'hello'); -assert.equal(shell.cat('tmp/file1').toString(), 'hello'); - -// make sure * in regex is not globbed -result = shell.sed(/alpha*beta/, 'hello', 'resources/grep/file'); -assert.equal(shell.error(), null); -assert.equal(result.code, 0); -assert.equal(result.toString(), 'hello\nhowareyou\nhello\nthis line ends in.js\nlllllllllllllllll.js\n'); - -// make sure * in string-regex is not globbed -result = shell.sed('alpha*beta', 'hello', 'resources/grep/file'); -assert.ok(!shell.error()); -assert.equal(result.code, 0); -assert.equal(result.toString(), 'hello\nhowareyou\nhello\nthis line ends in.js\nlllllllllllllllll.js\n'); - -// make sure * in regex is not globbed -result = shell.sed(/l*\.js/, '', 'resources/grep/file'); -assert.ok(!shell.error()); -assert.equal(result.code, 0); -assert.equal(result.toString(), 'alphaaaaaaabeta\nhowareyou\nalphbeta\nthis line ends in\n\n'); - -// make sure * in string-regex is not globbed -result = shell.sed('l*\\.js', '', 'resources/grep/file'); -assert.ok(!shell.error()); -assert.equal(result.code, 0); -assert.equal(result.toString(), 'alphaaaaaaabeta\nhowareyou\nalphbeta\nthis line ends in\n\n'); - -shell.cp('-f', 'resources/file1', 'tmp/file1'); -shell.cp('-f', 'resources/file2', 'tmp/file2'); - -// multiple file names -result = shell.sed('test', 'hello', 'tmp/file1', 'tmp/file2'); -assert.equal(shell.error(), null); -assert.equal(result.code, 0); -assert.equal(result.toString(), 'hello1\nhello2'); - -// array of file names (and try it out with a simple regex) -result = shell.sed(/t.*st/, 'hello', ['tmp/file1', 'tmp/file2']); -assert.equal(shell.error(), null); -assert.equal(result.code, 0); -assert.equal(result.toString(), 'hello1\nhello2'); - -// multiple file names, with in-place-replacement -result = shell.sed('-i', 'test', 'hello', ['tmp/file1', 'tmp/file2']); -assert.equal(shell.error(), null); -assert.equal(result.code, 0); -assert.equal(result.toString(), 'hello1\nhello2'); -assert.equal(shell.cat('tmp/file1').toString(), 'hello1'); -assert.equal(shell.cat('tmp/file2').toString(), 'hello2'); - -// glob file names, with in-place-replacement -shell.cp('resources/file*.txt', 'tmp/'); -assert.equal(shell.cat('tmp/file1.txt').toString(), 'test1\n'); -assert.equal(shell.cat('tmp/file2.txt').toString(), 'test2\n'); -result = shell.sed('-i', 'test', 'hello', 'tmp/file*.txt'); -assert.equal(shell.error(), null); -assert.equal(result.code, 0); -assert.equal(result.toString(), 'hello1\n\nhello2\n'); // TODO: fix sed's behavior -assert.equal(shell.cat('tmp/file1.txt').toString(), 'hello1\n'); -assert.equal(shell.cat('tmp/file2.txt').toString(), 'hello2\n'); - -shell.exit(123);