-
Notifications
You must be signed in to change notification settings - Fork 243
/
test.ps1
210 lines (174 loc) · 6.7 KB
/
test.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for full license information.
# Script to run tests for testfx.
[CmdletBinding(PositionalBinding=$false)]
Param(
[Parameter(Mandatory=$false)]
[ValidateSet("Debug", "Release")]
[Alias("c")]
[string] $Configuration = "Debug",
[Parameter(Mandatory=$false)]
[Alias("p")]
[System.String] $Pattern = "UnitTests",
[Parameter(Mandatory=$false)]
[Alias("pl")]
[Switch] $Parallel = $false,
[Parameter(Mandatory=$false)]
[Switch] $All = $false,
[Parameter(Mandatory=$false)]
[Alias("h")]
[Switch] $Help = $false
)
#
# Script Preferences
#
$ErrorActionPreference = "Stop"
$CurrentScriptDir = (Get-Item (Split-Path $MyInvocation.MyCommand.Path))
#
# Variables
#
$env:TF_ROOT_DIR = $CurrentScriptDir.Parent.FullName
$env:TF_TOOLS_DIR = Join-Path $env:TF_ROOT_DIR "tools"
$env:DOTNET_CLI_VERSION = "6.0.100-alpha.1.21067.8"
$env:TF_TESTS_OUTDIR_PATTERN = "*Tests"
$env:TF_UNITTEST_FILES_PATTERN = "*.UnitTests*.dll"
$env:TF_COMPONENTTEST_FILES_PATTERN = "*.ComponentTests*.dll"
$env:TF_E2ETEST_FILES_PATTERN = "*.E2ETests*.dll"
$env:TF_NetCoreContainers =@("MSTestAdapter.PlatformServices.NetCore.UnitTests.dll")
#
# Test configuration
#
Write-Verbose "Setup build configuration."
$TFT_Configuration = $Configuration
$TFT_Pattern = $Pattern
$TFT_Parallel = $Parallel
$TFT_All = $All
$TestFramework = ".NETCoreApp2.1"
. $PSScriptRoot\common.lib.ps1
#
# Prints help text for the switches this script supports.
#
function Print-Help {
if (-not $Help) {
return
}
Write-Host -object ""
Write-Host -object "********* MSTest Adapter Test Script *********"
Write-Host -object ""
Write-Host -object " Help (-h) - [Switch] - Prints this help message."
Write-Host -object " Parallel (-pl) - [Switch] - Indicates that the tests should be run in parallel."
Write-Host -object " All - [Switch] - Indicates that all tests should be run. This ignores the pattern provided."
Write-Host -object ""
Write-Host -object " Configuration (-c) - [String] - Specifies the build configuration. Defaults to 'Debug'."
Write-Host -object " Pattern (-p) - [String] - Runs tests from the test container that matches this pattern. For instance, specify -p E2E to run all End-to-End tests. Alternatively specifying -p TestFramework.UnitTests runs only tests from Microsoft.VisualStudio.TestPlatform.TestFramework.UnitTests.dll."
Write-Host -object ""
Exit 0
}
function Invoke-Test
{
& dotnet --info
$timer = Start-Timer
Write-Log "Run-Test: Started."
Write-Log " Computing Test Containers."
# Get all the test project folders. They should all be ending with ".Tests"
$outDir = Join-Path $env:TF_OUT_DIR -ChildPath $TFT_Configuration
$testFolders = Get-ChildItem $outDir -Directory -Filter $env:TF_TESTS_OUTDIR_PATTERN | %{$_.FullName}
# Get test assemblies from these folders that match the pattern specified.
foreach($container in $testFolders)
{
$testContainer = Get-ChildItem $container\* -Recurse -Include $env:TF_UNITTEST_FILES_PATTERN, $env:TF_COMPONENTTEST_FILES_PATTERN, $env:TF_E2ETEST_FILES_PATTERN, "DiscoveryAndExecutionTests.dll"
$testContainerName = $testContainer.Name
$testContainerPath = $testContainer.FullName
$allContainers += ,"$testContainerName"
if($TFT_All)
{
if($env:TF_NetCoreContainers -Contains $testContainerName)
{
$netCoreTestContainers += ,"$testContainerPath"
}
else
{
$testContainers += ,"$testContainerPath"
}
}
else
{
if($testContainerPath -match $TFT_Pattern)
{
if($env:TF_NetCoreContainers -Contains $testContainerName)
{
$netCoreTestContainers += ,"$testContainerPath"
}
else
{
$testContainers += ,"$testContainerPath"
}
}
}
}
if($testContainers.Count -gt 0 -Or $netCoreTestContainers.Count -gt 0)
{
$testContainersString = [system.String]::Join(",",$testContainers)
Write-Log " Matched Test Containers: $testContainersString."
Run-Test -testContainers $testContainers -netCoreTestContainers $netCoreTestContainers
}
else
{
$allContainersString = [system.String]::Join(",",$allContainers)
Write-Log " None of the test containers matched the pattern $TFT_Pattern."
Write-Log " Test Containers available: $allContainersString."
}
Write-Log "Run-Test: Complete. {$(Get-ElapsedTime($timer))}"
}
function Run-Test([string[]] $testContainers, [string[]] $netCoreTestContainers)
{
$vstestPath = Get-VSTestPath
$additionalArguments = ''
if($TFT_Parallel)
{
$additionalArguments += "/parallel"
}
if($testContainers.Count -gt 0)
{
if(!(Test-Path $vstestPath))
{
Write-Error "Unable to find vstest.console.exe at $vstestPath. Test aborted."
}
Write-Verbose "$vstestPath $testContainers $additionalArguments /logger:trx"
& $vstestPath $testContainers $additionalArguments /logger:trx
if ($lastExitCode -ne 0)
{
throw "Tests failed."
}
}
if($netCoreTestContainers.Count -gt 0)
{
Try
{
Write-Verbose "dotnet test $netCoreTestContainers /framework:`"$TestFramework`" $additionalArguments /logger:trx"
& dotnet test $netCoreTestContainers /framework:"$TestFramework" $additionalArguments /logger:trx
}
Catch [System.Management.Automation.CommandNotFoundException]
{
Write-Error "Unable to find dotnet.exe. Test aborted."
}
if ($lastExitCode -ne 0)
{
throw "Tests failed."
}
}
}
function Get-VSTestPath
{
$versionsFile = "$PSScriptRoot\build\TestFx.Versions.targets"
$TestPlatformVersion = (([XML](Get-Content $versionsFile)).Project.PropertyGroup.TestPlatformVersion).InnerText
$vsInstallPath = "$PSScriptRoot\..\packages\Microsoft.TestPlatform.$TestPlatformVersion\"
if(-not (Test-Path $vsInstallPath)) {
$vsInstallPath = "$PSScriptRoot\..\packages\Microsoft.TestPlatform\$TestPlatformVersion\"
}
$vstestPath = Join-Path -path $vsInstallPath "tools\net451\Common7\IDE\Extensions\TestPlatform\vstest.console.exe"
return Resolve-Path -path $vstestPath
}
Print-Help
Install-DotNetCli
Invoke-Test