@@ -2,34 +2,57 @@ import * as sinon from 'sinon';
22import { expect } from 'chai' ;
33import { window } from 'vscode' ;
44import * as pq from 'proxyquire' ;
5+ import * as fs from 'fs-extra' ;
56import { UserCancellationException } from '../../../commandRunner' ;
67
78const proxyquire = pq . noPreserveCache ( ) ;
89
9- describe ( 'repository-selection' , function ( ) {
10-
11- describe ( 'getRepositorySelection' , ( ) => {
12- let sandbox : sinon . SinonSandbox ;
13- let quickPickSpy : sinon . SinonStub ;
14- let showInputBoxSpy : sinon . SinonStub ;
15- let getRemoteRepositoryListsSpy : sinon . SinonStub ;
16- let mod : any ;
17- beforeEach ( ( ) => {
18- sandbox = sinon . createSandbox ( ) ;
19- quickPickSpy = sandbox . stub ( window , 'showQuickPick' ) ;
20- showInputBoxSpy = sandbox . stub ( window , 'showInputBox' ) ;
21- getRemoteRepositoryListsSpy = sandbox . stub ( ) ;
22- mod = proxyquire ( '../../../remote-queries/repository-selection' , {
23- '../config' : {
24- getRemoteRepositoryLists : getRemoteRepositoryListsSpy
25- } ,
26- } ) ;
27- } ) ;
10+ describe ( 'repository selection' , async ( ) => {
11+ let sandbox : sinon . SinonSandbox ;
12+
13+ let quickPickSpy : sinon . SinonStub ;
14+ let showInputBoxSpy : sinon . SinonStub ;
15+
16+ let getRemoteRepositoryListsSpy : sinon . SinonStub ;
17+ let getRemoteRepositoryListsPathSpy : sinon . SinonStub ;
18+
19+ let pathExistsStub : sinon . SinonStub ;
20+ let fsStatStub : sinon . SinonStub ;
21+ let fsReadFileStub : sinon . SinonStub ;
22+
23+ let mod : any ;
2824
29- afterEach ( ( ) => {
30- sandbox . restore ( ) ;
25+ beforeEach ( ( ) => {
26+ sandbox = sinon . createSandbox ( ) ;
27+
28+ quickPickSpy = sandbox . stub ( window , 'showQuickPick' ) ;
29+ showInputBoxSpy = sandbox . stub ( window , 'showInputBox' ) ;
30+
31+ getRemoteRepositoryListsSpy = sandbox . stub ( ) ;
32+ getRemoteRepositoryListsPathSpy = sandbox . stub ( ) ;
33+
34+ pathExistsStub = sandbox . stub ( fs , 'pathExists' ) ;
35+ fsStatStub = sandbox . stub ( fs , 'stat' ) ;
36+ fsReadFileStub = sandbox . stub ( fs , 'readFile' ) ;
37+
38+ mod = proxyquire ( '../../../remote-queries/repository-selection' , {
39+ '../config' : {
40+ getRemoteRepositoryLists : getRemoteRepositoryListsSpy ,
41+ getRemoteRepositoryListsPath : getRemoteRepositoryListsPathSpy
42+ } ,
43+ 'fs-extra' : {
44+ pathExists : pathExistsStub ,
45+ stat : fsStatStub ,
46+ readFile : fsReadFileStub
47+ }
3148 } ) ;
49+ } ) ;
3250
51+ afterEach ( ( ) => {
52+ sandbox . restore ( ) ;
53+ } ) ;
54+
55+ describe ( 'repo lists from settings' , async ( ) => {
3356 it ( 'should allow selection from repo lists from your pre-defined config' , async ( ) => {
3457 // Fake return values
3558 quickPickSpy . resolves (
@@ -52,7 +75,9 @@ describe('repository-selection', function() {
5275 [ 'foo/bar' , 'foo/baz' ]
5376 ) ;
5477 } ) ;
78+ } ) ;
5579
80+ describe ( 'system level repo lists' , async ( ) => {
5681 it ( 'should allow selection from repo lists defined at the system level' , async ( ) => {
5782 // Fake return values
5883 quickPickSpy . resolves (
@@ -121,7 +146,9 @@ describe('repository-selection', function() {
121146 await expect ( mod . getRepositorySelection ( ) ) . to . be . rejectedWith ( Error , `Invalid user or organization: ${ owner } ` ) ;
122147 } ) ;
123148 } ) ;
149+ } ) ;
124150
151+ describe ( 'custom repo' , async ( ) => {
125152 // Test the repo regex in various "good" cases
126153 const goodRepos = [
127154 'owner/repo' ,
@@ -169,6 +196,85 @@ describe('repository-selection', function() {
169196 await expect ( mod . getRepositorySelection ( ) ) . to . be . rejectedWith ( UserCancellationException , 'Invalid repository format' ) ;
170197 } ) ;
171198 } ) ;
199+ } ) ;
200+
201+ describe ( 'external repository lists file' , async ( ) => {
202+ it ( 'should fail if path does not exist' , async ( ) => {
203+ const fakeFilePath = '/path/that/does/not/exist.json' ;
204+ getRemoteRepositoryListsPathSpy . returns ( fakeFilePath ) ;
205+ pathExistsStub . resolves ( false ) ;
206+
207+ await expect ( mod . getRepositorySelection ( ) ) . to . be . rejectedWith ( Error , `External repository lists file does not exist at ${ fakeFilePath } ` ) ;
208+ } ) ;
209+
210+ it ( 'should fail if path points to directory' , async ( ) => {
211+ const fakeFilePath = '/path/to/dir' ;
212+ getRemoteRepositoryListsPathSpy . returns ( fakeFilePath ) ;
213+ pathExistsStub . resolves ( true ) ;
214+ fsStatStub . resolves ( { isDirectory : ( ) => true } as any ) ;
215+
216+ await expect ( mod . getRepositorySelection ( ) ) . to . be . rejectedWith ( Error , 'External repository lists path should not point to a directory' ) ;
217+ } ) ;
172218
219+ it ( 'should fail if file does not have valid JSON' , async ( ) => {
220+ const fakeFilePath = '/path/to/file.json' ;
221+ getRemoteRepositoryListsPathSpy . returns ( fakeFilePath ) ;
222+ pathExistsStub . resolves ( true ) ;
223+ fsStatStub . resolves ( { isDirectory : ( ) => false } as any ) ;
224+ fsReadFileStub . resolves ( 'not-json' as any as Buffer ) ;
225+
226+ await expect ( mod . getRepositorySelection ( ) ) . to . be . rejectedWith ( Error , 'Invalid repository lists file. It should contain valid JSON.' ) ;
227+ } ) ;
228+
229+ it ( 'should fail if file contains array' , async ( ) => {
230+ const fakeFilePath = '/path/to/file.json' ;
231+ getRemoteRepositoryListsPathSpy . returns ( fakeFilePath ) ;
232+ pathExistsStub . resolves ( true ) ;
233+ fsStatStub . resolves ( { isDirectory : ( ) => false } as any ) ;
234+ fsReadFileStub . resolves ( '[]' as any as Buffer ) ;
235+
236+ await expect ( mod . getRepositorySelection ( ) ) . to . be . rejectedWith ( Error , 'Invalid repository lists file. It should be an object mapping names to a list of repositories.' ) ;
237+ } ) ;
238+
239+ it ( 'should fail if file does not contain repo lists in the right format' , async ( ) => {
240+ const fakeFilePath = '/path/to/file.json' ;
241+ getRemoteRepositoryListsPathSpy . returns ( fakeFilePath ) ;
242+ pathExistsStub . resolves ( true ) ;
243+ fsStatStub . resolves ( { isDirectory : ( ) => false } as any ) ;
244+ const repoLists = {
245+ 'list1' : 'owner1/repo1' ,
246+ } ;
247+ fsReadFileStub . resolves ( JSON . stringify ( repoLists ) as any as Buffer ) ;
248+
249+ await expect ( mod . getRepositorySelection ( ) ) . to . be . rejectedWith ( Error , 'Invalid repository lists file. It should contain an array of repositories for each list.' ) ;
250+ } ) ;
251+
252+ it ( 'should get repo lists from file' , async ( ) => {
253+ const fakeFilePath = '/path/to/file.json' ;
254+ getRemoteRepositoryListsPathSpy . returns ( fakeFilePath ) ;
255+ pathExistsStub . resolves ( true ) ;
256+ fsStatStub . resolves ( { isDirectory : ( ) => false } as any ) ;
257+ const repoLists = {
258+ 'list1' : [ 'owner1/repo1' , 'owner2/repo2' ] ,
259+ 'list2' : [ 'owner3/repo3' ]
260+ } ;
261+ fsReadFileStub . resolves ( JSON . stringify ( repoLists ) as any as Buffer ) ;
262+ getRemoteRepositoryListsSpy . returns (
263+ {
264+ 'list3' : [ 'onwer4/repo4' ] ,
265+ 'list4' : [ ] ,
266+ }
267+ ) ;
268+
269+ quickPickSpy . resolves (
270+ { repositories : [ 'owner3/repo3' ] }
271+ ) ;
272+
273+ const repoSelection = await mod . getRepositorySelection ( ) ;
274+
275+ expect ( repoSelection . repositoryLists ) . to . be . undefined ;
276+ expect ( repoSelection . owners ) . to . be . undefined ;
277+ expect ( repoSelection . repositories ) . to . deep . eq ( [ 'owner3/repo3' ] ) ;
278+ } ) ;
173279 } ) ;
174280} ) ;
0 commit comments