-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmaterialize-standalone.test.ts
More file actions
152 lines (136 loc) · 6.24 KB
/
Copy pathmaterialize-standalone.test.ts
File metadata and controls
152 lines (136 loc) · 6.24 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
import { existsSync, promises as fs } from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { afterEach, beforeEach, describe, expect, test } from 'vitest';
import {
materializeStandalone,
SCRIPTS_DIR_NAME,
STANDALONE_ROOT_FILES,
STANDALONE_SCRIPT_FILES,
STANDALONE_SUPPORT_FILES,
SUPPORT_DIR_NAME,
} from '../../request-validation/src/emit/materializeStandalone.ts';
describe('materializeStandalone', () => {
let tmp: string;
beforeEach(async () => {
tmp = await fs.mkdtemp(path.join(os.tmpdir(), 'mat-standalone-'));
});
afterEach(async () => {
await fs.rm(tmp, { recursive: true, force: true });
});
test('writes all support files into <outDir>/support/ and all root scaffolding files into <outDir>/', async () => {
const supportDir = await materializeStandalone(tmp);
expect(supportDir).toBe(path.join(tmp, SUPPORT_DIR_NAME));
const supportEntries = (await fs.readdir(supportDir)).sort();
expect(supportEntries).toEqual([...STANDALONE_SUPPORT_FILES].sort());
for (const name of STANDALONE_ROOT_FILES) {
const dest = path.join(tmp, name);
expect(existsSync(dest)).toBe(true);
const stat = await fs.stat(dest);
expect(stat.size).toBeGreaterThan(0);
}
});
test('writes all analyser scripts into <outDir>/scripts/', async () => {
await materializeStandalone(tmp);
const scriptsDir = path.join(tmp, SCRIPTS_DIR_NAME);
expect(existsSync(scriptsDir)).toBe(true);
const entries = (await fs.readdir(scriptsDir)).sort();
expect(entries).toEqual([...STANDALONE_SCRIPT_FILES].sort());
for (const name of STANDALONE_SCRIPT_FILES) {
const stat = await fs.stat(path.join(scriptsDir, name));
expect(stat.size).toBeGreaterThan(0);
}
});
test('is idempotent: a second call overwrites without error', async () => {
await materializeStandalone(tmp);
await expect(materializeStandalone(tmp)).resolves.toBe(path.join(tmp, SUPPORT_DIR_NAME));
});
test('respects the templatesDir override and copies from a custom source', async () => {
const fakeSrc = await fs.mkdtemp(path.join(os.tmpdir(), 'mat-standalone-src-'));
try {
const fakeSupport = path.join(fakeSrc, SUPPORT_DIR_NAME);
await fs.mkdir(fakeSupport, { recursive: true });
for (const name of STANDALONE_SUPPORT_FILES) {
await fs.writeFile(path.join(fakeSupport, name), `// fake-support-${name}\n`, 'utf8');
}
const fakeScripts = path.join(fakeSrc, SCRIPTS_DIR_NAME);
await fs.mkdir(fakeScripts, { recursive: true });
for (const name of STANDALONE_SCRIPT_FILES) {
await fs.writeFile(path.join(fakeScripts, name), `// fake-script-${name}\n`, 'utf8');
}
for (const name of STANDALONE_ROOT_FILES) {
await fs.writeFile(path.join(fakeSrc, name), `// fake-root-${name}\n`, 'utf8');
}
await materializeStandalone(tmp, fakeSrc);
for (const name of STANDALONE_SUPPORT_FILES) {
const content = await fs.readFile(path.join(tmp, SUPPORT_DIR_NAME, name), 'utf8');
expect(content).toBe(`// fake-support-${name}\n`);
}
for (const name of STANDALONE_SCRIPT_FILES) {
const content = await fs.readFile(path.join(tmp, SCRIPTS_DIR_NAME, name), 'utf8');
expect(content).toBe(`// fake-script-${name}\n`);
}
for (const name of STANDALONE_ROOT_FILES) {
const content = await fs.readFile(path.join(tmp, name), 'utf8');
expect(content).toBe(`// fake-root-${name}\n`);
}
} finally {
await fs.rm(fakeSrc, { recursive: true, force: true });
}
});
test('overwriteRoot=false preserves existing root files but still refreshes support files', async () => {
const fakeSrc = await fs.mkdtemp(path.join(os.tmpdir(), 'mat-standalone-src-'));
try {
const fakeSupport = path.join(fakeSrc, SUPPORT_DIR_NAME);
await fs.mkdir(fakeSupport, { recursive: true });
for (const name of STANDALONE_SUPPORT_FILES) {
await fs.writeFile(path.join(fakeSupport, name), 'NEW SUPPORT', 'utf8');
}
const fakeScripts = path.join(fakeSrc, SCRIPTS_DIR_NAME);
await fs.mkdir(fakeScripts, { recursive: true });
for (const name of STANDALONE_SCRIPT_FILES) {
await fs.writeFile(path.join(fakeScripts, name), 'NEW SCRIPT', 'utf8');
}
for (const name of STANDALONE_ROOT_FILES) {
await fs.writeFile(path.join(fakeSrc, name), 'NEW ROOT', 'utf8');
}
// Pre-populate one root file with user edits.
await fs.writeFile(path.join(tmp, 'package.json'), 'USER EDITED', 'utf8');
await materializeStandalone(tmp, fakeSrc, /* overwriteRoot */ false);
// User-edited root file preserved.
expect(await fs.readFile(path.join(tmp, 'package.json'), 'utf8')).toBe('USER EDITED');
// Other root files materialized for the first time.
expect(await fs.readFile(path.join(tmp, 'tsconfig.json'), 'utf8')).toBe('NEW ROOT');
// Support files always overwritten.
for (const name of STANDALONE_SUPPORT_FILES) {
expect(await fs.readFile(path.join(tmp, SUPPORT_DIR_NAME, name), 'utf8')).toBe(
'NEW SUPPORT',
);
}
} finally {
await fs.rm(fakeSrc, { recursive: true, force: true });
}
});
test('fails with a clear filesystem error when a required template is missing', async () => {
const fakeSrc = await fs.mkdtemp(path.join(os.tmpdir(), 'mat-standalone-src-'));
try {
// Create support/ but omit one required support file.
const fakeSupport = path.join(fakeSrc, SUPPORT_DIR_NAME);
await fs.mkdir(fakeSupport, { recursive: true });
for (const name of STANDALONE_SUPPORT_FILES.slice(1)) {
await fs.writeFile(path.join(fakeSupport, name), 'x', 'utf8');
}
const fakeScripts = path.join(fakeSrc, SCRIPTS_DIR_NAME);
await fs.mkdir(fakeScripts, { recursive: true });
for (const name of STANDALONE_SCRIPT_FILES) {
await fs.writeFile(path.join(fakeScripts, name), 'x', 'utf8');
}
for (const name of STANDALONE_ROOT_FILES) {
await fs.writeFile(path.join(fakeSrc, name), 'x', 'utf8');
}
await expect(materializeStandalone(tmp, fakeSrc)).rejects.toThrow(/ENOENT|no such file/i);
} finally {
await fs.rm(fakeSrc, { recursive: true, force: true });
}
});
});