Skip to content

Commit 5dcf914

Browse files
mazhelezCopilot
andauthored
Fix issue with transfering $schema propery when destination file does't exist (#1739)
Fixes #1738 --------- Co-authored-by: Copilot <[email protected]>
1 parent 9ea437f commit 5dcf914

File tree

2 files changed

+78
-10
lines changed

2 files changed

+78
-10
lines changed

Actions/CheckForUpdates/CheckForUpdates.HelperFunctions.ps1

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -474,21 +474,26 @@ function GetModifiedSettingsContent {
474474

475475
$srcSettings = Get-ContentLF -Path $srcSettingsFile | ConvertFrom-Json
476476

477-
if(Test-Path -Path $dstFile -PathType Leaf) {
477+
$dstSettings = $null
478+
if(Test-Path -Path $dstSettingsFile -PathType Leaf) {
478479
$dstSettings = Get-ContentLF -Path $dstSettingsFile | ConvertFrom-Json
479480
}
480-
else {
481-
# If the destination settings file does not exist, create an empty object
482-
$dstSettings = [PSCustomObject]@{}
481+
482+
if(!$dstSettings) {
483+
# If the destination settings file does not exist or it's empty, create an new settings object with default values from the source settings (which includes the $schema property already)
484+
$dstSettings = $srcSettings
483485
}
486+
else {
487+
# Change the $schema property to be the same as the source settings file (add it if it doesn't exist)
488+
$schemaKey = '$schema'
489+
$schemaValue = $srcSettings."$schemaKey"
484490

485-
$schemaKey = '$schema'
486-
$schemaValue = $srcSettings."$schemaKey"
491+
$dstSettings | Add-Member -MemberType NoteProperty -Name "$schemaKey" -Value $schemaValue -Force
487492

488-
$dstSettings | Add-Member -MemberType NoteProperty -Name "$schemaKey" -Value $schemaValue -Force
493+
# Make sure the $schema property is the first property in the object
494+
$dstSettings = $dstSettings | Select-Object @{ Name = '$schema'; Expression = { $_.'$schema' } }, * -ExcludeProperty '$schema'
495+
}
489496

490-
# Make sure the $schema property is the first property in the object
491-
$dstSettings = $dstSettings | Select-Object @{ Name = '$schema'; Expression = { $_.'$schema' } }, * -ExcludeProperty '$schema'
492497

493498
return $dstSettings | ConvertTo-JsonLF
494499
}

Tests/CheckForUpdates.Action.Test.ps1

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,69 @@ Describe "CheckForUpdates Action Tests" {
126126
}
127127
}
128128

129-
130129
# Call action
131130
}
131+
132+
Describe "CheckForUpdates Action: CheckForUpdates.HelperFunctions.ps1" {
133+
BeforeAll {
134+
$actionName = "CheckForUpdates"
135+
$scriptRoot = Join-Path $PSScriptRoot "..\Actions\$actionName" -Resolve
136+
Import-Module (Join-Path $scriptRoot "..\Github-Helper.psm1") -DisableNameChecking -Force
137+
. (Join-Path -Path $scriptRoot -ChildPath "CheckForUpdates.HelperFunctions.ps1")
138+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', 'actionScript', Justification = 'False positive.')]
139+
$tmpSrcFile = Join-Path $PSScriptRoot "tempSrcFile.json"
140+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', 'actionScript', Justification = 'False positive.')]
141+
$tmpDstFile = Join-Path $PSScriptRoot "tempDestFile.json"
142+
}
143+
144+
AfterEach {
145+
# Clean up temporary files
146+
if (Test-Path $tmpSrcFile) {
147+
Remove-Item -Path $tmpSrcFile -Force
148+
}
149+
if (Test-Path $tmpDstFile) {
150+
Remove-Item -Path $tmpDstFile -Force
151+
}
152+
}
153+
154+
It 'GetModifiedSettingsContent returns correct content when destination file is not empty' {
155+
# Create settings files with the content
156+
@{ "`$schema" = "someSchema"; "srcSetting" = "value1" } | ConvertTo-Json -Depth 10 | Out-File -FilePath $tmpSrcFile -Force
157+
@{ "setting1" = "value2" } | ConvertTo-Json -Depth 10 | Out-File -FilePath $tmpDstFile -Force
158+
159+
$modifiedContentJson = GetModifiedSettingsContent -srcSettingsFile $tmpSrcFile -dstSettingsFile $tmpDstFile
160+
161+
$modifiedContent = $modifiedContentJson | ConvertFrom-Json
162+
$modifiedContent | Should -Not -BeNullOrEmpty
163+
$modifiedContent.PSObject.Properties.Name.Count | Should -Be 2 # setting1 and $schema
164+
$modifiedContent."setting1" | Should -Be "value2"
165+
$modifiedContent."`$schema" | Should -Be "someSchema"
166+
}
167+
168+
It 'GetModifiedSettingsContent returns correct content when destination file is empty' {
169+
# Create only the source file
170+
@{ "`$schema" = "someSchema"; "srcSetting" = "value1" } | ConvertTo-Json -Depth 10 | Out-File -FilePath $tmpSrcFile -Force
171+
'' | Out-File -FilePath $tmpDstFile -Force
172+
$modifiedContentJson = GetModifiedSettingsContent -srcSettingsFile $tmpSrcFile -dstSettingsFile $tmpDstFile
173+
174+
$modifiedContent = $modifiedContentJson | ConvertFrom-Json
175+
$modifiedContent | Should -Not -BeNullOrEmpty
176+
@($modifiedContent.PSObject.Properties.Name).Count | Should -Be 2 # srcSetting and $schema
177+
$modifiedContent."`$schema" | Should -Be "someSchema"
178+
$modifiedContent."srcSetting" | Should -Be "value1"
179+
}
180+
181+
It 'GetModifiedSettingsContent returns correct content when destination file does not exist' {
182+
# Create only the source file
183+
@{ "`$schema" = "someSchema"; "srcSetting" = "value1" } | ConvertTo-Json -Depth 10 | Out-File -FilePath $tmpSrcFile -Force
184+
185+
Test-Path $tmpDstFile | Should -Be $false
186+
$modifiedContentJson = GetModifiedSettingsContent -srcSettingsFile $tmpSrcFile -dstSettingsFile $tmpDstFile
187+
188+
$modifiedContent = $modifiedContentJson | ConvertFrom-Json
189+
$modifiedContent | Should -Not -BeNullOrEmpty
190+
$modifiedContent.PSObject.Properties.Name.Count | Should -Be 2 # srcSetting and $schema
191+
$modifiedContent."srcSetting" | Should -Be "value1"
192+
$modifiedContent."`$schema" | Should -Be "someSchema"
193+
}
194+
}

0 commit comments

Comments
 (0)