Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Tests/WinImageBuilder.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,33 @@ Describe "Test New-WindowsOnlineImage" {

Remove-Item -Force -ErrorAction SilentlyContinue "${fakeConfigPath}.offline"
}

Describe "Test ConvertFrom-CommaSeparatedString" {
InModuleScope $moduleName {
It "splits comma separated values into separate entries" {
$result = @(ConvertFrom-CommaSeparatedString "OpenSSH.Server,OpenSSH.Client")
$result.Count | Should -Be 2
$result[0] | Should -Be "OpenSSH.Server"
$result[1] | Should -Be "OpenSSH.Client"
}
It "trims whitespace around each entry" {
$result = @(ConvertFrom-CommaSeparatedString "Microsoft-Hyper-V, Microsoft-Hyper-V-Management-Clients")
$result[0] | Should -Be "Microsoft-Hyper-V"
$result[1] | Should -Be "Microsoft-Hyper-V-Management-Clients"
}
It "drops empty entries produced by a trailing comma" {
$result = @(ConvertFrom-CommaSeparatedString "OpenSSH.Server,")
$result.Count | Should -Be 1
$result[0] | Should -Be "OpenSSH.Server"
}
It "returns an empty array for an empty value" {
$result = @(ConvertFrom-CommaSeparatedString "")
$result.Count | Should -Be 0
}
It "returns a single entry for a value without commas" {
$result = @(ConvertFrom-CommaSeparatedString "OpenSSH.Server")
$result.Count | Should -Be 1
$result[0] | Should -Be "OpenSSH.Server"
}
}
}
19 changes: 16 additions & 3 deletions WinImageBuilder.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,19 @@ function Add-PackageToImage {
}
}

function ConvertFrom-CommaSeparatedString {
# Splits a comma separated config value into a clean array, trimming whitespace
# around each entry and dropping empty entries (e.g. from a trailing comma).
Param(
[Parameter(Mandatory=$false)]
[string]$Value
)
if (!$Value) {
return @()
}
return @($Value.Split(",") | ForEach-Object { $_.Trim() } | Where-Object { $_ })
}

function Enable-FeaturesInImage {
Param(
[Parameter(Mandatory=$true)]
Expand Down Expand Up @@ -1737,15 +1750,15 @@ function New-WindowsCloudImage {
-driversBasePath $windowsImageConfig.virtio_base_path
}
if ($windowsImageConfig.extra_features) {
Enable-FeaturesInImage $winImagePath $windowsImageConfig.extra_features.split(",")
Enable-FeaturesInImage $winImagePath (ConvertFrom-CommaSeparatedString $windowsImageConfig.extra_features)
}
if ($windowsImageConfig.extra_packages) {
foreach ($package in $windowsImageConfig.extra_packages.split(",")) {
foreach ($package in (ConvertFrom-CommaSeparatedString $windowsImageConfig.extra_packages)) {
Add-PackageToImage $winImagePath $package -ignoreErrors $windowsImageConfig.extra_packages_ignore_errors
}
}
if ($windowsImageConfig.extra_capabilities) {
Add-CapabilitiesToImage $winImagePath $windowsImageConfig.extra_capabilities
Add-CapabilitiesToImage $winImagePath (ConvertFrom-CommaSeparatedString $windowsImageConfig.extra_capabilities)
}
if ($windowsImageConfig.clean_updates_offline) {
Clean-WindowsUpdates $winImagePath -PurgeUpdates $windowsImageConfig.purge_updates
Expand Down
Loading