-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathrepository-selection.test.ts
More file actions
306 lines (258 loc) · 10.5 KB
/
repository-selection.test.ts
File metadata and controls
306 lines (258 loc) · 10.5 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import * as sinon from 'sinon';
import { expect } from 'chai';
import { window } from 'vscode';
import * as pq from 'proxyquire';
import * as fs from 'fs-extra';
import { UserCancellationException } from '../../../commandRunner';
const proxyquire = pq.noPreserveCache();
describe('repository selection', async () => {
let sandbox: sinon.SinonSandbox;
let quickPickSpy: sinon.SinonStub;
let showInputBoxSpy: sinon.SinonStub;
let getRemoteRepositoryListsSpy: sinon.SinonStub;
let getRemoteRepositoryListsPathSpy: sinon.SinonStub;
let pathExistsStub: sinon.SinonStub;
let fsStatStub: sinon.SinonStub;
let fsReadFileStub: sinon.SinonStub;
let mod: any;
beforeEach(() => {
sandbox = sinon.createSandbox();
quickPickSpy = sandbox.stub(window, 'showQuickPick');
showInputBoxSpy = sandbox.stub(window, 'showInputBox');
getRemoteRepositoryListsSpy = sandbox.stub();
getRemoteRepositoryListsPathSpy = sandbox.stub();
pathExistsStub = sandbox.stub(fs, 'pathExists');
fsStatStub = sandbox.stub(fs, 'stat');
fsReadFileStub = sandbox.stub(fs, 'readFile');
mod = proxyquire('../../../remote-queries/repository-selection', {
'../config': {
getRemoteRepositoryLists: getRemoteRepositoryListsSpy,
getRemoteRepositoryListsPath: getRemoteRepositoryListsPathSpy
},
'fs-extra': {
pathExists: pathExistsStub,
stat: fsStatStub,
readFile: fsReadFileStub
}
});
});
afterEach(() => {
sandbox.restore();
});
describe('repo lists from settings', async () => {
it('should allow selection from repo lists from your pre-defined config', async () => {
// Fake return values
quickPickSpy.resolves(
{ repositories: ['foo/bar', 'foo/baz'] }
);
getRemoteRepositoryListsSpy.returns(
{
'list1': ['foo/bar', 'foo/baz'],
'list2': [],
}
);
// Make the function call
const repoSelection = await mod.getRepositorySelection();
// Check that the return value is correct
expect(repoSelection.repositoryLists).to.be.undefined;
expect(repoSelection.owners).to.be.undefined;
expect(repoSelection.repositories).to.deep.eq(
['foo/bar', 'foo/baz']
);
});
});
describe('system level repo lists', async () => {
it('should allow selection from repo lists defined at the system level', async () => {
// Fake return values
quickPickSpy.resolves(
{ repositoryList: 'top_100' }
);
getRemoteRepositoryListsSpy.returns(
{
'list1': ['foo/bar', 'foo/baz'],
'list2': [],
}
);
// Make the function call
const repoSelection = await mod.getRepositorySelection();
// Check that the return value is correct
expect(repoSelection.repositories).to.be.undefined;
expect(repoSelection.owners).to.be.undefined;
expect(repoSelection.repositoryLists).to.deep.eq(
['top_100']
);
});
});
describe('custom owner', async () => {
// Test the owner regex in various "good" cases
const goodOwners = [
'owner',
'owner-with-hyphens',
'ownerWithNumbers58',
'owner_with_underscores',
'owner.with.periods.'
];
goodOwners.forEach(owner => {
it(`should run on a valid owner that you enter in the text box: ${owner}`, async () => {
// Fake return values
quickPickSpy.resolves(
{ useAllReposOfOwner: true }
);
getRemoteRepositoryListsSpy.returns({}); // no pre-defined repo lists
showInputBoxSpy.resolves(owner);
// Make the function call
const repoSelection = await mod.getRepositorySelection();
// Check that the return value is correct
expect(repoSelection.repositories).to.be.undefined;
expect(repoSelection.repositoryLists).to.be.undefined;
expect(repoSelection.owners).to.deep.eq([owner]);
});
});
// Test the owner regex in various "bad" cases
const badOwners = [
'invalid&owner',
'owner-with-repo/repo'
];
badOwners.forEach(owner => {
it(`should show an error message if you enter an invalid owner in the text box: ${owner}`, async () => {
// Fake return values
quickPickSpy.resolves(
{ useAllReposOfOwner: true }
);
getRemoteRepositoryListsSpy.returns({}); // no pre-defined repo lists
showInputBoxSpy.resolves(owner);
// Function call should throw a UserCancellationException
await expect(mod.getRepositorySelection()).to.be.rejectedWith(Error, `Invalid user or organization: ${owner}`);
});
});
it('should be ok for the user to change their mind', async () => {
quickPickSpy.resolves(
{ useAllReposOfOwner: true }
);
getRemoteRepositoryListsSpy.returns({});
// The user pressed escape to cancel the operation
showInputBoxSpy.resolves(undefined);
await expect(mod.getRepositorySelection()).to.be.rejectedWith(UserCancellationException, 'No repositories selected');
});
});
describe('custom repo', async () => {
// Test the repo regex in various "good" cases
const goodRepos = [
'owner/repo',
'owner_with.symbols-/repo.with-symbols_',
'ownerWithNumbers58/repoWithNumbers37'
];
goodRepos.forEach(repo => {
it(`should run on a valid repo that you enter in the text box: ${repo}`, async () => {
// Fake return values
quickPickSpy.resolves(
{ useCustomRepo: true }
);
getRemoteRepositoryListsSpy.returns({}); // no pre-defined repo lists
showInputBoxSpy.resolves(repo);
// Make the function call
const repoSelection = await mod.getRepositorySelection();
// Check that the return value is correct
expect(repoSelection.repositoryLists).to.be.undefined;
expect(repoSelection.owners).to.be.undefined;
expect(repoSelection.repositories).to.deep.equal(
[repo]
);
});
});
// Test the repo regex in various "bad" cases
const badRepos = [
'invalid*owner/repo',
'owner/repo+some&invalid&stuff',
'owner-with-no-repo/',
'/repo-with-no-owner'
];
badRepos.forEach(repo => {
it(`should show an error message if you enter an invalid repo in the text box: ${repo}`, async () => {
// Fake return values
quickPickSpy.resolves(
{ useCustomRepo: true }
);
getRemoteRepositoryListsSpy.returns({}); // no pre-defined repo lists
showInputBoxSpy.resolves(repo);
// Function call should throw a UserCancellationException
await expect(mod.getRepositorySelection()).to.be.rejectedWith(UserCancellationException, 'Invalid repository format');
});
});
it('should be ok for the user to change their mind', async () => {
quickPickSpy.resolves(
{ useCustomRepo: true }
);
getRemoteRepositoryListsSpy.returns({});
// The user pressed escape to cancel the operation
showInputBoxSpy.resolves(undefined);
await expect(mod.getRepositorySelection()).to.be.rejectedWith(UserCancellationException, 'No repositories selected');
});
});
describe('external repository lists file', async () => {
it('should fail if path does not exist', async () => {
const fakeFilePath = '/path/that/does/not/exist.json';
getRemoteRepositoryListsPathSpy.returns(fakeFilePath);
pathExistsStub.resolves(false);
await expect(mod.getRepositorySelection()).to.be.rejectedWith(Error, `External repository lists file does not exist at ${fakeFilePath}`);
});
it('should fail if path points to directory', async () => {
const fakeFilePath = '/path/to/dir';
getRemoteRepositoryListsPathSpy.returns(fakeFilePath);
pathExistsStub.resolves(true);
fsStatStub.resolves({ isDirectory: () => true } as any);
await expect(mod.getRepositorySelection()).to.be.rejectedWith(Error, 'External repository lists path should not point to a directory');
});
it('should fail if file does not have valid JSON', async () => {
const fakeFilePath = '/path/to/file.json';
getRemoteRepositoryListsPathSpy.returns(fakeFilePath);
pathExistsStub.resolves(true);
fsStatStub.resolves({ isDirectory: () => false } as any);
fsReadFileStub.resolves('not-json' as any as Buffer);
await expect(mod.getRepositorySelection()).to.be.rejectedWith(Error, 'Invalid repository lists file. It should contain valid JSON.');
});
it('should fail if file contains array', async () => {
const fakeFilePath = '/path/to/file.json';
getRemoteRepositoryListsPathSpy.returns(fakeFilePath);
pathExistsStub.resolves(true);
fsStatStub.resolves({ isDirectory: () => false } as any);
fsReadFileStub.resolves('[]' as any as Buffer);
await expect(mod.getRepositorySelection()).to.be.rejectedWith(Error, 'Invalid repository lists file. It should be an object mapping names to a list of repositories.');
});
it('should fail if file does not contain repo lists in the right format', async () => {
const fakeFilePath = '/path/to/file.json';
getRemoteRepositoryListsPathSpy.returns(fakeFilePath);
pathExistsStub.resolves(true);
fsStatStub.resolves({ isDirectory: () => false } as any);
const repoLists = {
'list1': 'owner1/repo1',
};
fsReadFileStub.resolves(JSON.stringify(repoLists) as any as Buffer);
await expect(mod.getRepositorySelection()).to.be.rejectedWith(Error, 'Invalid repository lists file. It should contain an array of repositories for each list.');
});
it('should get repo lists from file', async () => {
const fakeFilePath = '/path/to/file.json';
getRemoteRepositoryListsPathSpy.returns(fakeFilePath);
pathExistsStub.resolves(true);
fsStatStub.resolves({ isDirectory: () => false } as any);
const repoLists = {
'list1': ['owner1/repo1', 'owner2/repo2'],
'list2': ['owner3/repo3']
};
fsReadFileStub.resolves(JSON.stringify(repoLists) as any as Buffer);
getRemoteRepositoryListsSpy.returns(
{
'list3': ['onwer4/repo4'],
'list4': [],
}
);
quickPickSpy.resolves(
{ repositories: ['owner3/repo3'] }
);
const repoSelection = await mod.getRepositorySelection();
expect(repoSelection.repositoryLists).to.be.undefined;
expect(repoSelection.owners).to.be.undefined;
expect(repoSelection.repositories).to.deep.eq(['owner3/repo3']);
});
});
});