-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patheditFileBySubstring.test.js
More file actions
89 lines (81 loc) · 3.1 KB
/
editFileBySubstring.test.js
File metadata and controls
89 lines (81 loc) · 3.1 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
import test from 'ava';
import editFileBySubstring from '../../src/tools/editFileBySubstring.js';
import fs from 'fs/promises';
import path from 'path';
import os from 'os';
const testDir = path.join(os.tmpdir(), 'editFileTests');
let i = 1;
function setup() {
return {
testFile: path.join(testDir, `testReplaceInFile.tmp${i++}.txt`),
};
}
// Creating a temporary test file before the tests
test.before(async () => {
await fs.mkdir(testDir, { recursive: true });
});
// Cleanup: remove the temporary file and directory after the tests
test.after.always(async () => {
await fs.rm(testDir, { force: true, recursive: true });
});
test('editFileBySubstring replaces a string within a file', async (t) => {
const { testFile } = setup();
await fs.writeFile(testFile, 'Hello World, Hello Universe', 'utf8');
await editFileBySubstring({
filepath: testFile,
uniqueContext: 'Hello World, Hello Universe',
exactTarget: 'Universe',
replacement: 'AVA',
});
const content = await fs.readFile(testFile, 'utf8');
t.is(content, 'Hello World, Hello AVA', 'Content should be replaced correctly');
});
test('editFileBySubstring fails with multiple instances of the search context', async (t) => {
const { testFile } = setup();
await fs.writeFile(testFile, 'Hello World. Hello World.', 'utf8');
try {
await editFileBySubstring({
filepath: testFile,
uniqueContext: 'Hello World',
exactTarget: 'World',
replacement: 'AVA',
});
t.fail('editFileBySubstring should throw an error if the search context appears more than once.');
} catch (error) {
t.pass('editFileBySubstring should throw an error if the search context appears more than once.');
}
});
test('editFileBySubstring works on a file with many lines', async (t) => {
const { testFile } = setup();
const multilineContent = `First line\nSecond line target\nThird line`;
await fs.writeFile(testFile, multilineContent);
await editFileBySubstring({
filepath: testFile,
uniqueContext: '\nSecond line target\n',
exactTarget: 'target',
replacement: 'replacement',
});
const content = await fs.readFile(testFile, 'utf8');
t.is(
content,
`First line\nSecond line replacement\nThird line`,
'Content should be replaced correctly in a multi-line file.'
);
});
test('editFileBySubstring uniqueContext lets you specify a given replacement among many', async (t) => {
const { testFile } = setup();
const multiTargetContent = `Target line one\nUseless line\nTarget line two\nTarget line one`;
await fs.writeFile(testFile, multiTargetContent);
await editFileBySubstring({
filepath: testFile,
uniqueContext: 'Target line two',
exactTarget: 'Target',
replacement: 'Selected',
});
const content = await fs.readFile(testFile, 'utf8');
t.is(
content,
`Target line one\nUseless line\nSelected line two\nTarget line one`,
'Only the target within the specified unique context should be replaced.'
);
});