/
find-files.test.ts
209 lines (190 loc) · 7.25 KB
/
find-files.test.ts
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import * as path from 'path';
import { test } from 'tap';
import { find } from '../src/lib/find-files';
const testFixture = path.join(__dirname, 'fixtures', 'find-files');
test('find all files in test fixture', async (t) => {
// six levels deep to find all
const result = await find(testFixture, [], [], 6);
const expected = [
path.join(testFixture, 'README.md'),
path.join(
testFixture,
'golang',
'golang-app-govendor',
'vendor',
'vendor.json',
),
path.join(testFixture, 'golang', 'golang-app', 'Gopkg.lock'),
path.join(testFixture, 'golang', 'golang-app', 'Gopkg.toml'),
path.join(testFixture, 'golang', 'golang-gomodules', 'go.mod'),
path.join(testFixture, 'gradle', 'build.gradle'),
path.join(testFixture, 'gradle-kts', 'build.gradle.kts'),
path.join(testFixture, 'gradle-and-kotlin', 'build.gradle'),
path.join(testFixture, 'gradle-multiple', 'gradle/build.gradle'),
path.join(testFixture, 'gradle-multiple', 'gradle-another/build.gradle'),
path.join(testFixture, 'maven', 'pom.xml'),
path.join(testFixture, 'maven', 'test.txt'),
path.join(testFixture, 'npm-with-lockfile', 'package-lock.json'),
path.join(testFixture, 'mvn', 'pom.xml'),
path.join(testFixture, 'mvn', 'test.txt'),
path.join(testFixture, 'npm', 'package.json'),
path.join(testFixture, 'npm', 'test.txt'),
path.join(testFixture, 'ruby', 'Gemfile.lock'),
path.join(testFixture, 'ruby', 'test.txt'),
path.join(testFixture, 'yarn', 'yarn.lock'),
].sort();
t.same(result.sort(), expected, 'should return all files');
});
test('find all files in test fixture ignoring node_modules', async (t) => {
// six levels deep to ensure node_modules is tested
const result = await find(testFixture, ['node_modules'], [], 6);
const expected = [
path.join(testFixture, 'README.md'),
path.join(
testFixture,
'golang',
'golang-app-govendor',
'vendor',
'vendor.json',
),
path.join(testFixture, 'golang', 'golang-app', 'Gopkg.lock'),
path.join(testFixture, 'golang', 'golang-app', 'Gopkg.toml'),
path.join(testFixture, 'golang', 'golang-gomodules', 'go.mod'),
path.join(testFixture, 'gradle', 'build.gradle'),
path.join(testFixture, 'gradle-kts', 'build.gradle.kts'),
path.join(testFixture, 'gradle-and-kotlin', 'build.gradle'),
path.join(testFixture, 'gradle-multiple', 'gradle/build.gradle'),
path.join(testFixture, 'gradle-multiple', 'gradle-another/build.gradle'),
path.join(testFixture, 'maven', 'pom.xml'),
path.join(testFixture, 'maven', 'test.txt'),
path.join(testFixture, 'mvn', 'pom.xml'),
path.join(testFixture, 'mvn', 'test.txt'),
path.join(testFixture, 'npm-with-lockfile', 'package-lock.json'),
path.join(testFixture, 'npm', 'package.json'),
path.join(testFixture, 'npm', 'test.txt'),
path.join(testFixture, 'ruby', 'Gemfile.lock'),
path.join(testFixture, 'ruby', 'test.txt'),
path.join(testFixture, 'yarn', 'yarn.lock'),
].sort();
t.same(result.sort(), expected, 'should return expected files');
});
test('find package.json file in test fixture ignoring node_modules', async (t) => {
// six levels deep to ensure node_modules is tested
const nodeModulesPath = path.join(testFixture, 'node_modules');
const result = await find(nodeModulesPath, [], ['package.json'], 6);
const expected = [];
t.same(result, expected, 'should return expected file');
});
test('find package.json file in test fixture (by default ignoring node_modules)', async (t) => {
// six levels deep to ensure node_modules is tested
const result = await find(testFixture, [], ['package.json'], 6);
const expected = [
path.join(testFixture, 'npm', 'package.json'),
path.join(testFixture, 'npm-with-lockfile', 'package.json'),
path.join(testFixture, 'yarn', 'package.json'),
];
t.same(result, expected, 'should return expected file');
});
test('find package-lock.json file in test fixture (ignore package.json in the same folder)', async (t) => {
const npmLockfilePath = path.join(testFixture, 'npm-with-lockfile');
const result = await find(
npmLockfilePath,
[],
['package.json', 'package-lock.json'],
1,
);
const expected = [path.join(npmLockfilePath, 'package-lock.json')];
t.same(result, expected, 'should return expected file');
});
test('find build.gradle file in test fixture (ignore build.gradle in the same folder)', async (t) => {
const buildGradle = path.join(testFixture, 'gradle-and-kotlin');
const result = await find(
buildGradle,
[],
['build.gradle.kts', 'build.gradle'],
1,
);
const expected = [path.join(buildGradle, 'build.gradle')];
t.same(result, expected, 'should return expected file');
});
test('find Gemfile.lock file in test fixture (ignore Gemfile in the same folder)', async (t) => {
const npmLockfilePath = path.join(testFixture, 'ruby');
const result = await find(
npmLockfilePath,
[],
['Gemfile', 'Gemfile.lock'],
1,
);
const expected = [path.join(npmLockfilePath, 'Gemfile.lock')];
t.same(result, expected, 'should return expected file');
});
test('find yarn.lock file in test fixture (ignore package.json in the same folder)', async (t) => {
const yarnLockfilePath = path.join(testFixture, 'yarn');
const result = await find(
yarnLockfilePath,
[],
['package.json', 'yarn.lock'],
1,
);
const expected = [path.join(yarnLockfilePath, 'yarn.lock')];
t.same(result, expected, 'should return expected file');
});
test('find package.json file in test fixture (by default ignoring node_modules)', async (t) => {
// four levels deep to ensure node_modules is tested
const result = await find(testFixture, [], ['package.json'], 4);
const expected = [
path.join(testFixture, 'npm', 'package.json'),
path.join(testFixture, 'npm-with-lockfile', 'package.json'),
path.join(testFixture, 'yarn', 'package.json'),
];
t.same(result, expected, 'should return expected file');
});
test('find Gemfile file in test fixture', async (t) => {
const result = await find(testFixture, [], ['Gemfile']);
const expected = [path.join(testFixture, 'ruby', 'Gemfile')];
t.same(result, expected, 'should return expected file');
});
test('find pom.xml files in test fixture', async (t) => {
const result = await find(testFixture, [], ['pom.xml']);
const expected = [
path.join(testFixture, 'maven', 'pom.xml'),
path.join(testFixture, 'mvn', 'pom.xml'),
].sort();
t.same(result.sort(), expected, 'should return expected files');
});
test('find path that does not exist', async (t) => {
try {
await find('does-not-exist');
t.fail('expected exception to be thrown');
} catch (err) {
t.match(
err.message,
'Error finding files in path',
'throws expected exception',
);
}
});
test('find path is empty string', async (t) => {
try {
await find('');
t.fail('expected exception to be thrown');
} catch (err) {
t.match(
err.message,
'Error finding files in path',
'throws expected exception',
);
}
});
test('find path is relative', async (t) => {
try {
await find('fixtures/find-files');
t.fail('expected exception to be thrown');
} catch (err) {
t.match(
err.message,
'Error finding files in path',
'throws expected exception',
);
}
});