ps/Modules/Alkami.PowerShell.Common/Public/Add-HostsFileEntry.tests.ps1

77 lines
3.4 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
. $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-HostsFileContent" {
Mock -CommandName Write-Warning {} -ModuleName $moduleForMock
Mock -CommandName Write-Host {} -ModuleName $moduleForMock
Mock -CommandName Write-Error {} -ModuleName $moduleForMock
Mock -CommandName Test-IsCollectionNullOrEmpty { $false } -ModuleName $moduleForMock
$tempPath = Join-Path $TestDrive "hoststFile.txt"
Set-Content -Path $tempPath @"
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
10.10.10.10 bork.world
25.63.10 x.acme.com # x client host
127.0.0.1 already.exists
7.7.7.7 tobeforce.write
11.11.11.11 comment.dupe
127.0.0.1 jw smartsite.jw
127.0.0.1 dev.filepresso.com dev.filepresso.com.pl dev.filepresso.pl dev.filefly.pl
#### 89.31.66.248 sp.biatelbit.pl
192.168.72.51 subversion.biatel.com.pl
# 127.0.0.1 bip.smartsite.bit-sa.pl
# 127.0.0.1 bip.augustow.wrotapodlasia.pl
127.0.0.1 rpowp.smartsite.bit-sa.pl
127.0.0.1 cms.smartsite.bit-sa.pl
127.0.0.1 cms-test.smartsite.bit-sa.pl
############################################################
#10.200.14.70 ST-sapp1
#10.200.14.70 tv.smartsite.bit-sa.pl wp.smartsite.bit-sa.pl cms.smartsite.bit-sa.pl test.smartsite.bit-sa.pl bip.smartsite.bit-sa.pl si.smartsite.bit-sa.pl sspw.wrotapodlasia.pl
#10.200.14.70 radny.smartsite.bit-sa.pl stats.smartsite.bit-sa.pl test9.smartsite.bit-sa.pl
"@
Context "Negative cases" {
It "Overwrites hostname" {
Add-HostsFileEntry -Ip "127.0.0.1" -Hostname "not.exist" -HostsPath $tempPath
$hostsFile = Get-HostsFileContent -HostsPath $tempPath
$hostsFile -match "127.0.0.1 not.exist" | Should -BeTrue
}
It "Does not add multihost" {
Add-HostsFileEntry -Ip "9.9.9.9" -Hostname "bork.world blarg.host" -Comment "foo" -HostsPath $tempPath
$content = Get-Content $tempPath
$content -match "9.9.9.9 bork.world blarg.host # foo" | Should -BeNullOrEmpty
}
}
Context "Happy path" {
It "Comments out the dupe entry" {
Add-HostsFileEntry -Ip "11.11.11.11" -Hostname "comment.dupe" -HostsPath $tempPath
Add-HostsFileEntry -Ip "8.8.8.8" -Hostname "comment.dupe" -HostsPath $tempPath
$content = Get-Content $tempPath
$content -match "#11.11.11.11 comment.dupe" | Should -BeTrue
}
It "Adds line" {
Add-HostsFileEntry -Ip "9.9.9.9" -Hostname "bork.world" -HostsPath $tempPath
$content = Get-Content $tempPath
$content -match "9.9.9.9 bork.World" | Should -BeTrue
}
It "Adds line with comment" {
Add-HostsFileEntry -Ip "4.4.4.4" -Hostname "comment.line" -Comment "foo" -HostsPath $tempPath
$content = Get-Content $tempPath
$content -match "4.4.4.4 comment.line # foo" | Should -BeTrue
}
}
Context "Bad override" {
It "Throws if array is null" {
Mock -CommandName Test-IsCollectionNullOrEmpty { $true }
{ Add-HostsFileEntry -Ip "11.11.11.12" -Hostname "comment.duplicate" -HostsPath $tempPath } | Should Throw
}
}
}