-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtest.js
More file actions
379 lines (300 loc) · 11.7 KB
/
test.js
File metadata and controls
379 lines (300 loc) · 11.7 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';
import test from 'ava';
import {execa} from 'execa';
import tempWrite from 'temp-write';
test('--string', async t => {
const filePath = await tempWrite('foo bar foo');
await execa('./cli.js', ['--string=bar', '--replacement=foo', filePath]);
t.is(fs.readFileSync(filePath, 'utf8'), 'foo foo foo');
});
test('--regex', async t => {
const filePath = await tempWrite('foo bar foo');
await execa('./cli.js', [String.raw`--regex=\bb.*?\b`, '--replacement=foo', filePath]);
t.is(fs.readFileSync(filePath, 'utf8'), 'foo foo foo');
});
test('newlines and tabs', async t => {
const filePath = await tempWrite('a,b,c');
await execa('./cli.js', ['--string=,', String.raw`--replacement=\n`, filePath]);
t.is(fs.readFileSync(filePath, 'utf8'), 'a\nb\nc');
const filePath2 = await tempWrite('a,b,c');
await execa('./cli.js', ['--string=,', String.raw`--replacement=\t`, filePath2]);
t.is(fs.readFileSync(filePath2, 'utf8'), 'a\tb\tc');
const filePath3 = await tempWrite('a,b,c');
await execa('./cli.js', ['--string=,', String.raw`--replacement=\r`, filePath3]);
t.is(fs.readFileSync(filePath3, 'utf8'), 'a\rb\rc');
});
test('multiple newlines and tabs', async t => {
const filePath = await tempWrite('a,b,c');
await execa('./cli.js', ['--string=,', String.raw`--replacement=\n\n\t\r`, filePath]);
t.is(fs.readFileSync(filePath, 'utf8'), 'a\n\n\t\rb\n\n\t\rc');
});
test('globs', async t => {
const filePaths = [await tempWrite('foo bar foo', 'a.glob'), await tempWrite('foo bar foo', 'b.glob')];
const dirnames = filePaths.map(filePath => path.dirname(filePath));
await execa('./cli.js', ['--string=bar', '--replacement=foo', path.join(dirnames[0], '*.glob'), path.join(dirnames[1], '*.glob')]);
t.is(fs.readFileSync(filePaths[0], 'utf8'), 'foo foo foo');
t.is(fs.readFileSync(filePaths[1], 'utf8'), 'foo foo foo');
});
test('no globs', async t => {
const filePaths = [await tempWrite('foo bar foo', process.platform === 'win32' ? 'STAR.glob' : '*.glob'), await tempWrite('foo bar foo', 'foo.glob')];
const dirnames = filePaths.map(filePath => path.dirname(filePath));
await t.throwsAsync(
execa('./cli.js', [
'--string=bar',
'--replacement=foo',
'--no-glob',
path.join(dirnames[0], '*.glob'),
path.join(dirnames[1], '*.glob'),
]),
{
message: /ENOENT/,
},
);
});
test('--dry-run shows changes without modifying files', async t => {
const filePath = await tempWrite('foo bar foo');
const {stdout} = await execa('./cli.js', ['--dry-run', '--string=bar', '--replacement=baz', filePath]);
t.is(fs.readFileSync(filePath, 'utf8'), 'foo bar foo');
t.true(stdout.includes(filePath));
t.true(stdout.includes('foo bar foo'));
t.true(stdout.includes('foo baz foo'));
});
test('--dry-run with no matches', async t => {
const filePath = await tempWrite('foo bar foo');
const {stdout} = await execa('./cli.js', ['--dry-run', '--string=notfound', '--replacement=replacement', filePath]);
t.is(fs.readFileSync(filePath, 'utf8'), 'foo bar foo');
t.is(stdout.trim(), 'No matches found.');
});
test('--dry-run with regex', async t => {
const filePath = await tempWrite('version 1.2.3 here');
const {stdout} = await execa('./cli.js', ['--dry-run', String.raw`--regex=\d+\.\d+\.\d+`, '--replacement=2.0.0', filePath]);
t.is(fs.readFileSync(filePath, 'utf8'), 'version 1.2.3 here');
t.true(stdout.includes('version 1.2.3 here'));
t.true(stdout.includes('version 2.0.0 here'));
});
test('prepend using regex ^', async t => {
const filePath = await tempWrite('line 1\nline 2');
await execa('./cli.js', ['--regex=^', '--replacement=PREPENDED: ', filePath]);
t.is(fs.readFileSync(filePath, 'utf8'), 'PREPENDED: line 1\nline 2');
});
test('append using regex $ with global flag', async t => {
const filePath = await tempWrite('line 1\nline 2');
await execa('./cli.js', ['--regex=$', '--replacement= :APPENDED', filePath]);
t.is(fs.readFileSync(filePath, 'utf8'), 'line 1\nline 2 :APPENDED');
});
test('transform option - prepend', async t => {
const {default: replaceInFiles} = await import('./api.js');
const filePath = await tempWrite('line 1\nline 2');
await replaceInFiles(filePath, {
transform: content => `PREPENDED\n${content}`,
});
t.is(fs.readFileSync(filePath, 'utf8'), 'PREPENDED\nline 1\nline 2');
});
test('transform option - append', async t => {
const {default: replaceInFiles} = await import('./api.js');
const filePath = await tempWrite('line 1\nline 2');
await replaceInFiles(filePath, {
transform: content => `${content}\nAPPENDED`,
});
t.is(fs.readFileSync(filePath, 'utf8'), 'line 1\nline 2\nAPPENDED');
});
test('transform option - with file path parameter', async t => {
const {default: replaceInFiles} = await import('./api.js');
const filePath = await tempWrite('content');
await replaceInFiles(filePath, {
transform: (content, path) => `FILE: ${path} | CONTENT: ${content}`,
});
const result = fs.readFileSync(filePath, 'utf8');
t.true(result.includes('FILE: ') && result.includes('CONTENT: content'));
});
test('transform option - dry run', async t => {
const {default: replaceInFiles} = await import('./api.js');
const filePath = await tempWrite('original');
const changes = await replaceInFiles(filePath, {
transform: content => `transformed: ${content}`,
dryRun: true,
});
t.is(fs.readFileSync(filePath, 'utf8'), 'original');
t.is(changes.length, 1);
t.is(changes[0].originalContent, 'original');
t.is(changes[0].newContent, 'transformed: original');
});
test('transform option - no change when content identical', async t => {
const {default: replaceInFiles} = await import('./api.js');
const filePath = await tempWrite('unchanged');
const changes = await replaceInFiles(filePath, {
transform: content => content,
dryRun: true,
});
t.is(changes.length, 0);
});
test('transform option - error when not a function', async t => {
const {default: replaceInFiles} = await import('./api.js');
const filePath = await tempWrite('content');
await t.throwsAsync(
replaceInFiles(filePath, {
transform: 'not a function',
}),
{
message: 'The `transform` option must be a function',
},
);
});
test('transform + find/replace - transform runs after find/replace', async t => {
const {default: replaceInFiles} = await import('./api.js');
const filePath = await tempWrite('Hello world!');
await replaceInFiles(filePath, {
find: ['world'],
replacement: 'universe',
transform: content => `BANNER\n${content}\nFOOTER`,
});
t.is(fs.readFileSync(filePath, 'utf8'), 'BANNER\nHello universe!\nFOOTER');
});
test('transform + find/replace - dry run', async t => {
const {default: replaceInFiles} = await import('./api.js');
const filePath = await tempWrite('Hello world!');
const changes = await replaceInFiles(filePath, {
find: ['world'],
replacement: 'universe',
transform: content => `[${content}]`,
dryRun: true,
});
t.is(fs.readFileSync(filePath, 'utf8'), 'Hello world!');
t.is(changes.length, 1);
t.is(changes[0].originalContent, 'Hello world!');
t.is(changes[0].newContent, '[Hello universe!]');
});
test('transform only - no find/replace required', async t => {
const {default: replaceInFiles} = await import('./api.js');
const filePath = await tempWrite('content');
await replaceInFiles(filePath, {
transform: content => `wrapped: ${content}`,
});
t.is(fs.readFileSync(filePath, 'utf8'), 'wrapped: content');
});
test('find/replace without transform - still works', async t => {
const {default: replaceInFiles} = await import('./api.js');
const filePath = await tempWrite('Hello world!');
await replaceInFiles(filePath, {
find: ['world'],
replacement: 'universe',
});
t.is(fs.readFileSync(filePath, 'utf8'), 'Hello universe!');
});
test('error when neither find nor transform provided', async t => {
const {default: replaceInFiles} = await import('./api.js');
const filePath = await tempWrite('content');
await t.throwsAsync(
replaceInFiles(filePath, {}),
{
message: 'Expected at least one `find` pattern or a `transform` function',
},
);
});
test('ignoreCase overrides string patterns', async t => {
const {default: replaceInFiles} = await import('./api.js');
const filePath = await tempWrite('Hello WORLD world');
await replaceInFiles(filePath, {
find: ['world'],
replacement: 'universe',
ignoreCase: true,
});
t.is(fs.readFileSync(filePath, 'utf8'), 'Hello universe universe');
});
test('ignoreCase overrides regex patterns', async t => {
const {default: replaceInFiles} = await import('./api.js');
const filePath = await tempWrite('Hello WORLD world');
await replaceInFiles(filePath, {
find: [/world/],
replacement: 'universe',
ignoreCase: true,
});
t.is(fs.readFileSync(filePath, 'utf8'), 'Hello universe universe');
});
test('ignoreCase overrides regex with existing i flag', async t => {
const {default: replaceInFiles} = await import('./api.js');
const filePath = await tempWrite('Hello WORLD world');
await replaceInFiles(filePath, {
find: [/world/i],
replacement: 'universe',
ignoreCase: false, // Should remove the i flag
});
// Without ignoreCase, only lowercase 'world' should match
t.is(fs.readFileSync(filePath, 'utf8'), 'Hello WORLD universe');
});
test('multiple find patterns applied in order', async t => {
const {default: replaceInFiles} = await import('./api.js');
const filePath = await tempWrite('apple banana cherry');
await replaceInFiles(filePath, {
find: ['apple', 'banana'],
replacement: 'fruit',
});
t.is(fs.readFileSync(filePath, 'utf8'), 'fruit fruit cherry');
});
test('no-op writes - unchanged files not touched', async t => {
const {default: replaceInFiles} = await import('./api.js');
const filePath = await tempWrite('unchanged content');
const statBefore = await fs.promises.stat(filePath);
// Add a tiny delay to ensure mtime would change if file was written
await new Promise(resolve => {
setTimeout(resolve, 10);
});
await replaceInFiles(filePath, {
find: ['nonexistent'],
replacement: 'something',
});
const statAfter = await fs.promises.stat(filePath);
t.deepEqual(statBefore.mtime, statAfter.mtime);
t.is(fs.readFileSync(filePath, 'utf8'), 'unchanged content');
});
test('transform must return string', async t => {
const {default: replaceInFiles} = await import('./api.js');
const filePath = await tempWrite('content');
await t.throwsAsync(
replaceInFiles(filePath, {
transform: () => 123, // Returns number instead of string
}),
{
message: '`transform` must return a string',
},
);
});
test('dry-run returns structured FileChange array', async t => {
const {default: replaceInFiles} = await import('./api.js');
const filePath = await tempWrite('original content');
const changes = await replaceInFiles(filePath, {
find: ['original'],
replacement: 'modified',
dryRun: true,
});
t.true(Array.isArray(changes));
t.is(changes.length, 1);
t.is(changes[0].filePath, filePath);
t.is(changes[0].originalContent, 'original content');
t.is(changes[0].newContent, 'modified content');
});
test('file deduplication', async t => {
const {default: replaceInFiles} = await import('./api.js');
const filePath = await tempWrite('test content');
// Pass same file multiple times
await replaceInFiles([filePath, filePath, filePath], {
find: ['test'],
replacement: 'modified',
});
t.is(fs.readFileSync(filePath, 'utf8'), 'modified content');
});
test('globby options applied correctly', async t => {
const {default: replaceInFiles} = await import('./api.js');
// Create files and test absolute paths are returned
const filePath = await tempWrite('content');
const changes = await replaceInFiles([filePath], {
find: ['content'],
replacement: 'modified',
dryRun: true,
});
// Should have absolute path
t.is(changes.length, 1);
t.true(path.isAbsolute(changes[0].filePath));
});