-
Notifications
You must be signed in to change notification settings - Fork 0
/
pats.ps1
executable file
·109 lines (89 loc) · 2.83 KB
/
pats.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
param (
[String] $TestCase = 'test.pats'
)
Set-StrictMode -Version 2.0
$testNameKey = "testName"
$testPSKey = "testPS"
function ParseTestcase() {
param(
[string] $TestCase
)
# read the file as one long string
[String]$raw = Get-Content $TestCase -raw
$testRe = '@test "([^"]+)"\s*{\s*(.*)\s*}'
$matches = [regex]::Matches($raw, $testRe)
$tests = @()
foreach ($match in $matches) {
$testName = $match.captures.groups[1]
$testPS = $match.captures.groups[2]
$test = @{
$testNameKey=$testName;
$testPSKey = $testPS
}
$tests += $test
}
return $tests
}
function RunTest() {
param([string] $PS)
# temporary files can only be created as .tmp, we must move it to end .ps1 or powershell refuse to run it
$tmp = New-TemporaryFile
$tmp.MoveTo("$($tmp.FullName).ps1")
# exit with the status of the subcommand, not the powershell command itself
"$($PS) ; exit `$lastExitCode" | Out-File -filepath $tmp.FullName
$exitCode = 255
try {
# https://stackoverflow.com/a/8762068/3441106
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = "powershell.exe"
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$pinfo.Arguments = "-File $($tmp.FullName)"
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$p.WaitForExit()
$stdout = $p.StandardOutput.ReadToEnd()
$stderr = $p.StandardError.ReadToEnd()
$exitCode = $p.ExitCode
#write-host $stdout
#write-host $stderr
#write-host "test done: status $($exitCode)"
} finally {
Remove-Item $tmp.FullName -Force
}
return $exitCode
}
function RunTests() {
param(
[array] $Tests
)
$failedTests = 0
foreach ($test in $Tests) {
$status = RunTest $test[$testPSKey]
if ($status -eq 0) {
$emoji = "[*]"
write-host " $($emoji) $($test[$testNameKey])"
} else {
$emoji = "[X]"
$failedTests += 1
write-error " $($emoji) $($test[$testNameKey])"
}
}
write-host "$($tests.Count) test, $($failedTests) failure"
return $failedTests
}
if ($TestCase -eq "") {
write-error "Must specify Testcase File"
} else {
if (Test-Path $TestCase) {
write-host "processing tests in $($TestCase)"
$tests = ParseTestcase $TestCase
$status = RunTests $tests
exit $status
} else {
Write-Error "Missing testcase file: $($TestCase)"
exit 1
}
}