Skip to content

Commit beb2b1f

Browse files
authored
fix(head): handle zero line counts (#1276)
1 parent 5a4f6ed commit beb2b1f

2 files changed

Lines changed: 98 additions & 2 deletions

File tree

‎src/head.js‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ function _head(options, files) {
5151
var head = [];
5252
var pipe = common.readFromPipe();
5353

54-
if (!files && !pipe) common.error('no paths given');
55-
5654
var idx = 1;
5755
if (options.numLines === true) {
5856
idx = 2;
@@ -61,6 +59,7 @@ function _head(options, files) {
6159
options.numLines = 10;
6260
}
6361
files = [].slice.call(arguments, idx);
62+
if (files.length === 0 && !pipe) common.error('no paths given');
6463

6564
if (pipe) {
6665
files.unshift('-');

‎test/head.js‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,30 @@ test('no args', t => {
1717
t.is(result.code, 1);
1818
});
1919

20+
test('line count option without a value or files', t => {
21+
const result = shell.head('-n');
22+
t.is(result.code, 1);
23+
t.is(result.stderr, 'head: no paths given');
24+
});
25+
26+
test('zero line count still requires files or piped input', t => {
27+
const result = shell.head('-n', 0);
28+
t.is(result.code, 1);
29+
t.is(result.stderr, 'head: no paths given');
30+
});
31+
32+
test('positive line count still requires files or piped input', t => {
33+
const result = shell.head('-n', 1);
34+
t.is(result.code, 1);
35+
t.is(result.stderr, 'head: no paths given');
36+
});
37+
38+
test('negative line count still requires files or piped input', t => {
39+
const result = shell.head('-n', -1);
40+
t.is(result.code, 1);
41+
t.is(result.stderr, 'head: no paths given');
42+
});
43+
2044
test('file does not exist', t => {
2145
t.falsy(fs.existsSync('/asdfasdf')); // sanity check
2246
const result = shell.head('/asdfasdf'); // file does not exist
@@ -53,6 +77,79 @@ test('simple', t => {
5377
t.is(result.toString(), topOfFile1.slice(0, 10).join('\n') + '\n');
5478
});
5579

80+
test('zero lines with separate option', t => {
81+
const result = shell.head('-n', 0, 'test/resources/head/shortfile1');
82+
t.falsy(shell.error());
83+
t.is(result.code, 0);
84+
t.is(result.toString(), '');
85+
});
86+
87+
test('zero lines from multiple files', t => {
88+
const result = shell.head('-n', 0, 'test/resources/head/shortfile1',
89+
'test/resources/file1');
90+
t.falsy(shell.error());
91+
t.is(result.code, 0);
92+
t.is(result.toString(), '');
93+
});
94+
95+
test('zero lines with array syntax', t => {
96+
const result = shell.head('-n', 0, ['test/resources/head/shortfile1',
97+
'test/resources/file1']);
98+
t.falsy(shell.error());
99+
t.is(result.code, 0);
100+
t.is(result.toString(), '');
101+
});
102+
103+
test('zero lines with object option', t => {
104+
const result = shell.head({ '-n': 0 }, 'test/resources/head/shortfile1',
105+
'test/resources/file1');
106+
t.falsy(shell.error());
107+
t.is(result.code, 0);
108+
t.is(result.toString(), '');
109+
});
110+
111+
test('numeric zero lines from a pipe', t => {
112+
const result = shell.ShellString('first\nlast\n').head('-n', 0);
113+
t.falsy(shell.error());
114+
t.is(result.code, 0);
115+
t.is(result.toString(), '');
116+
});
117+
118+
test('string zero lines from a pipe', t => {
119+
const result = shell.ShellString('first\nlast\n').head('-n', '0');
120+
t.falsy(shell.error());
121+
t.is(result.code, 0);
122+
t.is(result.toString(), '');
123+
});
124+
125+
test('negative string zero lines from a pipe', t => {
126+
const result = shell.ShellString('first\nlast\n').head('-n', '-0');
127+
t.falsy(shell.error());
128+
t.is(result.code, 0);
129+
t.is(result.toString(), '');
130+
});
131+
132+
test('positive string zero lines from a pipe', t => {
133+
const result = shell.ShellString('first\nlast\n').head('-n', '+0');
134+
t.falsy(shell.error());
135+
t.is(result.code, 0);
136+
t.is(result.toString(), '');
137+
});
138+
139+
test('zero lines still reports missing files', t => {
140+
const result = shell.head('-n', 0, '/asdfasdf');
141+
t.truthy(shell.error());
142+
t.is(result.code, 1);
143+
t.is(result.stderr, 'head: no such file or directory: /asdfasdf');
144+
});
145+
146+
test('zero lines still reports directories', t => {
147+
const result = shell.head('-n', 0, 'test/resources/');
148+
t.truthy(shell.error());
149+
t.is(result.code, 1);
150+
t.is(result.stderr, "head: error reading 'test/resources/': Is a directory");
151+
});
152+
56153
test('multiple files', t => {
57154
const result = shell.head('test/resources/head/file2.txt',
58155
'test/resources/head/file1.txt');

0 commit comments

Comments
 (0)