Skip to content

Commit a30dc6b

Browse files
committed
make goalstate immutable
1 parent 31599c5 commit a30dc6b

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

src/LibraryManager.Contracts/LibraryInstallationGoalState.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ public class LibraryInstallationGoalState
1414
/// <summary>
1515
/// Initialize a new goal state from the desired installation state.
1616
/// </summary>
17-
public LibraryInstallationGoalState(ILibraryInstallationState installationState)
17+
public LibraryInstallationGoalState(ILibraryInstallationState installationState, Dictionary<string, string> installedFiles)
1818
{
1919
InstallationState = installationState;
20+
InstalledFiles = installedFiles;
2021
}
2122

2223
/// <summary>
@@ -27,7 +28,7 @@ public LibraryInstallationGoalState(ILibraryInstallationState installationState)
2728
/// <summary>
2829
/// Mapping from destination file to source file
2930
/// </summary>
30-
public IDictionary<string, string> InstalledFiles { get; } = new Dictionary<string, string>();
31+
public IDictionary<string, string> InstalledFiles { get; }
3132

3233
/// <summary>
3334
/// Returns whether the goal is in an achieved state - that is, all files are up to date.

src/LibraryManager/Providers/BaseProvider.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,6 @@ public async Task<OperationResult<LibraryInstallationGoalState>> GetInstallation
235235

236236
private OperationResult<LibraryInstallationGoalState> GenerateGoalState(ILibraryInstallationState desiredState, ILibrary library)
237237
{
238-
var goalState = new LibraryInstallationGoalState(desiredState);
239238
List<IError> errors = null;
240239

241240
if (string.IsNullOrEmpty(desiredState.DestinationPath))
@@ -253,6 +252,7 @@ private OperationResult<LibraryInstallationGoalState> GenerateGoalState(ILibrary
253252
outFiles = FileGlobbingUtility.ExpandFileGlobs(desiredState.Files, library.Files.Keys);
254253
}
255254

255+
Dictionary<string, string> installFiles = new();
256256
if (library.GetInvalidFiles(outFiles.ToList()) is IReadOnlyList<string> invalidFiles
257257
&& invalidFiles.Count > 0)
258258
{
@@ -275,16 +275,16 @@ private OperationResult<LibraryInstallationGoalState> GenerateGoalState(ILibrary
275275
string sourceFile = GetCachedFileLocalPath(desiredState, outFile);
276276
sourceFile = FileHelpers.NormalizePath(sourceFile);
277277

278-
// TODO: make goalState immutable
279278
// map destination back to the library-relative file it originated from
280-
goalState.InstalledFiles.Add(destinationFile, sourceFile);
279+
installFiles.Add(destinationFile, sourceFile);
281280
}
282281

283282
if (errors is not null)
284283
{
285284
return OperationResult<LibraryInstallationGoalState>.FromErrors([.. errors]);
286285
}
287286

287+
var goalState = new LibraryInstallationGoalState(desiredState, installFiles);
288288
return OperationResult<LibraryInstallationGoalState>.FromSuccess(goalState);
289289
}
290290

test/Microsoft.Web.LibraryManager.Vsix.Test/Shared/LibraryCommandServiceTest.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System.Collections.Generic;
45
using System.IO;
56
using System.Text;
67
using System.Threading;
@@ -34,8 +35,11 @@ public async Task UninstallAsync_DeletesFilesFromDisk()
3435
Files = new[] { "test.js" },
3536
DestinationPath = "testDestination",
3637
};
37-
var testGoalState = new LibraryInstallationGoalState(testInstallationState);
38-
testGoalState.InstalledFiles.Add(Path.Combine(mockInteraction.WorkingDirectory, "testDestination", "test.js"), Path.Combine(mockInteraction.WorkingDirectory, "test.js"));
38+
Dictionary<string, string> installedFiles = new()
39+
{
40+
{ Path.Combine(mockInteraction.WorkingDirectory, "testDestination", "test.js"), Path.Combine(mockInteraction.WorkingDirectory, "test.js")}
41+
};
42+
var testGoalState = new LibraryInstallationGoalState(testInstallationState, installedFiles);
3943
var mockDependencies = new Dependencies(mockInteraction, new IProvider[]
4044
{
4145
new Mocks.Provider(mockInteraction)

0 commit comments

Comments
 (0)