From d53986ed65db75963f796fe643f45bd5d8056ef1 Mon Sep 17 00:00:00 2001 From: Jimmy Lewis Date: Fri, 26 Apr 2024 09:04:36 -0700 Subject: [PATCH] make goalstate immutable --- .../LibraryInstallationGoalState.cs | 5 +++-- src/LibraryManager/Providers/BaseProvider.cs | 6 +++--- .../Shared/LibraryCommandServiceTest.cs | 8 ++++++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/LibraryManager.Contracts/LibraryInstallationGoalState.cs b/src/LibraryManager.Contracts/LibraryInstallationGoalState.cs index b9e14779..3c6df96f 100644 --- a/src/LibraryManager.Contracts/LibraryInstallationGoalState.cs +++ b/src/LibraryManager.Contracts/LibraryInstallationGoalState.cs @@ -14,9 +14,10 @@ public class LibraryInstallationGoalState /// /// Initialize a new goal state from the desired installation state. /// - public LibraryInstallationGoalState(ILibraryInstallationState installationState) + public LibraryInstallationGoalState(ILibraryInstallationState installationState, Dictionary installedFiles) { InstallationState = installationState; + InstalledFiles = installedFiles; } /// @@ -27,7 +28,7 @@ public LibraryInstallationGoalState(ILibraryInstallationState installationState) /// /// Mapping from destination file to source file /// - public IDictionary InstalledFiles { get; } = new Dictionary(); + public IDictionary InstalledFiles { get; } /// /// Returns whether the goal is in an achieved state - that is, all files are up to date. diff --git a/src/LibraryManager/Providers/BaseProvider.cs b/src/LibraryManager/Providers/BaseProvider.cs index 8c8806b0..60955bd1 100644 --- a/src/LibraryManager/Providers/BaseProvider.cs +++ b/src/LibraryManager/Providers/BaseProvider.cs @@ -247,7 +247,6 @@ public async Task> GetInstallation private OperationResult GenerateGoalState(ILibraryInstallationState desiredState, ILibrary library) { - var goalState = new LibraryInstallationGoalState(desiredState); List errors = null; if (string.IsNullOrEmpty(desiredState.DestinationPath)) @@ -265,6 +264,7 @@ private OperationResult GenerateGoalState(ILibrary outFiles = FileGlobbingUtility.ExpandFileGlobs(desiredState.Files, library.Files.Keys); } + Dictionary installFiles = new(); if (library.GetInvalidFiles(outFiles.ToList()) is IReadOnlyList invalidFiles && invalidFiles.Count > 0) { @@ -287,9 +287,8 @@ private OperationResult GenerateGoalState(ILibrary string sourceFile = GetCachedFileLocalPath(desiredState, outFile); sourceFile = FileHelpers.NormalizePath(sourceFile); - // TODO: make goalState immutable // map destination back to the library-relative file it originated from - goalState.InstalledFiles.Add(destinationFile, sourceFile); + installFiles.Add(destinationFile, sourceFile); } if (errors is not null) @@ -297,6 +296,7 @@ private OperationResult GenerateGoalState(ILibrary return OperationResult.FromErrors([.. errors]); } + var goalState = new LibraryInstallationGoalState(desiredState, installFiles); return OperationResult.FromSuccess(goalState); } diff --git a/test/Microsoft.Web.LibraryManager.Vsix.Test/Shared/LibraryCommandServiceTest.cs b/test/Microsoft.Web.LibraryManager.Vsix.Test/Shared/LibraryCommandServiceTest.cs index 3713b70c..990a6de8 100644 --- a/test/Microsoft.Web.LibraryManager.Vsix.Test/Shared/LibraryCommandServiceTest.cs +++ b/test/Microsoft.Web.LibraryManager.Vsix.Test/Shared/LibraryCommandServiceTest.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System.Collections.Generic; using System.IO; using System.Text; using System.Threading; @@ -34,8 +35,11 @@ public async Task UninstallAsync_DeletesFilesFromDisk() Files = new[] { "test.js" }, DestinationPath = "testDestination", }; - var testGoalState = new LibraryInstallationGoalState(testInstallationState); - testGoalState.InstalledFiles.Add(Path.Combine(mockInteraction.WorkingDirectory, "testDestination", "test.js"), Path.Combine(mockInteraction.WorkingDirectory, "test.js")); + Dictionary installedFiles = new() + { + { Path.Combine(mockInteraction.WorkingDirectory, "testDestination", "test.js"), Path.Combine(mockInteraction.WorkingDirectory, "test.js")} + }; + var testGoalState = new LibraryInstallationGoalState(testInstallationState, installedFiles); var mockDependencies = new Dependencies(mockInteraction, new IProvider[] { new Mocks.Provider(mockInteraction)