Skip to content

Commit 511e725

Browse files
authored
Merge pull request #168 from filzrev/chore-update-tests
Update tests related projects
2 parents e78109b + c4a86c0 commit 511e725

14 files changed

+93
-62
lines changed

ConsoleAppFramework.sln

+3
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

sandbox/GeneratorSandbox/Program.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
// services.Configure<PositionOptions>(configuration.GetSection("Position"));
3232
// });
3333

34-
35-
args = ["run", "--project", "foo.csproj", "--", "--foo", "100", "--bar", "bazbaz"];
34+
//// Uncomment following line to overwrite args.
35+
// args = ["run", "--project", "foo.csproj", "--", "--foo", "100", "--bar", "bazbaz"];
3636

3737
// dotnet run --project foo.csproj -- --foo 100 --bar bazbaz
3838

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"profiles": {
3+
"Default": {
4+
"commandName": "Project",
5+
"commandLineArgs": "run --project foo.csproj -- --foo 100 --bar bazbaz"
6+
},
7+
"ShowVersion": {
8+
"commandName": "Project",
9+
"commandLineArgs": "--version"
10+
},
11+
"ShowRootCommandHelp": {
12+
"commandName": "Project",
13+
"commandLineArgs": "--help"
14+
},
15+
"ShowRunCommandHelp": {
16+
"commandName": "Project",
17+
"commandLineArgs": "run --help"
18+
}
19+
}
20+
}

tests/ConsoleAppFramework.GeneratorTests/ArrayParseTest.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using Xunit.Abstractions;
2-
31
namespace ConsoleAppFramework.GeneratorTests
42
{
53
public class ArrayParseTest(ITestOutputHelper output)

tests/ConsoleAppFramework.GeneratorTests/CSharpGeneratorRunner.cs

+12-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Diagnostics.CodeAnalysis;
77
using System.Runtime.CompilerServices;
88
using System.Runtime.Loader;
9-
using Xunit.Abstractions;
109

1110
public static class CSharpGeneratorRunner
1211
{
@@ -25,7 +24,13 @@ public static void InitializeCompilation()
2524

2625
var references = AppDomain.CurrentDomain.GetAssemblies()
2726
.Where(x => !x.IsDynamic && !string.IsNullOrWhiteSpace(x.Location))
28-
.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+
]);
2934

