shelljs/test/cat.js
Nate Fischer a2e13b62dc Properly handle directories as arguments (#713)
* fix(cat): do not cat directories

Fixes #707

* fix(head): do not let head() read directories

Also fixes a typo

* fix(sort): do not sort directories

Also fixes a typo

* fix(tail): do not let tail() read directories

Also fixes a typo

* fix(uniq): do not let uniq() read directories

We also had a test which called sort() instead of uniq(), so we never
actually tested the missing-file case. This fixes that as well.

This also throws an error for using a directory as output.

* fix(pipe): fix breakages with piped commands
2017-05-08 22:57:58 -07:00

67 lines
1.6 KiB
JavaScript

import fs from 'fs';
import test from 'ava';
import shell from '..';
shell.config.silent = true;
//
// Invalids
//
test('no paths given', t => {
const result = shell.cat();
t.truthy(shell.error());
t.is(result.code, 1);
t.is(result.stderr, 'cat: no paths given');
});
test('nonexistent file', t => {
t.falsy(fs.existsSync('/asdfasdf')); // sanity check
const result = shell.cat('/asdfasdf'); // file does not exist
t.truthy(shell.error());
t.is(result.code, 1);
t.is(result.stderr, 'cat: no such file or directory: /asdfasdf');
});
test('directory', t => {
const result = shell.cat('resources/cat');
t.truthy(shell.error());
t.is(result.code, 1);
t.is(result.stderr, 'cat: resources/cat: Is a directory');
});
//
// Valids
//
test('simple', t => {
const result = shell.cat('resources/cat/file1');
t.falsy(shell.error());
t.is(result.code, 0);
t.is(result.toString(), 'test1\n');
});
test('multiple files', t => {
const result = shell.cat('resources/cat/file2', 'resources/cat/file1');
t.falsy(shell.error());
t.is(result.code, 0);
t.is(result.toString(), 'test2\ntest1\n');
});
test('multiple files, array syntax', t => {
const result = shell.cat(['resources/cat/file2', 'resources/cat/file1']);
t.falsy(shell.error());
t.is(result.code, 0);
t.is(result.toString(), 'test2\ntest1\n');
});
test('glob', t => {
const result = shell.cat('resources/file*.txt');
t.falsy(shell.error());
t.is(result.code, 0);
t.truthy(result.search('test1') > -1); // file order might be random
t.truthy(result.search('test2') > -1);
});