-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.spec.ts
More file actions
89 lines (80 loc) · 2.53 KB
/
Copy pathcli.spec.ts
File metadata and controls
89 lines (80 loc) · 2.53 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
import { existsSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
import { afterAll, afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
import { createDir } from '../index.js';
async function cleanupFolders() {
try {
rmSync('input2', { recursive: true, force: true });
rmSync('output2', { recursive: true, force: true });
} catch (_) {}
}
describe('copyfiles', () => {
afterEach(async () => {
vi.clearAllMocks();
cleanupFolders();
process.exitCode = undefined;
});
afterAll(() => cleanupFolders());
beforeEach(() => {
createDir('input2/other');
});
test(
'CLI exclude',
() =>
new Promise((done: any) => {
writeFileSync('input2/a.txt', 'a');
writeFileSync('input2/b.txt', 'b');
writeFileSync('input2/c.js.txt', 'c');
writeFileSync('input2/d.ps.txt', 'd');
vi.spyOn(process, 'argv', 'get').mockReturnValue([
'node.exe',
'native-copyfiles/dist/cli.js',
'input2',
'output2',
'--exclude',
'**/*.js.txt',
'**/*.ps.txt',
]);
// Mock process.exit so it doesn't kill the test runner
// @ts-ignore
const exitSpy = vi.spyOn(process, 'exit').mockImplementation((code?: string | number | null | undefined) => {
if (code && code !== 0) {
exitSpy.mockRestore();
done(new Error(`process.exit called with code ${code}`));
}
// Do nothing for code 0
});
import('../cli.js')
.then(() => {
// Wait until output2/input2 exists, then check files
const start = Date.now();
const check = () => {
if (!existsSync('output2/input2')) {
if (Date.now() - start > 55) {
exitSpy.mockRestore();
return done(new Error('Timeout: output2/input2 was not created'));
}
setTimeout(check, 50);
return;
}
try {
setTimeout(() => {
const files = readdirSync('output2/input2');
expect(files).toEqual(['a.txt', 'b.txt']);
exitSpy.mockRestore();
done();
}, 50);
} catch (e) {
exitSpy.mockRestore();
done(e);
}
};
check();
})
.catch(e => {
exitSpy.mockRestore();
done(e);
});
}),
300,
);
});