Skip to content

Commit

Permalink
refactor: split external interfaces to independent Lip.Context package
Browse files Browse the repository at this point in the history
  • Loading branch information
futrime committed Jan 22, 2025
1 parent b58e7c6 commit bb24aef
Show file tree
Hide file tree
Showing 21 changed files with 278 additions and 208 deletions.
8 changes: 8 additions & 0 deletions Lip.Context/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Auto detect text files and perform LF normalization
* text=auto

*.cs text diff=csharp
*.cshtml text diff=html
*.csx text diff=csharp
*.sln text eol=crlf
*.csproj text eol=crlf
37 changes: 37 additions & 0 deletions Lip.Context/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
*.swp
*.*~
project.lock.json
.DS_Store
*.pyc
nupkg/

# Visual Studio Code
.vscode

# Rider
.idea

# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates

# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
build/
bld/
[Bb]in/
[Oo]bj/
[Oo]ut/
msbuild.log
msbuild.err
msbuild.wrn

# Visual Studio 2015
.vs/
14 changes: 14 additions & 0 deletions Lip.Context/IContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.IO.Abstractions;
using Microsoft.Extensions.Logging;

namespace Lip.Context;

public interface IContext
{
IDownloader Downloader { get; }
IFileSystem FileSystem { get; }
IGit? Git { get; }
ILogger Logger { get; }
string RuntimeIdentifier { get; }
IUserInteraction UserInteraction { get; }
}
6 changes: 6 additions & 0 deletions Lip.Context/IDownloader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Lip.Context;

public interface IDownloader
{
Task DownloadFile(Uri url, string destinationPath);
}
6 changes: 6 additions & 0 deletions Lip.Context/IGit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Lip.Context;

public interface IGit
{
Task Clone(string repository, string directory, int? depth = null);
}
10 changes: 5 additions & 5 deletions Lip/IUserInteraction.cs → Lip.Context/IUserInteraction.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Lip;
namespace Lip.Context;

/// <summary>
/// Represents a user interaction interface.
Expand All @@ -16,22 +16,22 @@ public interface IUserInteraction
/// Prompts user for text input.
/// </summary>
/// <param name="format">The prompt message</param>
/// <param name="defaultValue">Optional default value</param>
/// <returns>User input as string</returns>
Task<string?> PromptForInput(string format, params object[] args);

/// <summary>
/// Prompts user to select from multiple options.
/// </summary>
/// <param name="format">The prompt message</param>
/// <param name="options">Available options</param>
/// <param name="format">The prompt message</param>
/// <returns>Selected option</returns>
Task<string> PromptForSelection(IEnumerable<string> options, string format, params object[] args);

/// <summary>
/// Shows progress for long-running operations.
/// </summary>
/// <param name="format">Progress message</param>
/// <param name="id">Progress ID</param>
/// <param name="progress">Progress value (0.0-1.0)</param>
Task UpdateProgress(float progress, string format, params object[] args);
/// <param name="format">Progress message</param>
Task UpdateProgress(string id, float progress, string format, params object[] args);
}
14 changes: 14 additions & 0 deletions Lip.Context/Lip.Context.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.1" />
<PackageReference Include="TestableIO.System.IO.Abstractions" Version="21.2.1" />
</ItemGroup>

</Project>
107 changes: 48 additions & 59 deletions Lip.Tests/LipConfigTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO.Abstractions.TestingHelpers;
using Lip.Context;
using Microsoft.Extensions.Logging;
using Moq;

Expand All @@ -24,10 +25,10 @@ public async Task ConfigDelete_SingleItem_ResetsToDefault()
{ s_runtimeConfigPath, new MockFileData(initialRuntimeConfig.ToBytes()) },
});

Mock<ILogger> logger = new();
Mock<IUserInteraction> userInteraction = new();
Mock<IContext> context = new();
context.SetupGet(c => c.FileSystem).Returns(fileSystem);

Lip lip = new(initialRuntimeConfig, fileSystem, logger.Object, userInteraction.Object);
Lip lip = new(initialRuntimeConfig, context.Object);

// Act.
await lip.ConfigDelete(["color"], new Lip.ConfigDeleteArgs());
Expand Down Expand Up @@ -66,10 +67,10 @@ public async Task ConfigDelete_MultipleItems_ResetsToDefault()
{ s_runtimeConfigPath, new MockFileData(initialRuntimeConfig.ToBytes()) },
});

Mock<ILogger> logger = new();
Mock<IUserInteraction> userInteraction = new();
Mock<IContext> context = new();
context.SetupGet(c => c.FileSystem).Returns(fileSystem);

