Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let "Use Python Version" handle prelease Python versions #6815

Merged
merged 6 commits into from
Apr 2, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Tasks/UsePythonVersion/Tests/L0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,44 @@ describe('UsePythonVersion L0 Suite', function () {
mockery.resetCache();
})

it('converts Python prerelease versions to the semantic version format', function () {
mockery.registerMock('vsts-task-lib/task', mockTask);
mockery.registerMock('vsts-task-tool-lib/tool', {});
const uut = reload();

const testCases = [
{
versionSpec: '3.x',
expected: '3.x'
},
{
versionSpec: '3.3.6',
expected: '3.3.6'
},
{
versionSpec: '3.7.0b2',
expected: '3.7.0-b2'
},
{
versionSpec: '3.7.0rc',
expected: '3.7.0-rc'
},
{
versionSpec: '3.6.6b2 || >= 3.7.0rc',
expected: '3.6.6-b2 || >= 3.7.0-rc'
},
{
versionSpec: '3.7rc1', // invalid
expected: '3.7rc1'
},
];

for (let tc of testCases) { // Node 5 can't handle destructuring assignment
const actual = uut.pythonVersionToSemantic(tc.versionSpec);
assert.strictEqual(actual, tc.expected);
}
})

it('finds version in cache', async function () {
let buildVariables: any = {};
const mockBuildVariables = {
Expand Down
2 changes: 1 addition & 1 deletion Tasks/UsePythonVersion/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"author": "Microsoft Corporation",
"version": {
"Major": 0,
"Minor": 132,
"Minor": 133,
"Patch": 0
},
"preview": true,
Expand Down
2 changes: 1 addition & 1 deletion Tasks/UsePythonVersion/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"author": "Microsoft Corporation",
"version": {
"Major": 0,
"Minor": 132,
"Minor": 133,
"Patch": 0
},
"preview": true,
Expand Down
13 changes: 12 additions & 1 deletion Tasks/UsePythonVersion/usepythonversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,19 @@ interface TaskParameters {
readonly addToPath: boolean
}

export function pythonVersionToSemantic(versionSpec: string) {
const prereleaseVersion = /(\d\.\d\.\d)([a|b|rc]\d?)/g;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could a, b or rc be followed multiple digits?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the spec for valid version strings yes, but in practice nothing has ever gotten that high. I'll make it more robust just so we don't have to touch it again.

return versionSpec.replace(prereleaseVersion, '$1-$2');
}

export async function usePythonVersion(parameters: TaskParameters, platform: Platform): Promise<void> {
const installDir: string | null = tool.findLocalTool('Python', parameters.versionSpec);
// Python's prelease versions look like `3.7.0b2`.
// This is the one part of Python versioning that does not look like semantic versioning, which specifies `3.7.0-b2`.
// If the version spec contains prerelease versions, we need to convert them to the semantic version equivalent
const semanticVersionSpec = pythonVersionToSemantic(parameters.versionSpec);
task.debug(`Semantic version spec of ${parameters.versionSpec} is ${semanticVersionSpec}`);

const installDir: string | null = tool.findLocalTool('Python', semanticVersionSpec);
if (!installDir) {
// Fail and list available versions
throw new Error([
Expand Down