Skip to content

Commit 97bc0d4

Browse files
authored
Merge pull request #3809 from vexx32/3802-fix-agent-config-read-loop
(#3802) Avoid unnecessary writes to the config file
2 parents 9e183a7 + 94ba0ec commit 97bc0d4

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,23 @@ private static void AddOrRemoveLicensedSource(ChocolateyLicense license, ConfigF
150150
var existingSource = configFileSettings.Sources?.FirstOrDefault(s => s.Id.IsEqualTo(ApplicationParameters.ChocolateyLicensedFeedSourceName));
151151
if (existingSource != null)
152152
{
153+
// Avoid making any changes since everything is correct and return.
154+
// This avoids making unnecessary updates to the chocolatey.config file.
155+
// A direct Equals isn't possible because we do want to allow users to change it being
156+
// Disabled or not, and we do have to check the decrypted password value, as the
157+
// encrypted strings are not the same every time they're generated.
158+
if (existingSource.Id == configSource.Id
159+
&& existingSource.Value == configSource.Value
160+
&& existingSource.UserName == configSource.UserName
161+
&& NugetEncryptionUtility.DecryptString(existingSource.Password) == license.Id
162+
&& existingSource.Priority == configSource.Priority
163+
&& existingSource.BypassProxy == configSource.BypassProxy
164+
&& existingSource.AllowSelfService == configSource.AllowSelfService
165+
&& existingSource.VisibleToAdminsOnly == configSource.VisibleToAdminsOnly)
166+
{
167+
return;
168+
}
169+
153170
// Ensure we retain whether the licensed source is disabled.
154171
configSource.Disabled = existingSource.Disabled;
155172

tests/pester-tests/chocolatey.Tests.ps1

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,30 @@ exit $error.count
280280
}
281281
}
282282

283+
# This is skipped when not run in CI because it requires a valid license
284+
Context 'Config file updates when a valid license is present' -Tag License -Skip:(-not $env:TEST_KITCHEN) {
285+
BeforeAll {
286+
Restore-ChocolateyInstallSnapshot
287+
288+
Enable-ChocolateySource -Name hermes-setup
289+
290+
Invoke-Choco install chocolatey-license-business
291+
292+
# Run once post-license-install to ensure the config file is updated with the licensed source
293+
$null = Invoke-Choco list
294+
}
295+
296+
It 'does not overwrite the config file when nothing has changed' {
297+
$originalHash = Get-FileHash -Path "$env:ChocolateyInstall/config/chocolatey.config"
298+
299+
$null = Invoke-Choco list
300+
301+
$newHash = Get-FileHash -Path "$env:ChocolateyInstall/config/chocolatey.config"
302+
303+
$originalHash.Hash | Should -BeExactly $newHash.Hash -Because 'the configuration file should not have changed between runs'
304+
}
305+
}
306+
283307
# This is skipped when not run in CI because it requires a valid license
284308
Context 'Changing licenses after modifying the Chocolatey Licensed repository' -Tag License -Skip:(-not $env:TEST_KITCHEN) {
285309
BeforeAll {

0 commit comments

Comments
 (0)