@@ -36,7 +36,6 @@ class PyenvTest extends BasePipelineTest {
3636 void assertPyenvRootInvalidRoot () {
3737 String pyenvRoot = ' /mock/pyenv/root'
3838 helper. registerAllowedMethod(' fileExists' , [String ]) { return false }
39- helper. registerAllowedMethod(' isUnix' , []) { return true }
4039
4140 assertThrows(Exception ) { new Pyenv (script, ' 1.2.3' , pyenvRoot). createVirtualEnv() }
4241 }
@@ -53,115 +52,56 @@ class PyenvTest extends BasePipelineTest {
5352 String pythonVersion = ' 1.2.3'
5453 String pyenvRoot = ' /mock/pyenv/root'
5554 helper. registerAllowedMethod(' fileExists' , [String ]) { return true }
56- helper. registerAllowedMethod(' isUnix' , []) { return true }
57- // Indentation must match the actual command
58- helper. addShMock("""
59- export PYENV_ROOT=${ pyenvRoot}
60- export PATH=\$ PYENV_ROOT/bin:\$ PATH
61- eval "\$ (pyenv init --path)"
62- eval "\$ (pyenv init -)"
63- pyenv install --skip-existing ${ pythonVersion}
64- pyenv shell ${ pythonVersion}
65- pip install virtualenv
66- virtualenv /workspace/.venv/${ TEST_RANDOM_NAME}
67- """ , ' ' , 0 )
68- helper. addShMock(" ${ pyenvRoot} /bin/pyenv --version" , ' pyenv 1.2.3' , 0 )
69- helper. addShMock(" ${ pyenvRoot} /bin/pyenv install --list" , ' 1.2.3' , 0 )
55+ List shMocks = [
56+ new Tuple (installCommands(pyenvRoot, pythonVersion), ' ' , 0 ),
57+ new Tuple (" ${ pyenvRoot} /bin/pyenv install --list" , ' 1.2.3' , 0 ),
58+ ]
59+ shMocks. each { mock -> helper. addShMock(mock[0 ], mock[1 ], mock[2 ]) }
7060
7161 Object venv = new Pyenv (script, pyenvRoot). createVirtualEnv(pythonVersion, 1 )
7262
7363 assertEquals (" /workspace/.venv/${ TEST_RANDOM_NAME} " as String , venv. venvRootDir)
64+ shMocks. each { mock -> assertCallStackContains(mock[0 ]) }
7465 }
7566
7667 @Test
7768 void createVirtualEnvWithTrailingNewline () {
78- String pythonVersion = ' 1.2.3\n '
69+ String pythonVersion = ' 1.2.3'
7970 String pyenvRoot = ' /mock/pyenv/root'
8071 helper. registerAllowedMethod(' fileExists' , [String ]) { return true }
81- helper. registerAllowedMethod(' isUnix' , []) { return true }
82- // Indentation must match the actual command
83- helper. addShMock("""
84- export PYENV_ROOT=${ pyenvRoot}
85- export PATH=\$ PYENV_ROOT/bin:\$ PATH
86- eval "\$ (pyenv init --path)"
87- eval "\$ (pyenv init -)"
88- pyenv install --skip-existing ${ pythonVersion}
89- pyenv shell ${ pythonVersion}
90- pip install virtualenv
91- virtualenv /workspace/.venv/${ TEST_RANDOM_NAME}
92- """ , ' ' , 0 )
93- helper. addShMock(" ${ pyenvRoot} /bin/pyenv --version" , ' pyenv 1.2.3' , 0 )
94- helper. addShMock(" ${ pyenvRoot} /bin/pyenv install --list" , ' 1.2.3' , 0 )
72+ List shMocks = [
73+ new Tuple (installCommands(pyenvRoot, pythonVersion), ' ' , 0 ),
74+ new Tuple (" ${ pyenvRoot} /bin/pyenv install --list" , ' 1.2.3' , 0 ),
75+ ]
76+ shMocks. each { mock -> helper. addShMock(mock[0 ], mock[1 ], mock[2 ]) }
9577
96- Object venv = new Pyenv (script, pyenvRoot). createVirtualEnv(pythonVersion, 1 )
78+ Object venv = new Pyenv (script, pyenvRoot). createVirtualEnv(" ${ pythonVersion} \n " , 1 )
9779
9880 assertEquals (" /workspace/.venv/${ TEST_RANDOM_NAME} " as String , venv. venvRootDir)
81+ shMocks. each { mock -> assertCallStackContains(mock[0 ]) }
9982 }
10083
10184 @Test
10285 void createVirtualEnvInstallationFails () {
10386 String pythonVersion = ' 1.2.3'
10487 String pyenvRoot = ' /mock/pyenv/root'
105- helper. with {
106- registerAllowedMethod(' fileExists' , [String ]) { return true }
107- registerAllowedMethod(' isUnix' , []) { return true }
108- // Indentation must match the actual command
109- addShMock("""
110- export PYENV_ROOT=${ pyenvRoot}
111- export PATH=\$ PYENV_ROOT/bin:\$ PATH
112- eval "\$ (pyenv init --path)"
113- eval "\$ (pyenv init -)"
114- pyenv install --skip-existing ${ pythonVersion}
115- pyenv shell ${ pythonVersion}
116- pip install virtualenv
117- virtualenv /workspace/.venv/${ TEST_RANDOM_NAME}
118- """ , ' ' , 1 )
119- addShMock(" ${ pyenvRoot} /bin/pyenv --version" , ' pyenv 1.2.3' , 0 )
120- addShMock(" ${ pyenvRoot} /bin/pyenv install --list" , ' 1.2.3' , 0 )
121- }
88+ helper. registerAllowedMethod(' fileExists' , [String ]) { return true }
89+ List shMocks = [
90+ new Tuple (installCommands(pyenvRoot, pythonVersion), ' ' , 1 ),
91+ new Tuple (" ${ pyenvRoot} /bin/pyenv install --list" , ' 1.2.3' , 0 ),
92+ ]
93+ shMocks. each { mock -> helper. addShMock(mock[0 ], mock[1 ], mock[2 ]) }
12294
12395 assertThrows(Exception ) {
12496 new Pyenv (script, pyenvRoot). createVirtualEnv(pythonVersion, 1 )
12597 }
12698
127- assertEquals (3 , helper. callStack. findAll { call ->
128- call. methodName == ' sh' &&
129- call. args[0 ]. label == ' Install Python version 1.2.3 with pyenv'
130- }. size())
131- }
132-
133- @Test
134- void createVirtualEnvFailedSupportedPythonVersion () {
135- String pythonVersion = ' 1.2.3'
136- String pyenvRoot = ' /mock/pyenv/root'
137- helper. with {
138- registerAllowedMethod(' fileExists' , [String ]) { return true }
139- registerAllowedMethod(' isUnix' , []) { return true }
140- addShMock(" ${ pyenvRoot} /bin/pyenv --version" , ' pyenv 1.2.3' , 0 )
141- addShMock(" ${ pyenvRoot} /bin/pyenv install --list" , ''' Available versions:
142- 1.2.3
143- ''' , 0 )
144- // Indentation must match the actual command
145- addShMock("""
146- export PYENV_ROOT=${ pyenvRoot}
147- export PATH=\$ PYENV_ROOT/bin:\$ PATH
148- eval "\$ (pyenv init --path)"
149- eval "\$ (pyenv init -)"
150- pyenv install --skip-existing ${ pythonVersion}
151- pyenv shell ${ pythonVersion}
152- pip install virtualenv
153- virtualenv /workspace/.venv/${ TEST_RANDOM_NAME}
154- """ , ' ' , 1 )
155- }
156-
157- assertThrows(Exception ) {
158- new Pyenv (script, pyenvRoot). createVirtualEnv(pythonVersion, 1 )
159- }
99+ shMocks. each { mock -> assertCallStackContains(mock[0 ]) }
160100 }
161101
162102 @Test
163103 void createVirtualEnvWindows () {
164- helper . registerAllowedMethod( ' isUnix ' , []) { return false }
104+ script . env[ ' OS ' ] = ' Windows_NT '
165105
166106 assertThrows(Exception ) { new Pyenv (script, ' C:\\ pyenv' ). createVirtualEnv(' 1.2.3' ) }
167107 }
@@ -170,25 +110,15 @@ class PyenvTest extends BasePipelineTest {
170110 void createVirtualEnvUnsupportedPythonVersion () {
171111 String pythonVersion = ' 6.6.6'
172112 String pyenvRoot = ' /mock/pyenv/root'
113+ boolean errorCalled = false
173114 helper. registerAllowedMethod(' error' , [String ]) { errorCalled = true }
174115 helper. registerAllowedMethod(' fileExists' , [String ]) { return true }
175- helper. registerAllowedMethod( ' isUnix ' , []) { return true }
116+ helper. addShMock( " ${ pyenvRoot } /bin/pyenv install --list " , ' 1.0.0 ' , 0 )
176117 helper. addShMock(" ${ pyenvRoot} /bin/pyenv --version" , ' pyenv 1.2.3' , 0 )
177- // Indentation must match the actual command
178- helper. addShMock("""
179- export PYENV_ROOT=${ pyenvRoot}
180- export PATH=\$ PYENV_ROOT/bin:\$ PATH
181- eval "\$ (pyenv init --path)"
182- eval "\$ (pyenv init -)"
183- pyenv install --skip-existing ${ pythonVersion}
184- pyenv shell ${ pythonVersion}
185- pip install virtualenv
186- virtualenv /workspace/.venv/${ TEST_RANDOM_NAME}
187- """ , ' ' , 1 )
188118
189- assertThrows( Exception ) {
190- new Pyenv (script, pyenvRoot) . createVirtualEnv(pythonVersion, 1 )
191- }
119+ new Pyenv (script, pyenvRoot) . createVirtualEnv(pythonVersion, 1 )
120+
121+ assertTrue (errorCalled)
192122 }
193123
194124 @Test
@@ -199,18 +129,33 @@ class PyenvTest extends BasePipelineTest {
199129 2.2.3
200130 2.3.7
201131'''
202- helper. addShMock(' /pyenv/bin/pyenv install --list' , mockPyenvVersions, 0 )
132+ String pyenvRoot = ' /pyenv'
133+ helper. addShMock(" ${ pyenvRoot} /bin/pyenv install --list" , mockPyenvVersions, 0 )
203134 helper. registerAllowedMethod(' fileExists' , [String ]) { return true }
204- helper. registerAllowedMethod(' isUnix' , []) { return true }
205135
206- assertTrue (new Pyenv (script, ' /pyenv ' ). versionSupported(' 2.1.3' ))
207- assertFalse (new Pyenv (script, ' /pyenv ' ). versionSupported(' 2.1.3333' ))
136+ assertTrue (new Pyenv (script, pyenvRoot ). versionSupported(' 2.1.3' ))
137+ assertFalse (new Pyenv (script, pyenvRoot ). versionSupported(' 2.1.3333' ))
208138 }
209139
210140 @Test
211141 void versionSupportedWindows () {
212- helper . registerAllowedMethod( ' isUnix ' , []) { return false }
142+ script . env[ ' OS ' ] = ' Windows_NT '
213143
214144 assertThrows(Exception ) { new Pyenv (script, ' C:\\ pyenv' ). versionSupported(' 1.2.3' ) }
215145 }
146+
147+ private String installCommands (String pyenvRoot , String pythonVersion ) {
148+ List installCommands = [
149+ " export PYENV_ROOT=${ pyenvRoot} " ,
150+ " export PATH=\$ PYENV_ROOT/bin:\$ PATH" ,
151+ ' eval "\$ (pyenv init --path)"' ,
152+ ' eval "\$ (pyenv init -)"' ,
153+ " pyenv install --skip-existing ${ pythonVersion} " ,
154+ " pyenv shell ${ pythonVersion} " ,
155+ ' pip install virtualenv' ,
156+ " virtualenv /workspace/.venv/${ TEST_RANDOM_NAME} " ,
157+ ]
158+
159+ return installCommands. join(' \n ' ) + ' \n '
160+ }
216161}
0 commit comments