Skip to content

Commit

Permalink
make goalstate immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmylewis committed May 19, 2024
1 parent 4e665b3 commit d53986e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/LibraryManager.Contracts/LibraryInstallationGoalState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ public class LibraryInstallationGoalState
/// <summary>
/// Initialize a new goal state from the desired installation state.
/// </summary>
public LibraryInstallationGoalState(ILibraryInstallationState installationState)
public LibraryInstallationGoalState(ILibraryInstallationState installationState, Dictionary<string, string> installedFiles)
{
InstallationState = installationState;
InstalledFiles = installedFiles;
}

/// <summary>
Expand All @@ -27,7 +28,7 @@ public LibraryInstallationGoalState(ILibraryInstallationState installationState)
/// <summary>
/// Mapping from destination file to source file
/// </summary>
public IDictionary<string, string> InstalledFiles { get; } = new Dictionary<string, string>();
public IDictionary<string, string> InstalledFiles { get; }

/// <summary>
/// Returns whether the goal is in an achieved state - that is, all files are up to date.
Expand Down
6 changes: 3 additions & 3 deletions src/LibraryManager/Providers/BaseProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ public async Task<OperationResult<LibraryInstallationGoalState>> GetInstallation

private OperationResult<LibraryInstallationGoalState> GenerateGoalState(ILibraryInstallationState desiredState, ILibrary library)
{
var goalState = new LibraryInstallationGoalState(desiredState);
List<IError> errors = null;

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

Dictionary<string, string> installFiles = new();
if (library.GetInvalidFiles(outFiles.ToList()) is IReadOnlyList<string> invalidFiles
&& invalidFiles.Count > 0)
{
Expand All @@ -287,16 +287,16 @@ private OperationResult<LibraryInstallationGoalState> 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)
{
return OperationResult<LibraryInstallationGoalState>.FromErrors([.. errors]);
}

var goalState = new LibraryInstallationGoalState(desiredState, installFiles);
return OperationResult<LibraryInstallationGoalState>.FromSuccess(goalState);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<string, string> 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)
Expand Down

0 comments on commit d53986e

Please sign in to comment.