Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/make invoke pester calls ci agnostic #23

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -38,19 +38,19 @@ 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:\"

Assert-VerifiableMocks
}

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:\"
Expand All @@ -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:\"
Expand All @@ -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:\"
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion OctopusStepTemplateCi/OctopusStepTemplateCi.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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')"