forked from pester/Pester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPester.psm1
92 lines (72 loc) · 2.53 KB
/
Pester.psm1
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
# Pester
# Version: $version$
# Changeset:
Resolve-Path $PSScriptRoot\Functions\*.ps1 |
? { -not ($_.ProviderPath.Contains(".Tests.")) } |
% { . $_.ProviderPath }
function Invoke-Pester {
param(
[Parameter(Position=0,Mandatory=0)]
[string]$relative_path = ".",
[Parameter(Position=1,Mandatory=0)]
[string]$testName = $null,
[Parameter(Position=2,Mandatory=0)]
[switch]$EnableExit,
[Parameter(Position=3,Mandatory=0)]
[string]$OutputXml = ''
)
Reset-GlobalTestResults
. "$PSScriptRoot\ObjectAdaptations\PesterFailure.ps1"
Update-TypeData -pre "$PSScriptRoot\ObjectAdaptations\types.ps1xml" -ErrorAction SilentlyContinue
$fixtures_path = Resolve-Path $relative_path
Write-Host "Executing all tests in $fixtures_path"
Get-ChildItem $fixtures_path -Include "*.ps1" -Recurse |
? { $_.Name -match "\.Tests\." } |
% { & $_.PSPath }
Write-TestReport
if($OutputXml) {
$Global:ModulePath = $PSScriptRoot
Write-NunitTestReport (Get-GlobalTestResults) $OutputXml
}
if ($EnableExit) { Exit-WithCode }
}
function Write-UsageForCreateFixture {
"invalid usage, please specify (path, name)" | Write-Host
"eg: .\Create-Fixture -Path Foo -Name Bar" | Write-Host
"creates .\Foo\Bar.ps1 and .\Foo.Bar.Tests.ps1" | Write-Host
}
function Create-File($file_path, $contents = "") {
if (-not (Test-Path $file_path)) {
$contents | Out-File $file_path -Encoding ASCII
"Creating" | Write-Host -Fore Green -NoNewLine
} else {
"Skipping" | Write-Host -Fore Yellow -NoNewLine
}
" => $file_path" | Write-Host
}
function New-Fixture($path, $name) {
if ([String]::IsNullOrEmpty($path) -or [String]::IsNullOrEmpty($name)) {
Write-UsageForCreateFixture
return
}
# TODO clean up $path cleanup
$path = $path.TrimStart(".")
$path = $path.TrimStart("\")
if (-not (Test-Path $path)) {
& md $path | Out-Null
}
$test_code = "function $name {
}"
$fixture_code = "`$here = Split-Path -Parent `$MyInvocation.MyCommand.Path
`$sut = (Split-Path -Leaf `$MyInvocation.MyCommand.Path).Replace(`".Tests.`", `".`")
. `"`$here\`$sut`"
Describe `"$name`" {
It `"does something useful`" {
`$true.should.be(`$false)
}
}"
Create-File "$path\$name.ps1" $test_code
Create-File "$path\$name.Tests.ps1" $fixture_code
}
Export-ModuleMember Describe, Context, It, In
Export-ModuleMember Invoke-Pester, New-Fixture