Skip to content

Commit d53986e

Browse files
committed
make goalstate immutable
1 parent 4e665b3 commit d53986e

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
@@ -247,7 +247,6 @@ public async Task<OperationResult<LibraryInstallationGoalState>> GetInstallation
247247

248248
private OperationResult<LibraryInstallationGoalState> GenerateGoalState(ILibraryInstallationState desiredState, ILibrary library)
249249
{
250-
var goalState = new LibraryInstallationGoalState(desiredState);
251250
List<IError> errors = null;
252251

253252
if (string.IsNullOrEmpty(desiredState.DestinationPath))
@@ -265,6 +264,7 @@ private OperationResult<LibraryInstallationGoalState> GenerateGoalState(ILibrary
265264
outFiles = FileGlobbingUtility.ExpandFileGlobs(desiredState.Files, library.Files.Keys);
266265
}
267266

267+
Dictionary<string, string> installFiles = new();
268268
if (library.GetInvalidFiles(outFiles.ToList()) is IReadOnlyList<string> invalidFiles
269269
&& invalidFiles.Count > 0)
270270
{
@@ -287,16 +287,16 @@ private OperationResult<LibraryInstallationGoalState> GenerateGoalState(ILibrary
287287
string sourceFile = GetCachedFileLocalPath(desiredState, outFile);
288288
sourceFile = FileHelpers.NormalizePath(sourceFile);
289289

290-
// TODO: make goalState immutable
291290
// map destination back to the library-relative file it originated from
292-
goalState.InstalledFiles.Add(destinationFile, sourceFile);
291+
installFiles.Add(destinationFile, sourceFile);
293292
}
294293

295294
if (errors is not null)
296295
{
297296
return OperationResult<LibraryInstallationGoalState>.FromErrors([.. errors]);
298297
}
299298

299+
var goalState = new LibraryInstallationGoalState(desiredState, installFiles);
300300
return OperationResult<LibraryInstallationGoalState>.FromSuccess(goalState);
301301
}
302302

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)