|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import assert from 'node:assert'; |
| 10 | +import { existsSync } from 'node:fs'; |
| 11 | +import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'; |
| 12 | +import { join } from 'node:path'; |
| 13 | +import { type EmittableFile, deleteFilesFromDisk, emitFilesToDisk } from './disk-files'; |
| 14 | + |
| 15 | +describe('disk-files', () => { |
| 16 | + let tempRoot: string; |
| 17 | + |
| 18 | + beforeEach(async () => { |
| 19 | + const baseTmpDir = process.env['TEST_TMPDIR']; |
| 20 | + assert(baseTmpDir, 'TEST_TMPDIR is not set'); |
| 21 | + tempRoot = await mkdtemp(join(baseTmpDir, 'angular-cli-disk-files-test-')); |
| 22 | + }); |
| 23 | + |
| 24 | + afterEach(async () => { |
| 25 | + if (tempRoot) { |
| 26 | + await rm(tempRoot, { recursive: true, force: true }); |
| 27 | + } |
| 28 | + }); |
| 29 | + |
| 30 | + describe('emitFilesToDisk', () => { |
| 31 | + it('should write memory files and copy disk files while creating nested directories', async () => { |
| 32 | + const sourceFile = join(tempRoot, 'source-asset.txt'); |
| 33 | + await writeFile(sourceFile, 'from-disk'); |
| 34 | + |
| 35 | + const files: (EmittableFile & { path: string })[] = [ |
| 36 | + { |
| 37 | + origin: 'memory', |
| 38 | + path: 'nested/a/mem1.txt', |
| 39 | + contents: new TextEncoder().encode('mem-1'), |
| 40 | + }, |
| 41 | + { origin: 'memory', path: 'nested/a/mem2.txt', contents: 'mem-2' }, |
| 42 | + { origin: 'disk', path: 'nested/b/disk1.txt', source: sourceFile }, |
| 43 | + { origin: 'disk', path: 'nested/c/disk2.txt', source: sourceFile }, |
| 44 | + { origin: 'memory', path: 'skipped/file.txt', contents: 'should-not-exist' }, |
| 45 | + ]; |
| 46 | + |
| 47 | + const outDir = join(tempRoot, 'out'); |
| 48 | + await emitFilesToDisk(files, (file) => |
| 49 | + file.path.startsWith('skipped/') ? undefined : join(outDir, file.path), |
| 50 | + ); |
| 51 | + |
| 52 | + expect(await readFile(join(outDir, 'nested/a/mem1.txt'), 'utf-8')).toBe('mem-1'); |
| 53 | + expect(await readFile(join(outDir, 'nested/a/mem2.txt'), 'utf-8')).toBe('mem-2'); |
| 54 | + expect(await readFile(join(outDir, 'nested/b/disk1.txt'), 'utf-8')).toBe('from-disk'); |
| 55 | + expect(await readFile(join(outDir, 'nested/c/disk2.txt'), 'utf-8')).toBe('from-disk'); |
| 56 | + expect(existsSync(join(outDir, 'skipped/file.txt'))).toBeFalse(); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + describe('deleteFilesFromDisk', () => { |
| 61 | + it('should delete existing files and ignore non-existent files', async () => { |
| 62 | + const fileA = join(tempRoot, 'a.txt'); |
| 63 | + const fileB = join(tempRoot, 'b.txt'); |
| 64 | + const nonExistent = join(tempRoot, 'missing.txt'); |
| 65 | + |
| 66 | + await writeFile(fileA, 'a'); |
| 67 | + await writeFile(fileB, 'b'); |
| 68 | + |
| 69 | + await deleteFilesFromDisk([fileA, nonExistent]); |
| 70 | + |
| 71 | + expect(existsSync(fileA)).toBeFalse(); |
| 72 | + expect(existsSync(fileB)).toBeTrue(); |
| 73 | + }); |
| 74 | + }); |
| 75 | +}); |
0 commit comments