Skip to content

Commit 89438ae

Browse files
committed
Apply azdo ext best practices for testing and fix mocks for Windows and Linux (as it used to be compatible for Linux only)
1 parent 2a90b0a commit 89438ae

File tree

11 files changed

+636
-606
lines changed

11 files changed

+636
-606
lines changed

ext/azuredevops/setupAzd/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ node_modules
33
.taskkey
44
index.js
55
tests/*.js
6+
tests/*.log

ext/azuredevops/setupAzd/index.ts

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as task from 'azure-pipelines-task-lib/task'
2-
import * as cp from 'child_process'
2+
import * as toolRunner from 'azure-pipelines-task-lib/toolrunner'
33

44
export async function runMain(): Promise<void> {
55
try {
@@ -16,25 +16,51 @@ export async function runMain(): Promise<void> {
1616
return
1717
}
1818
const version = task.getInput('version') || 'latest'
19-
const windowsInstallScript = `powershell -c "$scriptPath = \\"$($env:TEMP)\\install-azd.ps1\\"; Invoke-RestMethod 'https://aka.ms/install-azd.ps1' -OutFile $scriptPath; . $scriptPath -Version '${version}' -Verbose:$true; Remove-Item $scriptPath"`
20-
const linuxOrMacOSInstallScript = `curl -fsSL https://aka.ms/install-azd.sh | sudo bash -s -- --version ${version} --verbose`
2119

2220
console.log(`Installing azd version ${version} on ${os}.`)
2321

2422
if (os === 'win32') {
25-
console.log(cp.execSync(windowsInstallScript).toString())
23+
const powershellPath = task.which('powershell', true)
24+
const powershell: toolRunner.ToolRunner = task.tool(powershellPath)
25+
const installScript = `$scriptPath = "$($env:TEMP)\\install-azd.ps1"; Invoke-RestMethod 'https://aka.ms/install-azd.ps1' -OutFile $scriptPath; . $scriptPath -Version '${version}' -Verbose:$true; Remove-Item $scriptPath`
26+
powershell.arg('-NoLogo')
27+
powershell.arg('-NoProfile')
28+
powershell.arg('-NonInteractive')
29+
powershell.arg('-Command')
30+
powershell.arg(installScript)
31+
32+
const installResult = await powershell.exec()
33+
if (installResult !== 0) {
34+
task.setResult(task.TaskResult.Failed, `Failed to install azd. Exit code: ${installResult}`)
35+
return
36+
}
2637

2738
// Add azd to PATH
2839
task.setVariable('PATH', `${envPath};${localAppData}\\Programs\\Azure Dev CLI`)
29-
} else {
30-
console.log(cp.execSync(linuxOrMacOSInstallScript).toString())
31-
}
3240

33-
// Run `azd version` to make sure if azd installation failed, it returns error on windows
34-
if (os === 'win32') {
35-
const azdVersion = `"${localAppData}\\Programs\\Azure Dev CLI\\azd.exe" version`
36-
cp.execSync(azdVersion)
41+
// Run `azd version` to make sure installation succeeded
42+
const azdPath = `${localAppData}\\Programs\\Azure Dev CLI\\azd.exe`
43+
const azd: toolRunner.ToolRunner = task.tool(azdPath)
44+
azd.arg('version')
45+
const versionResult = await azd.exec()
46+
if (versionResult !== 0) {
47+
task.setResult(task.TaskResult.Failed, `azd version check failed. Exit code: ${versionResult}`)
48+
return
49+
}
50+
} else {
51+
const bashPath = task.which('bash', true)
52+
const bash: toolRunner.ToolRunner = task.tool(bashPath)
53+
bash.arg('-c')
54+
bash.arg(`curl -fsSL https://aka.ms/install-azd.sh | sudo bash -s -- --version ${version} --verbose`)
55+
56+
const installResult = await bash.exec()
57+
if (installResult !== 0) {
58+
task.setResult(task.TaskResult.Failed, `Failed to install azd. Exit code: ${installResult}`)
59+
return
60+
}
3761
}
62+
63+
console.log(`Successfully installed azd version ${version}.`)
3864
} catch (err: any) {
3965
task.setResult(task.TaskResult.Failed, err.message)
4066
}

0 commit comments

Comments
 (0)