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 c4c64c5 commit 2ad116d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
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
21 changes: 19 additions & 2 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), '', 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,9 +109,19 @@ 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) {
Expand Down

0 comments on commit 2ad116d

Please sign in to comment.