shelljs/test/toEnd.js
Nate Fischer 1a31356343 refactor: use require instead of import
No change to logic. This swaps over tests to use require() since
everything is currently designed for the commonjs module system.
2025-04-07 22:44:24 -07:00

77 lines
2.2 KiB
JavaScript

const fs = require('fs');
const test = require('ava');
const shell = require('..');
const utils = require('./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(str.toEnd, undefined);
});
test('missing file argument', t => {
shell.ShellString('hello world').toEnd();
t.truthy(shell.error());
});
test('cannot write to a non-existent directory', t => {
t.falsy(fs.existsSync('/asdfasdf')); // sanity check
shell.ShellString('hello world').toEnd('/asdfasdf/file');
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');
});