Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/private/package/InitializeNovaPackageOutputDirectory.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ function Initialize-NovaPackageOutputDirectory {
[CmdletBinding()]
param(
[Parameter(Mandatory)][pscustomobject]$ProjectInfo,
[Alias('PackageMetadata')][Parameter(Mandatory)][object[]]$PackageMetadataList
[AllowEmptyCollection()][Alias('PackageMetadata')][Parameter(Mandatory)][object[]]$PackageMetadataList
)

$packageMetadata = @($PackageMetadataList)[0]
$packageMetadata = @($PackageMetadataList) | Select-Object -First 1
if ($null -eq $packageMetadata) {
Stop-NovaOperation -Message 'Package metadata list cannot be empty.' -ErrorId 'Nova.Validation.PackageMetadataListEmpty' -Category InvalidArgument -TargetObject 'PackageMetadataList'
}

if ($PackageMetadata.CleanOutputDirectory) {
Clear-NovaPackageOutputDirectory -ProjectInfo $ProjectInfo -OutputDirectory $PackageMetadata.OutputDirectory
if ($packageMetadata.CleanOutputDirectory) {
Clear-NovaPackageOutputDirectory -ProjectInfo $ProjectInfo -OutputDirectory $packageMetadata.OutputDirectory
}

if (-not (Test-Path -LiteralPath $PackageMetadata.OutputDirectory)) {
$null = New-Item -ItemType Directory -Path $PackageMetadata.OutputDirectory -Force
if (-not (Test-Path -LiteralPath $packageMetadata.OutputDirectory)) {
$null = New-Item -ItemType Directory -Path $packageMetadata.OutputDirectory -Force
}
}
5 changes: 3 additions & 2 deletions src/private/update/InvokeNovaPowerShellScriptWithTimeout.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ function Invoke-NovaPowerShellScriptWithTimeout {
param(
[Parameter(Mandatory)][string]$Script,
[object[]]$ArgumentList = @(),
[int]$TimeoutMilliseconds = 3000
[int]$TimeoutMilliseconds = 3000,
[scriptblock]$PowerShellFactory = {[powershell]::Create()}
)

$powershell = [powershell]::Create()
$powershell = & $PowerShellFactory
try {
$null = $powershell.AddScript($Script)
foreach ($argument in $ArgumentList) {
Expand Down
221 changes: 221 additions & 0 deletions tests/CiCoverage.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,47 @@ BeforeAll {
. (Join-Path $PSScriptRoot '..' 'scripts' 'build' 'ci' 'CodeSceneCoverageMap.ps1')
. (Join-Path $PSScriptRoot '..' 'scripts' 'build' 'ci' 'CodeSceneCoverageXml.ps1')
. (Join-Path $PSScriptRoot '..' 'scripts' 'build' 'ci' 'CoverageLowReport.ps1')

foreach ($functionName in @(
'ConvertTo-CoberturaRelativePath'
'Get-CodeSceneCoverageErrorRecord'
'Get-SourceSectionListFromBuiltModule'
'Find-SourceSectionForLine'
'Get-EmptyCoberturaLineBucket'
'Add-CoberturaLineHit'
'Get-CoberturaLineStat'
'Get-CoberturaPackageName'
'Get-CoberturaSourceLineRange'
'Test-CoberturaLineOutsideSourceRange'
'Add-CoberturaMappedLineHit'
'Add-CoberturaLineNodeHit'
'Get-CoberturaLineBucketMap'
'Add-CoberturaAttribute'
'Get-CoberturaClassElement'
'Get-CoberturaPackageElement'
'Get-CoberturaCoverageAttributeMap'
'Get-CoberturaCoverageDocument'
'Convert-CoberturaCoverageToSourcePath'
'ConvertTo-CoverageLineRate'
'Get-CoverageLowReportEntryList'
'Format-CoverageLowReportLine'
'Write-CoverageLowReport'
)) {
$scriptBlock = (Get-Command -Name $functionName -CommandType Function -ErrorAction Stop).ScriptBlock
Set-Item -Path "function:global:$functionName" -Value $scriptBlock
}

$here = Split-Path -Parent $PSCommandPath
$script:repoRoot = Split-Path -Parent $here
$script:moduleName = (Get-Content -LiteralPath (Join-Path $script:repoRoot 'project.json') -Raw | ConvertFrom-Json).ProjectName
$script:distModuleDir = Join-Path $script:repoRoot "dist/$script:moduleName"

if (-not (Test-Path -LiteralPath $script:distModuleDir)) {
throw "Expected built $script:moduleName module at: $script:distModuleDir. Run Invoke-NovaBuild in the repo root first."
}

Remove-Module $script:moduleName -ErrorAction SilentlyContinue
Import-Module $script:distModuleDir -Force
}

Describe 'CodeScene Cobertura remapping helpers' {
Expand Down Expand Up @@ -204,3 +245,183 @@ Describe 'CodeScene Cobertura remapping helpers' {
)
}
}

Describe 'Coverage gaps for quality helpers' {
It 'Write-NovaPesterTestResultReport writes a success NUnit-style report with default suite name' {
$outputPath = Join-Path $TestDrive 'success-report.xml'

InModuleScope $script:moduleName -Parameters @{OutputPath = $outputPath} {
param($OutputPath)

$testResult = [pscustomobject]@{
Tests = @(
[pscustomobject]@{Result = 'Passed'}
[pscustomobject]@{Result = 'Passed'}
[pscustomobject]@{Result = 'Skipped'}
[pscustomobject]@{Result = 'Inconclusive'}
)
}

Write-NovaPesterTestResultReport -TestResult $testResult -OutputPath $OutputPath

[xml]$report = Get-Content -LiteralPath $OutputPath -Raw
$testResultsNode = $report.SelectSingleNode('/test-results')
$testSuiteNode = $report.SelectSingleNode('/test-results/test-suite')

$testResultsNode.name | Should -Be 'NovaModuleTools'
$testResultsNode.total | Should -Be '4'
$testResultsNode.failures | Should -Be '0'
$testResultsNode.inconclusive | Should -Be '1'
$testResultsNode.skipped | Should -Be '1'
$testSuiteNode.result | Should -Be 'Success'
$testSuiteNode.success | Should -Be 'True'
$testSuiteNode.passed | Should -Be '2'
}
}

It 'Write-NovaPesterTestResultReport writes a failure NUnit-style report when failed tests are present' {
$outputPath = Join-Path $TestDrive 'failure-report.xml'

InModuleScope $script:moduleName -Parameters @{OutputPath = $outputPath} {
param($OutputPath)

$testResult = [pscustomobject]@{
Tests = @(
[pscustomobject]@{Result = 'Passed'}
[pscustomobject]@{Result = 'Failed'}
)
}

Write-NovaPesterTestResultReport -TestResult $testResult -OutputPath $OutputPath -TestSuiteName 'FocusedSuite'

[xml]$report = Get-Content -LiteralPath $OutputPath -Raw
$testResultsNode = $report.SelectSingleNode('/test-results')
$testSuiteNode = $report.SelectSingleNode('/test-results/test-suite')

$testResultsNode.name | Should -Be 'FocusedSuite'
$testResultsNode.failures | Should -Be '1'
$testSuiteNode.result | Should -Be 'Failure'
$testSuiteNode.success | Should -Be 'False'
$testSuiteNode.failed | Should -Be '1'
}
}

It 'Write-NovaPesterTestResultArtifact returns without writing when the result has no Tests property' {
InModuleScope $script:moduleName {
Mock Get-Command {throw 'Get-Command should not be called when Tests is missing.'}

{Write-NovaPesterTestResultArtifact -TestResult ([pscustomobject]@{Summary = 'no tests'}) -OutputPath '/tmp/unused.xml'} | Should -Not -Throw

Assert-MockCalled Get-Command -Times 0
}
}

It 'Write-NovaPesterTestResultArtifact uses the provided report writer when one is supplied' {
InModuleScope $script:moduleName {
$calls = [System.Collections.Generic.List[object]]::new()
$reportWriter = {
param($TestResult, $OutputPath)

$calls.Add([pscustomobject]@{
TestResult = $TestResult
OutputPath = $OutputPath
}) | Out-Null
}.GetNewClosure()
$testResult = [pscustomobject]@{Tests = @([pscustomobject]@{Result = 'Passed'})}

Write-NovaPesterTestResultArtifact -TestResult $testResult -OutputPath '/tmp/provided.xml' -ReportWriter $reportWriter

$calls.Count | Should -Be 1
$calls[0].OutputPath | Should -Be '/tmp/provided.xml'
$calls[0].TestResult.Tests[0].Result | Should -Be 'Passed'
}
}

It 'Write-NovaPesterTestResultArtifact resolves the default report writer when none is supplied' {
InModuleScope $script:moduleName {
Mock Get-Command {
[pscustomobject]@{
ScriptBlock = {
param($TestResult, $OutputPath)

return [pscustomobject]@{
TestCount = @($TestResult.Tests).Count
OutputPath = $OutputPath
}
}
}
} -ParameterFilter {
$Name -eq 'Write-NovaPesterTestResultReport' -and $CommandType -eq 'Function'
}

$result = Write-NovaPesterTestResultArtifact -TestResult ([pscustomobject]@{Tests = @([pscustomobject]@{Result = 'Passed'})}) -OutputPath '/tmp/default.xml'

$result.TestCount | Should -Be 1
$result.OutputPath | Should -Be '/tmp/default.xml'
Assert-MockCalled Get-Command -Times 1 -ParameterFilter {
$Name -eq 'Write-NovaPesterTestResultReport' -and $CommandType -eq 'Function'
}
}
}

It 'Initialize-NovaPesterExecutionConfiguration applies returned overrides when <Name>' -ForEach @(
@{
Name = 'both verbosity and render mode are provided'
Override = [pscustomobject]@{Verbosity = 'Detailed'; RenderMode = 'Plaintext'}
BoundParameters = @{OutputVerbosity = 'Detailed'; OutputRenderMode = 'Plaintext'}
ExpectedVerbosity = 'Detailed'
ExpectedRenderMode = 'Plaintext'
}
@{
Name = 'only render mode is provided'
Override = [pscustomobject]@{Verbosity = $null; RenderMode = 'Ansi'}
BoundParameters = @{OutputRenderMode = 'Ansi'}
ExpectedVerbosity = 'Normal'
ExpectedRenderMode = 'Ansi'
}
) {
$pesterConfig = [pscustomobject]@{
Output = [pscustomobject]@{
Verbosity = 'Normal'
RenderMode = 'Auto'
}
TestResult = [pscustomobject]@{Enabled = $true}
}

InModuleScope $script:moduleName -Parameters @{TestCase = $_; PesterConfig = $pesterConfig} {
param($TestCase, $PesterConfig)

Mock Get-NovaPesterOutputOptionOverride {$TestCase.Override}

Initialize-NovaPesterExecutionConfiguration -PesterConfig $PesterConfig -BoundParameters $TestCase.BoundParameters

$PesterConfig.Output.Verbosity | Should -Be $TestCase.ExpectedVerbosity
$PesterConfig.Output.RenderMode | Should -Be $TestCase.ExpectedRenderMode
$PesterConfig.TestResult.Enabled | Should -BeFalse
Assert-MockCalled Get-NovaPesterOutputOptionOverride -Times 1
}
}

It 'Initialize-NovaPesterExecutionConfiguration preserves unsupported settings when no overrides are returned' {
$pesterConfig = [pscustomobject]@{
Output = [pscustomobject]@{
Verbosity = 'Normal'
RenderMode = 'Auto'
}
TestResult = [pscustomobject]@{Summary = 'No Enabled property'}
}

InModuleScope $script:moduleName -Parameters @{PesterConfig = $pesterConfig} {
param($PesterConfig)

Mock Get-NovaPesterOutputOptionOverride {$null}

Initialize-NovaPesterExecutionConfiguration -PesterConfig $PesterConfig -BoundParameters @{}

$PesterConfig.Output.Verbosity | Should -Be 'Normal'
$PesterConfig.Output.RenderMode | Should -Be 'Auto'
$PesterConfig.TestResult.PSObject.Properties.Name | Should -Not -Contain 'Enabled'
Assert-MockCalled Get-NovaPesterOutputOptionOverride -Times 1
}
}
}
Loading
Loading