Skip to content

Commit

Permalink
Added integration tests for PSCodeHealth report object
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuBuisson committed May 20, 2017
1 parent 3289ebd commit ac3591d
Show file tree
Hide file tree
Showing 41 changed files with 600 additions and 50 deletions.
5 changes: 2 additions & 3 deletions PSCodeHealth.build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,8 @@ task Upload_Test_Results_To_AppVeyor {
task Test Unit_Tests,
Fail_If_Failed_Unit_Test,
Publish_Unit_Tests_Coverage,
# There are no integration tests at the moment
# Integration_Tests,
# Fail_If_Failed_Integration_Test,
Integration_Tests,
Fail_If_Failed_Integration_Test,
Upload_Test_Results_To_AppVeyor

task Analyze {
Expand Down
2 changes: 1 addition & 1 deletion PSCodeHealth/Private/Get-PowerShellFile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ Function Get-PowerShellFile {
[string[]]$Exclude
)

$PowerShellFiles = Get-ChildItem @PSBoundParameters -Filter '*.ps*1' -File | Where-Object { $_.FullName -notmatch "Tests|xml$" }
$PowerShellFiles = Get-ChildItem @PSBoundParameters -Filter '*.ps*1' -File | Where-Object { $_.BaseName -notmatch "Test|xml$" }
return $PowerShellFiles.FullName
}
143 changes: 143 additions & 0 deletions Tests/Integration/PSCodeHealthReport.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
$ModuleName = 'PSCodeHealth'
Import-Module "$PSScriptRoot\..\..\$ModuleName\$($ModuleName).psd1" -Force
$CodePath = "$PSScriptRoot\..\TestData\coveralls"

Describe 'Invoke-PSCodeHealth' {

$Result = Invoke-PSCodeHealth -Path $CodePath
$ExpectedFunctionNames = @('Add-CoverageInfo','Merge-CoverageResult','Get-CoverageArray','Format-FileCoverage','Get-CommandsForFile','Get-GitInfo','Format-Coverage','Publish-Coverage','Get-CoveragePercentage')
$AddCoverageInfo = $Result.FunctionHealthRecords | Where-Object FunctionName -eq 'Add-CoverageInfo'
$GetCoverageArray = $Result.FunctionHealthRecords | Where-Object FunctionName -eq 'Get-CoverageArray'
$FormatFileCoverage = $Result.FunctionHealthRecords | Where-Object FunctionName -eq 'Format-FileCoverage'
$FormatCoverage = $Result.FunctionHealthRecords | Where-Object FunctionName -eq 'Format-Coverage'
$GetCoveragePercentage = $Result.FunctionHealthRecords | Where-Object FunctionName -eq 'Get-CoveragePercentage'

Context 'Given code in coveralls module, Invoke-PSCodeHealth return the expected FunctionHealthRecords' {

It 'Should find 9 function definitions' {
$Result.FunctionHealthRecords.Count | Should Be 9
}
It 'Should return only functions with the expected function names' {
Foreach ( $FunctionName in $Result.FunctionHealthRecords.FunctionName ) {
$FunctionName | Should BeIn $ExpectedFunctionNames
}
}
It 'Should return correct LinesOfCode metric for the function Add-CoverageInfo' {
$AddCoverageInfo.LinesOfCode | Should Be 14
}
It 'Should return correct ScriptAnalyzerFindings metric for the function Add-CoverageInfo' {
$AddCoverageInfo.ScriptAnalyzerFindings | Should Be 0
}
It 'Should return correct TestCoverage metric for the function Add-CoverageInfo' {
$AddCoverageInfo.TestCoverage | Should Be 0
}
It 'Should return correct Complexity metric for the function Add-CoverageInfo' {
$AddCoverageInfo.Complexity | Should Be 1
}
It 'Should return correct MaximumNestingDepth metric for the function Add-CoverageInfo' {
$AddCoverageInfo.MaximumNestingDepth | Should Be 1
}
It 'Should return correct LinesOfCode metric for the function Get-CoverageArray' {
$GetCoverageArray.LinesOfCode | Should Be 30
}
It 'Should return correct ScriptAnalyzerFindings metric for the function Get-CoverageArray' {
$GetCoverageArray.ScriptAnalyzerFindings | Should Be 0
}
It 'Should return correct TestCoverage metric for the function Get-CoverageArray' {
$GetCoverageArray.TestCoverage | Should Be 0
}
It 'Should return correct Complexity metric for the function Get-CoverageArray' {
$GetCoverageArray.Complexity | Should Be 5
}
It 'Should return correct MaximumNestingDepth metric for the function Get-CoverageArray' {
$GetCoverageArray.MaximumNestingDepth | Should Be 3
}
It 'Should return correct LinesOfCode metric for the function Format-FileCoverage' {
$FormatFileCoverage.LinesOfCode | Should Be 24
}
It 'Should return correct ScriptAnalyzerFindings metric for the function Format-FileCoverage' {
$FormatFileCoverage.ScriptAnalyzerFindings | Should Be 0
}
It 'Should return correct TestCoverage metric for the function Format-FileCoverage' {
$FormatFileCoverage.TestCoverage | Should Be 100
}
It 'Should return correct Complexity metric for the function Format-FileCoverage' {
$FormatFileCoverage.Complexity | Should Be 2
}
It 'Should return correct MaximumNestingDepth metric for the function Format-FileCoverage' {
$FormatFileCoverage.MaximumNestingDepth | Should Be 1
}
It 'Should return correct LinesOfCode metric for the function Format-Coverage' {
$FormatCoverage.LinesOfCode | Should Be 39
}
It 'Should return correct ScriptAnalyzerFindings metric for the function Format-Coverage' {
$FormatCoverage.ScriptAnalyzerFindings | Should Be 0
}
It 'Should return correct TestCoverage metric for the function Format-Coverage' {
$FormatCoverage.TestCoverage | Should Be 0
}
It 'Should return correct Complexity metric for the function Format-Coverage' {
$FormatCoverage.Complexity | Should Be 3
}
It 'Should return correct MaximumNestingDepth metric for the function Format-Coverage' {
$FormatCoverage.MaximumNestingDepth | Should Be 1
}
It 'Should return correct LinesOfCode metric for the function Get-CoveragePercentage' {
$GetCoveragePercentage.LinesOfCode | Should Be 16
}
It 'Should return correct ScriptAnalyzerFindings metric for the function Get-CoveragePercentage' {
$GetCoveragePercentage.ScriptAnalyzerFindings | Should Be 0
}
It 'Should return correct TestCoverage metric for the function Get-CoveragePercentage' {
$GetCoveragePercentage.TestCoverage | Should Be 100
}
It 'Should return correct Complexity metric for the function Get-CoveragePercentage' {
$GetCoveragePercentage.Complexity | Should Be 2
}
It 'Should return correct MaximumNestingDepth metric for the function Get-CoveragePercentage' {
$GetCoveragePercentage.MaximumNestingDepth | Should Be 1
}
It 'The health report should have the expected Files property' {
$Result.Files | Should Be 3
}
It 'The health report should have the expected Functions property' {
$Result.Functions | Should Be 9
}
It 'The health report should have the expected LinesOfCodeTotal property' {
$Result.LinesOfCodeTotal | Should Be 201
}
It 'The health report should have the expected LinesOfCodeAverage property' {
$Result.LinesOfCodeAverage | Should Be 22.33
}
It 'The health report should have the expected LinesOfCodeAverage property' {
$Result.LinesOfCodeAverage | Should Be 22.33
}
It 'The health report should have the expected ScriptAnalyzerFindingsTotal property' {
$Result.ScriptAnalyzerFindingsTotal | Should Be 0
}
It 'The health report should have the expected NumberOfTests property' {
$Result.NumberOfTests | Should Be 10
}
It 'The health report should have the expected NumberOfFailedTests property' {
$Result.NumberOfFailedTests | Should Be 0
}
It 'The health report should have the expected NumberOfPassedTests property' {
$Result.NumberOfPassedTests | Should Be 10
}
It 'The health report should have the expected TestsPassRate property' {
$Result.TestsPassRate | Should Be 100
}
It 'The health report should have the expected TestCoverage property' {
$Result.TestCoverage | Should Be 21.43
}
It 'The health report should have the expected CommandsMissedTotal property' {
$Result.CommandsMissedTotal | Should Be 77
}
It 'The health report should have the expected ComplexityAverage property' {
$Result.ComplexityAverage | Should Be 2
}
It 'The health report should have the expected NestingDepthAverage property' {
$Result.NestingDepthAverage | Should Be 1.11
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions Tests/TestData/FakeTestFile.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Describe 'Fake tests' {

Context 'Fake test case' {

}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
112 changes: 112 additions & 0 deletions Tests/TestData/coveralls/Coveralls.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".")
. "$here\$sut"

Describe "Get-CoveragePercentage" {
Context "Providing a correct return" {
BeforeAll {
$content = "{""created_at"":""2017-03-16T12:37:17Z"",""url"":null,""commit_message"":""Add fake data to CA_KEY when it's not present"",""branch"":""master"",""committer_name"":""Jan De Dobbeleer"",""committer_email"":""jan@gmail.com"",""commit_sha"":""ba5947862030d86208d0181189c160df04e5c309"",""repo_name"":""JanJoris/coveralls"",""badge_url"":""https://s3.amazonaws.com/assets.coveralls.io/badges/coveralls_13.svg"",""coverage_change"":0.0,""covered_percent"":%percentage%}"
}
It "has a coverage percentage of 13" {
Mock Invoke-WebRequest { return @{ Content = $content.Replace('%percentage%', '13') }}
Get-CoveragePercentage -RepositoryLink coveralls | Should Be 13
}
It "has a coverage percentage of 28" {
Mock Invoke-WebRequest { return @{ Content = $content.Replace('%percentage%', '28') }}
Get-CoveragePercentage -RepositoryLink coveralls | Should Be 28
}
}
Context "Providing an exception return" {
It "throws an error when providing bogus info" {
{ Get-CoveragePercentage -RepositoryLink coveralls -ErrorAction Stop } | Should Throw
}
It "throws an error when providing a non-existing repository" {
{ Get-CoveragePercentage -RepositoryLink https://coveralls.io/coveralls/coveralls -ErrorAction Stop } | Should Throw
}
}
Context "Providing crappy data" {
It "has no content object to parse" {
Mock Invoke-WebRequest { return @{ NotContent = "I am so crappy" }}
{ Get-CoveragePercentage -RepositoryLink coveralls -ErrorAction Stop } | Should Throw
}
}
}

Describe "Get-CommandsForFile" {
Context "Receiving proper input" {
It "has 2 commands that match the file" {
$commands = @(
@{
File = "file.ps1"
Line = 1
},
@{
File = "file.ps1"
Line = 2
},
@{
File = "file2.ps1"
Line = 3
}
)
Mock Get-Item { return @{ FullName = 'file.ps1' }}
$result = Get-CommandsForFile -Commands $commands -File 'file.ps1'
$result.Count | Should be 2
}
It "has 0 commands that match the file" {
$commands = @(
@{
File = "file2.ps1"
Line = 1
},
@{
File = "file2.ps1"
Line = 2
}
)
Mock Get-Item { return @{ FullName = 'file.ps1' }}
$result = Get-CommandsForFile -Commands $commands -File 'file.ps1'
$result.Count | Should be 0
}
}
Context "Receiving crappy input" {
It "should not output any match" {
$commands = @{
Balony = "garbage"
Ohn0 = 1324343
}
Mock Get-Item { return @{ FullName = 'file.ps1' }}
$result = Get-CommandsForFile -Commands $commands -File 'file.ps1'
$result.Count | Should be 0
}
}
}

Describe "Format-FileCoverage" {
Context "Receiving proper input" {
It "outputs the expected information" {
$expected = New-Object -TypeName PSObject -Property @{
name = 'file.ps1'
source_digest = '123456789'
coverage = 'Test'
}
Mock Get-FileHash { return @{ Hash = '123456789' }}
Mock Get-Item -ParameterFilter { $Path -eq 'RootFolder' } { @{ FullName = 'root' } }
Mock Get-Item -ParameterFilter { $Path -eq 'File' } { @{ FullName = 'root\file.ps1' } }
$result = Format-FileCoverage -CoverageArray 'Test' -File 'File' -RootFolder 'RootFolder'
$result | Should MatchExactly $expected
}
It "handles the paths without an issue" {
$expected = New-Object -TypeName PSObject -Property @{
name = 'file.ps1'
source_digest = '123456789'
coverage = 'Test'
}
Mock Get-FileHash { return @{ Hash = '123456789' }}
Mock Get-Item -ParameterFilter { $Path -eq 'RootFolder' } { @{ FullName = 'root' } }
Mock Get-Item -ParameterFilter { $Path -eq 'File' } { @{ FullName = 'file.ps1' } }
$result = Format-FileCoverage -CoverageArray 'Test' -File 'File' -RootFolder 'RootFolder'
$result | Should MatchExactly $expected
}
}
}

0 comments on commit ac3591d

Please sign in to comment.