3035
var compilation = CSharpCompilation.Create("generatortest",
3136
references: references,
@@ -144,7 +149,7 @@ public class VerifyHelper(ITestOutputHelper output, string idPrefix)
144149

145150
public void Ok([StringSyntax("C#-test")] string code, [CallerArgumentExpression("code")] string? codeExpr = null)
146151
{
147-
output.WriteLine(codeExpr);
152+
output.WriteLine(codeExpr!);
148153

149154
var (compilation, diagnostics) = CSharpGeneratorRunner.RunGenerator(code);
150155
foreach (var item in diagnostics)
@@ -158,7 +163,7 @@ public void Ok([StringSyntax("C#-test")] string code, [CallerArgumentExpression(
158163

159164
public void Verify(int id, [StringSyntax("C#-test")] string code, string diagnosticsCodeSpan, [CallerArgumentExpression("code")] string? codeExpr = null)
160165
{
161-
output.WriteLine(codeExpr);
166+
output.WriteLine(codeExpr!);
162167

163168
var (compilation, diagnostics) = CSharpGeneratorRunner.RunGenerator(code);
164169
foreach (var item in diagnostics)
@@ -176,7 +181,7 @@ public void Verify(int id, [StringSyntax("C#-test")] string code, string diagnos
176181

177182
public (string, string)[] Verify([StringSyntax("C#-test")] string code, [CallerArgumentExpression("code")] string? codeExpr = null)
178183
{
179-
output.WriteLine(codeExpr);
184+
output.WriteLine(codeExpr!);
180185

181186
var (compilation, diagnostics) = CSharpGeneratorRunner.RunGenerator(code);
182187
OutputGeneratedCode(compilation);
@@ -187,7 +192,7 @@ public void Verify(int id, [StringSyntax("C#-test")] string code, string diagnos
187192

188193
public void Execute([StringSyntax("C#-test")] string code, string args, string expected, [CallerArgumentExpression("code")] string? codeExpr = null)
189194
{
190-
output.WriteLine(codeExpr);
195+
output.WriteLine(codeExpr!);
191196

192197
var (compilation, diagnostics, stdout) = CSharpGeneratorRunner.CompileAndExecute(code, args == "" ? [] : args.Split(' '));
193198
foreach (var item in diagnostics)
@@ -201,7 +206,7 @@ public void Execute([StringSyntax("C#-test")] string code, string args, string e
201206

202207
public string Error([StringSyntax("C#-test")] string code, string args, [CallerArgumentExpression("code")] string? codeExpr = null)
203208
{
204-
output.WriteLine(codeExpr);
209+
output.WriteLine(codeExpr!);
205210

206211
var (compilation, diagnostics, stdout) = CSharpGeneratorRunner.CompileAndExecute(code, args == "" ? [] : args.Split(' '));
207212
foreach (var item in diagnostics)

tests/ConsoleAppFramework.GeneratorTests/ConsoleAppBuilderTest.cs

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
using Microsoft.VisualStudio.TestPlatform.Utilities;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
7-
using Xunit.Abstractions;
8-
91
namespace ConsoleAppFramework.GeneratorTests;
102

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

159
[Fact]
1610
public void BuilderRun()

tests/ConsoleAppFramework.GeneratorTests/ConsoleAppFramework.GeneratorTests.csproj

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
45
<TargetFramework>net9.0</TargetFramework>
56
<ImplicitUsings>enable</ImplicitUsings>
67
<Nullable>enable</Nullable>
@@ -10,11 +11,11 @@
1011
</PropertyGroup>
1112

1213
<ItemGroup>
13-
<PackageReference Include="Shouldly" Version="4.2.1" />
14+
<PackageReference Include="Shouldly" Version="4.3.0" />
1415
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0" />
15-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
16-
<PackageReference Include="xunit" Version="2.4.2" />
17-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
16+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
17+
<PackageReference Include="xunit.v3" Version="1.1.0" />
18+
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.2">
1819
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1920
<PrivateAssets>all</PrivateAssets>
2021
</PackageReference>
@@ -26,7 +27,6 @@
2627

2728
<ItemGroup>
2829
<Using Include="Xunit" />
29-
<Using Include="Xunit.Abstractions" />
3030
<Using Include="Shouldly" />
3131
</ItemGroup>
3232
</Project>

tests/ConsoleAppFramework.GeneratorTests/DiagnosticsTest.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using Xunit.Abstractions;
2-
31
namespace ConsoleAppFramework.GeneratorTests;
42

53
public class DiagnosticsTest(ITestOutputHelper output)

tests/ConsoleAppFramework.GeneratorTests/FilterTest.cs

-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
using Xunit.Abstractions;
7-
81
namespace ConsoleAppFramework.GeneratorTests;
92

103
public class FilterTest(ITestOutputHelper output)

tests/ConsoleAppFramework.GeneratorTests/HelpTest.cs

+19-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Reflection;
55
using System.Text;
66
using System.Threading.Tasks;
7-
using Xunit.Abstractions;
87

98
namespace ConsoleAppFramework.GeneratorTests;
109

@@ -15,7 +14,7 @@ public class HelpTest(ITestOutputHelper output)
1514
[Fact]
1615
public void Version()
1716
{
18-
var version = Assembly.GetEntryAssembly()?.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "1.0.0";
17+
var version = GetEntryAssemblyVersion();
1918

2019
verifier.Execute(code: $$"""
2120
ConsoleApp.Run(args, (int x, int y) => { });
@@ -40,7 +39,7 @@ public void Version()
4039
[Fact]
4140
public void VersionOnBuilder()
4241
{
43-
var version = Assembly.GetEntryAssembly()?.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "1.0.0";
42+
var version = GetEntryAssemblyVersion();
4443

4544
verifier.Execute(code: """
4645
var app = ConsoleApp.Create();
@@ -321,4 +320,21 @@ hello my world.
321320
322321
""");
323322
}
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+
}
324340
}

tests/ConsoleAppFramework.GeneratorTests/NameConverterTest.cs

-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
using Xunit.Abstractions;
7-
81
namespace ConsoleAppFramework.GeneratorTests;
92

103
public class NameConverterTest(ITestOutputHelper output)

tests/ConsoleAppFramework.GeneratorTests/RunTest.cs

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1-
using Microsoft.CodeAnalysis;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.ComponentModel.DataAnnotations;
5-
using System.Linq;
6-
using System.Text;
7-
using System.Threading.Tasks;
8-
using Xunit.Abstractions;
9-
101
namespace ConsoleAppFramework.GeneratorTests;
112

12-
public class Test(ITestOutputHelper output)
3+
public class Test(ITestOutputHelper output) : IDisposable
134
{
145
VerifyHelper verifier = new VerifyHelper(output, "CAF");
156

7+
public void Dispose() => Environment.ExitCode = 0;
8+
169
[Fact]
1710
public void SyncRun()
1811
{

tests/ConsoleAppFramework.GeneratorTests/SubCommandTest.cs

-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
using Xunit.Abstractions;
7-
81
namespace ConsoleAppFramework.GeneratorTests;
92

103
public class SubCommandTest(ITestOutputHelper output)

tests/Directory.Build.props

+25
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)