Skip to content

Commit 10192cd

Browse files
authored
Merge pull request #164 from Ableton/nre/main/2597-prep-for-pyenv-win
Various refactorings and small fixes in preparation for Pyenv support on Windows
2 parents 6460919 + 188e16a commit 10192cd

3 files changed

Lines changed: 61 additions & 115 deletions

File tree

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.14.3
1+
0.14.4

src/com/ableton/Pyenv.groovy

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Pyenv implements Serializable {
4444

4545
String trimmedPythonVersion = pythonVersion.trim()
4646

47-
if (!script.isUnix()) {
47+
if (script.env.OS == 'Windows_NT') {
4848
script.error 'This method is not supported on Windows'
4949
}
5050

@@ -62,18 +62,19 @@ class Pyenv implements Serializable {
6262

6363
VirtualEnv venv = new VirtualEnv(script, randomSeed)
6464
script.retry(INSTALLATION_RETRIES) {
65+
List installCommands = [
66+
"export PYENV_ROOT=${pyenvRoot}",
67+
"export PATH=\$PYENV_ROOT/bin:\$PATH",
68+
'eval "\$(pyenv init --path)"',
69+
'eval "\$(pyenv init -)"',
70+
"pyenv install --skip-existing ${trimmedPythonVersion}",
71+
"pyenv shell ${trimmedPythonVersion}",
72+
'pip install virtualenv',
73+
"virtualenv ${venv.venvRootDir}",
74+
]
6575
venv.script.sh(
6676
label: "Install Python version ${trimmedPythonVersion} with pyenv",
67-
script: """
68-
export PYENV_ROOT=${pyenvRoot}
69-
export PATH=\$PYENV_ROOT/bin:\$PATH
70-
eval "\$(pyenv init --path)"
71-
eval "\$(pyenv init -)"
72-
pyenv install --skip-existing ${trimmedPythonVersion}
73-
pyenv shell ${trimmedPythonVersion}
74-
pip install virtualenv
75-
virtualenv ${venv.venvRootDir}
76-
""",
77+
script: installCommands.join('\n') + '\n',
7778
)
7879
}
7980

@@ -92,7 +93,7 @@ class Pyenv implements Serializable {
9293
assertPyenvRoot()
9394
boolean result = false
9495

95-
if (!script.isUnix()) {
96+
if (script.env.OS == 'Windows_NT') {
9697
script.error 'This method is not supported on Windows'
9798
}
9899

test/com/ableton/PyenvTest.groovy

Lines changed: 47 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)