Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trim the line before splitting parts in HostsFile #527

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Updated CHANGELOG.md
- Renamed NetworkingDSc to NetworkingDsc in CHANGELOG.md - fixes [Issue #513](https://github.com/dsccommunity/NetworkingDsc/issues/513).
- HostsFile
- Fix bad return data when line contains leading spaces - fixes [Issue #526](https://github.com/dsccommunity/NetworkingDsc/issues/526)
- CI Pipeline
- Updated pipeline files to match current DSC Community patterns - fixes [Issue #528](https://github.com/dsccommunity/NetworkingDsc/issues/528).
- Updated HQRM and build steps to use windows-latest image.
Expand Down
2 changes: 1 addition & 1 deletion source/DSCResources/DSC_HostsFile/DSC_HostsFile.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ function Get-HostEntry

foreach ($hosts in $allHosts)
{
$data = $hosts -split '\s+'
$data = $hosts.Trim() -split '\s+'

if ($data.Length -gt 2)
{
Expand Down
24 changes: 24 additions & 0 deletions tests/Unit/DSC_HostsFile.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,30 @@ try
}
}
}

Context 'When a host entry has leading spaces' {

$testParams = @{
HostName = 'www.anotherexample.com'
IPAddress = '127.0.0.1'
Verbose = $true
}

Mock -CommandName Get-Content -MockWith {
return @(
'# A mocked example of a host file - this line is a comment',
'',
'127.0.0.1 localhost',
'# comment',
' 127.0.0.1 www.anotherexample.com',
''
)
}

It 'Should return absent from the get method' {
(Get-TargetResource @testParams).Ensure | Should -Be 'Present'
}
}
} #end InModuleScope $DSCResourceName
}
finally
Expand Down