/
sed.js
182 lines (156 loc) · 5.31 KB
/
sed.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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 arguments', t => {
const result = shell.sed();
t.truthy(shell.error());
t.is(result.code, 1);
t.truthy(result.stderr);
});
test('only one argument', t => {
const result = shell.sed(/asdf/g);
t.truthy(shell.error());
t.is(result.code, 1);
});
test('only two arguments', t => {
const result = shell.sed(/asdf/g, 'nada');
t.truthy(shell.error());
t.is(result.code, 1);
});
test('no such file', t => {
t.falsy(fs.existsSync('asdfasdf')); // sanity check
const result = shell.sed(/asdf/g, 'nada', 'asdfasdf');
t.truthy(shell.error());
t.is(result.code, 2);
t.is(result.stderr, 'sed: no such file or directory: asdfasdf');
});
// TODO(nate): flaky test
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.sed(/asdf/g, 'nada', `${t.context.tmp}/file1`, 'asdfasdf');
t.truthy(shell.error());
t.is(result.code, 2);
t.is(result.stderr, 'sed: no such file or directory: asdfasdf');
});
//
// Valids
//
test('search with a string', t => {
const result = shell.sed('test1', 'hello', `${t.context.tmp}/file1`);
t.falsy(shell.error());
t.is(result.code, 0);
t.is(result.toString(), 'hello');
});
test('search with a regex', t => {
const result = shell.sed(/test1/, 'hello', `${t.context.tmp}/file1`);
t.falsy(shell.error());
t.is(result.code, 0);
t.is(result.toString(), 'hello');
});
test('replace with a number instead of a string', t => {
const result = shell.sed(/test1/, 1234, `${t.context.tmp}/file1`);
t.falsy(shell.error());
t.is(result.code, 0);
t.is(result.toString(), '1234');
});
test('replace using a function', t => {
const replaceFun = match => match.toUpperCase() + match;
const result = shell.sed(/test1/, replaceFun, `${t.context.tmp}/file1`);
t.falsy(shell.error());
t.is(result.code, 0);
t.is(result.toString(), 'TEST1test1');
});
test('-i option', t => {
const result = shell.sed('-i', /test1/, 'hello', `${t.context.tmp}/file1`);
t.falsy(shell.error());
t.is(result.code, 0);
t.is(result.toString(), 'hello');
t.is(shell.cat(`${t.context.tmp}/file1`).toString(), 'hello');
});
test('make sure * in regex is not globbed', t => {
const result = shell.sed(/alpha*beta/, 'hello', 'test/resources/grep/file');
t.falsy(shell.error());
t.is(result.code, 0);
t.is(
result.toString(),
'hello\nhowareyou\nhello\nthis line ends in.js\nlllllllllllllllll.js\n'
);
});
test('make sure * in string-regex is not globbed', t => {
const result = shell.sed('alpha*beta', 'hello', 'test/resources/grep/file');
t.falsy(shell.error());
t.is(result.code, 0);
t.is(
result.toString(),
'hello\nhowareyou\nhello\nthis line ends in.js\nlllllllllllllllll.js\n'
);
});
test('make sure * in regex is not globbed (matches something)', t => {
const result = shell.sed(/l*\.js/, '', 'test/resources/grep/file');
t.falsy(shell.error());
t.is(result.code, 0);
t.is(
result.toString(),
'alphaaaaaaabeta\nhowareyou\nalphbeta\nthis line ends in\n\n'
);
});
test('make sure * in string-regex is not globbed (matches something)', t => {
const result = shell.sed('l*\\.js', '', 'test/resources/grep/file');
t.falsy(shell.error());
t.is(result.code, 0);
t.is(
result.toString(),
'alphaaaaaaabeta\nhowareyou\nalphbeta\nthis line ends in\n\n'
);
});
test('multiple file names', t => {
const result = shell.sed('test', 'hello', `${t.context.tmp}/file1`,
`${t.context.tmp}/file2`);
t.falsy(shell.error());
t.is(result.code, 0);
t.is(result.toString(), 'hello1\nhello2');
});
test('array of file names (and try it out with a simple regex)', t => {
const result = shell.sed(/t.*st/, 'hello', [`${t.context.tmp}/file1`,
`${t.context.tmp}/file2`]);
t.falsy(shell.error());
t.is(result.code, 0);
t.is(result.toString(), 'hello1\nhello2');
});
test('multiple file names, with in-place-replacement', t => {
const result = shell.sed('-i', 'test', 'hello', [`${t.context.tmp}/file1`,
`${t.context.tmp}/file2`]);
t.falsy(shell.error());
t.is(result.code, 0);
t.is(result.toString(), 'hello1\nhello2');
t.is(shell.cat(`${t.context.tmp}/file1`).toString(), 'hello1');
t.is(shell.cat(`${t.context.tmp}/file2`).toString(), 'hello2');
});
test('glob file names, with in-place-replacement', t => {
t.is(shell.cat(`${t.context.tmp}/file1.txt`).toString(), 'test1\n');
t.is(shell.cat(`${t.context.tmp}/file2.txt`).toString(), 'test2\n');
const result = shell.sed('-i', 'test', 'hello', `${t.context.tmp}/file*.txt`);
t.falsy(shell.error());
t.is(result.code, 0);
t.is(result.toString(), 'hello1\n\nhello2\n'); // TODO: fix sed's behavior
t.is(shell.cat(`${t.context.tmp}/file1.txt`).toString(), 'hello1\n');
t.is(shell.cat(`${t.context.tmp}/file2.txt`).toString(), 'hello2\n');
});
test('empty file', t => {
const result = shell.sed('widget', 'wizzle', 'test/resources/sed/empty.txt');
t.is(result.code, 0);
t.is(result.toString(), '');
});