fix(cat): make behavior more like unix

`cat()` no longer puts '\n's in weird places (causing double newlines), and
no longer improperly strips off a trailing newline.
This commit is contained in:
Nate Fischer 2016-01-31 00:07:53 -08:00
parent 6bdee43ab5
commit 580d6d3301
4 changed files with 9 additions and 10 deletions

View File

@ -32,12 +32,9 @@ function _cat(options, files) {
if (!fs.existsSync(file))
common.error('no such file or directory: ' + file);
cat += fs.readFileSync(file, 'utf8') + '\n';
cat += fs.readFileSync(file, 'utf8');
});
if (cat[cat.length-1] === '\n')
cat = cat.substring(0, cat.length-1);
return common.ShellString(cat);
}
module.exports = _cat;

View File

@ -24,19 +24,19 @@ assert.ok(shell.error());
//
// simple
var result = shell.cat('resources/file1');
var result = shell.cat('resources/cat/file1');
assert.equal(shell.error(), null);
assert.equal(result, 'test1');
assert.equal(result, 'test1\n');
// multiple files
var result = shell.cat('resources/file2', 'resources/file1');
var result = shell.cat('resources/cat/file2', 'resources/cat/file1');
assert.equal(shell.error(), null);
assert.equal(result, 'test2\ntest1');
assert.equal(result, 'test2\ntest1\n');
// multiple files, array syntax
var result = shell.cat(['resources/file2', 'resources/file1']);
var result = shell.cat(['resources/cat/file2', 'resources/cat/file1']);
assert.equal(shell.error(), null);
assert.equal(result, 'test2\ntest1');
assert.equal(result, 'test2\ntest1\n');
var result = shell.cat('resources/file*.txt');
assert.equal(shell.error(), null);

1
test/resources/cat/file1 Normal file
View File

@ -0,0 +1 @@
test1

1
test/resources/cat/file2 Normal file
View File

@ -0,0 +1 @@
test2