-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathvirtual-fs.unit.test.ts
More file actions
191 lines (161 loc) · 5.56 KB
/
virtual-fs.unit.test.ts
File metadata and controls
191 lines (161 loc) · 5.56 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
import { toUnixPath } from '@code-pushup/utils';
import type { FileSystemAdapter } from './types.js';
import { createTree } from './virtual-fs.js';
function createMockFs(
files: Record<string, string> = {},
): FileSystemAdapter & { written: Map<string, string>; dirs: Set<string> } {
const store = new Map(Object.entries(files));
const written = new Map<string, string>();
const dirs = new Set<string>();
return {
written,
dirs,
async readFile(path: string) {
const content = store.get(toUnixPath(path));
if (content == null) {
throw new Error(`ENOENT: no such file or directory, open '${path}'`);
}
return content;
},
async writeFile(path: string, content: string) {
store.set(toUnixPath(path), content);
written.set(toUnixPath(path), content);
},
async exists(path: string) {
return store.has(toUnixPath(path));
},
async mkdir(path: string): Promise<undefined> {
dirs.add(toUnixPath(path));
},
};
}
describe('createTree', () => {
it('should report the root directory', () => {
expect(createTree('/project').root).toBe('/project');
});
describe('exists', () => {
it('should return false for non-existent files', async () => {
await expect(
createTree('/project', createMockFs()).exists('missing.ts'),
).resolves.toBeFalse();
});
it('should return true for files on disk', async () => {
await expect(
createTree(
'/project',
createMockFs({ '/project/existing.ts': 'content' }),
).exists('existing.ts'),
).resolves.toBeTrue();
});
it('should return true for files written to the tree', async () => {
const tree = createTree('/project', createMockFs());
await tree.write('new.ts', 'content');
await expect(tree.exists('new.ts')).resolves.toBeTrue();
});
});
describe('read', () => {
it('should return null for non-existent files', async () => {
await expect(
createTree('/project', createMockFs()).read('missing.ts'),
).resolves.toBeNull();
});
it('should read files from disk', async () => {
await expect(
createTree(
'/project',
createMockFs({ '/project/existing.ts': 'disk content' }),
).read('existing.ts'),
).resolves.toBe('disk content');
});
it('should return pending content over disk content', async () => {
const tree = createTree(
'/project',
createMockFs({ '/project/file.ts': 'old' }),
);
await tree.write('file.ts', 'new');
await expect(tree.read('file.ts')).resolves.toBe('new');
});
});
describe('write', () => {
it('should mark new files as CREATE', async () => {
const tree = createTree('/project', createMockFs());
await tree.write('new.ts', 'content');
expect(tree.listChanges()).toStrictEqual([
{ path: 'new.ts', type: 'CREATE', content: 'content' },
]);
});
it('should preserve CREATE type when writing to the same path twice', async () => {
const tree = createTree('/project', createMockFs());
await tree.write('new.ts', 'first');
await tree.write('new.ts', 'second');
expect(tree.listChanges()).toStrictEqual([
{ path: 'new.ts', type: 'CREATE', content: 'second' },
]);
});
it('should mark existing files as UPDATE', async () => {
const tree = createTree(
'/project',
createMockFs({ '/project/existing.ts': 'old' }),
);
await tree.write('existing.ts', 'new');
expect(tree.listChanges()).toStrictEqual([
{ path: 'existing.ts', type: 'UPDATE', content: 'new' },
]);
});
});
describe('listChanges', () => {
it('should return empty array when no changes are detected', () => {
expect(
createTree('/project', createMockFs()).listChanges(),
).toStrictEqual([]);
});
it('should return all pending changes', async () => {
const tree = createTree(
'/project',
createMockFs({ '/project/existing.ts': 'old' }),
);
await tree.write('new.ts', 'created');
await tree.write('existing.ts', 'updated');
expect(tree.listChanges()).toHaveLength(2);
expect(tree.listChanges()).toContainEqual({
path: 'new.ts',
type: 'CREATE',
content: 'created',
});
expect(tree.listChanges()).toContainEqual({
path: 'existing.ts',
type: 'UPDATE',
content: 'updated',
});
});
});
describe('flush', () => {
it('should write all pending files to the fs', async () => {
const fs = createMockFs();
const tree = createTree('/project', fs);
await tree.write('src/config.ts', 'export default {};');
await tree.flush();
expect(fs.written.get('/project/src/config.ts')).toBe(
'export default {};',
);
});
it('should create parent directories', async () => {
const fs = createMockFs();
const tree = createTree('/project', fs);
await tree.write('src/deep/config.ts', 'content');
await tree.flush();
expect(fs.dirs).toContain('/project/src/deep');
});
it('should clear pending changes after flush', async () => {
const tree = createTree('/project', createMockFs());
await tree.write('file.ts', 'content');
await tree.flush();
expect(tree.listChanges()).toStrictEqual([]);
});
it('should not write anything when no changes are pending', async () => {
const fs = createMockFs();
await createTree('/project', fs).flush();
expect(fs.written.size).toBe(0);
});
});
});