shelljs/test/toEnd.js
Nate Fischer ac0ff873f1 refactor: add config.reset() and .resetForTesting() (#641)
Add .reset() and .resetForTesting() to shell.config and use .resetForTesting()
as a standard set-up for unit tests.
2017-01-07 22:40:38 -08:00

71 lines
2.0 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.mkdir(t.context.tmp);
});
test.afterEach.always(t => {
shell.rm('-rf', t.context.tmp);
});
//
// Invalids
//
test('Normal strings don\'t have \'.toEnd()\' anymore', t => {
const str = 'hello world';
t.is(typeof str.toEnd, 'undefined');
});
test('missing file argument', t => {
shell.ShellString('hello world').toEnd();
t.truthy(shell.error());
});
//
// Valids
//
test('creates a new file', t => {
t.falsy(fs.existsSync(`${t.context.tmp}/toEnd1`)); // Check file toEnd() creates does not already exist
shell.ShellString('hello ').toEnd(`${t.context.tmp}/toEnd1`);
t.truthy(fs.existsSync(`${t.context.tmp}/toEnd1`)); // Check that file was created
const result = shell.cat(`${t.context.tmp}/toEnd1`);
t.is(result.toString(), 'hello ');
});
test('can be chained', t => {
t.falsy(fs.existsSync(`${t.context.tmp}/toEnd1`));
t.falsy(fs.existsSync(`${t.context.tmp}/toEnd2`));
shell.ShellString('hello ').toEnd(`${t.context.tmp}/toEnd1`);
shell.ShellString('world')
.toEnd(`${t.context.tmp}/toEnd1`)
.toEnd(`${t.context.tmp}/toEnd2`); // Write some more to the file
const result1 = shell.cat(`${t.context.tmp}/toEnd1`);
t.falsy(shell.error());
t.is(result1.toString(), 'hello world'); // Check that the result is what we expect
const result2 = shell.cat(`${t.context.tmp}/toEnd2`);
t.falsy(shell.error());
t.is(result2.toString(), 'world'); // Check that the result is what we expect
});
test('With a glob', t => {
shell.touch(`${t.context.tmp}/toEnd1`);
shell.ShellString('good').to(`${t.context.tmp}/toE*1`);
shell.ShellString('bye').toEnd(`${t.context.tmp}/toE*1`);
t.falsy(
fs.existsSync(`${t.context.tmp}/toE*1`)
);
const result = shell.cat(`${t.context.tmp}/toEnd1`);
t.falsy(shell.error());
t.is(result.toString(), 'goodbye');
});