PSCodeCovIo.psm1

# Copied from https://github.com/PowerShell/DscResource.Tests/blob/491688867dc53894b92ca53520a18d145deb7760/DscResource.CodeCoverage/CodeCovIo.psm1
# Modified to work cross-platform
#
# https://github.com/PowerShell/DscResource.Tests/blob/491688867dc53894b92ca53520a18d145deb7760/LICENSE
#
# The MIT License (MIT)
#
# Copyright (c) 2015 Microsoft Corporation.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

<#
  .SYNOPSIS
  Updates a hash table with the Unique file lines
  Structure:
  RootTable.[FileKey].[SubTable].[Line]

  .PARAMETER FileLine
  The table to update

  .PARAMETER Command
  The list of Command from pester to update the table based on

  .PARAMETER RepoRoot
  The path to the root of the repo. This part of the path will not be included in the report. Needed to normalize all the reports.

  .PARAMETER TableName
  The path of the file to write the report to.
#>

function Add-UniqueFileLineToTable {
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [HashTable]
        $FileLine,

        [Parameter(Mandatory = $true)]
        [Object]
        $Command,

        [Parameter(Mandatory = $true)]
        [String]
        $RepoRoot,

        [Parameter(Mandatory = $true)]
        [String]
        $TableName
    )

    # file paths need to be relative to repo root when querying GIT
    Push-Location -LiteralPath $RepoRoot
    try {
        Write-Verbose -Message "running git ls-files" -Verbose

        # Get the list of files as Git sees them
        $fileKeys = git ls-files

        # Populate the sub-table
        foreach ($command in $Command) {
            #Find the file as Git sees it
            $file = $command.File
            $fileKey = $file.replace($RepoRoot, '').replace('\', '/').TrimStart('/')
            $fileKey = $fileKeys.where{$_ -like $fileKey}

            if ($null -eq $fileKey) {
                Write-Warning -Message "Unexpected error filekey was null"
                continue
            }
            elseif ($fileKey.Count -ne 1) {
                Write-Warning -Message "Unexpected error, more than one git file matched file ($file): $($fileKey -join ', ')"
                continue
            }

            $fileKey = $fileKey | Select-Object -First 1

            if (!$FileLine.ContainsKey($fileKey)) {
                $FileLine.add($fileKey, @{ $TableName = @{}})
            }

            if (!$FileLine.$fileKey.ContainsKey($TableName)) {
                $FileLine.$fileKey.Add($TableName, @{})
            }

            $lines = $FileLine.($fileKey).$TableName
            $lineKey = $($command.line)
            if (!$lines.ContainsKey($lineKey)) {
                $lines.Add($lineKey, 1)
            }
            else {
                $lines.$lineKey ++
            }
        }
    }
    finally {
        Pop-Location
    }
}

<#
  .SYNOPSIS
  Tests if the specified property is a CodeCoverage object
  For use in a ValidateScript attribute

  .PARAMETER CodeCoverage
  The CodeCoverage property of the output of Invoke-Pester with the -PassThru and -CodeCoverage options

#>

function Test-CodeCoverage {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [Object]
        $CodeCoverage
    )

    if (!($CodeCoverage | Get-Member -Name MissedCommands)) {
        throw 'Must be a Pester CodeCoverage object'
    }

    return $true
}

<#
  .SYNOPSIS
  Exports a Pester CodeCoverage report as a CodeCov.io json report

  .PARAMETER CodeCoverage
  The CodeCoverage property of the output of Invoke-Pester with the -PassThru and -CodeCoverage options

  .PARAMETER RepoRoot
  The path to the root of the repo. This part of the path will not be included in the report. Needed to normalize all the reports.

  .PARAMETER Path
  The path of the file to write the report to.
#>

