diff --git a/OctopusStepTemplateCi/Cmdlets/Interface/Invoke-OctopusScriptTestSuite.Tests.ps1 b/OctopusStepTemplateCi/Cmdlets/Interface/Invoke-OctopusScriptTestSuite.Tests.ps1 index 2408b52..3bddb44 100644 --- a/OctopusStepTemplateCi/Cmdlets/Interface/Invoke-OctopusScriptTestSuite.Tests.ps1 +++ b/OctopusStepTemplateCi/Cmdlets/Interface/Invoke-OctopusScriptTestSuite.Tests.ps1 @@ -27,7 +27,7 @@ Set-StrictMode -Version Latest $here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.' . "$here\$sut" -. "$here\..\Internal\Tests\Invoke-PesterForTeamCity.ps1" +. "$here\..\Internal\Tests\Invoke-PesterForCi.ps1" . "$here\..\Internal\Tests\Get-ScriptValidationTestsPath.ps1" Describe "Invoke-OctopusScriptTestSuite" { @@ -38,11 +38,11 @@ Describe "Invoke-OctopusScriptTestSuite" { New-Item "TestDrive:\Generic" -ItemType Directory | Out-Null New-Item "TestDrive:\StepTemplates" -ItemType Directory | Out-Null New-Item "TestDrive:\ScriptModules" -ItemType Directory | Out-Null - Mock Invoke-PesterForTeamCity {} + Mock Invoke-PesterForCi {} Mock Get-ScriptValidationTestsPath {"TestDrive:\"} It "Should run the script's own tests" { - Mock Invoke-PesterForTeamCity {} -ParameterFilter { $Script -like "*\test.steptemplate.Tests.ps1" } -Verifiable + Mock Invoke-PesterForCi {} -ParameterFilter { $Script -like "*\test.steptemplate.Tests.ps1" } -Verifiable Invoke-OctopusScriptTestSuite -Path "TestDrive:\" -ResultFilesPath "TestDrive:\" @@ -50,7 +50,7 @@ Describe "Invoke-OctopusScriptTestSuite" { } It "Should run the generic tests" { - Mock Invoke-PesterForTeamCity {} -ParameterFilter { $TestResultsFile -like "*.generic.TestResults.xml" } -Verifiable + Mock Invoke-PesterForCi {} -ParameterFilter { $TestResultsFile -like "*.generic.TestResults.xml" } -Verifiable Mock Get-ChildItem { @(@{ FullName = "TestDrive:\Generic\1.ScriptValidationTest.ps1" }, @{FullName = "TestDrive:\Generic\2.ScriptValidationTest.ps1" }) } -ParameterFilter { $Path -like "*\Generic\*.ScriptValidationTest.ps1" } -Verifiable Invoke-OctopusScriptTestSuite -Path "TestDrive:\" -ResultFilesPath "TestDrive:\" @@ -59,7 +59,7 @@ Describe "Invoke-OctopusScriptTestSuite" { } It "Should run the step template tests" { - Mock Invoke-PesterForTeamCity {} -ParameterFilter { $TestResultsFile -like "*.step-template.TestResults.xml" } -Verifiable + Mock Invoke-PesterForCi {} -ParameterFilter { $TestResultsFile -like "*.step-template.TestResults.xml" } -Verifiable Mock Get-ChildItem { @(@{ FullName = "TestDrive:\StepTemplates\1.ScriptValidationTest.ps1" }, @{FullName = "TestDrive:\StepTemplates\2.ScriptValidationTest.ps1" }) } -ParameterFilter { $Path -like "*\StepTemplates\*.ScriptValidationTest.ps1" } -Verifiable Invoke-OctopusScriptTestSuite -Path "TestDrive:\" -ResultFilesPath "TestDrive:\" @@ -68,7 +68,7 @@ Describe "Invoke-OctopusScriptTestSuite" { } It "Should run the script module tests" { - Mock Invoke-PesterForTeamCity {} -ParameterFilter { $TestResultsFile -like "*.script-module.TestResults.xml" } -Verifiable + Mock Invoke-PesterForCi {} -ParameterFilter { $TestResultsFile -like "*.script-module.TestResults.xml" } -Verifiable Mock Get-ChildItem { @(@{ FullName = "TestDrive:\ScriptModules\1.ScriptValidationTest.ps1" }, @{FullName = "TestDrive:\ScriptModules\2.ScriptValidationTest.ps1" }) } -ParameterFilter { $Path -like "*\ScriptModules\*.ScriptValidationTest.ps1" } -Verifiable Invoke-OctopusScriptTestSuite -Path "TestDrive:\" -ResultFilesPath "TestDrive:\" @@ -77,7 +77,7 @@ Describe "Invoke-OctopusScriptTestSuite" { } It "Should set success to false if there are any failed tests" { - Mock Invoke-PesterForTeamCity { New-Object -TypeName PSObject -Property @{ Passed = 0; Failed = 1 } } -ParameterFilter { $TestResultsFile -like "*.script-module.TestResults.xml" } + Mock Invoke-PesterForCi { New-Object -TypeName PSObject -Property @{ Passed = 0; Failed = 1 } } -ParameterFilter { $TestResultsFile -like "*.script-module.TestResults.xml" } Invoke-OctopusScriptTestSuite -Path "TestDrive:\" -ResultFilesPath "TestDrive:\" | % Success | Should Be $false } diff --git a/OctopusStepTemplateCi/Cmdlets/Interface/Invoke-OctopusScriptTestSuite.ps1 b/OctopusStepTemplateCi/Cmdlets/Interface/Invoke-OctopusScriptTestSuite.ps1 index 21dcccb..f129c1a 100644 --- a/OctopusStepTemplateCi/Cmdlets/Interface/Invoke-OctopusScriptTestSuite.ps1 +++ b/OctopusStepTemplateCi/Cmdlets/Interface/Invoke-OctopusScriptTestSuite.ps1 @@ -58,7 +58,8 @@ function Invoke-OctopusScriptTestSuite { [Parameter(Mandatory=$false)][ValidateNotNullOrEmpty()][System.String]$StepTemplateFilter = "*.steptemplate.ps1", [Parameter(Mandatory=$false)][ValidateNotNullOrEmpty()][System.String]$ScriptModuleFilter = "*.scriptmodule.ps1", [Parameter(Mandatory=$false)][ValidateNotNull()][System.Collections.Hashtable]$TestSettings = @{}, - [Parameter(Mandatory=$false)][System.Management.Automation.SwitchParameter]$SuppressPesterOutput + [Parameter(Mandatory=$false)][System.Management.Automation.SwitchParameter]$SuppressPesterOutput, + $CiTool = 'Other' ) $stepTemplates = @(Get-ChildItem -Path $Path -File -Recurse -Filter $StepTemplateFilter) @@ -67,27 +68,31 @@ function Invoke-OctopusScriptTestSuite { $results = ($stepTemplates + $scriptModules) | ? Name -NotLike "*.Tests.ps1" | % { $sut = $_.FullName $testResultsFile = Join-Path $ResultFilesPath $_.Name.Replace(".ps1", ".TestResults.xml") - Invoke-PesterForTeamCity -TestName $_.Name ` + Invoke-PesterForCi -TestName $_.Name ` -Script $_.FullName.Replace(".ps1", ".Tests.ps1") ` -TestResultsFile $testResultsFile ` - -SuppressPesterOutput:$SuppressPesterOutput + -SuppressPesterOutput:$SuppressPesterOutput ` + -CiTool $CiTool - Invoke-PesterForTeamCity -TestName $_.Name ` + Invoke-PesterForCi -TestName $_.Name ` -Script @(Get-ChildItem -Path (Join-Path (Get-ScriptValidationTestsPath) "\Generic\*.ScriptValidationTest.ps1") -File | % { @{ Path = $_.FullName; Parameters = @{ sut = $sut; TestResultsFile = $testResultsFile; Settings = $TestSettings } } }) ` -TestResultsFile (Join-Path $ResultFilesPath $_.Name.Replace(".ps1", ".generic.TestResults.xml")) ` - -SuppressPesterOutput:$SuppressPesterOutput + -SuppressPesterOutput:$SuppressPesterOutput ` + -CiTool $CiTool if ($_.Name -like $ScriptModuleFilter) { - Invoke-PesterForTeamCity -TestName $_.Name ` + Invoke-PesterForCi -TestName $_.Name ` -Script @(Get-ChildItem -Path (Join-Path (Get-ScriptValidationTestsPath) "\ScriptModules\*.ScriptValidationTest.ps1") -File | % { @{ Path = $_.FullName; Parameters = @{ sut = $sut; TestResultsFile = $testResultsFile; Settings = $TestSettings } } }) ` -TestResultsFile (Join-Path $ResultFilesPath $_.Name.Replace(".ps1", ".script-module.TestResults.xml")) ` - -SuppressPesterOutput:$SuppressPesterOutput + -SuppressPesterOutput:$SuppressPesterOutput ` + -CiTool $CiTool } elseif ($_.Name -like $StepTemplateFilter) { - Invoke-PesterForTeamCity -TestName $_.Name ` + Invoke-PesterForCi -TestName $_.Name ` -Script @(Get-ChildItem -Path (Join-Path (Get-ScriptValidationTestsPath) "\StepTemplates\*.ScriptValidationTest.ps1") -File | % { @{ Path = $_.FullName; Parameters = @{ sut = $sut; TestResultsFile = $testResultsFile; Settings = $TestSettings } } }) ` -TestResultsFile (Join-Path $ResultFilesPath $_.Name.Replace(".ps1", ".step-template.TestResults.xml")) ` - -SuppressPesterOutput:$SuppressPesterOutput + -SuppressPesterOutput:$SuppressPesterOutput ` + -CiTool $CiTool } } | Measure-Object -Sum -Property @("Passed", "Failed") diff --git a/OctopusStepTemplateCi/Cmdlets/Interface/Invoke-TeamCityCiUpload.ps1 b/OctopusStepTemplateCi/Cmdlets/Interface/Invoke-TeamCityCiUpload.ps1 index 2ad1cab..54d6e40 100644 --- a/OctopusStepTemplateCi/Cmdlets/Interface/Invoke-TeamCityCiUpload.ps1 +++ b/OctopusStepTemplateCi/Cmdlets/Interface/Invoke-TeamCityCiUpload.ps1 @@ -100,7 +100,8 @@ function Invoke-TeamCityCiUpload { -StepTemplateFilter $StepTemplateFilter ` -ScriptModuleFilter $ScriptModuleFilter ` -TestSettings $TestSettings ` - -SuppressPesterOutput:$SuppressPesterOutput + -SuppressPesterOutput:$SuppressPesterOutput ` + -CiTool "TeamCity" $passedTests += $testResults.Passed $failedTests += $testResults.Failed diff --git a/OctopusStepTemplateCi/Cmdlets/Internal/Tests/Invoke-PesterForTeamCity.Tests.ps1 b/OctopusStepTemplateCi/Cmdlets/Internal/Tests/Invoke-PesterForCi.Tests.ps1 similarity index 65% rename from OctopusStepTemplateCi/Cmdlets/Internal/Tests/Invoke-PesterForTeamCity.Tests.ps1 rename to OctopusStepTemplateCi/Cmdlets/Internal/Tests/Invoke-PesterForCi.Tests.ps1 index 09bd147..61db6f2 100644 --- a/OctopusStepTemplateCi/Cmdlets/Internal/Tests/Invoke-PesterForTeamCity.Tests.ps1 +++ b/OctopusStepTemplateCi/Cmdlets/Internal/Tests/Invoke-PesterForCi.Tests.ps1 @@ -16,10 +16,10 @@ limitations under the License. <# .NAME - Invoke-PesterForTeamCity.Tests + Invoke-PesterForCi.Tests .SYNOPSIS - Pester tests for Invoke-PesterForTeamCity. + Pester tests for Invoke-PesterForCi. #> Set-StrictMode -Version Latest @@ -29,13 +29,13 @@ $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.' . "$here\Update-XPathValue.ps1" . "$here\..\TeamCity\Write-TeamCityMessage.ps1" -Describe "Invoke-PesterForTeamCity" { +Describe "Invoke-PesterForCi" { It "Invokes pester with the provided arguments" { Mock Update-XPathValue {} Mock Write-TeamCityMessage {} Mock Invoke-Pester { @{PassedCount = 1; FailedCount = 1} } -ParameterFilter { $Script -eq "TestDrive:\test.Tests.ps1" -and $OutputFile -eq "TestDrive:\results.xml" } -Verifiable - Invoke-PesterForTeamCity -TestName "test" -Script "TestDrive:\test.Tests.ps1" -TestResultsFile "TestDrive:\results.xml" + Invoke-PesterForCi -TestName "test" -Script "TestDrive:\test.Tests.ps1" -TestResultsFile "TestDrive:\results.xml" Assert-VerifiableMocks } @@ -45,27 +45,37 @@ Describe "Invoke-PesterForTeamCity" { Mock Write-TeamCityMessage {} Mock Invoke-Pester { @{PassedCount = 1; FailedCount = 1} } - Invoke-PesterForTeamCity -TestName "test" -Script "TestDrive:\test.Tests.ps1" -TestResultsFile "TestDrive:\results.xml" + Invoke-PesterForCi -TestName "test" -Script "TestDrive:\test.Tests.ps1" -TestResultsFile "TestDrive:\results.xml" Assert-VerifiableMocks } - It "Should write out a teamcity message to import the test results file" { + It "Should write out a teamcity message to import the test results file when parameter is supplied" { Mock Update-XPathValue {} Mock Write-TeamCityMessage {} -ParameterFilter { $Message -eq "##teamcity[importData type='nunit' path='TestDrive:\results.xml' verbose='true']" } -Verifiable Mock Invoke-Pester { @{PassedCount = 1; FailedCount = 1} } - Invoke-PesterForTeamCity -TestName "test" -Script "TestDrive:\test.Tests.ps1" -TestResultsFile "TestDrive:\results.xml" + Invoke-PesterForCi -TestName "test" -Script "TestDrive:\test.Tests.ps1" -TestResultsFile "TestDrive:\results.xml" -CiTool "TeamCity" Assert-VerifiableMocks } + + It "Should NOT write out a teamcity message to import the test results file when parameter has not supplied" { + Mock Update-XPathValue {} + Mock Write-TeamCityMessage {Param ($Message)} + Mock Invoke-Pester { @{PassedCount = 1; FailedCount = 1} } + + Invoke-PesterForCi -TestName "test" -Script "TestDrive:\test.Tests.ps1" -TestResultsFile "TestDrive:\results.xml" + + Assert-MockCalled Write-TeamCityMessage -Scope It -Exactly 0 -ParameterFilter { $Message -eq "##teamcity[importData type='nunit' path='TestDrive:\results.xml' verbose='true']" } + } It "Should return a hashtable containing the passed and failed count" { Mock Update-XPathValue {} Mock Write-TeamCityMessage {} Mock Invoke-Pester { @{PassedCount = 1; FailedCount = 1} } - $results = Invoke-PesterForTeamCity -TestName "test" -Script "TestDrive:\test.Tests.ps1" -TestResultsFile "TestDrive:\results.xml" + $results = Invoke-PesterForCi -TestName "test" -Script "TestDrive:\test.Tests.ps1" -TestResultsFile "TestDrive:\results.xml" $results.Passed | Should Be 1 $results.Failed | Should Be 1 diff --git a/OctopusStepTemplateCi/Cmdlets/Internal/Tests/Invoke-PesterForTeamCity.ps1 b/OctopusStepTemplateCi/Cmdlets/Internal/Tests/Invoke-PesterForCi.ps1 similarity index 82% rename from OctopusStepTemplateCi/Cmdlets/Internal/Tests/Invoke-PesterForTeamCity.ps1 rename to OctopusStepTemplateCi/Cmdlets/Internal/Tests/Invoke-PesterForCi.ps1 index 16eb256..a9889a8 100644 --- a/OctopusStepTemplateCi/Cmdlets/Internal/Tests/Invoke-PesterForTeamCity.ps1 +++ b/OctopusStepTemplateCi/Cmdlets/Internal/Tests/Invoke-PesterForCi.ps1 @@ -16,17 +16,18 @@ limitations under the License. <# .NAME - Invoke-PesterForTeamCity + Invoke-PesterForCi .SYNOPSIS Invokes Pester's tests, handles the results file and teamcity integration to link the results file into team city and returns the passed & failed tests count #> -function Invoke-PesterForTeamCity { +function Invoke-PesterForCi { param ( $TestName, $Script, $TestResultsFile, - [switch]$SuppressPesterOutput + [switch]$SuppressPesterOutput, + [ValidateSet("TeamCity","Jenkins","Bamboo","TFS","Other")]$CiTool = "Other" ) $testResult = Invoke-Pester -Script $Script -PassThru -OutputFile $TestResultsFile -OutputFormat NUnitXml -Quiet:$SuppressPesterOutput @@ -35,7 +36,10 @@ function Invoke-PesterForTeamCity { Update-XPathValue -Path $TestResultsFile -XPath '//test-results/test-suite/@name' -Value $TestName #tell teamcity to import the test results. Cant use the xml report processor feature of TeamCity, due to aysnc issues around updating the test suite name - Write-TeamCityMessage "##teamcity[importData type='nunit' path='$TestResultsFile' verbose='true']" + if ($CiTool -eq "TeamCity"){ + Write-TeamCityMessage "##teamcity[importData type='nunit' path='$TestResultsFile' verbose='true']" + } + New-Object -TypeName PSObject -Property @{ Passed = $testResult.PassedCount diff --git a/OctopusStepTemplateCi/OctopusStepTemplateCi.psm1 b/OctopusStepTemplateCi/OctopusStepTemplateCi.psm1 index 049ce85..798b505 100644 --- a/OctopusStepTemplateCi/OctopusStepTemplateCi.psm1 +++ b/OctopusStepTemplateCi/OctopusStepTemplateCi.psm1 @@ -52,5 +52,5 @@ Set-StrictMode -Version Latest . "$(Join-Path $PSScriptRoot '\Cmdlets\Internal\TeamCity\Reset-BuildOutputDirectory.ps1')" . "$(Join-Path $PSScriptRoot '\Cmdlets\Internal\TeamCity\Write-TeamCityMessage.ps1')" . "$(Join-Path $PSScriptRoot '\Cmdlets\Internal\Tests\Get-ScriptValidationTestsPath.ps1')" -. "$(Join-Path $PSScriptRoot '\Cmdlets\Internal\Tests\Invoke-PesterForTeamCity.ps1')" +. "$(Join-Path $PSScriptRoot '\Cmdlets\Internal\Tests\Invoke-PesterForCi.ps1')" . "$(Join-Path $PSScriptRoot '\Cmdlets\Internal\Tests\Update-XPathValue.ps1')" \ No newline at end of file