forked from nodejs/doc-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopying.test.mjs
More file actions
126 lines (103 loc) · 3.47 KB
/
Copy pathcopying.test.mjs
File metadata and controls
126 lines (103 loc) · 3.47 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
import assert from 'node:assert/strict';
import { join } from 'node:path';
import { describe, it, mock, beforeEach } from 'node:test';
const mockCp = mock.fn(() => Promise.resolve());
mock.module('node:fs/promises', {
namedExports: { cp: mockCp },
});
const mockLogError = mock.fn();
mock.module('../../../../logger/index.mjs', {
defaultExport: { error: mockLogError },
});
const { copyStaticAssets } = await import('../copying.mjs');
describe('copyStaticAssets', () => {
beforeEach(() => {
mockCp.mock.resetCalls();
mockLogError.mock.resetCalls();
mockCp.mock.mockImplementation(() => Promise.resolve());
});
it('does nothing if config.pathsToCopy is not an array', async () => {
await copyStaticAssets({ pathsToCopy: undefined });
assert.strictEqual(mockCp.mock.callCount(), 0);
});
it('ignores falsy items in pathsToCopy array', async () => {
const config = {
output: '/out',
pathsToCopy: [null, undefined, false, ''],
};
await copyStaticAssets(config);
assert.strictEqual(mockCp.mock.callCount(), 0);
});
it('copies simple string paths correctly to the output directory', async () => {
const config = {
output: '/out',
pathsToCopy: ['src/assets', 'docs/images'],
};
await copyStaticAssets(config);
assert.strictEqual(mockCp.mock.callCount(), 2);
assert.deepStrictEqual(mockCp.mock.calls[0].arguments, [
'src/assets',
join('/out', 'assets'),
{ recursive: true, force: true },
]);
assert.deepStrictEqual(mockCp.mock.calls[1].arguments, [
'docs/images',
join('/out', 'images'),
{ recursive: true, force: true },
]);
});
it('copies object mappings correctly and strips leading slashes from dest', async () => {
const config = {
output: '/out',
pathsToCopy: [
{
'src/custom': '/dest-folder/custom', // Leading slash should be stripped
'src/another': 'another-folder',
},
],
};
await copyStaticAssets(config);
assert.strictEqual(mockCp.mock.callCount(), 2);
assert.deepStrictEqual(mockCp.mock.calls[0].arguments, [
'src/custom',
join('/out', 'dest-folder/custom'),
{ recursive: true, force: true },
]);
assert.deepStrictEqual(mockCp.mock.calls[1].arguments, [
'src/another',
join('/out', 'another-folder'),
{ recursive: true, force: true },
]);
});
it('ignores ENOENT errors silently', async () => {
// Simulate an ENOENT error when trying to copy
mockCp.mock.mockImplementationOnce(() => {
const err = new Error('File not found');
err.code = 'ENOENT';
throw err;
});
await copyStaticAssets({
output: '/out',
pathsToCopy: ['missing-file'],
});
assert.strictEqual(mockCp.mock.callCount(), 1);
assert.strictEqual(mockLogError.mock.callCount(), 0);
});
it('logs errors that are not ENOENT using the logger', async () => {
// Simulate a generic/permission error
mockCp.mock.mockImplementationOnce(() => {
throw new Error('Permission denied');
});
await copyStaticAssets({
output: '/out',
pathsToCopy: ['protected-file'],
});
assert.strictEqual(mockCp.mock.callCount(), 1);
assert.strictEqual(mockLogError.mock.callCount(), 1);
const logMessage = mockLogError.mock.calls[0].arguments[0];
assert.match(
logMessage,
/\[web-generator\] Failed to copy asset from protected-file to \/out\/protected-file: Permission denied/
);
});
});