Lip lip = new(initialRuntimeConfig, fileSystem, logger.Object, userInteraction.Object);
Lip lip = new(initialRuntimeConfig, context.Object);

// Act.
await lip.ConfigDelete(["color", "git"], new Lip.ConfigDeleteArgs());
Expand Down Expand Up @@ -97,11 +98,10 @@ public async Task ConfigDelete_EmptyKeys_ThrowsArgumentException()
{
// Arrange.
RuntimeConfig initialRuntimeConfig = new();
MockFileSystem fileSystem = new();
Mock<ILogger> logger = new();
Mock<IUserInteraction> userInteraction = new();

Lip lip = new(initialRuntimeConfig, fileSystem, logger.Object, userInteraction.Object);
Mock<IContext> context = new();

Lip lip = new(initialRuntimeConfig, context.Object);

// Act & Assert.
ArgumentException exception = await Assert.ThrowsAsync<ArgumentException>(
Expand All @@ -114,11 +114,10 @@ public async Task ConfigDelete_UnknownKey_ThrowsArgumentException()
{
// Arrange.
RuntimeConfig initialRuntimeConfig = new();
MockFileSystem fileSystem = new();
Mock<ILogger> logger = new();
Mock<IUserInteraction> userInteraction = new();

Lip lip = new(initialRuntimeConfig, fileSystem, logger.Object, userInteraction.Object);
Mock<IContext> context = new();

Lip lip = new(initialRuntimeConfig, context.Object);

// Act & Assert.
ArgumentException exception = await Assert.ThrowsAsync<ArgumentException>(
Expand All @@ -131,11 +130,10 @@ public async Task ConfigDelete_PartialUnknownItem_ThrowsArgumentException()
{
// Arrange.
RuntimeConfig initialRuntimeConfig = new();
MockFileSystem fileSystem = new();
Mock<ILogger> logger = new();
Mock<IUserInteraction> userInteraction = new();

Lip lip = new(initialRuntimeConfig, fileSystem, logger.Object, userInteraction.Object);
Mock<IContext> context = new();

Lip lip = new(initialRuntimeConfig, context.Object);

// Act & Assert.
ArgumentException argumentException = await Assert.ThrowsAsync<ArgumentException>(
Expand All @@ -154,10 +152,10 @@ public void ConfigGet_SingleItem_Passes()
{ s_runtimeConfigPath, new MockFileData(initialRuntimeConfig.ToBytes()) },
});

Mock<ILogger> logger = new();
Mock<IUserInteraction> userInteraction = new();
Mock<IContext> context = new();
context.SetupGet(c => c.FileSystem).Returns(fileSystem);

Lip lip = new(initialRuntimeConfig, fileSystem, logger.Object, userInteraction.Object);
Lip lip = new(initialRuntimeConfig, context.Object);

// Act.
Dictionary<string, string> result = lip.ConfigGet(["cache"], new Lip.ConfigGetArgs());
Expand All @@ -182,10 +180,10 @@ public void ConfigGet_MultipleItems_Passes()
{ s_runtimeConfigPath, new MockFileData(initialRuntimeConfig.ToBytes()) },
});

Mock<ILogger> logger = new();
Mock<IUserInteraction> userInteraction = new();
Mock<IContext> context = new();
context.SetupGet(c => c.FileSystem).Returns(fileSystem);

Lip lip = new(initialRuntimeConfig, fileSystem, logger.Object, userInteraction.Object);
Lip lip = new(initialRuntimeConfig, context.Object);

// Act.
Dictionary<string, string> result = lip.ConfigGet(
Expand All @@ -203,11 +201,10 @@ public void ConfigGet_UnknownKey_ThrowsArgumentException()
{
// Arrange.
RuntimeConfig initialRuntimeConfig = new();
MockFileSystem fileSystem = new();
Mock<ILogger> logger = new();
Mock<IUserInteraction> userInteraction = new();

Lip lip = new(initialRuntimeConfig, fileSystem, logger.Object, userInteraction.Object);
Mock<IContext> context = new();

Lip lip = new(initialRuntimeConfig, context.Object);

// Act & Assert.
ArgumentException exception = Assert.Throws<ArgumentException>(
Expand All @@ -220,11 +217,10 @@ public void ConfigGet_PartialUnknownItem_ThrowsArgumentException()
{
// Arrange.
RuntimeConfig initialRuntimeConfig = new();
MockFileSystem fileSystem = new();
Mock<ILogger> logger = new();
Mock<IUserInteraction> userInteraction = new();

Lip lip = new(initialRuntimeConfig, fileSystem, logger.Object, userInteraction.Object);
Mock<IContext> context = new();

Lip lip = new(initialRuntimeConfig, context.Object);

// Act & Assert.
ArgumentException exception = Assert.Throws<ArgumentException>(
Expand All @@ -237,11 +233,10 @@ public void ConfigGet_EmptyKeys_ThrowsArgumentException()
{
// Arrange.
RuntimeConfig initialRuntimeConfig = new();
MockFileSystem fileSystem = new();
Mock<ILogger> logger = new();
Mock<IUserInteraction> userInteraction = new();

Lip lip = new(initialRuntimeConfig, fileSystem, logger.Object, userInteraction.Object);
Mock<IContext> context = new();

Lip lip = new(initialRuntimeConfig, context.Object);

// Act & Assert.
ArgumentException exception = Assert.Throws<ArgumentException>(
Expand All @@ -266,11 +261,9 @@ public void ConfigList_ReturnsAllConfigurations()
ScriptShell = "/custom/shell"
};

MockFileSystem fileSystem = new();
Mock<ILogger> logger = new();
Mock<IUserInteraction> userInteraction = new();
Mock<IContext> context = new();

Lip lip = new(initialRuntimeConfig, fileSystem, logger.Object, userInteraction.Object);
Lip lip = new(initialRuntimeConfig, context.Object);

// Act.
Dictionary<string, string> result = lip.ConfigList(new Lip.ConfigGetArgs());
Expand Down Expand Up @@ -299,11 +292,10 @@ public async Task ConfigSet_SingleItem_Passes()
{ s_runtimeConfigPath, new MockFileData(initialRuntimeConfig.ToBytes()) },
});

Mock<ILogger> logger = new();

Mock<IUserInteraction> userInteraction = new();
Mock<IContext> context = new();
context.SetupGet(c => c.FileSystem).Returns(fileSystem);

Lip lip = new(new(), fileSystem, logger.Object, userInteraction.Object);
Lip lip = new(initialRuntimeConfig, context.Object);

Dictionary<string, string> keyValuePairs = new()
{
Expand Down Expand Up @@ -342,11 +334,10 @@ public async Task ConfigSet_MultipleItems_Passes()
{ s_runtimeConfigPath, new MockFileData(initialRuntimeConfig.ToBytes()) },
});

Mock<ILogger> logger = new();

Mock<IUserInteraction> userInteraction = new();
Mock<IContext> context = new();
context.SetupGet(c => c.FileSystem).Returns(fileSystem);

Lip lip = new(new(), fileSystem, logger.Object, userInteraction.Object);
Lip lip = new(initialRuntimeConfig, context.Object);

Dictionary<string, string> keyValuePairs = new()
{
Expand Down Expand Up @@ -393,11 +384,10 @@ public async Task ConfigSet_NoItems_ThrowsArgumentException()
{ s_runtimeConfigPath, new MockFileData(initialRuntimeConfig.ToBytes()) },
});

Mock<ILogger> logger = new();
Mock<IContext> context = new();
context.SetupGet(c => c.FileSystem).Returns(fileSystem);

Mock<IUserInteraction> userInteraction = new();

Lip lip = new(new(), fileSystem, logger.Object, userInteraction.Object);
Lip lip = new(initialRuntimeConfig, context.Object);

Dictionary<string, string> keyValuePairs = [];

Expand All @@ -419,11 +409,10 @@ public async Task ConfigSet_UnknownItem_ThrowsArgumentException()
{ s_runtimeConfigPath, new MockFileData(initialRuntimeConfig.ToBytes()) },
});

Mock<ILogger> logger = new();

Mock<IUserInteraction> userInteraction = new();
Mock<IContext> context = new();
context.SetupGet(c => c.FileSystem).Returns(fileSystem);

Lip lip = new(new(), fileSystem, logger.Object, userInteraction.Object);
Lip lip = new(initialRuntimeConfig, context.Object);

Dictionary<string, string> keyValuePairs = new()
{
Expand All @@ -449,10 +438,10 @@ public async Task ConfigSet_PartialUnknownItem_ThrowsArgumentException()
{ s_runtimeConfigPath, new MockFileData(initialRuntimeConfig.ToBytes()) },
});

Mock<ILogger> logger = new();
Mock<IUserInteraction> userInteraction = new();
Mock<IContext> context = new();
context.SetupGet(c => c.FileSystem).Returns(fileSystem);

Lip lip = new(new(), fileSystem, logger.Object, userInteraction.Object);
Lip lip = new(initialRuntimeConfig, context.Object);

Dictionary<string, string> keyValuePairs = new()
{
Expand Down
Loading

0 comments on commit bb24aef

Please sign in to comment.