Functions/Add-LineToFile.Tests.ps1

describe "BitTitan.Runbooks.Common/Add-LineToFile" -Tag "module", "unit" {

    # Import the function to test
    . "$($PSScriptRoot)\Add-LineToFile.ps1"

    # Declare mocks
    mock Get-Content {
        "current contents"
    }
    mock Set-Content {}

    it "adds a line to the front of the file by default" {
        # Call the function
        Add-LineToFile "path" "line" -Front

        # Verify mocks
        Assert-MockCalled Get-Content -Times 1 -Exactly -ParameterFilter {
            $Path -eq "path"
        } -Scope it
        Assert-MockCalled Set-Content -Times 1 -Exactly -ParameterFilter {
            $Value -eq "line`r`ncurrent contents"
        } -Scope it
    }

    it "adds a line to the front of the file when specified" {
        # Call the function
        Add-LineToFile "path" "line"

        # Verify mocks
        Assert-MockCalled Get-Content -Times 1 -Exactly -ParameterFilter {
            $Path -eq "path"
        } -Scope it
        Assert-MockCalled Set-Content -Times 1 -Exactly -ParameterFilter {
            $Value -eq "line`r`ncurrent contents"
        } -Scope it
    }

    it "adds a line to the back of the file" {
        # Call the function
        Add-LineToFile "path" "line" -Back

        # Verify mocks
        Assert-MockCalled Get-Content -Times 1 -Exactly -ParameterFilter {
            $Path -eq "path"
        } -Scope it
        Assert-MockCalled Set-Content -Times 1 -Exactly -ParameterFilter {
            $Value -eq "current contents`r`nline"
        } -Scope it
    }
}