Skip to content

Commit 669d300

Browse files
Merge pull request #1556 from bcullman/fix-script-under-test-unpack
Unpack scripts under test automatically
2 parents 439408c + b060fc6 commit 669d300

File tree

1 file changed

+54
-30
lines changed

1 file changed

+54
-30
lines changed

step-templates/tests/Invoke-PesterTests.ps1

+54-30
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,60 @@ Set-StrictMode -Version "Latest";
33

44
$thisScript = $MyInvocation.MyCommand.Path;
55
$thisFolder = [System.IO.Path]::GetDirectoryName($thisScript);
6-
$rootFolder = [System.IO.Path]::GetDirectoryName($thisFolder);
7-
$parentFolder = [System.IO.Path]::GetDirectoryName($rootFolder);
8-
9-
# Unpack any tests that are not present
10-
$testableScripts = Get-ChildItem -Path $thisFolder -Filter "*.ScriptBody.ps1"
11-
foreach ($script in $testableScripts) {
12-
$filename = [System.IO.Path]::Combine($rootFolder, $script.Name)
13-
if (-not [System.IO.File]::Exists($filename)) {
14-
$searchpattern = $script.BaseName -replace "\.ScriptBody$"
15-
$toolsFolder = [System.IO.Path]::Combine($parentFolder, "tools")
16-
$converter = [System.IO.Path]::Combine($toolsFolder, "Converter.ps1")
17-
& $converter -operation unpack -searchpattern $searchpattern
18-
}
19-
. $filename;
6+
$rootFolder = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($thisFolder, "..", "..")); # Adjust to always point to the root
7+
$testFiles = Get-ChildItem -Path "$thisFolder" -Filter "*.tests.ps1" -Recurse
8+
9+
function Unpack-Scripts-Under-Test {
10+
foreach ($testFile in $testFiles) {
11+
$baseName = $testFile.BaseName -replace "\.ScriptBody.Tests$"
12+
$scriptFileName = "$baseName.ScriptBody.ps1"
13+
$scriptFilePath = [System.IO.Path]::Combine($rootFolder, "step-templates", $scriptFileName)
14+
15+
# If the .ps1 file is missing, find the corresponding .json file and unpack it
16+
if (-not [System.IO.File]::Exists($scriptFilePath)) {
17+
Write-Host "Unpacking script for $($testFile.Name) since $scriptFileName is missing..."
18+
19+
$jsonFileName = "$baseName.json"
20+
$jsonFilePath = [System.IO.Path]::Combine($rootFolder, "step-templates", $jsonFileName)
21+
22+
if (-not [System.IO.File]::Exists($jsonFilePath)) {
23+
throw "JSON file $jsonFileName not found. Cannot unpack script."
24+
}
25+
26+
$converter = [System.IO.Path]::Combine($rootFolder, "tools", "Converter.ps1")
27+
& $converter -operation unpack -searchpattern $baseName
28+
29+
if (-not [System.IO.File]::Exists($scriptFilePath)) {
30+
throw "Failed to unpack $scriptFileName. Make sure the JSON template exists and the unpack operation succeeded."
31+
}
32+
} else {
33+
Write-Host "Script $scriptFileName already exists, no need to unpack."
34+
}
35+
}
2036
}
2137

22-
# Attempt to use local Pester module, fallback to global if not found
23-
try {
24-
$packagesFolder = [System.IO.Path]::Combine($rootFolder, "packages")
25-
$pester3Path = [System.IO.Path]::Combine($packagesFolder, "Pester\tools\Pester")
26-
# Import the specific version of Pester 3.4.0
27-
Import-Module -Name $pester3Path -RequiredVersion 3.4.0 -ErrorAction Stop
28-
} catch {
29-
Write-Host "Using globally installed Pester module version 3.4.0."
30-
# Specify the exact version of Pester 3.x you have installed
31-
Import-Module -Name Pester -RequiredVersion 3.4.0 -ErrorAction Stop}
32-
33-
# Find and run all Pester test files in the tests directory
34-
$testFiles = Get-ChildItem -Path "$thisFolder" -Filter "*.tests.ps1" -Recurse
35-
foreach ($testFile in $testFiles) {
36-
Write-Host "Running tests in: $($testFile.FullName)"
37-
Invoke-Pester -Path $testFile.FullName
38+
function Import-Pester {
39+
# Attempt to use local Pester module, fallback to global if not found
40+
try {
41+
$packagesFolder = [System.IO.Path]::Combine($rootFolder, "packages")
42+
$pester3Path = [System.IO.Path]::Combine($packagesFolder, "Pester\tools\Pester")
43+
# Import the specific version of Pester 3.4.0
44+
Import-Module -Name $pester3Path -RequiredVersion 3.4.0 -ErrorAction Stop
45+
} catch {
46+
Write-Host "Using globally installed Pester module version 3.4.0."
47+
# Specify the exact version of Pester 3.x you have installed
48+
Import-Module -Name Pester -RequiredVersion 3.4.0 -ErrorAction Stop
49+
}
3850
}
51+
52+
function Run-Tests {
53+
# Find and run all Pester test files in the tests directory
54+
foreach ($testFile in $testFiles) {
55+
Write-Host "Running tests in: $($testFile.FullName)"
56+
Invoke-Pester -Path $testFile.FullName
57+
}
58+
}
59+
60+
Import-Pester
61+
Unpack-Scripts-Under-Test
62+
Run-Tests

0 commit comments

Comments
 (0)