forked from martinhaintz/ga-file-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
161 lines (122 loc) · 4.68 KB
/
index.test.js
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const process = require('process');
const cp = require('child_process');
const path = require('path');
const fs = require('fs');
const fileName1 = "firstDummyFile"
const fileExtension1 = "yml"
const fileName2 = "secondDummyFile"
const fileExtension2 = "txt"
const fileName3 = "ignoreDummyFile"
const fileExtension3 = "json"
const fileName4 = "disabled_DummyFile"
const fileExtension4 = "txt"
const file1 = `${fileName1}.${fileExtension1}`
const file2 = `${fileName2}.${fileExtension2}`
const file3 = `${fileName3}.${fileExtension3}`
const file4 = `${fileName4}.${fileExtension4}`
const testDir = "testDir"
const ip = path.join(__dirname, 'index.js');
const outputFiles = "files";
const outputFileNames = "file_names";
const oldEnv = process.env
beforeAll(() => {
createEmptyFile(file1);
createEmptyFile(file2);
if (fs.existsSync(testDir)) {
fs.rmdirSync(testDir, { recursive: true, force: true });
}
fs.mkdirSync(testDir);
createEmptyFile(`${testDir}/${file1}`);
createEmptyFile(`${testDir}/${file2}`);
createEmptyFile(`${testDir}/${file3}`);
createEmptyFile(`${testDir}/${file4}`);
});
function createEmptyFile(filepath) {
fs.closeSync(fs.openSync(filepath, 'w'));
}
afterAll(() => {
fs.unlinkSync(file1)
fs.unlinkSync(file2)
fs.rmdirSync(testDir, { recursive: true, force: true });
});
beforeEach(() => {
process.env = { ...oldEnv }
});
test('test get all files from root directory', () => {
const result = cp.execSync(`node ${ip}`).toString();
expect(result).toContain(outputFiles)
expect(result).toContain(outputFileNames)
expect(result).toContain(file1)
expect(result).toContain(file2)
expect(result).toContain(fileName1)
expect(result).toContain(fileName2)
})
test('test get all files in specific directory', () => {
process.env['INPUT_DIRECTORY'] = testDir;
const result = cp.execSync(`node ${ip}`, { env: process.env }).toString();
expect(result).toContain(outputFiles)
expect(result).toContain(outputFileNames)
expect(result).toContain(file1)
expect(result).toContain(file2)
expect(result).toContain(fileName1)
expect(result).toContain(fileName2)
})
test('test get all files in specific directory with specific ending', () => {
process.env['INPUT_DIRECTORY'] = testDir;
process.env['INPUT_FILE_EXTENSION'] = "yml";
const result = cp.execSync(`node ${ip}`, { env: process.env }).toString();
expect(result).toContain(outputFiles)
expect(result).toContain(outputFileNames)
expect(result).toContain(file1)
expect(result).not.toContain(file2)
expect(result).toContain(fileName1)
expect(result).not.toContain(fileName2)
})
test('test ignores files which starts with *ignore* or *disabled_*', () => {
process.env['INPUT_DIRECTORY'] = testDir;
process.env['INPUT_IGNORE_FILES_STARTS_WITH'] = "ignore;disabled_";
process.env['INPUT_IGNORE_FILES_STARTS_WITH_DELIMITER'] = ";";
const result = cp.execSync(`node ${ip}`, { env: process.env }).toString();
expect(result).toContain(outputFiles)
expect(result).toContain(outputFileNames)
expect(result).toContain(file1)
expect(result).toContain(file2)
expect(result).not.toContain(file3)
expect(result).not.toContain(file4)
expect(result).toContain(fileName1)
expect(result).toContain(fileName2)
expect(result).not.toContain(fileName3)
expect(result).not.toContain(fileName4)
})
test('test only files which starts with *second*', () => {
process.env['INPUT_DIRECTORY'] = testDir;
process.env['INPUT_ONLY_FILES_STARTS_WITH'] = "second";
process.env['INPUT_ONLY_FILES_STARTS_WITH_DELIMITER'] = ",";
const result = cp.execSync(`node ${ip}`, { env: process.env }).toString();
expect(result).toContain(outputFiles)
expect(result).toContain(outputFileNames)
expect(result).not.toContain(file1)
expect(result).toContain(file2)
expect(result).not.toContain(file3)
expect(result).not.toContain(file4)
expect(result).not.toContain(fileName1)
expect(result).toContain(fileName2)
expect(result).not.toContain(fileName3)
expect(result).not.toContain(fileName4)
})
test('test only files which starts with *first* or *second*', () => {
process.env['INPUT_DIRECTORY'] = testDir;
process.env['INPUT_ONLY_FILES_STARTS_WITH'] = "first;second";
process.env['INPUT_ONLY_FILES_STARTS_WITH_DELIMITER'] = ";";
const result = cp.execSync(`node ${ip}`, { env: process.env }).toString();
expect(result).toContain(outputFiles)
expect(result).toContain(outputFileNames)
expect(result).toContain(file1)
expect(result).toContain(file2)
expect(result).not.toContain(file3)
expect(result).not.toContain(file4)
expect(result).toContain(fileName1)
expect(result).toContain(fileName2)
expect(result).not.toContain(fileName3)
expect(result).not.toContain(fileName4)
})