@@ -53,8 +53,6 @@ it('finds the Conda installation with the CONDA variable', async function () {
5353 getVariable : getVariable
5454 } ) ) ;
5555
56- mockery . registerMock ( 'vsts-task-tool-lib/tool' , { } ) ;
57-
5856 { // executable exists and is a file
5957 existsSync . returns ( true ) ;
6058 statSync . returns ( {
@@ -114,55 +112,71 @@ it('finds the Conda installation with PATH', async function () {
114112 getVariable : getVariable
115113 } ) ) ;
116114
117- mockery . registerMock ( 'vsts-task-tool-lib/tool' , { } ) ;
118-
119115 const uut = reload ( '../conda_internal' ) ;
120116
121117 assert . strictEqual ( uut . findConda ( Platform . Linux ) , 'path-to-conda' ) ;
122118 assert . strictEqual ( uut . findConda ( Platform . MacOS ) , 'path-to-conda' ) ;
123119 assert . strictEqual ( uut . findConda ( Platform . Windows ) , 'path-to-conda' ) ;
124120} ) ;
125121
126- it ( 'creates Conda environment' , async function ( done : MochaDone ) {
122+ it ( 'creates Conda environment' , async function ( ) {
127123 mockery . registerMock ( 'vsts-task-lib/task' , mockTask ) ;
128124 mockery . registerMock ( 'vsts-task-lib/toolrunner' , mockToolRunner ) ;
129- mockery . registerMock ( 'vsts-task-tool-lib/tool' , { } ) ;
130125 const uut = reload ( '../conda_internal' ) ;
131126
132- { // success
133- mockToolRunner . setAnswers ( {
134- exec : {
135- 'conda create --quiet --prefix envsDir/env --mkdir --yes' : {
136- code : 0
137- } ,
138- // workaround for running tests cross-platform
139- 'conda create --quiet --prefix envsDir\\env --mkdir --yes' : {
140- code : 0
141- }
127+ for ( const platform of [ Platform . Windows , Platform . Linux , Platform . MacOS ] )
128+ {
129+ { // success
130+ if ( platform === Platform . Windows ) {
131+ mockToolRunner . setAnswers ( {
132+ exec : {
133+ [ `conda create --quiet --prefix ${ path . join ( 'envsDir' , 'env' ) } --mkdir --yes` ] : {
134+ code : 0
135+ }
136+ }
137+ } ) ;
138+ } else {
139+ mockToolRunner . setAnswers ( {
140+ exec : {
141+ [ `sudo conda create --quiet --prefix ${ path . join ( 'envsDir' , 'env' ) } --mkdir --yes` ] : {
142+ code : 0
143+ }
144+ }
145+ } ) ;
142146 }
143- } ) ;
144147
145- await uut . createEnvironment ( path . join ( 'envsDir' , 'env' ) ) ;
146- }
147- { // failure
148- mockToolRunner . setAnswers ( {
149- exec : {
150- 'conda create --quiet --prefix envsDir/env --mkdir --yes' : {
151- code : 1
152- } ,
153- // workaround for running tests cross-platform
154- 'conda create --quiet --prefix envsDir\\env --mkdir --yes' : {
155- code : 1
156- }
148+ await uut . createEnvironment ( path . join ( 'envsDir' , 'env' ) , platform ) ;
149+ }
150+ { // failure
151+ if ( platform === Platform . Windows ) {
152+ mockToolRunner . setAnswers ( {
153+ exec : {
154+ [ `conda create --quiet --prefix ${ path . join ( 'envsDir' , 'env' ) } --mkdir --yes` ] : {
155+ code : 1
156+ }
157+ }
158+ } ) ;
159+ } else {
160+ mockToolRunner . setAnswers ( {
161+ exec : {
162+ [ `sudo conda create --quiet --prefix ${ path . join ( 'envsDir' , 'env' ) } --mkdir --yes` ] : {
163+ code : 1
164+ }
165+ }
166+ } ) ;
157167 }
158- } ) ;
159168
160- try {
161- await uut . createEnvironment ( path . join ( 'envsDir' , 'env' ) ) ;
162- done ( new Error ( 'should not have succeeded' ) ) ;
163- } catch ( e ) {
164- assert . strictEqual ( e . message , `loc_mock_CreateFailed ${ path . join ( 'envsDir' , 'env' ) } Error: conda failed with return code: 1` ) ;
165- done ( ) ;
169+ // Can't use `assert.throws` with an async function
170+ // Node 10: use `assert.rejects`
171+ let error : any | undefined ;
172+ try {
173+ await uut . createEnvironment ( path . join ( 'envsDir' , 'env' ) , platform ) ;
174+ } catch ( e ) {
175+ error = e ;
176+ }
177+
178+ assert ( error instanceof Error ) ;
179+ assert . strictEqual ( error . message , `loc_mock_CreateFailed ${ path . join ( 'envsDir' , 'env' ) } Error: ${ platform === Platform . Windows ? 'conda' : 'sudo' } failed with return code: 1` ) ;
166180 }
167181 }
168182} ) ;
@@ -173,37 +187,66 @@ it('activates Conda environment', async function () {
173187 setVariable : setVariable
174188 } ) ) ;
175189
176- const prependPath = sinon . spy ( ) ;
177- mockery . registerMock ( 'vsts-task-tool-lib/tool ' , {
178- prependPath : prependPath
190+ const prependPathSafe = sinon . spy ( ) ;
191+ mockery . registerMock ( './toolutil ' , {
192+ prependPathSafe : prependPathSafe
179193 } ) ;
180194
181195 const uut = reload ( '../conda_internal' ) ;
182196
183197 { // Linux
184198 uut . activateEnvironment ( 'envs' , 'env' , Platform . Linux ) ;
185- assert ( prependPath . calledOnceWithExactly ( path . join ( 'envs' , 'env' , 'bin' ) ) ) ;
199+ assert ( prependPathSafe . calledOnceWithExactly ( path . join ( 'envs' , 'env' , 'bin' ) ) ) ;
186200 assert ( setVariable . calledTwice ) ;
187201 assert ( setVariable . calledWithExactly ( 'CONDA_DEFAULT_ENV' , 'env' ) ) ;
188202 assert ( setVariable . calledWithExactly ( 'CONDA_PREFIX' , path . join ( 'envs' , 'env' ) ) ) ;
189203 }
190204 { // macOS
191205 setVariable . resetHistory ( ) ;
192- prependPath . resetHistory ( ) ;
206+ prependPathSafe . resetHistory ( ) ;
193207 uut . activateEnvironment ( 'envs' , 'env' , Platform . MacOS ) ;
194- assert ( prependPath . calledOnceWithExactly ( path . join ( 'envs' , 'env' , 'bin' ) ) ) ;
208+ assert ( prependPathSafe . calledOnceWithExactly ( path . join ( 'envs' , 'env' , 'bin' ) ) ) ;
195209 assert ( setVariable . calledTwice ) ;
196210 assert ( setVariable . calledWithExactly ( 'CONDA_DEFAULT_ENV' , 'env' ) ) ;
197211 assert ( setVariable . calledWithExactly ( 'CONDA_PREFIX' , path . join ( 'envs' , 'env' ) ) ) ;
198212 }
199213 { // Windows
200214 setVariable . resetHistory ( ) ;
201- prependPath . resetHistory ( ) ;
215+ prependPathSafe . resetHistory ( ) ;
202216 uut . activateEnvironment ( 'envs' , 'env' , Platform . Windows ) ;
203- assert ( prependPath . calledWithExactly ( path . join ( 'envs' , 'env' ) ) ) ;
204- assert ( prependPath . calledWithExactly ( path . join ( 'envs' , 'env' , 'Scripts' ) ) ) ;
217+ assert ( prependPathSafe . calledWithExactly ( path . join ( 'envs' , 'env' ) ) ) ;
218+ assert ( prependPathSafe . calledWithExactly ( path . join ( 'envs' , 'env' , 'Scripts' ) ) ) ;
205219 assert ( setVariable . calledTwice ) ;
206220 assert ( setVariable . calledWithExactly ( 'CONDA_DEFAULT_ENV' , 'env' ) ) ;
207221 assert ( setVariable . calledWithExactly ( 'CONDA_PREFIX' , path . join ( 'envs' , 'env' ) ) ) ;
208222 }
223+ } ) ;
224+
225+ it ( 'adds base environment to path successfully' , function ( ) {
226+ mockTask . setAnswers ( {
227+ which : {
228+ 'conda' : '/miniconda/bin/conda'
229+ } ,
230+ exec : {
231+ '/miniconda/bin/conda info --base' : {
232+ code : 0 ,
233+ stdout : '/base/environment'
234+ }
235+ } ,
236+ checkPath : {
237+ '/miniconda/bin/conda' : true
238+ }
239+ } ) ;
240+
241+ mockery . registerMock ( 'vsts-task-lib/task' , mockTask ) ;
242+
243+ const prependPathSafe = sinon . spy ( ) ;
244+ mockery . registerMock ( './toolutil' , {
245+ prependPathSafe : prependPathSafe
246+ } ) ;
247+
248+ const uut = reload ( '../conda_internal' ) ;
249+ uut . addBaseEnvironmentToPath ( Platform . Linux ) ;
250+
251+ assert ( prependPathSafe . calledOnceWithExactly ( path . join ( '/base/environment' , 'bin' ) ) ) ;
209252} ) ;
0 commit comments