function Export-CodeCovIoJson {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [ValidateScript( {Test-CodeCoverage -CodeCoverage $_})]
        [Object]
        $CodeCoverage,

        [Parameter(Mandatory = $true)]
        [String]
        $RepoRoot,

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [String]
        $Path = (Join-Path -Path $env:TEMP -ChildPath 'codeCov.json')
    )

    Write-Verbose -Message "RepoRoot: $RepoRoot"

    # Get a list of all unique files
    $files = @()
    foreach ($file in ($CodeCoverage.MissedCommands | Select-Object -ExpandProperty File -Unique)) {
        if ($files -notcontains $file) {
            $files += $file
        }
    }

    foreach ($file in ($CodeCoverage.HitCommands | Select-Object -ExpandProperty File -Unique)) {
        if ($files -notcontains $file) {
            $files += $file
        }
    }

    # A table of the file key then a sub-tables of `misses` and `hits` lines.
    $FileLine = @{}

    # define common parameters
    $addUniqueFileLineParams = @{
        FileLine = $FileLine
        RepoRoo  = $RepoRoot
    }

    <#
        Basically indexing all the hit and miss lines by file and line.
        Populate the misses sub-table
    #>

    Add-UniqueFileLineToTable -Command $CodeCoverage.MissedCommands -TableName 'misses' @addUniqueFileLineParams

    # Populate the hits sub-table
    Add-UniqueFileLineToTable -Command $CodeCoverage.HitCommands -TableName 'hits' @addUniqueFileLineParams

    # Create the results structure
    $resultLineData = @{}
    $resultMessages = @{}
    $result = @{
        coverage = $resultLineData
        messages = $resultMessages
    }

    foreach ($file in $FileLine.Keys) {
        $hit = 0
        $partial = 0
        $missed = 0
        Write-Verbose -Message "summarizing for file: $file"

        # Get the hits, if they exist
        $hits = @{}
        if ($FileLine.$file.ContainsKey('hits')) {
            $hits = $FileLine.$file.hits
        }

        # Get the misses, if they exist
        $misses = @{}
        if ($FileLine.$file.ContainsKey('misses')) {
            $misses = $FileLine.$file.misses
        }

        Write-Verbose -Message "fileKeys: $($FileLine.$file.Keys)"
        $max = $hits.Keys | Sort-Object -Descending | Select-Object -First 1
        $maxMissLine = $misses.Keys | Sort-Object -Descending | Select-Object -First 1

        <#
            if max missed line is greater than maxed hit line
            used max missed line as the max line
        #>

        if ($maxMissLine -gt $max) {
            $max = $maxMissLine
        }

        $lineData = @()
        $messages = @{}

        <#
            produce the results
            start at line 0 per codecov doc
        #>

        for ($lineNumber = 0; $lineNumber -le $max; $lineNumber++) {
            $hitInfo = $null
            $missInfo = $null
            switch ($true) {
                # Hit
                ($hits.ContainsKey($lineNumber) -and ! $misses.ContainsKey($lineNumber)) {
                    Write-Verbose -Message "Got code coverage hit at $lineNumber"
                    $lineData += "$($hits.$lineNumber)"
                }

                # Miss
                ($misses.ContainsKey($lineNumber) -and ! $hits.ContainsKey($lineNumber)) {
                    Write-Verbose -Message "Got code coverage miss at $lineNumber"
                    $lineData += '0'
                }

                # Partial Hit
                ($misses.ContainsKey($lineNumber) -and $hits.ContainsKey($lineNumber)) {
                    Write-Verbose -Message "Got code coverage partial at $lineNumber"

                    $missInfo = $misses.$lineNumber
                    $hitInfo = $hits.$lineNumber
                    $lineData += "$hitInfo/$($hitInfo+$missInfo)"
                }

                # Non-Code Line
                (!$misses.ContainsKey($lineNumber) -and !$hits.ContainsKey($lineNumber)) {
                    Write-Verbose -Message "Got non-code at $lineNumber"

                    <#
                        If I put an actual null in an array ConvertTo-Json just leaves it out
                        I'll put this string in and clean it up later.
                    #>

                    $lineData += '!null!'
                }

                default {
                    throw "Unexpected error occured generating codeCov.io report for $file at line $lineNumber with hits: $($hits.ContainsKey($lineNumber)) and misses: $($misses.ContainsKey($lineNumber))"
                }
            }
        }

        $resultLineData.Add($file, $lineData)
        $resultMessages.add($file, $messages)
    }

    $commitOutput = @(&git log -1 --pretty=format:%H)
    $commit = $commitOutput[0]

    Write-Verbose -Message "Branch: $Branch"

    $json = $result | ConvertTo-Json
    $json = $json.Replace('"!null!"', 'null')

    $json | Out-File -Encoding ascii -LiteralPath $Path -Force
    return $Path
}