-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathutils.test.ts
More file actions
106 lines (90 loc) · 3.23 KB
/
utils.test.ts
File metadata and controls
106 lines (90 loc) · 3.23 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
import { existsSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { INPUT_FILE_REG_EXP } from '../../../src/lib/consts.js';
import { execWithLog } from '../../../src/lib/exec.js';
import { ensureFolderExistsSync } from '../../../src/lib/files.js';
import { createActZip, getActorLocalFilePaths } from '../../../src/lib/utils.js';
import { useTempPath } from '../../__setup__/hooks/useTempPath.js';
import { withRetries } from '../../__setup__/hooks/withRetries.js';
const TEST_DIR = 'my-test-dir';
const FOLDERS = ['my_test', 'my_test/test_in_test', 'my_next_test', '.dot_test'];
const FOLDERS_TO_IGNORE = ['test_to_ignore', 'my_test/this_ignore'];
const FILES = [
'main.js',
'my_module.js',
'next_module.js',
'my_test/test.js',
'my_test/test_in_test/test.js',
'my_next_test/test.js',
'.dot_test/test.js',
];
const FILES_IN_IGNORED_DIR = ['test_to_ignore/in_test_ignore.js'];
const FILES_TO_IGNORE = ['ignored_module.js'];
describe('Utils', () => {
describe('createActZip()', () => {
const { tmpPath, joinPath, beforeAllCalls, afterAllCalls } = useTempPath(TEST_DIR, {
create: true,
remove: true,
cwd: false,
cwdParent: false,
});
beforeAll(async () => {
await beforeAllCalls();
await execWithLog({
cmd: 'git',
args: ['init'],
opts: { cwd: tmpPath },
});
FOLDERS.concat(FOLDERS_TO_IGNORE).forEach((folder) => {
ensureFolderExistsSync(tmpPath, folder);
});
FILES.concat(FILES_TO_IGNORE, FILES_IN_IGNORED_DIR).forEach((file) =>
writeFileSync(joinPath(file), Math.random().toString(36).substring(7), { flag: 'w' }),
);
const toIgnore = FOLDERS_TO_IGNORE.concat(FILES_TO_IGNORE).join('\n');
writeFileSync(joinPath('.gitignore'), toIgnore, { flag: 'w' });
});
afterAll(async () => {
await afterAllCalls();
});
it('should create zip without files in .gitignore', async () => {
const zipName = joinPath('test.zip');
const tempFolder = joinPath('unzip_temp');
ensureFolderExistsSync(tmpPath, 'unzip_temp');
const pathsToZip = await getActorLocalFilePaths(tmpPath);
await createActZip(zipName, pathsToZip, tmpPath);
// Unzip with same command as on Apify worker
// Add in some retries for when the FS is slow to update
await withRetries(
async () => {
await execWithLog({
cmd: 'unzip',
args: ['-oq', zipName, '-d', tempFolder],
});
},
3,
20,
);
FOLDERS.forEach((folder) => expect(existsSync(join(tempFolder, folder))).toBeTruthy());
FOLDERS_TO_IGNORE.forEach((folder) => expect(existsSync(join(tempFolder, folder))).toBeFalsy());
FILES.forEach((file) => expect(existsSync(join(tempFolder, file))).toBeTruthy());
FILES_IN_IGNORED_DIR.concat(FILES_TO_IGNORE).forEach((file) =>
expect(existsSync(join(tempFolder, file))).toBeFalsy(),
);
});
});
describe('input file regex', () => {
const validFiles = ['INPUT', 'INPUT.json', 'INPUT.bin'];
const invalidFiles = ['INPUT_', 'INPUT.__metadata__.json'];
validFiles.forEach((file) => {
it(`should match ${file}`, () => {
expect(!!file.match(INPUT_FILE_REG_EXP)).toBeTruthy();
});
});
invalidFiles.forEach((file) => {
it(`should not match ${file}`, () => {
expect(!!file.match(INPUT_FILE_REG_EXP)).toBeFalsy();
});
});
});
});