diff --git a/Jenkinsfile b/Jenkinsfile index bcece1c..ab7eb17 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -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' } } } diff --git a/src/com/ableton/Pyenv.groovy b/src/com/ableton/Pyenv.groovy index fb17bf1..66bc6c8 100644 --- a/src/com/ableton/Pyenv.groovy +++ b/src/com/ableton/Pyenv.groovy @@ -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}"]) { @@ -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}", @@ -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', diff --git a/test/com/ableton/PyenvTest.groovy b/test/com/ableton/PyenvTest.groovy index d984774..63298bd 100644 --- a/test/com/ableton/PyenvTest.groovy +++ b/test/com/ableton/PyenvTest.groovy @@ -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 @@ -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}",