Skip to content

Commit

Permalink
Fix throwing on missing folders (#2012)
Browse files Browse the repository at this point in the history
* Fix throwing on missing folders

And improve error message for no files.

Fix #1875

* Fix test

* fix

* remove dt
  • Loading branch information
nohwnd committed Jun 23, 2021
1 parent 8fb1317 commit 5bc79e2
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/Main.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,7 @@ function Invoke-Pester {
}

if ((none $containers)) {
throw "No test files were found and no scriptblocks were provided."
throw "No test files were found and no scriptblocks were provided. Please ensure that you provided at least one path to a *$($PesterPreference.Run.TestExtension.Value) file, or a directory that contains such file.$(if ($null -ne $PesterPreference.Run.ExcludePath.Value -and 0 -lt @($PesterPreference.Run.ExcludePath.Value).Length) {" And that there is at least one file not excluded by ExcludeFile filter '$($PesterPreference.Run.ExcludePath.Value -join "', '")'."}) Or that you provided a ScriptBlock test container."
return
}

Expand Down Expand Up @@ -1129,7 +1129,13 @@ function Invoke-Pester {
}
}

$stringWriter.ToString() | & $SafeCommands['Out-File'] $PesterPreference.CodeCoverage.OutputPath.Value -Encoding $PesterPreference.CodeCoverage.OutputEncoding.Value
$resolvedPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($PesterPreference.CodeCoverage.OutputPath.Value)
if (-not (& $SafeCommands['Test-Path'] $resolvedPath)) {
$dir = & $SafeCommands['Split-Path'] $resolvedPath
$null = & $SafeCommands['New-Item'] $dir -Force -ItemType Container
}

$stringWriter.ToString() | & $SafeCommands['Out-File'] $resolvedPath -Encoding $PesterPreference.CodeCoverage.OutputEncoding.Value -Force
if ($PesterPreference.Output.Verbosity.Value -in "Detailed", "Diagnostic") {
& $SafeCommands["Write-Host"] -ForegroundColor Magenta "Code Coverage result processed in $($sw.ElapsedMilliseconds) ms."
}
Expand Down
4 changes: 4 additions & 0 deletions src/functions/TestResults.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ function GetFullPath ([string]$Path) {
$File = & $SafeCommands['Split-Path'] -Path $Path -Leaf

if ( -not ([String]::IsNullOrEmpty($Folder))) {
if (-not (& $SafeCommands['Test-Path'] $Folder)) {
$null = & $SafeCommands['New-Item'] $Folder -ItemType Container -Force
}

$FolderResolved = & $SafeCommands['Resolve-Path'] -Path $Folder
}
else {
Expand Down
40 changes: 40 additions & 0 deletions tst/Pester.RSpec.Coverage.ts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ i -PassThru:$PassThru {
$bb = Invoke-Pester -Configuration $c


$bb | Verify-NotNull
$pp | Verify-NotNull

Write-Host "is different?: $($bb.CodeCoverage.CommandsMissed.Count -ne $pp.CodeCoverage.CommandsMissed.Count)"
Write-Host "is less?: $($bb.CodeCoverage.CommandsMissed.Count -lt $pp.CodeCoverage.CommandsMissed.Count)"

Expand Down Expand Up @@ -249,4 +252,41 @@ i -PassThru:$PassThru {
Get-HitLocation $b | Verify-Location $b
}
}

b "Coverage result creates missing folder" {
t "Coverage result will create the destination folder if it is missing" {
# https://github.com/pester/Pester/issues/1875 point 2
$sb = {
Describe 'VSCode Output Test' {
It 'Single error' {
. "$PSScriptRoot/CoverageTestFile.ps1"
}
}
}

$c = New-PesterConfiguration

$c.Run.Container = New-PesterContainer -ScriptBlock $sb
$c.Run.PassThru = $true

$c.Output.Verbosity = "Detailed"

$c.CodeCoverage.Enabled = $true
$c.CodeCoverage.Path = "$PSScriptRoot/CoverageTestFile.ps1"
$c.CodeCoverage.UseBreakpoints = $true
$dir = [IO.Path]::GetTempPath() + "/nonExistingDirectory" + [Guid]::NewGuid()
$c.CodeCoverage.OutputPath = "$dir/coverage.xml"

try {
$r = Invoke-Pester -Configuration $c
}
finally {
if (Test-Path $dir) {
Remove-Item $dir -Force -Recurse
}
}

$r.Result | Verify-Equal 'Passed'
}
}
}
35 changes: 35 additions & 0 deletions tst/Pester.RSpec.Junit.TestResults.ts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -420,5 +420,40 @@ i -PassThru:$PassThru {
}
}
}

