Skip to content

Commit dbb10c6

Browse files
[ubuntu] Refactor pester tests (#9006)
* [ubuntu] Refactor pester tests * Fix key name and add BeforeAll * Fix ActionArchiveCache test
1 parent 2179765 commit dbb10c6

15 files changed

+70
-179
lines changed

images/ubuntu/scripts/tests/ActionArchiveCache.Tests.ps1

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
Describe "ActionArchiveCache" {
2+
BeforeDiscovery {
3+
$actionArchiveCachePath = "/opt/actionarchivecache"
4+
$tarballTestCases = Get-ChildItem -Path "$actionArchiveCachePath/*.tar.gz" -Recurse | ForEach-Object { @{ ActionTarball = $_.FullName } }
5+
}
6+
27
Context "Action archive cache directory not empty" {
3-
It "/opt/actionarchivecache not empty" {
4-
(Get-ChildItem -Path "/opt/actionarchivecache/*.tar.gz" -Recurse).Count | Should -BeGreaterThan 0
8+
It "<ActionArchiveCachepath> not empty" -TestCases @{ ActionArchiveCachepath = $actionArchiveCachePath } {
9+
(Get-ChildItem -Path "$ActionArchiveCachepath/*.tar.gz" -Recurse).Count | Should -BeGreaterThan 0
510
}
611
}
712

813
Context "Action tarball not empty" {
9-
$testCases = Get-ChildItem -Path "/opt/actionarchivecache/*.tar.gz" -Recurse | ForEach-Object { @{ ActionTarball = $_.FullName } }
10-
It "<ActionTarball>" -TestCases $testCases {
11-
param ([string] $ActionTarball)
14+
It "<ActionTarball>" -TestCases $tarballTestCases {
1215
(Get-Item "$ActionTarball").Length | Should -BeGreaterThan 0
1316
}
1417
}

images/ubuntu/scripts/tests/Android.Tests.ps1

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,4 @@
11
Describe "Android" {
2-
BeforeAll {
3-
function Test-AndroidPackage {
4-
<#
5-
.SYNOPSIS
6-
This function tests existance of an Android package.
7-
8-
.DESCRIPTION
9-
The Test-AndroidPackage function is used to test an existance of Android package in ANDROID_HOME path.
10-
11-
.PARAMETER PackageName
12-
The name of the Android package to test.
13-
14-
.EXAMPLE
15-
Test-AndroidPackage
16-
17-
This command tests the Android package.
18-
19-
#>
20-
param (
21-
[Parameter(Mandatory=$true)]
22-
[string] $PackageName
23-
)
24-
25-
# Convert 'cmake;3.6.4111459' -> 'cmake/3.6.4111459'
26-
$PackageName = $PackageName.Replace(";", "/")
27-
$targetPath = Join-Path $env:ANDROID_HOME $PackageName
28-
$targetPath | Should -Exist
29-
}
30-
}
31-
322
function Get-AndroidPackages {
333
<#
344
.SYNOPSIS
@@ -130,8 +100,10 @@ Describe "Android" {
130100
$testCases = $androidPackages | ForEach-Object { @{ PackageName = $_ } }
131101

132102
It "<PackageName>" -TestCases $testCases {
133-
param ([string] $PackageName)
134-
Test-AndroidPackage $PackageName
103+
# Convert 'cmake;3.6.4111459' -> 'cmake/3.6.4111459'
104+
$PackageName = $PackageName.Replace(";", "/")
105+
$targetPath = Join-Path $env:ANDROID_HOME $PackageName
106+
$targetPath | Should -Exist
135107
}
136108
}
137109
}
Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,23 @@
11
Import-Module "$PSScriptRoot/../helpers/Common.Helpers.psm1"
22

3-
$cmd = (Get-ToolsetContent).apt.cmd_packages + (Get-ToolsetContent).apt.vital_packages
4-
53
Describe "Apt" {
6-
7-
$testCases = $cmd | ForEach-Object {
8-
@{ toolName = $_ }
9-
}
4+
$packages = (Get-ToolsetContent).apt.cmd_packages + (Get-ToolsetContent).apt.vital_packages
5+
$testCases = $packages | ForEach-Object { @{ toolName = $_ } }
106

117
It "<toolName> is available" -TestCases $testCases {
12-
if ($toolName -eq "acl")
13-
{
14-
$toolName = "getfacl"
15-
}
16-
17-
if ($toolName -eq "aria2")
18-
{
19-
$toolName = "aria2c"
20-
}
21-
22-
if ($toolName -eq "p7zip-full")
23-
{
24-
$toolName = "p7zip"
25-
}
26-
27-
if ($toolName -eq "subversion")
28-
{
29-
$toolName = "svn"
30-
}
31-
32-
if ($toolName -eq "sphinxsearch")
33-
{
34-
$toolName = "searchd"
35-
}
36-
37-
if ($toolName -eq "binutils")
38-
{
39-
$toolName = "strings"
40-
}
41-
42-
if ($toolName -eq "coreutils")
43-
{
44-
$toolName = "tr"
8+
switch ($toolName) {
9+
"acl" { $toolName = "getfacl"; break }
10+
"aria2" { $toolName = "aria2c"; break }
11+
"p7zip-full" { $toolName = "p7zip"; break }
12+
"subversion" { $toolName = "svn"; break }
13+
"sphinxsearch" { $toolName = "searchd"; break }
14+
"binutils" { $toolName = "strings"; break }
15+
"coreutils" { $toolName = "tr"; break }
16+
"net-tools" { $toolName = "netstat"; break }
17+
"mercurial" { $toolName = "hg"; break }
18+
"findutils" { $toolName = "find"; break }
4519
}
4620

47-
if ($toolName -eq "net-tools")
48-
{
49-
$toolName = "netstat"
50-
}
51-
52-
if ($toolName -eq "mercurial")
53-
{
54-
$toolName = "hg"
55-
}
56-
57-
if ($toolName -eq "findutils")
58-
{
59-
$toolName = "find"
60-
}
6121
(Get-Command -Name $toolName).CommandType | Should -BeExactly "Application"
6222
}
6323
}

images/ubuntu/scripts/tests/CLI.Tools.Tests.ps1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
Describe "Azure CLI" {
32
It "Azure CLI" {
43
"az --version" | Should -ReturnZeroExitCode

images/ubuntu/scripts/tests/Common.Tests.ps1

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
Describe "PHP" {
2-
3-
[array]$testCases = (Get-ToolsetContent).php.versions | ForEach-Object { @{phpVersion = $_} }
2+
$testCases = (Get-ToolsetContent).php.versions | ForEach-Object { @{PhpVersion = $_} }
43

54
It "php <phpVersion>" -TestCases $testCases {
6-
param (
7-
[string] $phpVersion
8-
)
9-
10-
"php$phpVersion --version" | Should -ReturnZeroExitCode
11-
"php-config$phpVersion --version" | Should -ReturnZeroExitCode
12-
"phpize$phpVersion --version" | Should -ReturnZeroExitCode
5+
"php${PhpVersion} --version" | Should -ReturnZeroExitCode
6+
"php-config${PhpVersion} --version" | Should -ReturnZeroExitCode
7+
"phpize${PhpVersion} --version" | Should -ReturnZeroExitCode
138
}
149

1510
It "PHPUnit" {
@@ -44,7 +39,7 @@ Describe "Swift" {
4439
}
4540

4641
Describe "PipxPackages" {
47-
[array]$testCases = (Get-ToolsetContent).pipx | ForEach-Object { @{package=$_.package; cmd = $_.cmd} }
42+
$testCases = (Get-ToolsetContent).pipx | ForEach-Object { @{package=$_.package; cmd = $_.cmd} }
4843

4944
It "<package>" -TestCases $testCases {
5045
"$cmd --version" | Should -ReturnZeroExitCode

images/ubuntu/scripts/tests/Databases.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Describe "MongoDB" -Skip:(Test-IsUbuntu22) {
44
@{ ToolName = "mongod" }
55
) {
66
$toolsetVersion = (Get-ToolsetContent).mongodb.version
7-
(&$ToolName --version)[2].Split('"')[-2] | Should -BeLike "$toolsetVersion*"
7+
(& $ToolName --version)[2].Split('"')[-2] | Should -BeLike "$toolsetVersion*"
88
}
99
}
1010

images/ubuntu/scripts/tests/DotnetSDK.Tests.ps1

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
Import-Module "$PSScriptRoot/../helpers/Common.Helpers.psm1"
22

33
Describe "Dotnet and tools" {
4-
54
BeforeAll {
65
$env:PATH = "/etc/skel/.dotnet/tools:$($env:PATH)"
76
$dotnetSDKs = dotnet --list-sdks | ConvertTo-Json
87
$dotnetRuntimes = dotnet --list-runtimes | ConvertTo-Json
98
}
10-
9+
1110
$dotnetVersions = (Get-ToolsetContent).dotnet.versions
1211

1312
Context "Default" {
@@ -20,11 +19,11 @@ Describe "Dotnet and tools" {
2019
Context "Dotnet $version" {
2120
$dotnet = @{ dotnetVersion = $version }
2221

23-
It "SDK $version is available" -TestCases $dotnet {
22+
It "SDK <dotnetVersion> is available" -TestCases $dotnet {
2423
$dotnetSDKs | Should -Match "$dotnetVersion.[1-9]*"
2524
}
2625

27-
It "Runtime $version is available" -TestCases $dotnet {
26+
It "Runtime <dotnetVersion> is available" -TestCases $dotnet {
2827
$dotnetRuntimes | Should -Match "$dotnetVersion.[1-9]*"
2928
}
3029
}
@@ -38,5 +37,4 @@ Describe "Dotnet and tools" {
3837
"$TestInstance" | Should -ReturnZeroExitCode
3938
}
4039
}
41-
42-
}
40+
}

images/ubuntu/scripts/tests/Haskell.Tests.ps1

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ Describe "Haskell" {
55
$testCase = @{ GHCVersions = $GHCVersions }
66

77
It "GHC directory contains two version of GHC" -TestCases $testCase {
8-
param ([object] $GHCVersions)
98
$GHCVersions.Count | Should -Be 2
109
}
1110

1211
$testCases = $GHCVersions | ForEach-Object { @{ GHCPath = "${_}/bin/ghc"} }
1312

1413
It "GHC version <GHCPath>" -TestCases $testCases {
15-
param ([string] $GHCPath)
16-
"$GHCPath --version" | Should -ReturnZeroExitCode
14+
"$GHCPath --version" | Should -ReturnZeroExitCode
1715
}
1816

1917
It "GHCup" {

images/ubuntu/scripts/tests/Helpers.psm1

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ function ShouldReturnZeroExitCode {
9696
[bool] $succeeded = $result.ExitCode -eq 0
9797
if ($Negate) { $succeeded = -not $succeeded }
9898

99-
if (-not $succeeded)
100-
{
99+
if (-not $succeeded) {
101100
$commandOutputIndent = " " * 4
102101
$commandOutput = ($result.Output | ForEach-Object { "${commandOutputIndent}${_}" }) -join "`n"
103102
$failureMessage = "Command '${ActualValue}' has finished with exit code`n${commandOutput}"
@@ -149,8 +148,7 @@ function ShouldOutputTextMatchingRegex {
149148
if (-not $succeeded) {
150149
if ($Negate) {
151150
$failureMessage = "Expected regular expression '$RegularExpression' for '$ActualValue' command to not match '$output', but it did match."
152-
}
153-
else {
151+
} else {
154152
$failureMessage = "Expected regular expression '$RegularExpression' for '$ActualValue' command to match '$output', but it did not match."
155153
}
156154
}

images/ubuntu/scripts/tests/Java.Tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ Describe "Java" {
55
$defaultVersion = $toolsetJava.default
66
$jdkVersions = $toolsetJava.versions
77

8-
[array]$testCases = $jdkVersions | ForEach-Object { @{Version = $_ } }
9-
108
It "Java <DefaultJavaVersion> is default" -TestCases @{ DefaultJavaVersion = $defaultVersion } {
119
$actualJavaPath = [System.Environment]::GetEnvironmentVariable("JAVA_HOME")
1210
$expectedJavaPath = [System.Environment]::GetEnvironmentVariable("JAVA_HOME_${DefaultJavaVersion}_X64")
@@ -35,6 +33,8 @@ Describe "Java" {
3533
"`"$GradlePath`" -version" | Should -ReturnZeroExitCode
3634
}
3735

36+
$testCases = $jdkVersions | ForEach-Object { @{Version = $_ } }
37+
3838
It "Java <Version>" -TestCases $testCases {
3939
$javaVariableValue = [System.Environment]::GetEnvironmentVariable("JAVA_HOME_${Version}_X64")
4040
$javaVariableValue | Should -Not -BeNullOrEmpty

0 commit comments

Comments
 (0)