-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRun-Tests.ps1
79 lines (65 loc) · 2.6 KB
/
Run-Tests.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
<#
.SYNOPSIS
Runs Python unit tests in multiple environments
.OUTPUTS
Array of test results. Each item has
* Python Path
* Test return code
* Error message. (Only non-zero return codes will have a message)
#>
# Get python.exe paths.
[System.IO.FileInfo[]]$pyenvs = Get-Item "C:\Python*\**\python.exe" # Python 2.x (ArcGIS Desktop)
# Get the Python 3.X ArcGIS Pro Python executables, excluding the ones in the /pkgs/ folder.
$pyenvs += Get-ChildItem "$env:ProgramFiles\ArcGIS" "python.exe" -File -Recurse | Where-Object { $_.FullName -notlike "*pkgs*" }
# Initialize a list of Process objects.
[System.Diagnostics.Process[]]$jobs = $()
# Create directory for test output
[System.IO.DirectoryInfo]$test_output_dir = "$env:TEMP\test_output"
New-Item $test_output_dir -ItemType Directory -ErrorAction Ignore | Write-Host
# Name for progress activity
$activity = "Running unittests"
Write-Progress $activity
# Loop through the paths to the various python executables.
# TODO: Update the progress meter as each process finishes.
$i = 0
foreach ($pypath in $pyenvs) {
# Run the unittests and store the error code as a variable.
$jobs += Start-Process -FilePath $pypath.FullName -ArgumentList "-m unittest discover --start-directory src" -PassThru -NoNewWindow -RedirectStandardError "$test_output_dir\Error$i.txt"
$i++
}
# Wait for all of the unit test processes to finish.
Wait-Process -InputObject $jobs
# Stop the progress meter now that the processes are finished.
Write-Progress $activity -Completed
# Create the TestResult class
class TestResult {
[System.IO.FileInfo]$PythonPath
[int]$ReturnCode
[string]$ErrorMessage
TestResult($python, $returnCode, $errorMessage) {
$this.PythonPath = $python
$this.ReturnCode = $returnCode
$this.ErrorMessage = $errorMessage
}
}
# Initialize output result list.
[TestResult[]]$jobResults = $()
# Loop through the process objects and collect the results.
for ($i = 0; $i -lt $jobs.Count; $i++) {
$path = $pyenvs[$i]
$proc = $jobs[$i]
$errorFile = "$test_output_dir\Error$i.txt"
# If there was an error message, read the result to a file.
[string]$msg = $null
if ($proc.ExitCode -ne 0) {
$msg = Get-Content $errorFile -Raw
}
# Now that the error message text has been stored (or ignored if there was no error),
# the corresponding file can be deleted.
Remove-Item $errorFile
$result = New-Object TestResult @($path.FullName, $proc.ExitCode, $msg)
$jobResults += $result
}
# Remove the directory that had the test output files.
Remove-Item $test_output_dir
return $jobResults