/
grep.js
151 lines (125 loc) · 4.35 KB
/
grep.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import fs from 'fs';
import test from 'ava';
import shell from '..';
import utils from './utils/utils';
test.beforeEach(t => {
t.context.tmp = utils.getTempDir();
shell.config.resetForTesting();
shell.cp('-r', 'test/resources', t.context.tmp);
});
test.afterEach.always(t => {
shell.rm('-rf', t.context.tmp);
});
//
// Invalids
//
test('no args', t => {
const result = shell.grep();
t.truthy(shell.error());
t.is(result.code, 2);
});
test('too few args', t => {
const result = shell.grep(/asdf/g); // too few args
t.truthy(shell.error());
t.is(result.code, 2);
});
test('no such file', t => {
t.falsy(fs.existsSync('/asdfasdf')); // sanity check
const result = shell.grep(/asdf/g, '/asdfasdf'); // no such file
t.truthy(shell.error());
t.is(result.stderr, 'grep: no such file or directory: /asdfasdf');
t.is(result.code, 2);
});
test('if at least one file is missing, this should be an error', t => {
t.falsy(fs.existsSync('asdfasdf')); // sanity check
t.truthy(fs.existsSync(`${t.context.tmp}/file1`)); // sanity check
const result = shell.grep(/asdf/g, `${t.context.tmp}/file1`, 'asdfasdf');
t.truthy(shell.error());
t.is(result.stderr, 'grep: no such file or directory: asdfasdf');
t.is(result.code, 2);
});
//
// Valids
//
test('basic', t => {
const result = shell.grep('line', 'test/resources/a.txt');
t.falsy(shell.error());
t.is(result.split('\n').length - 1, 4);
});
test('-v option', t => {
const result = shell.grep('-v', 'line', 'test/resources/a.txt');
t.falsy(shell.error());
t.is(result.split('\n').length - 1, 8);
});
test('matches one line', t => {
const result = shell.grep('line one', 'test/resources/a.txt');
t.falsy(shell.error());
t.is(result.toString(), 'This is line one\n');
});
test('multiple files', t => {
const result = shell.grep(/test/, 'test/resources/file1.txt',
'test/resources/file2.txt');
t.falsy(shell.error());
t.is(result.toString(), 'test1\ntest2\n');
});
test('multiple files, array syntax', t => {
const result = shell.grep(/test/, ['test/resources/file1.txt',
'test/resources/file2.txt']);
t.falsy(shell.error());
t.is(result.toString(), 'test1\ntest2\n');
});
test('multiple files, glob syntax, * for file name', t => {
const result = shell.grep(/test/, 'test/resources/file*.txt');
t.falsy(shell.error());
t.truthy(result.toString(), 'test1\ntest2\n');
});
test('multiple files, glob syntax, * for directory name', t => {
const result = shell.grep(/test/, 'test/r*/file*.txt');
t.falsy(shell.error());
t.is(result.toString(), 'test1\ntest2\n');
});
test('multiple files, double-star glob', t => {
const result = shell.grep(/test/, 'test/resources/**/file*.js');
t.falsy(shell.error());
t.is(result.toString(), 'test\ntest\ntest\ntest\n');
});
test('one file, * in regex', t => {
const result = shell.grep(/alpha*beta/, 'test/resources/grep/file');
t.falsy(shell.error());
t.is(result.toString(), 'alphaaaaaaabeta\nalphbeta\n');
});
test('one file, * in string-regex', t => {
const result = shell.grep('alpha*beta', 'test/resources/grep/file');
t.falsy(shell.error());
t.is(result.toString(), 'alphaaaaaaabeta\nalphbeta\n');
});
test('one file, * in regex, make sure * is not globbed', t => {
const result = shell.grep(/l*\.js/, 'test/resources/grep/file');
t.falsy(shell.error());
t.is(result.toString(), 'this line ends in.js\nlllllllllllllllll.js\n');
});
test('one file, * in string-regex, make sure * is not globbed', t => {
const result = shell.grep('l*\\.js', 'test/resources/grep/file');
t.falsy(shell.error());
t.is(result.toString(), 'this line ends in.js\nlllllllllllllllll.js\n');
});
test('-l option', t => {
const result = shell.grep('-l', 'test1', 'test/resources/file1', 'test/resources/file2',
'test/resources/file1.txt');
t.falsy(shell.error());
t.truthy(result.match(/file1(\n|$)/));
t.truthy(result.match(/file1.txt/));
t.falsy(result.match(/file2.txt/));
t.is(result.split('\n').length - 1, 2);
});
test('-i option', t => {
const result = shell.grep('-i', 'test', 'test/resources/grep/case1', 'test/resources/grep/case1.txt',
'test/resources/grep/case1.js');
t.falsy(shell.error());
t.is(result.split('\n').length - 1, 3);
});
test('the pattern looks like an option', t => {
const result = shell.grep('--', '-v', 'test/resources/grep/file2');
t.falsy(shell.error());
t.is(result.toString(), '-v\n-vv\n');
});