t "Write JUnit report using Invoke-Pester -OutputFormat JUnitXML into a folder that does not exist" {
$sb = {
Describe "Mocked Describe" {
It "Successful testcase" {
$true | Should -Be $true
}
}
}

try {
$script = Join-Path ([IO.Path]::GetTempPath()) "test$([Guid]::NewGuid()).Tests.ps1"
$sb | Set-Content -Path $script -Force

$dir = Join-Path ([IO.Path]::GetTempPath()) "dir$([Guid]::NewGuid())"

$xml = Join-Path $dir "TestResults.xml"
$r = Invoke-Pester -Show None -Path $script -OutputFormat JUnitXML -OutputFile $xml -PassThru

$xmlResult = [xml] (Get-Content -Path $xml)
$xmlTestCase = $xmlResult.'testsuites'.'testsuite'.'testcase'
$xmlTestCase.name | Verify-Equal "Mocked Describe.Successful testcase"
$xmlTestCase.status | Verify-Equal "Passed"
$xmlTestCase.time | Verify-XmlTime -Expected $r.Containers[0].Blocks[0].Tests[0].Duration
}
finally {
if (Test-Path $script) {
Remove-Item $script -Force -ErrorAction Ignore
}

if (Test-Path $dir) {
Remove-Item $dir -Force -ErrorAction Ignore -Recurse
}
}
}
}
}
35 changes: 35 additions & 0 deletions tst/Pester.RSpec.Nunit.TestResults.ts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -541,5 +541,40 @@ i -PassThru:$PassThru {
}
}
}

t "Write NUnit report using Invoke-Pester -OutputFormat NUnit2.5 into a folder that does not exist" {
$sb = {
Describe "Mocked Describe" {
It "Successful testcase" {
$true | Should -Be $true
}
}
}

try {
$script = Join-Path ([IO.Path]::GetTempPath()) "test$([Guid]::NewGuid()).Tests.ps1"
$sb | Set-Content -Path $script -Force

$dir = Join-Path ([IO.Path]::GetTempPath()) "dir$([Guid]::NewGuid())"

$xml = Join-Path $dir "TestResults.xml"
$r = Invoke-Pester -Show None -Path $script -OutputFormat NUnit2.5 -OutputFile $xml -PassThru

$xmlResult = [xml] (Get-Content $xml -Raw)
$xmlTestCase = $xmlResult.'test-results'.'test-suite'.'results'.'test-suite'.'results'.'test-suite'.'results'.'test-case'
$xmlTestCase.name | Verify-Equal "Mocked Describe.Successful testcase"
$xmlTestCase.result | Verify-Equal "Success"
$xmlTestCase.time | Verify-XmlTime $r.Containers[0].Blocks[0].Tests[0].Duration
}
finally {
if (Test-Path $script) {
Remove-Item $script -Force -ErrorAction Ignore
}

if (Test-Path $dir) {
Remove-Item $dir -Force -ErrorAction Ignore -Recurse
}
}
}
}
}
2 changes: 1 addition & 1 deletion tst/p.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ function t {
# otherwise show the assertion message and stacktrace to keep the noise
# on test failure low
if ([Exception] -ne $_.Exception.GetType()) {
Write-Host "ERROR: - $Name -> $($_| Out-String) " -ForegroundColor Black -BackgroundColor Red
Write-Host "[n] ERROR: - $Name -> $($_| Out-String) " -ForegroundColor Black -BackgroundColor Red
$(Get-FullStackTrace $_) -split [Environment]::NewLine | foreach {
Write-Host " " -NoNewline
Write-Host " $_ " -NoNewline -ForegroundColor Black -BackgroundColor Red
Expand Down

0 comments on commit 5bc79e2

Please sign in to comment.