Skip to content

Commit 0259c36

Browse files
committed
chore: fix test codes
1 parent 5247872 commit 0259c36

File tree

6 files changed

+61
-6
lines changed

6 files changed

+61
-6
lines changed

ConsoleAppFramework.sln

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ EndProject
88
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sandbox", "sandbox", "{A2CF2984-E8E2-48FC-B5A1-58D74A2467E6}"
99
EndProject
1010
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{AAD2D900-C305-4449-A9FC-6C7696FFEDFA}"
11+
ProjectSection(SolutionItems) = preProject
12+
tests\Directory.Build.props = tests\Directory.Build.props
13+
EndProjectSection
1114
EndProject
1215
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{6DF6534A-0F9D-44A4-BF89-AE1F3B243914}"
1316
ProjectSection(SolutionItems) = preProject

tests/ConsoleAppFramework.GeneratorTests/CSharpGeneratorRunner.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ public static void InitializeCompilation()
2424

2525
var references = AppDomain.CurrentDomain.GetAssemblies()
2626
.Where(x => !x.IsDynamic && !string.IsNullOrWhiteSpace(x.Location))
27-
.Select(x => MetadataReference.CreateFromFile(x.Location));
27+
.Select(x => MetadataReference.CreateFromFile(x.Location))
28+
.Concat([
29+
MetadataReference.CreateFromFile(typeof(Console).Assembly.Location), // System.Console.dll
30+
MetadataReference.CreateFromFile(typeof(IServiceProvider).Assembly.Location), // System.ComponentModel.dll
31+
MetadataReference.CreateFromFile(typeof(System.ComponentModel.DataAnnotations.RequiredAttribute).Assembly.Location), // System.ComponentModel.DataAnnotations
32+
MetadataReference.CreateFromFile(typeof(System.Text.Json.JsonDocument).Assembly.Location), // System.Text.Json.dll
33+
]);
2834

2935
var compilation = CSharpCompilation.Create("generatortest",
3036
references: references,

tests/ConsoleAppFramework.GeneratorTests/ConsoleAppBuilderTest.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
namespace ConsoleAppFramework.GeneratorTests;
22

3-
public class ConsoleAppBuilderTest(ITestOutputHelper output)
3+
public class ConsoleAppBuilderTest(ITestOutputHelper output) : IDisposable
44
{
5-
VerifyHelper verifier = new VerifyHelper(output, "CAF");
5+
VerifyHelper verifier = new VerifyHelper(output, "CAF");
6+
7+
public void Dispose() => Environment.ExitCode = 0;
68

79
[Fact]
810
public void BuilderRun()

tests/ConsoleAppFramework.GeneratorTests/HelpTest.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class HelpTest(ITestOutputHelper output)
1414
[Fact]
1515
public void Version()
1616
{
17-
var version = Assembly.GetEntryAssembly()?.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "1.0.0";
17+
var version = GetEntryAssemblyVersion();
1818

1919
verifier.Execute(code: $$"""
2020
ConsoleApp.Run(args, (int x, int y) => { });
@@ -39,7 +39,7 @@ public void Version()
3939
[Fact]
4040
public void VersionOnBuilder()
4141
{
42-
var version = Assembly.GetEntryAssembly()?.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "1.0.0";
42+
var version = GetEntryAssemblyVersion();
4343

4444
verifier.Execute(code: """
4545
var app = ConsoleApp.Create();
@@ -320,4 +320,21 @@ hello my world.
320320
321321
""");
322322
}
323+
324+
private static string GetEntryAssemblyVersion()
325+
{
326+
var version = Assembly.GetEntryAssembly()?.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
327+
328+
if (version == null)
329+
return "1.0.0";
330+
331+
// Trim SourceRevisionId (SourceLink feature is enabled by default when using .NET SDK 8 or later)
332+
var i = version.IndexOf('+');
333+
if (i != -1)
334+
{
335+
version = version.Substring(0, i);
336+
}
337+
338+
return version;
339+
}
323340
}

tests/ConsoleAppFramework.GeneratorTests/RunTest.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
namespace ConsoleAppFramework.GeneratorTests;
22

3-
public class Test(ITestOutputHelper output)
3+
public class Test(ITestOutputHelper output) : IDisposable
44
{
55
VerifyHelper verifier = new VerifyHelper(output, "CAF");
66

7+
public void Dispose() => Environment.ExitCode = 0;
8+
79
[Fact]
810
public void SyncRun()
911
{

tests/Directory.Build.props

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project>
2+
<Import Project="$([MSBuild]::GetPathOfFileAbove('$(MSBuildThisFile)', '$(MSBuildThisFileDirectory)../'))" />
3+
4+
<PropertyGroup>
5+
<!-- Enable Microsoft.Testing.Platform -->
6+
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
7+
8+
<!-- Use Microsoft.Testing.Platform exe entry point instead of xUnit.net -->
9+
<UseMicrosoftTestingPlatformRunner>true</UseMicrosoftTestingPlatformRunner>
10+
11+
<!-- Output all output logs to console -->
12+
<TestingPlatformCaptureOutput>false</TestingPlatformCaptureOutput>
13+
</PropertyGroup>
14+
15+
<PropertyGroup>
16+
<!-- Show xUnit.net headers and information -->
17+
<TestingPlatformCommandLineArguments>$(TestingPlatformCommandLineArguments) --xunit-info</TestingPlatformCommandLineArguments>
18+
19+
<!-- Set TestResults output directory -->
20+
<TestingPlatformCommandLineArguments>$(TestingPlatformCommandLineArguments) --results-directory "$(MSBuildThisFileDirectory)TestResults"</TestingPlatformCommandLineArguments>
21+
22+
<!-- Ignore exit code 8 (the test session run zero tests) -->
23+
<TestingPlatformCommandLineArguments>$(TestingPlatformCommandLineArguments) --ignore-exit-code 8</TestingPlatformCommandLineArguments>
24+
</PropertyGroup>
25+
</Project>

0 commit comments

Comments
 (0)