-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Expand file tree
/
Copy pathdelete-output-dir_spec.ts
More file actions
68 lines (54 loc) · 2.35 KB
/
delete-output-dir_spec.ts
File metadata and controls
68 lines (54 loc) · 2.35 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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { mkdir, mkdtemp, readdir, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { deleteOutputDir } from './delete-output-dir';
describe('deleteOutputDir', () => {
let root: string;
beforeEach(async () => {
root = await mkdtemp(join(tmpdir(), 'ng-test-'));
});
it('should throw when output path is the project root', async () => {
await expectAsync(deleteOutputDir(root, '.')).toBeRejectedWithError(
/MUST not be the project root directory or its parent/,
);
});
it('should throw when output path is a parent of the project root', async () => {
await expectAsync(deleteOutputDir(root, '..')).toBeRejectedWithError(
/MUST not be the project root directory or its parent/,
);
});
it('should throw when output path is a grandparent of the project root', async () => {
await expectAsync(deleteOutputDir(root, '../..')).toBeRejectedWithError(
/MUST not be the project root directory or its parent/,
);
});
it('should not throw when output path is a child of the project root', async () => {
const outputDir = join(root, 'dist');
await mkdir(outputDir, { recursive: true });
await writeFile(join(outputDir, 'old-file.txt'), 'content');
await expectAsync(deleteOutputDir(root, 'dist')).toBeResolved();
});
it('should delete contents of a valid output directory', async () => {
const outputDir = join(root, 'dist');
await mkdir(outputDir, { recursive: true });
await writeFile(join(outputDir, 'old-file.txt'), 'content');
await deleteOutputDir(root, 'dist');
const entries = await readdir(outputDir);
expect(entries.length).toBe(0);
});
it('should not throw when output directory does not exist', async () => {
await expectAsync(deleteOutputDir(root, 'nonexistent')).toBeResolved();
});
it('should not throw when output path is an absolute path outside the project', async () => {
const externalDir = await mkdtemp(join(tmpdir(), 'ng-test-external-'));
await writeFile(join(externalDir, 'old-file.txt'), 'content');
await expectAsync(deleteOutputDir(root, externalDir)).toBeResolved();
});
});