Skip to content

Commit ed97c1f

Browse files
author
Kapil Borle
committed
Add test functionality to build script
1 parent 854dcce commit ed97c1f

File tree

1 file changed

+94
-5
lines changed

1 file changed

+94
-5
lines changed

build.ps1

Lines changed: 94 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[CmdletBinding()]
22
param(
3+
34
[ValidateSet('PSV3 Debug','PSV3 Release','Debug','Release')]
45
[string] $Configuration = 'Debug',
56

@@ -11,7 +12,15 @@ param(
1112

1213
[switch] $CleanOutput = $false,
1314

14-
[switch] $Install = $false
15+
[switch] $Install = $false,
16+
17+
[switch] $Uninstall = $false,
18+
19+
[switch] $Test = $false,
20+
21+
[switch] $Engine = $false,
22+
23+
[switch] $Rules = $false
1524
)
1625

1726
# Some cmdlets like copy-item do not respond to the $verbosepreference variable
@@ -88,16 +97,96 @@ if ($BuildDocs)
8897
New-ExternalHelp -Path $markdownDocsPath -OutputPath $outputDocsPath -Force -Verbose:$verbosity
8998
}
9099

100+
101+
$moduleRootPath = Join-Path (Split-Path $profile) 'Modules'
102+
$modulePSSAPath = Join-Path $moduleRootPath 'PSScriptAnalyzer'
91103
if ($Install)
92104
{
93-
$modulePath = Join-Path (Split-Path $profile) 'Modules'
94-
if (-not (Test-Path $modulePath))
105+
if (-not (Test-Path $moduleRootPath))
95106
{
96-
New-Item -Path $modulePath -ItemType Directory -Force -Verbose:$verbosity
107+
New-Item -Path $moduleRootPath -ItemType Directory -Force -Verbose:$verbosity
97108
}
98109
if (-not (Test-Path -Path $destinationPath))
99110
{
100111
throw "Please build the module first."
101112
}
102-
Copy-Item -Path $destinationPath -Destination $modulePath -Recurse -Verbose:$verbosity
113+
Copy-Item -Path $destinationPath -Destination $modulePSSAPath -Recurse -Verbose:$verbosity
114+
}
115+
116+
if ($Test)
117+
{
118+
Import-Module PSScriptAnalyzer -ErrorAction Stop
119+
Import-Module -Name Pester -RequiredVersion 3.4.0 -ErrorAction Stop
120+
121+
122+
Function GetTestRunnerScriptContent($testPath)
123+
{
124+
$x = @"
125+
cd $testPath
126+
Invoke-Pester
127+
"@
128+
return $x
129+
}
130+
131+
Function CreateTestRunnerScript($testPath)
132+
{
133+
$tmptmpFilePath = [System.IO.Path]::GetTempFileName()
134+
$tmpFilePath = $tmptmpFilePath + '.ps1'
135+
Move-Item $tmptmpFilePath $tmpFilePath -Verbose:$verbosity
136+
$content = GetTestRunnerScriptContent $testPath
137+
Set-Content -Path $tmpFilePath -Value $content -Verbose:$verbosity
138+
return $tmpFilePath
139+
}
140+
141+
Function GetTestPath($TestType)
142+
{
143+
if ($TestType -eq "engine")
144+
{
145+
$testPath = Join-Path $projectRoot "Tests/Engine"
146+
}
147+
else
148+
{
149+
$testPath = Join-Path $projectRoot "Tests/Rules"
150+
}
151+
return $testPath
152+
}
153+
154+
Function RunTest($TestType, $DifferentProcess)
155+
{
156+
$testPath = GetTestPath($TestType)
157+
if ($DifferentProcess)
158+
{
159+
$testScriptFilePath = CreateTestRunnerScript $testPath
160+
Start-Process powershell -ArgumentList "-NoExit","-File $testScriptFilePath" -Verb runas
161+
}
162+
else
163+
{
164+
try
165+
{
166+
Push-Location .
167+
([scriptblock]::Create((GetTestRunnerScriptContent $testPath))).Invoke()
168+
}
169+
finally
170+
{
171+
Pop-Location
172+
}
173+
}
174+
# clean up the test file
175+
}
176+
177+
if ($Engine)
178+
{
179+
RunTest('engine')
180+
}
181+
if ($Rules)
182+
{
183+
RunTest('rules')
184+
}
103185
}
186+
187+
if ($Uninstall)
188+
{
189+
Remove-Item -Path $modulePSSAPath -Force -Verbose:$verbosity -Recurse
190+
}
191+
192+

0 commit comments

Comments
 (0)