Skip to content

Commit

Permalink
Tests for Format-FileCoverage and Get-CommandsForFile
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Mar 18, 2017
1 parent 2600f7e commit fb6a95a
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 9 deletions.
95 changes: 89 additions & 6 deletions Coveralls.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,105 @@ Describe "Get-CoveragePercentage" {
$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') }}
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') }}
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 -ErrorVariable err
$err[1].Exception.Message | Should Be 'The remote name could not be resolved: ''coveralls.json'''
{ 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 -ErrorVariable err
$err[1].Exception.Message | Should Be 'The remote server returned an error: (404) Not Found.'
{ 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
}
}
}
7 changes: 5 additions & 2 deletions Coveralls.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ function Format-FileCoverage {

$fileHash = Get-FileHash $File -Algorithm MD5
$root = (Get-Item $RootFolder).FullName
$fileName = (Get-Item $File).FullName.Replace($root, '').Replace('\','/').Remove(0,1)
$fileName = (Get-Item $File).FullName.Replace($root, '').Replace('\','/')
if ($fileName.StartsWith('/')) {
$fileName = $fileName.Remove(0,1)
}
return New-Object -TypeName PSObject -Property @{
name = $fileName
source_digest = $fileHash.Hash
Expand All @@ -103,7 +106,7 @@ function Get-CommandsForFile {
$File
)

$fullName = (Get-ChildItem $File).FullName
$fullName = (Get-Item $File).FullName
$matchedCommands = $Commands | Where-Object {
$_.File -eq $fullName
}
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# coveralls
coveralls
=========

[![Build status](https://img.shields.io/appveyor/ci/janjoris/coveralls/master.svg?maxAge=2592000)](https://ci.appveyor.com/project/JanJoris/coveralls) [![Coverage Status](https://coveralls.io/repos/github/JanJoris/coveralls/badge.svg)](https://coveralls.io/github/JanJoris/coveralls) [![Gitter](https://badges.gitter.im/posh-coveralls/Lobby.svg)](https://gitter.im/posh-coveralls/Lobby) [![PS Gallery](https://img.shields.io/badge/install-PS%20Gallery-blue.svg)](https://www.powershellgallery.com/packages/coveralls)

Expand Down Expand Up @@ -27,3 +28,9 @@ If you run you tests outside of the root folder of your repository, you need to
In case your CI does not allow you to fetch the branchname due to a checkout on a commit, you can specify it otherwise. This example uses Appveyor's environment variables.

$coverage = Format-Coverage -Include $files -CoverallsApiToken $token -BranchName $ENV:APPVEYOR_REPO_BRANCH

It's also possible to include a Pester result not to have to run Pester twice. Just provide the result and your good to go (make sure it has the code coverage included).

$coverage = Format-Coverage -PesterResults $result -CoverallsApiToken $token


0 comments on commit fb6a95a

Please sign in to comment.