1- import fs from "fs" ;
2- import inquirer from "inquirer" ;
3- import os from "os" ;
41import path from "path" ;
5- import uuid from "uuid/v4" ;
62import { readYaml } from "../config" ;
73import * as config from "../config" ;
84import * as azdoClient from "../lib/azdoClient" ;
95import { createTempDir } from "../lib/ioUtil" ;
6+ import * as projectService from "../lib/setup/projectService" ;
7+ import * as promptInstance from "../lib/setup/prompt" ;
108import { IConfigYaml } from "../types" ;
11- import {
12- createProject ,
13- createSPKConfig ,
14- execute ,
15- getAnswerFromFile ,
16- getProject ,
17- PROJECT_NAME ,
18- prompt
19- } from "./setup" ;
9+ import { createSPKConfig , execute } from "./setup" ;
2010import * as setup from "./setup" ;
2111
2212describe ( "test createSPKConfig function" , ( ) => {
@@ -38,190 +28,19 @@ describe("test createSPKConfig function", () => {
3828 } ) ;
3929} ) ;
4030
41- describe ( "test getProject function" , ( ) => {
42- it ( "positive test" , async ( ) => {
43- const res = await getProject (
44- {
45- getProject : async ( ) => {
46- return {
47- valid : true
48- } ;
49- }
50- } as any ,
51- "test"
52- ) ;
53- expect ( res ) . toBeDefined ( ) ;
54- } ) ;
55- it ( "negative test" , async ( ) => {
56- const res = await getProject (
57- {
58- getProject : async ( ) => {
59- return null ;
60- }
61- } as any ,
62- "test"
63- ) ;
64- expect ( res ) . toBeNull ( ) ;
65- } ) ;
66- it ( "negative test: Authorization issue" , async ( ) => {
67- await expect (
68- getProject (
69- {
70- getProject : ( ) => {
71- throw {
72- message : "Authentication Failed" ,
73- statusCode : 401
74- } ;
75- }
76- } as any ,
77- "test"
78- )
79- ) . rejects . toThrow ( ) ;
80- } ) ;
81- it ( "negative test: other error" , async ( ) => {
82- await expect (
83- getProject (
84- {
85- getProject : ( ) => {
86- throw new Error ( "fake" ) ;
87- }
88- } as any ,
89- "test"
90- )
91- ) . rejects . toThrow ( ) ;
92- } ) ;
93- } ) ;
94-
95- describe ( "test createProject function" , ( ) => {
96- it ( "positive test" , async ( ) => {
97- await createProject (
98- {
99- queueCreateProject : async ( ) => {
100- return ;
101- }
102- } as any ,
103- "test"
104- ) ;
105- } ) ;
106- it ( "negative test: Authorization issue" , async ( ) => {
107- await expect (
108- createProject (
109- {
110- queueCreateProject : ( ) => {
111- throw {
112- message : "Authentication Failed" ,
113- statusCode : 401
114- } ;
115- }
116- } as any ,
117- "test"
118- )
119- ) . rejects . toThrow ( ) ;
120- } ) ;
121- it ( "negative test: other error" , async ( ) => {
122- await expect (
123- createProject (
124- {
125- queueCreateProject : ( ) => {
126- throw new Error ( "fake" ) ;
127- }
128- } as any ,
129- "test"
130- )
131- ) . rejects . toThrow ( ) ;
132- } ) ;
133- } ) ;
134-
135- describe ( "test getAnswerFromFile function" , ( ) => {
136- it ( "positive test" , ( ) => {
137- const dir = createTempDir ( ) ;
138- const file = path . join ( dir , "testfile" ) ;
139- const data = [
140- "azdo_org_name=orgname" ,
141- "azdo_pat=pat" ,
142- "azdo_project_name=project"
143- ] ;
144- fs . writeFileSync ( file , data . join ( "\n" ) ) ;
145- const answer = getAnswerFromFile ( file ) ;
146- expect ( answer . azdo_org_name ) . toBe ( "orgname" ) ;
147- expect ( answer . azdo_pat ) . toBe ( "pat" ) ;
148- expect ( answer . azdo_project_name ) . toBe ( "project" ) ;
149- } ) ;
150- it ( "positive test: without project name" , ( ) => {
151- const dir = createTempDir ( ) ;
152- const file = path . join ( dir , "testfile" ) ;
153- const data = [ "azdo_org_name=orgname" , "azdo_pat=pat" ] ;
154- fs . writeFileSync ( file , data . join ( "\n" ) ) ;
155- const answer = getAnswerFromFile ( file ) ;
156- expect ( answer . azdo_org_name ) . toBe ( "orgname" ) ;
157- expect ( answer . azdo_pat ) . toBe ( "pat" ) ;
158- expect ( answer . azdo_project_name ) . toBe ( PROJECT_NAME ) ;
159- } ) ;
160- it ( "negative test: file does not exist" , ( ) => {
161- const file = path . join ( os . tmpdir ( ) , uuid ( ) ) ;
162- expect ( ( ) => {
163- getAnswerFromFile ( file ) ;
164- } ) . toThrow ( ) ;
165- } ) ;
166- it ( "negative test: missing org name" , ( ) => {
167- const dir = createTempDir ( ) ;
168- const file = path . join ( dir , "testfile" ) ;
169- const data = [ "azdo_pat=pat" ] ;
170- fs . writeFileSync ( file , data . join ( "\n" ) ) ;
171- expect ( ( ) => {
172- getAnswerFromFile ( file ) ;
173- } ) . toThrow ( ) ;
174- } ) ;
175- it ( "negative test: invalid project name" , ( ) => {
176- const dir = createTempDir ( ) ;
177- const file = path . join ( dir , "testfile" ) ;
178- const data = [
179- "azdo_org_name=orgname" ,
180- "azdo_project_name=.project" ,
181- "azdo_pat=pat"
182- ] ;
183- fs . writeFileSync ( file , data . join ( "\n" ) ) ;
184- expect ( ( ) => {
185- getAnswerFromFile ( file ) ;
186- } ) . toThrow ( ) ;
187- } ) ;
188- it ( "negative test: missing pat" , ( ) => {
189- const dir = createTempDir ( ) ;
190- const file = path . join ( dir , "testfile" ) ;
191- const data = [ "azdo_org_name=orgname" ] ;
192- fs . writeFileSync ( file , data . join ( "\n" ) ) ;
193- expect ( ( ) => {
194- getAnswerFromFile ( file ) ;
195- } ) . toThrow ( ) ;
196- } ) ;
197- } ) ;
198-
199- describe ( "test prompt function" , ( ) => {
200- it ( "positive test" , async ( ) => {
201- const answers = {
202- azdo_org_name : "org" ,
203- azdo_pat : "pat" ,
204- azdo_project_name : "project"
205- } ;
206- jest . spyOn ( inquirer , "prompt" ) . mockResolvedValueOnce ( answers ) ;
207- const ans = await prompt ( ) ;
208- expect ( ans ) . toStrictEqual ( answers ) ;
209- } ) ;
210- } ) ;
211-
21231const testExecuteFunc = async ( usePrompt = true , hasProject = true ) => {
21332 const exitFn = jest . fn ( ) ;
21433
21534 if ( usePrompt ) {
216- jest . spyOn ( setup , "prompt" ) . mockReturnValueOnce (
35+ jest . spyOn ( promptInstance , "prompt" ) . mockReturnValueOnce (
21736 Promise . resolve ( {
21837 azdo_org_name : "orgname" ,
21938 azdo_pat : "pat" ,
22039 azdo_project_name : "project"
22140 } )
22241 ) ;
22342 } else {
224- jest . spyOn ( setup , "getAnswerFromFile" ) . mockReturnValueOnce ( {
43+ jest . spyOn ( promptInstance , "getAnswerFromFile" ) . mockReturnValueOnce ( {
22544 azdo_org_name : "orgname" ,
22645 azdo_pat : "pat" ,
22746 azdo_project_name : "project"
@@ -237,15 +56,15 @@ const testExecuteFunc = async (usePrompt = true, hasProject = true) => {
23756 ) ;
23857 if ( hasProject ) {
23958 jest
240- . spyOn ( setup , "getProject" )
59+ . spyOn ( projectService , "getProject" )
24160 . mockReturnValueOnce ( Promise . resolve ( { } as any ) ) ;
24261 } else {
24362 jest
244- . spyOn ( setup , "getProject" )
63+ . spyOn ( projectService , "getProject" )
24564 . mockReturnValueOnce ( Promise . resolve ( undefined as any ) ) ;
24665 }
24766 const fncreateProject = jest
248- . spyOn ( setup , "createProject" )
67+ . spyOn ( projectService , "createProject" )
24968 . mockReturnValueOnce ( Promise . resolve ( ) ) ;
25069
25170 if ( usePrompt ) {
@@ -290,7 +109,7 @@ describe("test execute function", () => {
290109 it ( "negative test: 401 status code" , async ( ) => {
291110 const exitFn = jest . fn ( ) ;
292111
293- jest . spyOn ( setup , "prompt" ) . mockReturnValueOnce (
112+ jest . spyOn ( promptInstance , "prompt" ) . mockReturnValueOnce (
294113 Promise . resolve ( {
295114 azdo_org_name : "orgname" ,
296115 azdo_pat : "pat" ,
@@ -321,7 +140,7 @@ describe("test execute function", () => {
321140 it ( "negative test: VS402392 error" , async ( ) => {
322141 const exitFn = jest . fn ( ) ;
323142
324- jest . spyOn ( setup , "prompt" ) . mockReturnValueOnce (
143+ jest . spyOn ( promptInstance , "prompt" ) . mockReturnValueOnce (
325144 Promise . resolve ( {
326145 azdo_org_name : "orgname" ,
327146 azdo_pat : "pat" ,
@@ -345,6 +164,36 @@ describe("test execute function", () => {
345164 exitFn
346165 ) ;
347166
167+ expect ( exitFn ) . toBeCalledTimes ( 1 ) ;
168+ expect ( exitFn . mock . calls ) . toEqual ( [ [ 1 ] ] ) ;
169+ } ) ;
170+ it ( "negative test: other error" , async ( ) => {
171+ const exitFn = jest . fn ( ) ;
172+
173+ jest . spyOn ( promptInstance , "prompt" ) . mockReturnValueOnce (
174+ Promise . resolve ( {
175+ azdo_org_name : "orgname" ,
176+ azdo_pat : "pat" ,
177+ azdo_project_name : "project"
178+ } )
179+ ) ;
180+ jest . spyOn ( setup , "createSPKConfig" ) . mockReturnValueOnce ( ) ;
181+ jest . spyOn ( azdoClient , "getWebApi" ) . mockReturnValueOnce (
182+ Promise . resolve ( {
183+ getCoreApi : ( ) => {
184+ throw {
185+ message : "other error"
186+ } ;
187+ }
188+ } as any )
189+ ) ;
190+ await execute (
191+ {
192+ file : undefined
193+ } ,
194+ exitFn
195+ ) ;
196+
348197 expect ( exitFn ) . toBeCalledTimes ( 1 ) ;
349198 expect ( exitFn . mock . calls ) . toEqual ( [ [ 1 ] ] ) ;
350199 } ) ;
0 commit comments