mirror of
https://github.com/shelljs/shelljs.git
synced 2026-01-18 16:03:37 +00:00
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:
parent
6bdee43ab5
commit
580d6d3301
@ -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;
|
||||
|
||||
12
test/cat.js
12
test/cat.js
@ -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
1
test/resources/cat/file1
Normal file
@ -0,0 +1 @@
|
||||
test1
|
||||
1
test/resources/cat/file2
Normal file
1
test/resources/cat/file2
Normal file
@ -0,0 +1 @@
|
||||
test2
|
||||
Loading…
x
Reference in New Issue
Block a user