Skip to content

Commit

Permalink
Support pyenv on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
nre-ableton committed Sep 14, 2023
1 parent 1fe0f95 commit 0577c51
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
13 changes: 5 additions & 8 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,11 @@ eventRecorder.timedStage('Integration Test') {
String venvVersion = venv.run(returnStdout: true, script: 'python --version')
assert venvVersion.startsWith('Python 3')

if (isUnix()) {
echo 'Test VirtualEnv.createWithPyenv'
Object pyvenv = pyenv.createVirtualEnv('3.10.3')
String pyvenvVersion =
pyvenv.run(returnStdout: true, script: 'python --version')
echo pyvenvVersion
assert pyvenvVersion.trim() == 'Python 3.10.3'
}
echo 'Test VirtualEnv.createWithPyenv'
Object pyvenv = pyenv.createVirtualEnv('3.10.3')
String pyvenvVersion = pyvenv.run(returnStdout: true, script: 'python --version')
echo pyvenvVersion
assert pyvenvVersion.trim() == 'Python 3.10.3'
}
}
}
Expand Down
18 changes: 7 additions & 11 deletions src/com/ableton/Pyenv.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ class Pyenv implements Serializable {
assert pythonVersion
assertPyenvRoot()

if (!script.isUnix()) {
script.error 'This method is not supported on Windows'
}

VirtualEnv venv = new VirtualEnv(script, randomSeed)
int result
script.withEnv(["PYENV_VERSION=${pythonVersion}"]) {
Expand All @@ -51,10 +47,14 @@ class Pyenv implements Serializable {
// Only export PYENV_ROOT if it isn't part of the Jenkins environment
installCommands.add("export PYENV_ROOT=${pyenvRoot}")
}
installCommands.add("export PATH=${VirtualEnv.posixPath(pyenvRoot)}/bin:\$PATH")
if (script.isUnix()) {
installCommands += [
"eval \"\$(pyenv init --path)\"",
"eval \"\$(pyenv init -)\"",
]
}
installCommands += [
"export PATH=${VirtualEnv.posixPath(pyenvRoot)}/bin:\$PATH",
"eval \"\$(pyenv init --path)\"",
"eval \"\$(pyenv init -)\"",
"pyenv install --skip-existing ${pythonVersion}",
'pyenv exec pip install virtualenv',
"pyenv exec virtualenv ${venv.venvRootDir}",
Expand Down Expand Up @@ -96,10 +96,6 @@ class Pyenv implements Serializable {
assertPyenvRoot()
boolean result = false

if (!script.isUnix()) {
script.error 'This method is not supported on Windows'
}

script.withEnv(["PYENV_ROOT=${pyenvRoot}"]) {
String allVersions = script.sh(
label: 'Get Python versions supported by Pyenv',
Expand Down
35 changes: 30 additions & 5 deletions test/com/ableton/PyenvTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,16 @@ class PyenvTest extends BasePipelineTest {

@Test
void createVirtualEnvWindows() {
String pythonVersion = '1.2.3'
String pyenvRoot = 'C:\\mock\\pyenv\\root'
String cygwinPyenvRoot = '/c/mock/pyenv/root'
helper.registerAllowedMethod('isUnix', []) { return false }
helper.addShMock("${cygwinPyenvRoot}/bin/pyenv --version", '1.2.3', 0)
helper.addShMock(installCommands(cygwinPyenvRoot, pythonVersion, false), '', 0)

Object venv = new Pyenv(script, pyenvRoot).createVirtualEnv(pythonVersion, 1)

assertThrows(Exception) { new Pyenv(script, 'C:\\pyenv').createVirtualEnv('1.2.3') }
assertEquals("/workspace/.venv/${TEST_RANDOM_NAME}" as String, venv.venvRootDir)
}

@Test
Expand Down Expand Up @@ -102,17 +109,35 @@ class PyenvTest extends BasePipelineTest {

@Test
void versionSupportedWindows() {
// Resembles pyenv's output, at least as of version 2.3.x
String mockPyenvVersions = '''Available versions:
2.1.3
2.2.3
2.3.7
'''
String cygwinPyenvRoot = '/c/pyenv'
helper.addShMock("${cygwinPyenvRoot}/bin/pyenv --version", '1.2.3', 0)
helper.addShMock("${cygwinPyenvRoot}/bin/pyenv install --list", mockPyenvVersions, 0)
helper.registerAllowedMethod('isUnix', []) { return false }

assertThrows(Exception) { new Pyenv(script, 'C:\\pyenv').versionSupported('1.2.3') }
assertTrue(new Pyenv(script, cygwinPyenvRoot).versionSupported('2.1.3'))
assertFalse(new Pyenv(script, cygwinPyenvRoot).versionSupported('2.1.3333'))
}

private String installCommands(String pyenvRoot, String pythonVersion) {
private String installCommands(
String pyenvRoot, String pythonVersion, boolean isUnix = true
) {
List installCommands = [
"export PYENV_ROOT=${pyenvRoot}",
"export PATH=${pyenvRoot}/bin:\$PATH",
"eval \"\$(pyenv init --path)\"",
"eval \"\$(pyenv init -)\"",
]
if (isUnix) {
installCommands += [
"eval \"\$(pyenv init --path)\"",
"eval \"\$(pyenv init -)\"",
]
}
installCommands += [
"pyenv install --skip-existing ${pythonVersion}",
'pyenv exec pip install virtualenv',
"pyenv exec virtualenv /workspace/.venv/${TEST_RANDOM_NAME}",
Expand Down

0 comments on commit 0577c51

Please sign in to comment.