@@ -71,6 +71,98 @@ describe('run-remote-query', function() {
7171 } ) ;
7272 } ) ;
7373
74+ describe ( 'validateRepositories' , ( ) => {
75+ let sandbox : sinon . SinonSandbox ;
76+ let showAndLogErrorMessageSpy : sinon . SinonStub ;
77+ let showInformationMessageWithActionSpy : sinon . SinonStub ;
78+ let mockRequest : sinon . SinonStub ;
79+ let logSpy : sinon . SinonStub ;
80+ let mod : any ;
81+
82+ const error = {
83+ message : 'Unable to run query on the specified repositories. Some repositories were invalid or don\'t have database uploads enabled.' ,
84+ response : {
85+ data : {
86+ invalid_repos : [ 'abc/def' , 'ghi/jkl' ] ,
87+ repos_without_db_uploads : [ 'mno/pqr' , 'stu/vwx' ]
88+ }
89+ }
90+ } ;
91+ const ref = 'main' ;
92+ const language = 'javascript' ;
93+ const credentials = getMockCredentials ( 0 ) ;
94+ const query = 'select 1' ;
95+
96+ beforeEach ( ( ) => {
97+ sandbox = sinon . createSandbox ( ) ;
98+ logSpy = sandbox . stub ( ) ;
99+ showAndLogErrorMessageSpy = sandbox . stub ( ) ;
100+ showInformationMessageWithActionSpy = sandbox . stub ( ) ;
101+ mod = proxyquire ( '../../run-remote-query' , {
102+ './helpers' : {
103+ showAndLogErrorMessage : showAndLogErrorMessageSpy ,
104+ showInformationMessageWithAction : showInformationMessageWithActionSpy
105+ } ,
106+ './logging' : {
107+ 'logger' : {
108+ log : logSpy
109+ }
110+ } ,
111+ } ) ;
112+ } ) ;
113+ afterEach ( ( ) => {
114+ sandbox . restore ( ) ;
115+ } ) ;
116+
117+ it ( 'should return and log error if it can\'t run on any repos' , async ( ) => {
118+ const repositories = [ 'abc/def' , 'ghi/jkl' , 'mno/pqr' , 'stu/vwx' ] ;
119+
120+ // make the function call
121+ await mod . validateRepositories ( error , credentials , ref , language , repositories , query ) ;
122+
123+ // check logging output
124+ expect ( logSpy . firstCall . args [ 0 ] ) . to . contain ( 'Unable to run query' ) ;
125+ expect ( logSpy . secondCall . args [ 0 ] ) . to . contain ( 'Invalid repos: abc/def, ghi/jkl' ) ;
126+ expect ( logSpy . thirdCall . args [ 0 ] ) . to . contain ( 'Repos without DB uploads: mno/pqr, stu/vwx' ) ;
127+ expect ( showAndLogErrorMessageSpy . firstCall . args [ 0 ] ) . to . contain ( 'Unable to run query on any' ) ;
128+ } ) ;
129+
130+ it ( 'should list invalid repos and repos without DB uploads, and rerun on valid ones' , async ( ) => {
131+ const repositories = [ 'foo/bar' , 'abc/def' , 'ghi/jkl' , 'mno/pqr' , 'foo/baz' ] ;
132+
133+ // fake return values
134+ showInformationMessageWithActionSpy . resolves ( true ) ;
135+
136+ // make the function call
137+ await mod . validateRepositories ( error , credentials , ref , language , repositories , query ) ;
138+
139+ // check logging output
140+ expect ( logSpy . firstCall . args [ 0 ] ) . to . contain ( 'Unable to run query' ) ;
141+ expect ( logSpy . secondCall . args [ 0 ] ) . to . contain ( 'Invalid repos: abc/def, ghi/jkl' ) ;
142+ expect ( logSpy . thirdCall . args [ 0 ] ) . to . contain ( 'Repos without DB uploads: mno/pqr' ) ;
143+
144+ // check that the correct information message is displayed
145+ expect ( showInformationMessageWithActionSpy . firstCall . args [ 0 ] ) . to . contain ( 'Unable to run query on some' ) ;
146+ expect ( showInformationMessageWithActionSpy . firstCall . args [ 1 ] ) . to . contain ( 'Rerun' ) ;
147+
148+ // check that API request is made again, with only valid repos
149+ expect ( logSpy . lastCall . args [ 0 ] ) . to . contain ( 'valid repositories: ["foo/bar","foo/baz"]' ) ;
150+ // test a few values in the octokit request
151+ expect ( mockRequest . firstCall . args [ 1 ] . data . language ) . to . eq ( 'javascript' ) ;
152+ expect ( mockRequest . firstCall . args [ 1 ] . data . repositories ) . to . deep . eq ( [ 'foo/bar' , 'foo/baz' ] ) ;
153+
154+ } ) ;
155+
156+ function getMockCredentials ( response : any ) {
157+ mockRequest = sinon . stub ( ) . resolves ( response ) ;
158+ return {
159+ getOctokit : ( ) => ( {
160+ request : mockRequest
161+ } )
162+ } ;
163+ }
164+ } ) ;
165+
74166 describe ( 'runRemoteQuery' , ( ) => {
75167 // TODO
76168 } ) ;
0 commit comments