. $PSScriptRoot\..\..\Load-PesterModules.ps1 $here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.tests\.', '.' $functionPath = Join-Path -Path $here -ChildPath $sut Write-Host "Overriding SUT: $functionPath" Import-Module $functionPath -Force $moduleForMock = "" Describe "Add-OverflowCustomAttribute" { Mock -ModuleName $moduleForMock -CommandName Get-Content Mock -ModuleName $moduleForMock -CommandName Test-Path Mock -ModuleName $moduleForMock -CommandName Write-Warning Mock -ModuleName $moduleForMock -CommandName Write-Host Mock -ModuleName $moduleForMock -CommandName Get-LogLeadName Mock -ModuleName $moduleForMock -CommandName Import-Module Mock -ModuleName $moduleForMock -CommandName ConvertTo-Yaml Mock -ModuleName $moduleForMock -CommandName Get-NewRelicYamlPath -MockWith { "C:\Program Files\New Relic\newrelic-infra\newrelic-infra.yml" } Context "Updates custom attributes" { It "Overflow value was false" { Mock -ModuleName $moduleForMock -CommandName ConvertFrom-Yaml -MockWith { return @{ custom_attributes = @{ Overflow = $true } } } Add-OverflowCustomAttribute -IsOverflow $false Assert-MockCalled -CommandName ConvertTo-Yaml -Times 1 -Exactly -Scope It -ModuleName $moduleForMock -ParameterFilter { $data.custom_attributes.Overflow -eq $false } } It "Overflow value was true" { Mock -ModuleName $moduleForMock -CommandName ConvertFrom-Yaml -MockWith { return @{ custom_attributes = @{ Overflow = $false } } } Add-OverflowCustomAttribute -IsOverflow $true Assert-MockCalled -CommandName ConvertTo-Yaml -Times 1 -Exactly -Scope It -ModuleName $moduleForMock -ParameterFilter { $data.custom_attributes.Overflow -eq $true } } } Context "Adds custom attributes and Overflow when only custom_attributes is present" { Mock -ModuleName $moduleForMock -CommandName ConvertFrom-Yaml -MockWith { return @{ custom_attributes = @{ Pod = 17 } } } It "Overflow value was true" { Add-OverflowCustomAttribute -IsOverflow $true Assert-MockCalled -CommandName ConvertTo-Yaml -Times 1 -Exactly -Scope It -ModuleName $moduleForMock -ParameterFilter { $data.custom_attributes.Overflow -eq $true } } It "Overflow value was false" { Add-OverflowCustomAttribute -IsOverflow $false Assert-MockCalled -CommandName ConvertTo-Yaml -Times 1 -Exactly -Scope It -ModuleName $moduleForMock -ParameterFilter { $data.custom_attributes.Overflow -eq $false } } } Context "Adds custom attributes and Overflow when custom_attributes and Overflow are not present" { Mock -ModuleName $moduleForMock -CommandName ConvertFrom-Yaml -MockWith { return @{ } } It "Overflow value was false" { Add-OverflowCustomAttribute -IsOverflow $false Assert-MockCalled -CommandName ConvertTo-Yaml -Times 1 -Exactly -Scope It -ModuleName $moduleForMock -ParameterFilter { $data.custom_attributes.Overflow -eq $false } } It "Overflow Value was true" { Add-OverflowCustomAttribute -IsOverflow $true Assert-MockCalled -CommandName ConvertTo-Yaml -Times 1 -Exactly -Scope It -ModuleName $moduleForMock -ParameterFilter { $data.custom_attributes.Overflow -eq $true } } } Context "No file changes if no file changes are needed" { Mock -ModuleName $moduleForMock -CommandName ConvertFrom-Yaml -MockWith { return @{ custom_attributes = @{ Pod = 17; Overflow = $false } } } It "No file change if IsOverflow is the same value in file" { Add-OverflowCustomAttribute -IsOverflow $false Assert-MockCalled -CommandName ConvertTo-Yaml -Times 0 -Exactly -Scope It -ModuleName $moduleForMock } } }