-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathengine-filesystem.spec.ts
More file actions
342 lines (281 loc) · 11.3 KB
/
engine-filesystem.spec.ts
File metadata and controls
342 lines (281 loc) · 11.3 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
/**
* Real filesystem integration tests for file creation
*
* These tests use actual temporary directories to verify that:
* - 404.html is copied from index.html (handled by angular-cli-ghpages)
* - Dry-run mode prevents file creation
* - Error handling works as expected
*
* Note: CNAME and .nojekyll files are now handled by gh-pages v6+ via options.
* See "gh-pages v6 delegation" tests below for verification.
*/
import { logging } from '@angular-devkit/core';
import * as fs from 'fs/promises';
import * as os from 'os';
import * as path from 'path';
import * as engine from './engine';
import { cleanupMonkeypatch } from './engine.prepare-options-helpers';
import { pathExists } from '../utils';
describe('engine - real filesystem tests', () => {
const logger = new logging.Logger('test');
let testDir: string;
let loggerInfoSpy: jest.SpyInstance;
beforeEach(async () => {
// Clean up any previous monkeypatch so each test starts fresh
cleanupMonkeypatch();
// Create a unique temp directory for each test
const tmpBase = os.tmpdir();
const uniqueDir = `angular-cli-ghpages-test-${Date.now()}-${Math.random().toString(36).substring(7)}`;
testDir = path.join(tmpBase, uniqueDir);
await fs.mkdir(testDir, { recursive: true });
// Spy on logger to capture warnings
loggerInfoSpy = jest.spyOn(logger, 'info');
});
afterEach(async () => {
// Clean up temp directory after each test
if (await pathExists(testDir)) {
await fs.rm(testDir, { recursive: true });
}
loggerInfoSpy.mockRestore();
});
afterAll(() => {
// Clean up monkeypatch after all tests
cleanupMonkeypatch();
});
describe('404.html file creation', () => {
it('should create 404.html as exact copy of index.html when notfound is true', async () => {
// First create an index.html file
const indexPath = path.join(testDir, 'index.html');
const indexContent = '<!DOCTYPE html><html><head><title>Test</title></head><body><h1>Test App</h1></body></html>';
await fs.writeFile(indexPath, indexContent);
const ghpages = require('gh-pages');
jest.spyOn(ghpages, 'clean').mockImplementation(() => {});
jest.spyOn(ghpages, 'publish').mockImplementation((_dir: unknown, _opts: unknown, callback?: (error: Error | null) => void) => {
if (callback) {
callback(null);
}
return Promise.resolve(undefined);
});
const options = {
notfound: true,
nojekyll: false,
dotfiles: true
};
await engine.run(testDir, options, logger);
const notFoundPath = path.join(testDir, '404.html');
const exists = await pathExists(notFoundPath);
expect(exists).toBe(true);
const notFoundContent = await fs.readFile(notFoundPath, 'utf-8');
expect(notFoundContent).toBe(indexContent);
});
it('should NOT create 404.html when notfound is false', async () => {
const indexPath = path.join(testDir, 'index.html');
await fs.writeFile(indexPath, '<html><body>Test</body></html>');
const ghpages = require('gh-pages');
jest.spyOn(ghpages, 'clean').mockImplementation(() => {});
jest.spyOn(ghpages, 'publish').mockImplementation((_dir: unknown, _opts: unknown, callback?: (error: Error | null) => void) => {
if (callback) {
callback(null);
}
return Promise.resolve(undefined);
});
const options = {
notfound: false,
nojekyll: false,
dotfiles: true
};
await engine.run(testDir, options, logger);
const notFoundPath = path.join(testDir, '404.html');
const exists = await pathExists(notFoundPath);
expect(exists).toBe(false);
});
it('should gracefully continue when index.html does not exist (not throw error)', async () => {
// No index.html created - directory is empty
const ghpages = require('gh-pages');
jest.spyOn(ghpages, 'clean').mockImplementation(() => {});
jest.spyOn(ghpages, 'publish').mockImplementation((_dir: unknown, _opts: unknown, callback?: (error: Error | null) => void) => {
if (callback) {
callback(null);
}
return Promise.resolve(undefined);
});
const options = {
notfound: true,
nojekyll: false,
dotfiles: true
};
// Should NOT throw - this is the critical test for graceful handling
await expect(
engine.run(testDir, options, logger)
).resolves.toBeUndefined();
// Should log a warning message
expect(loggerInfoSpy).toHaveBeenCalledWith(
'index.html could not be copied to 404.html. Proceeding without it.'
);
const notFoundPath = path.join(testDir, '404.html');
const exists = await pathExists(notFoundPath);
expect(exists).toBe(false);
});
it('should NOT create 404.html when dry-run is true', async () => {
const indexPath = path.join(testDir, 'index.html');
await fs.writeFile(indexPath, '<html><body>Test</body></html>');
const ghpages = require('gh-pages');
jest.spyOn(ghpages, 'clean').mockImplementation(() => {});
const options = {
notfound: true,
nojekyll: false,
dotfiles: true,
dryRun: true
};
await engine.run(testDir, options, logger);
const notFoundPath = path.join(testDir, '404.html');
const exists = await pathExists(notFoundPath);
expect(exists).toBe(false);
});
});
/**
* gh-pages v6+ Delegation Tests
*
* gh-pages v6.1.0 added native support for creating CNAME and .nojekyll files:
* - See: https://github.com/tschaub/gh-pages/pull/533
*
* We now delegate file creation to gh-pages via the cname/nojekyll options
* instead of creating them ourselves. This is cleaner and avoids duplication.
*
* What we're testing:
* - Verify we DO pass cname option to gh-pages when provided
* - Verify we DO pass nojekyll option to gh-pages when enabled
* - Verify 404.html is still created by us (gh-pages doesn't handle this)
*/
describe('gh-pages v6 delegation - cname and nojekyll', () => {
it('should pass cname option to gh-pages when provided', async () => {
const indexPath = path.join(testDir, 'index.html');
await fs.writeFile(indexPath, '<html>test</html>');
const ghpages = require('gh-pages');
jest.spyOn(ghpages, 'clean').mockImplementation(() => {});
let capturedOptions: { cname?: string; nojekyll?: boolean } = {};
const publishSpy = jest.spyOn(ghpages, 'publish').mockImplementation(
(_dir: string, options: { cname?: string; nojekyll?: boolean }, callback?: (error: Error | null) => void) => {
capturedOptions = options;
if (callback) {
callback(null);
}
return Promise.resolve();
}
);
const testDomain = 'example.com';
const options = {
cname: testDomain,
nojekyll: false,
notfound: false,
dotfiles: true
};
await engine.run(testDir, options, logger);
expect(publishSpy).toHaveBeenCalled();
expect(capturedOptions.cname).toBe(testDomain);
});
it('should pass nojekyll option to gh-pages when enabled', async () => {
const indexPath = path.join(testDir, 'index.html');
await fs.writeFile(indexPath, '<html>test</html>');
const ghpages = require('gh-pages');
jest.spyOn(ghpages, 'clean').mockImplementation(() => {});
let capturedOptions: { cname?: string; nojekyll?: boolean } = {};
const publishSpy = jest.spyOn(ghpages, 'publish').mockImplementation(
(_dir: string, options: { cname?: string; nojekyll?: boolean }, callback?: (error: Error | null) => void) => {
capturedOptions = options;
if (callback) {
callback(null);
}
return Promise.resolve();
}
);
const options = {
nojekyll: true,
notfound: false,
dotfiles: true
};
await engine.run(testDir, options, logger);
expect(publishSpy).toHaveBeenCalled();
expect(capturedOptions.nojekyll).toBe(true);
});
it('should pass both cname and nojekyll options when both enabled', async () => {
const indexPath = path.join(testDir, 'index.html');
await fs.writeFile(indexPath, '<html>test</html>');
const ghpages = require('gh-pages');
jest.spyOn(ghpages, 'clean').mockImplementation(() => {});
let capturedOptions: { cname?: string; nojekyll?: boolean } = {};
const publishSpy = jest.spyOn(ghpages, 'publish').mockImplementation(
(_dir: string, options: { cname?: string; nojekyll?: boolean }, callback?: (error: Error | null) => void) => {
capturedOptions = options;
if (callback) {
callback(null);
}
return Promise.resolve();
}
);
const testDomain = 'test.example.com';
const options = {
cname: testDomain,
nojekyll: true,
notfound: true,
dotfiles: true
};
await engine.run(testDir, options, logger);
expect(publishSpy).toHaveBeenCalled();
expect(capturedOptions.cname).toBe(testDomain);
expect(capturedOptions.nojekyll).toBe(true);
// Verify 404.html is still created by us (not delegated to gh-pages)
const notFoundPath = path.join(testDir, '404.html');
expect(await pathExists(notFoundPath)).toBe(true);
});
it('should NOT pass cname when not provided (undefined)', async () => {
const indexPath = path.join(testDir, 'index.html');
await fs.writeFile(indexPath, '<html>test</html>');
const ghpages = require('gh-pages');
jest.spyOn(ghpages, 'clean').mockImplementation(() => {});
let capturedOptions: { cname?: string; nojekyll?: boolean } = {};
const publishSpy = jest.spyOn(ghpages, 'publish').mockImplementation(
(_dir: string, options: { cname?: string; nojekyll?: boolean }, callback?: (error: Error | null) => void) => {
capturedOptions = options;
if (callback) {
callback(null);
}
return Promise.resolve();
}
);
const options = {
nojekyll: false,
notfound: false,
dotfiles: true
// cname not provided
};
await engine.run(testDir, options, logger);
expect(publishSpy).toHaveBeenCalled();
expect(capturedOptions.cname).toBeUndefined();
});
it('should pass nojekyll: false when disabled', async () => {
const indexPath = path.join(testDir, 'index.html');
await fs.writeFile(indexPath, '<html>test</html>');
const ghpages = require('gh-pages');
jest.spyOn(ghpages, 'clean').mockImplementation(() => {});
let capturedOptions: { cname?: string; nojekyll?: boolean } = {};
const publishSpy = jest.spyOn(ghpages, 'publish').mockImplementation(
(_dir: string, options: { cname?: string; nojekyll?: boolean }, callback?: (error: Error | null) => void) => {
capturedOptions = options;
if (callback) {
callback(null);
}
return Promise.resolve();
}
);
const options = {
nojekyll: false,
notfound: false,
dotfiles: true
};
await engine.run(testDir, options, logger);
expect(publishSpy).toHaveBeenCalled();
expect(capturedOptions.nojekyll).toBe(false);
});
});
});