|
| 1 | +import * as assert from 'assert'; |
| 2 | +import * as os from 'os'; |
| 3 | +import * as path from 'path'; |
| 4 | +import * as task from 'azure-pipelines-task-lib/task'; |
| 5 | + |
| 6 | +import { MockTestRunner } from 'azure-pipelines-task-lib/mock-test'; |
| 7 | + |
| 8 | +function didSetVariable(testRunner: MockTestRunner, variableName: string, variableValue: string): boolean { |
| 9 | + return testRunner.stdOutContained(`##vso[task.setvariable variable=${variableName};isOutput=false;issecret=false;]${variableValue}`); |
| 10 | +} |
| 11 | + |
| 12 | +function didPrependPath(testRunner: MockTestRunner, toolPath: string): boolean { |
| 13 | + return testRunner.stdOutContained(`##vso[task.prependpath]${toolPath}`); |
| 14 | +} |
| 15 | + |
| 16 | +describe('UsePythonVersion L0 Suite', function () { |
| 17 | + this.timeout(parseInt(process.env.TASK_TEST_TIMEOUT) || 5000); |
| 18 | + |
| 19 | + describe('usepythonversion.ts', function () { |
| 20 | + require('./L0_usepythonversion'); |
| 21 | + }); |
| 22 | + |
| 23 | + describe('versionspec.ts', function () { |
| 24 | + require('./L0_versionspec'); |
| 25 | + }); |
| 26 | + |
| 27 | + it('succeeds when version is found', async function () { |
| 28 | + const testFile = path.join(__dirname, 'L0SucceedsWhenVersionIsFound.js'); |
| 29 | + const testRunner = new MockTestRunner(testFile); |
| 30 | + |
| 31 | + await testRunner.runAsync(); |
| 32 | + |
| 33 | + const pythonDir = path.join('/', 'Python', '3.6.4', 'x64'); |
| 34 | + const pythonBinDir = task.getPlatform() === task.Platform.Windows |
| 35 | + ? path.join(pythonDir, 'Scripts') |
| 36 | + : path.join(pythonDir, 'bin'); |
| 37 | + |
| 38 | + assert(didSetVariable(testRunner, 'pythonLocation', pythonDir)); |
| 39 | + assert(didPrependPath(testRunner, pythonDir)); |
| 40 | + assert(didPrependPath(testRunner, pythonBinDir)); |
| 41 | + assert.strictEqual(testRunner.stderr.length, 0, 'should not have written to stderr'); |
| 42 | + assert(testRunner.succeeded, 'task should have succeeded'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('downloads python from registry on Windows', async function () { |
| 46 | + const testFile = path.join(__dirname, 'L0DownloadsFromRegistryWindows.js'); |
| 47 | + const testRunner = new MockTestRunner(testFile); |
| 48 | + |
| 49 | + await testRunner.runAsync(); |
| 50 | + |
| 51 | + const pythonDir = path.join('C', 'tools', 'Python', '3.10.1', 'x64'); |
| 52 | + const pythonBinDir = path.join(pythonDir, 'Scripts'); |
| 53 | + const pythonAppdataDir = path.join('testappdata', 'Python', 'Python310', 'Scripts'); |
| 54 | + |
| 55 | + assert(didSetVariable(testRunner, 'pythonLocation', pythonDir)); |
| 56 | + assert(didPrependPath(testRunner, pythonDir)); |
| 57 | + assert(didPrependPath(testRunner, pythonBinDir)); |
| 58 | + assert(didPrependPath(testRunner, pythonAppdataDir)); |
| 59 | + assert.strictEqual(testRunner.stderr.length, 0, 'should not have written to stderr'); |
| 60 | + assert(testRunner.succeeded, 'task should have succeeded'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('downloads python from registry on Ubuntu', async function () { |
| 64 | + const testFile = path.join(__dirname, 'L0DownloadsFromRegistryUbuntu.js'); |
| 65 | + const testRunner = new MockTestRunner(testFile); |
| 66 | + |
| 67 | + await testRunner.runAsync(); |
| 68 | + |
| 69 | + const pythonDir = path.join('opt', 'hostedtoolcache', 'Python', '3.10.1', 'x64'); |
| 70 | + const pythonBinDir = path.join(pythonDir, 'bin'); |
| 71 | + |
| 72 | + assert(didSetVariable(testRunner, 'pythonLocation', pythonDir)); |
| 73 | + assert(didPrependPath(testRunner, pythonDir)); |
| 74 | + assert(didPrependPath(testRunner, pythonBinDir)); |
| 75 | + assert.strictEqual(testRunner.stderr.length, 0, 'should not have written to stderr'); |
| 76 | + assert(testRunner.succeeded, 'task should have succeeded'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('downloads unstable python from registry', async function () { |
| 80 | + const testFile = path.join(__dirname, 'L0DownloadsUnstable.js'); |
| 81 | + const testRunner = new MockTestRunner(testFile); |
| 82 | + |
| 83 | + await testRunner.runAsync(); |
| 84 | + |
| 85 | + const pythonDir = path.join('opt', 'hostedtoolcache', 'Python', '3.11.1', 'x64'); |
| 86 | + const pythonBinDir = path.join(pythonDir, 'bin'); |
| 87 | + |
| 88 | + assert(didSetVariable(testRunner, 'pythonLocation', pythonDir)); |
| 89 | + assert(didPrependPath(testRunner, pythonDir)); |
| 90 | + assert(didPrependPath(testRunner, pythonBinDir)); |
| 91 | + assert.strictEqual(testRunner.stderr.length, 0, 'should not have written to stderr'); |
| 92 | + assert(testRunner.succeeded, 'task should have succeeded'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('fails when version is not found', async function () { |
| 96 | + const testFile = path.join(__dirname, 'L0FailsWhenVersionIsMissing.js'); |
| 97 | + const testRunner = new MockTestRunner(testFile); |
| 98 | + |
| 99 | + await testRunner.runAsync(); |
| 100 | + |
| 101 | + assert(testRunner.createdErrorIssue('loc_mock_DownloadFailed Error: loc_mock_DownloadNotFound 3.11.x x64')); |
| 102 | + |
| 103 | + const errorMessage = [ |
| 104 | + 'loc_mock_VersionNotFound 3.11.x x64', |
| 105 | + 'loc_mock_ListAvailableVersions $(Agent.ToolsDirectory)', |
| 106 | + '2.6.0 (x86)', |
| 107 | + '2.7.13 (x86)', |
| 108 | + '2.6.0 (x64)', |
| 109 | + '2.7.13 (x64)', |
| 110 | + 'loc_mock_ToolNotFoundMicrosoftHosted Python https://aka.ms/hosted-agent-software', |
| 111 | + 'loc_mock_ToolNotFoundSelfHosted Python https://go.microsoft.com/fwlink/?linkid=871498', |
| 112 | + ].join('\r\n'); |
| 113 | + |
| 114 | + assert(testRunner.createdErrorIssue(errorMessage)); |
| 115 | + assert(testRunner.failed, 'task should have failed'); |
| 116 | + }); |
| 117 | + |
| 118 | + it('selects architecture passed as input', async function () { |
| 119 | + const testFile = path.join(__dirname, 'L0SelectsArchitecture.js'); |
| 120 | + const testRunner = new MockTestRunner(testFile); |
| 121 | + |
| 122 | + await testRunner.runAsync(); |
| 123 | + |
| 124 | + assert(didSetVariable(testRunner, 'pythonLocation', 'x86ToolPath')); |
| 125 | + assert.strictEqual(testRunner.stderr.length, 0, 'should not have written to stderr'); |
| 126 | + assert(testRunner.succeeded, 'task should have succeeded'); |
| 127 | + }); |
| 128 | + |
| 129 | + it('finds PyPy2', async function () { |
| 130 | + const testFile = path.join(__dirname, 'L0PyPy2.js'); |
| 131 | + const testRunner = new MockTestRunner(testFile); |
| 132 | + |
| 133 | + await testRunner.runAsync(); |
| 134 | + |
| 135 | + const pypyDir = path.join('/', 'PyPy', '2.7.9', 'x64'); |
| 136 | + const pypyBinDir = path.join(pypyDir, 'bin'); |
| 137 | + const pythonLocation = task.getPlatform() === task.Platform.Windows |
| 138 | + ? pypyDir |
| 139 | + : pypyBinDir; |
| 140 | + |
| 141 | + assert(didSetVariable(testRunner, 'pythonLocation', pythonLocation)); |
| 142 | + assert(didPrependPath(testRunner, pypyDir)); |
| 143 | + assert(didPrependPath(testRunner, pypyBinDir)); |
| 144 | + assert.strictEqual(testRunner.stderr.length, 0, 'should not have written to stderr'); |
| 145 | + assert(testRunner.succeeded, 'task should have succeeded'); |
| 146 | + }); |
| 147 | + |
| 148 | + it('finds PyPy3', async function () { |
| 149 | + const testFile = path.join(__dirname, 'L0PyPy3.js'); |
| 150 | + const testRunner = new MockTestRunner(testFile); |
| 151 | + |
| 152 | + await testRunner.runAsync(); |
| 153 | + |
| 154 | + const pypyDir = path.join('/', 'PyPy', '3.5.2', 'x64'); |
| 155 | + const pypyBinDir = path.join(pypyDir, 'bin'); |
| 156 | + const pythonLocation = task.getPlatform() === task.Platform.Windows |
| 157 | + ? pypyDir |
| 158 | + : pypyBinDir; |
| 159 | + |
| 160 | + assert(didSetVariable(testRunner, 'pythonLocation', pythonLocation)); |
| 161 | + assert(didPrependPath(testRunner, pypyDir)); |
| 162 | + assert(didPrependPath(testRunner, pypyBinDir)); |
| 163 | + assert.strictEqual(testRunner.stderr.length, 0, 'should not have written to stderr'); |
| 164 | + assert(testRunner.succeeded, 'task should have succeeded'); |
| 165 | + }); |
| 166 | +}); |
0 commit comments