|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { join } from 'node:path'; |
| 3 | +import { describe, it, mock, beforeEach } from 'node:test'; |
| 4 | + |
| 5 | +const mockCp = mock.fn(() => Promise.resolve()); |
| 6 | +mock.module('node:fs/promises', { |
| 7 | + namedExports: { cp: mockCp }, |
| 8 | +}); |
| 9 | + |
| 10 | +const mockLogError = mock.fn(); |
| 11 | +mock.module('../../../../logger/index.mjs', { |
| 12 | + defaultExport: { error: mockLogError }, |
| 13 | +}); |
| 14 | + |
| 15 | +const { copyStaticAssets } = await import('../copying.mjs'); |
| 16 | + |
| 17 | +describe('copyStaticAssets', () => { |
| 18 | + beforeEach(() => { |
| 19 | + mockCp.mock.resetCalls(); |
| 20 | + mockLogError.mock.resetCalls(); |
| 21 | + mockCp.mock.mockImplementation(() => Promise.resolve()); |
| 22 | + }); |
| 23 | + |
| 24 | + it('does nothing if config.pathsToCopy is not an array', async () => { |
| 25 | + await copyStaticAssets({ pathsToCopy: undefined }); |
| 26 | + assert.strictEqual(mockCp.mock.callCount(), 0); |
| 27 | + }); |
| 28 | + |
| 29 | + it('ignores falsy items in pathsToCopy array', async () => { |
| 30 | + const config = { |
| 31 | + output: '/out', |
| 32 | + pathsToCopy: [null, undefined, false, ''], |
| 33 | + }; |
| 34 | + await copyStaticAssets(config); |
| 35 | + assert.strictEqual(mockCp.mock.callCount(), 0); |
| 36 | + }); |
| 37 | + |
| 38 | + it('copies simple string paths correctly to the output directory', async () => { |
| 39 | + const config = { |
| 40 | + output: '/out', |
| 41 | + pathsToCopy: ['src/assets', 'docs/images'], |
| 42 | + }; |
| 43 | + |
| 44 | + await copyStaticAssets(config); |
| 45 | + |
| 46 | + assert.strictEqual(mockCp.mock.callCount(), 2); |
| 47 | + |
| 48 | + assert.deepStrictEqual(mockCp.mock.calls[0].arguments, [ |
| 49 | + 'src/assets', |
| 50 | + join('/out', 'assets'), |
| 51 | + { recursive: true, force: true }, |
| 52 | + ]); |
| 53 | + |
| 54 | + assert.deepStrictEqual(mockCp.mock.calls[1].arguments, [ |
| 55 | + 'docs/images', |
| 56 | + join('/out', 'images'), |
| 57 | + { recursive: true, force: true }, |
| 58 | + ]); |
| 59 | + }); |
| 60 | + |
| 61 | + it('copies object mappings correctly and strips leading slashes from dest', async () => { |
| 62 | + const config = { |
| 63 | + output: '/out', |
| 64 | + pathsToCopy: [ |
| 65 | + { |
| 66 | + 'src/custom': '/dest-folder/custom', // Leading slash should be stripped |
| 67 | + 'src/another': 'another-folder', |
| 68 | + }, |
| 69 | + ], |
| 70 | + }; |
| 71 | + |
| 72 | + await copyStaticAssets(config); |
| 73 | + |
| 74 | + assert.strictEqual(mockCp.mock.callCount(), 2); |
| 75 | + |
| 76 | + assert.deepStrictEqual(mockCp.mock.calls[0].arguments, [ |
| 77 | + 'src/custom', |
| 78 | + join('/out', 'dest-folder/custom'), |
| 79 | + { recursive: true, force: true }, |
| 80 | + ]); |
| 81 | + |
| 82 | + assert.deepStrictEqual(mockCp.mock.calls[1].arguments, [ |
| 83 | + 'src/another', |
| 84 | + join('/out', 'another-folder'), |
| 85 | + { recursive: true, force: true }, |
| 86 | + ]); |
| 87 | + }); |
| 88 | + |
| 89 | + it('ignores ENOENT errors silently', async () => { |
| 90 | + // Simulate an ENOENT error when trying to copy |
| 91 | + mockCp.mock.mockImplementationOnce(() => { |
| 92 | + const err = new Error('File not found'); |
| 93 | + err.code = 'ENOENT'; |
| 94 | + throw err; |
| 95 | + }); |
| 96 | + |
| 97 | + await copyStaticAssets({ |
| 98 | + output: '/out', |
| 99 | + pathsToCopy: ['missing-file'], |
| 100 | + }); |
| 101 | + |
| 102 | + assert.strictEqual(mockCp.mock.callCount(), 1); |
| 103 | + assert.strictEqual(mockLogError.mock.callCount(), 0); |
| 104 | + }); |
| 105 | + |
| 106 | + it('logs errors that are not ENOENT using the logger', async () => { |
| 107 | + // Simulate a generic/permission error |
| 108 | + mockCp.mock.mockImplementationOnce(() => { |
| 109 | + throw new Error('Permission denied'); |
| 110 | + }); |
| 111 | + |
| 112 | + await copyStaticAssets({ |
| 113 | + output: '/out', |
| 114 | + pathsToCopy: ['protected-file'], |
| 115 | + }); |
| 116 | + |
| 117 | + assert.strictEqual(mockCp.mock.callCount(), 1); |
| 118 | + assert.strictEqual(mockLogError.mock.callCount(), 1); |
| 119 | + |
| 120 | + const logMessage = mockLogError.mock.calls[0].arguments[0]; |
| 121 | + assert.match( |
| 122 | + logMessage, |
| 123 | + /\[web-generator\] Failed to copy asset from protected-file to \/out\/protected-file: Permission denied/ |
| 124 | + ); |
| 125 | + }); |
| 126 | +}); |
0 commit comments