Skip to content

Commit 0884495

Browse files
authored
Handle empty preference object when updating settings (#10)
* fix for #8 * added tests for Set-BloggerConfig
1 parent 1a14dc6 commit 0884495

2 files changed

Lines changed: 52 additions & 7 deletions

File tree

src/public/Set-BloggerConfig.ps1

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,34 @@ Function Set-BloggerConfig
22
{
33
[CmdletBinding()]
44
Param(
5+
[Parameter(Mandatory=$true)]
56
[ValidateSet("BlogId","PandocAdditionalArgs","PandocHtmlFormat","PandocMarkdownFormat")]
67
[string]$Name,
78

9+
[Parameter(Mandatory=$true)]
10+
[AllowEmptyString()]
811
[string]$Value
912
)
10-
11-
$userPreferences = @{}
13+
$userPreferences = [pscustomobject]@{}
1214

1315
if (Test-Path $BloggerSession.UserPreferences)
1416
{
15-
Write-Verbose "Loading preferences from $($BloggerSession.UserPreferences)"
16-
$userPreferences = Get-Content $BloggerSession.UserPreferences | ConvertFrom-Json
17+
Write-Verbose "Set-BloggerConfig: Loading preferences from $($BloggerSession.UserPreferences)"
18+
$userPreferences = [pscustomobject](Get-Content $BloggerSession.UserPreferences -Raw | ConvertFrom-Json)
19+
$userPreferences | Out-String | Write-Verbose
1720
}
1821

19-
if ($userPreferences.PsObject.Properties.Name -notcontains $Name)
22+
if (@($userPreferences.PsObject.Properties).Count -eq 0 -or $Name -notin $userPreferences.PsObject.Properties.Name)
2023
{
21-
Write-Verbose "Adding Property $Name"
24+
Write-Verbose "Set-BloggerConfig: Adding Property $Name"
2225
$userPreferences | Add-Member -Name $Name -Value $Value -MemberType NoteProperty
2326
}
2427
else {
25-
Write-Verbose "Updating Propery $Name"
28+
Write-Verbose "Set-BloggerConfig: Updating Propery $Name"
2629
$userPreferences.$Name = $Value
2730
}
2831

32+
Write-Verbose "Set-BloggerConfig: Saving preferences to $($BloggerSession.UserPreferences)"
2933
Set-Content -Path $BloggerSession.UserPreferences -Value ($userPreferences | ConvertTo-Json)
3034
$BloggerSession.$Name = $Value
3135
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
Describe "Set-BloggerConfig" {
3+
BeforeEach {
4+
Import-Module $PSScriptRoot\..\PSBlogger.psm1 -Force
5+
InModuleScope "PSBlogger" {
6+
$BloggerSession = [pscustomobject]@{
7+
BlogId = $null
8+
UserPreferences = "TestDrive:\UserPreferences.json"
9+
}
10+
}
11+
}
12+
13+
It "Should persist new value to <UserPreference> to BloggerSession.UserPreferences" -TestCases @(
14+
@{ UserPreference="BlogId"; UserPreferenceValue="12345" }
15+
) {
16+
# act
17+
Set-BloggerConfig -Name $UserPreference -Value $UserPreferenceValue -ErrorAction Stop
18+
19+
# assert
20+
InModuleScope "PSBlogger" {
21+
$userPreferences = Get-Content $BloggerSession.UserPreferences -Raw | ConvertFrom-Json
22+
$userPreferences.$UserPreference | Should -Be $UserPreferenceValue
23+
} -Parameters @{ UserPreference=$UserPreference; UserPreferenceValue=$UserPreferenceValue }
24+
}
25+
26+
It "Should persist new value to <UserPreference> to empty BloggerSession.UserPreferences file" -TestCases @(
27+
@{ UserPreference="BlogId"; UserPreferenceValue="12345" }
28+
) {
29+
# arrange: empty file
30+
Set-Content TestDrive:\UserPreferences.json -Value "{}"
31+
32+
# act
33+
Set-BloggerConfig -Name $UserPreference -Value $UserPreferenceValue
34+
35+
# assert
36+
InModuleScope "PSBlogger" {
37+
$userPreferences = Get-Content $BloggerSession.UserPreferences -Raw | ConvertFrom-Json
38+
$userPreferences.$UserPreference | Should -Be $UserPreferenceValue
39+
} -Parameters @{ UserPreference=$UserPreference; UserPreferenceValue=$UserPreferenceValue }
40+
}
41+
}

0 commit comments

Comments
 (0)