Skip to content

Commit

Permalink
Merge pull request #78 from floralvikings/main
Browse files Browse the repository at this point in the history
  • Loading branch information
jolexxa authored Sep 16, 2024
2 parents 7cbe52c + bdac4d6 commit 19a406d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public async Task DoesNothingIfNothingToInstall() {
addonsRepo.Setup(repo => repo.EnsureCacheAndAddonsDirectoriesExists());

addonsFileRepo.Setup(repo => repo.LoadAddonsFile(
projectPath, out addonsFilePath
projectPath, out addonsFilePath, null
)).Returns(addonsFile);

var state = new AddonsLogic.State.Unresolved();
Expand Down Expand Up @@ -80,7 +80,7 @@ public async Task EndsInCannotBeResolvedIfFatalErrorEncountered() {
addonsRepo.Setup(repo => repo.EnsureCacheAndAddonsDirectoriesExists());

addonsFileRepo.Setup(repo => repo.LoadAddonsFile(
projectPath, out addonsFilePath
projectPath, out addonsFilePath, null
)).Returns(addonsFile);

addonsRepo.Setup(repo => repo.ResolveUrl(entry, addonsFilePath))
Expand Down Expand Up @@ -137,7 +137,7 @@ public async Task DeterminesCanonicalAddonCorrectly() {
addonsRepo.Setup(repo => repo.EnsureCacheAndAddonsDirectoriesExists());

addonsFileRepo.Setup(repo => repo.LoadAddonsFile(
projectPath, out addonsFilePath
projectPath, out addonsFilePath, null
)).Returns(addonsFile);

addonsRepo.Setup(repo => repo.ResolveUrl(entry, addonsFilePath))
Expand Down Expand Up @@ -185,7 +185,7 @@ public async Task DeterminesCanonicalAddonCorrectly() {
var otherAddonsFilePath = "";
addonsFileRepo
.Setup(repo => repo.LoadAddonsFile(
pathToCachedAddon, out otherAddonsFilePath
pathToCachedAddon, out otherAddonsFilePath, null
))
.Returns(otherAddonAddonsFile);

Expand Down Expand Up @@ -233,7 +233,7 @@ public async Task InstallsSymlinkAddon() {
addonsRepo.Setup(repo => repo.EnsureCacheAndAddonsDirectoriesExists());

addonsFileRepo.Setup(repo => repo.LoadAddonsFile(
projectPath, out addonsFilePath
projectPath, out addonsFilePath, null
)).Returns(addonsFile);

addonsRepo.Setup(repo => repo.ResolveUrl(entry, addonsFilePath))
Expand All @@ -256,7 +256,7 @@ public async Task InstallsSymlinkAddon() {
var addonsAddonsFilePath = "";
addonsFileRepo
.Setup(repo => repo.LoadAddonsFile(
pathToCachedAddon, out addonsAddonsFilePath
pathToCachedAddon, out addonsAddonsFilePath, null
)
).Returns(new AddonsFile());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,32 @@ public void CreatesAddonsConfiguration() {
fileClient.Object.Combine(projectPath, addonsFile.CacheRelativePath)
);
}

[Fact]
public void LoadsAddonsFileWhenCalledWithAddonsFileNameArgumentShouldLoadCorrectFile() {
var fileClient = new Mock<IFileClient>();
var repo = new AddonsFileRepository(fileClient.Object);

var projectPath = "/";
string filename;
var addonsFileName = "foobar.json";

var addonsFile = new AddonsFile();

var expectedArgs = new[] { addonsFileName };
fileClient.Setup(client => client.ReadJsonFile(
projectPath,
It.Is<string[]>(
value => value.SequenceEqual(expectedArgs)
),
out filename,
It.IsAny<AddonsFile>()
)).Returns(addonsFile);

var result = repo.LoadAddonsFile(projectPath, out filename, addonsFileName);

fileClient.VerifyAll();

result.ShouldBe(addonsFile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ public class AddonsInstallCommand :
)]
public int? MaxDepth { get; init; }

[CommandOption(
"addons-file-name",
'a',
Description = "The file from to from which to install addons."
)]
public string? AddonsFileName { get; init; }

// If we have any top-level addons that are symlinks, we know we're going
// to need to elevate on Windows.
public bool IsWindowsElevationRequired =>
Expand Down Expand Up @@ -54,7 +61,9 @@ public async ValueTask ExecuteAsync(IConsole console) {

var state = await logic.Input(
new AddonsLogic.Input.Install(
ProjectPath: ExecutionContext.WorkingDir, MaxDepth: MaxDepth
ProjectPath: ExecutionContext.WorkingDir,
MaxDepth: MaxDepth,
AddonsFileName: AddonsFileName
)
);

Expand Down
4 changes: 2 additions & 2 deletions GodotEnv/src/features/addons/commands/install/AddonsLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Chickensoft.GodotEnv.Features.Addons.Commands;
[StateMachine]
public partial class AddonsLogic : LogicBlockAsync<AddonsLogic.State> {
public abstract record Input {
public readonly record struct Install(string ProjectPath, int? MaxDepth);
public readonly record struct Install(string ProjectPath, int? MaxDepth, string? AddonsFileName = null);
}

public abstract record State : StateLogic {
Expand All @@ -37,7 +37,7 @@ public async Task<State> On(Input.Install input) {
do {
var path = searchPaths.Dequeue();
var addonsFile = addonsFileRepo.LoadAddonsFile(
path, out var addonsFilePath
path, out var addonsFilePath, input.AddonsFileName
);

foreach ((var addonName, var addonEntry) in addonsFile.Addons) {
Expand Down
8 changes: 5 additions & 3 deletions GodotEnv/src/features/addons/domain/AddonsFileRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ public interface IAddonsFileRepository {
/// <param name="projectPath">Where to search for an addons file.</param>
/// <param name="filename">Filename that was loaded, or the first of the
/// possible addons file names.</param>
/// <param name="addonsFileName">Optional path to the addons file to load.
/// If unspecified will load "addons.jsonc" or "addons.json". </param>
/// <returns>Loaded addons file (or an empty one).</returns>
AddonsFile LoadAddonsFile(string projectPath, out string filename);
AddonsFile LoadAddonsFile(string projectPath, out string filename, string? addonsFileName = null);

/// <summary>
/// Creates an addons configuration object that represents the configuration
Expand Down Expand Up @@ -44,10 +46,10 @@ public AddonsFileRepository(IFileClient fileClient) {
FileClient = fileClient;
}

public AddonsFile LoadAddonsFile(string projectPath, out string filename) =>
public AddonsFile LoadAddonsFile(string projectPath, out string filename, string? addonsFileName = null) =>
FileClient.ReadJsonFile(
projectPath: projectPath,
possibleFilenames: _possibleFilenames,
possibleFilenames: addonsFileName == null ? _possibleFilenames : [addonsFileName],
filename: out filename,
defaultValue: new AddonsFile()
);
Expand Down

0 comments on commit 19a406d

Please sign in to comment.