Skip to content

Commit f45c7c3

Browse files
Add coverlet code coverage
1 parent 9368dbc commit f45c7c3

File tree

7 files changed

+74
-21
lines changed

7 files changed

+74
-21
lines changed

.github/workflows/test.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,10 @@ jobs:
2626
run: dotnet build src/UnityCommandLineLauncher.sln --no-restore
2727

2828
- name: Test
29-
run: dotnet test src/UnityCommandLineLauncher.sln --no-build
29+
run: dotnet test src/UnityCommandLineLauncher.sln --no-build --collect:"XPlat Code Coverage"
30+
31+
- name: Upload coverage to Codecov
32+
if: matrix.os == 'ubuntu-latest'
33+
uses: codecov/codecov-action@v5
34+
with:
35+
token: ${{ secrets.CODECOV_TOKEN }}

src/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22

33
bin/
44
obj/
5+
6+
ucll.tests/TestResults/

src/ucll.tests/OpenCommandTests.cs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
public class OpenCommandTests
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Spectre.Console.Cli.Testing;
3+
using Xunit.Abstractions;
4+
5+
public class OpenCommandTests(ITestOutputHelper output)
26
{
37
[Fact]
48
public async Task WaitForFileAsyncFindsExistingFile()
59
{
6-
await CreateTempProject(async tempDir =>
10+
await TestUtil.WithinTempDirectoryAsync(async tempDir =>
711
{
8-
string testFile = Path.Combine(tempDir, "test.sln");
12+
string testFile = Path.Combine(tempDir.FullName, "test.sln");
913
await File.WriteAllTextAsync(testFile, "content");
1014

11-
string result = await OpenCommand.WaitForFileAsync(tempDir, "*.sln");
15+
string result = await OpenCommand.WaitForFileAsync(tempDir.FullName, "*.sln");
1216

1317
Assert.Equal(testFile, result);
1418
});
@@ -17,11 +21,11 @@ await CreateTempProject(async tempDir =>
1721
[Fact]
1822
public async Task WaitForFileAsyncWaitsForNewFile()
1923
{
20-
await CreateTempProject(async tempDir =>
24+
await TestUtil.WithinTempDirectoryAsync(async tempDir =>
2125
{
22-
string testFile = Path.Combine(tempDir, "delayed.sln");
26+
string testFile = Path.Combine(tempDir.FullName, "delayed.sln");
2327

24-
Task<string> waitTask = OpenCommand.WaitForFileAsync(tempDir, "*.sln");
28+
Task<string> waitTask = OpenCommand.WaitForFileAsync(tempDir.FullName, "*.sln");
2529

2630
// Simulate Unity project generating the solution file.
2731
await Task.Delay(100);
@@ -33,18 +37,24 @@ await CreateTempProject(async tempDir =>
3337
});
3438
}
3539

36-
private static async Task CreateTempProject(Func<string, Task> action)
40+
[Fact]
41+
public void Open_DirectPath()
3742
{
38-
string tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
39-
Directory.CreateDirectory(tempDir);
43+
var services = new ServiceCollection();
44+
services.AddSingleton(PlatformSupport.Create());
45+
services.AddSingleton<UnityHub>();
4046

41-
try
42-
{
43-
await action.Invoke(tempDir);
44-
}
45-
finally
47+
var registrar = new TypeRegistrar(services);
48+
var app = new CommandAppTester(registrar);
49+
app.Configure(AppConfiguration.Build);
50+
51+
TestUtil.WithinTempDirectory(tempDir =>
4652
{
47-
Directory.Delete(tempDir, recursive: true);
48-
}
53+
string projectPath = Path.Combine(tempDir.FullName, "MyTestProject");
54+
app.Run("create", projectPath, "6000.0.64f1", "--minimal");
55+
var result = app.Run("open", projectPath, "--dry-run");
56+
output.WriteLine(result.Output);
57+
Assert.Equal(0, result.ExitCode);
58+
});
4959
}
5060
}

src/ucll.tests/TestUtil.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
internal static class TestUtil
2+
{
3+
public static async Task WithinTempDirectoryAsync(Func<DirectoryInfo, Task> action)
4+
{
5+
string path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
6+
DirectoryInfo directory = Directory.CreateDirectory(path);
7+
8+
try
9+
{
10+
await action.Invoke(directory);
11+
}
12+
finally
13+
{
14+
directory.Delete(recursive: true);
15+
}
16+
}
17+
18+
public static void WithinTempDirectory(Action<DirectoryInfo> action)
19+
{
20+
string path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
21+
DirectoryInfo directory = Directory.CreateDirectory(path);
22+
23+
try
24+
{
25+
action.Invoke(directory);
26+
}
27+
finally
28+
{
29+
directory.Delete(recursive: true);
30+
}
31+
}
32+
}

src/ucll.tests/ucll.tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9+
<PackageReference Include="coverlet.collector" Version="6.0.2">
10+
<PrivateAssets>all</PrivateAssets>
11+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
12+
</PackageReference>
913
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
1014
<PackageReference Include="Spectre.Console.Cli.Testing" Version="1.0.0-alpha.0.12" />
1115
<PackageReference Include="xunit" Version="2.9.3" />

src/ucll/Shared/SearchPathCommand.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ protected string ResolveSearchPath(string? searchPath, bool favoriteOnly)
1111

1212
if (!Directory.Exists(searchPath) && !File.Exists(searchPath))
1313
{
14-
WriteError($"'{searchPath}' does not exist.");
15-
Environment.Exit(1);
14+
throw new UserException($"'{searchPath}' does not exist.");
1615
}
1716
return searchPath;
1817
}

src/ucll/ucll.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
</ItemGroup>
3939

4040
<ItemGroup>
41-
<InternalsVisibleTo Include="ucll.Tests" />
41+
<InternalsVisibleTo Include="ucll.tests" />
4242
</ItemGroup>
4343

4444
<ItemGroup>

0 commit comments

Comments
 (0)