Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ConsoleAppFramework.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sandbox", "sandbox", "{A2CF2984-E8E2-48FC-B5A1-58D74A2467E6}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{AAD2D900-C305-4449-A9FC-6C7696FFEDFA}"
ProjectSection(SolutionItems) = preProject
tests\Directory.Build.props = tests\Directory.Build.props
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{6DF6534A-0F9D-44A4-BF89-AE1F3B243914}"
ProjectSection(SolutionItems) = preProject
Expand Down
4 changes: 2 additions & 2 deletions sandbox/GeneratorSandbox/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
// services.Configure<PositionOptions>(configuration.GetSection("Position"));
// });


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

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

Expand Down
20 changes: 20 additions & 0 deletions sandbox/GeneratorSandbox/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"profiles": {
"Default": {
"commandName": "Project",
"commandLineArgs": "run --project foo.csproj -- --foo 100 --bar bazbaz"
},
"ShowVersion": {
"commandName": "Project",
"commandLineArgs": "--version"
},
"ShowRootCommandHelp": {
"commandName": "Project",
"commandLineArgs": "--help"
},
"ShowRunCommandHelp": {
"commandName": "Project",
"commandLineArgs": "run --help"
}
}
}
2 changes: 0 additions & 2 deletions tests/ConsoleAppFramework.GeneratorTests/ArrayParseTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using Xunit.Abstractions;

namespace ConsoleAppFramework.GeneratorTests
{
public class ArrayParseTest(ITestOutputHelper output)
Expand Down
19 changes: 12 additions & 7 deletions tests/ConsoleAppFramework.GeneratorTests/CSharpGeneratorRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.Loader;
using Xunit.Abstractions;

public static class CSharpGeneratorRunner
{
Expand All @@ -25,7 +24,13 @@ public static void InitializeCompilation()

var references = AppDomain.CurrentDomain.GetAssemblies()
.Where(x => !x.IsDynamic && !string.IsNullOrWhiteSpace(x.Location))
.Select(x => MetadataReference.CreateFromFile(x.Location));
.Select(x => MetadataReference.CreateFromFile(x.Location))
.Concat([
MetadataReference.CreateFromFile(typeof(Console).Assembly.Location), // System.Console.dll
MetadataReference.CreateFromFile(typeof(IServiceProvider).Assembly.Location), // System.ComponentModel.dll
MetadataReference.CreateFromFile(typeof(System.ComponentModel.DataAnnotations.RequiredAttribute).Assembly.Location), // System.ComponentModel.DataAnnotations
MetadataReference.CreateFromFile(typeof(System.Text.Json.JsonDocument).Assembly.Location), // System.Text.Json.dll
]);

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

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

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

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

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

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

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

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

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

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

var (compilation, diagnostics, stdout) = CSharpGeneratorRunner.CompileAndExecute(code, args == "" ? [] : args.Split(' '));
foreach (var item in diagnostics)
Expand Down
14 changes: 4 additions & 10 deletions tests/ConsoleAppFramework.GeneratorTests/ConsoleAppBuilderTest.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
using Microsoft.VisualStudio.TestPlatform.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit.Abstractions;

namespace ConsoleAppFramework.GeneratorTests;

public class ConsoleAppBuilderTest(ITestOutputHelper output)
public class ConsoleAppBuilderTest(ITestOutputHelper output) : IDisposable
{
VerifyHelper verifier = new VerifyHelper(output, "CAF");
VerifyHelper verifier = new VerifyHelper(output, "CAF");

public void Dispose() => Environment.ExitCode = 0;

[Fact]
public void BuilderRun()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand All @@ -10,11 +11,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="Shouldly" Version="4.3.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
<PackageReference Include="xunit.v3" Version="1.1.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand All @@ -26,7 +27,6 @@

<ItemGroup>
<Using Include="Xunit" />
<Using Include="Xunit.Abstractions" />
<Using Include="Shouldly" />
</ItemGroup>
</Project>
2 changes: 0 additions & 2 deletions tests/ConsoleAppFramework.GeneratorTests/DiagnosticsTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using Xunit.Abstractions;

namespace ConsoleAppFramework.GeneratorTests;

public class DiagnosticsTest(ITestOutputHelper output)
Expand Down
7 changes: 0 additions & 7 deletions tests/ConsoleAppFramework.GeneratorTests/FilterTest.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit.Abstractions;

namespace ConsoleAppFramework.GeneratorTests;

public class FilterTest(ITestOutputHelper output)
Expand Down
22 changes: 19 additions & 3 deletions tests/ConsoleAppFramework.GeneratorTests/HelpTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Xunit.Abstractions;

namespace ConsoleAppFramework.GeneratorTests;

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

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

verifier.Execute(code: """
var app = ConsoleApp.Create();
Expand Down Expand Up @@ -321,4 +320,21 @@ hello my world.

""");
}

private static string GetEntryAssemblyVersion()
{
var version = Assembly.GetEntryAssembly()?.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;

if (version == null)
return "1.0.0";

// Trim SourceRevisionId (SourceLink feature is enabled by default when using .NET SDK 8 or later)
var i = version.IndexOf('+');
if (i != -1)
{
version = version.Substring(0, i);
}

return version;
}
}
7 changes: 0 additions & 7 deletions tests/ConsoleAppFramework.GeneratorTests/NameConverterTest.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit.Abstractions;

namespace ConsoleAppFramework.GeneratorTests;

public class NameConverterTest(ITestOutputHelper output)
Expand Down
13 changes: 3 additions & 10 deletions tests/ConsoleAppFramework.GeneratorTests/RunTest.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
using Microsoft.CodeAnalysis;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit.Abstractions;

namespace ConsoleAppFramework.GeneratorTests;

public class Test(ITestOutputHelper output)
public class Test(ITestOutputHelper output) : IDisposable
{
VerifyHelper verifier = new VerifyHelper(output, "CAF");

public void Dispose() => Environment.ExitCode = 0;

[Fact]
public void SyncRun()
{
Expand Down
7 changes: 0 additions & 7 deletions tests/ConsoleAppFramework.GeneratorTests/SubCommandTest.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit.Abstractions;

namespace ConsoleAppFramework.GeneratorTests;

public class SubCommandTest(ITestOutputHelper output)
Expand Down
25 changes: 25 additions & 0 deletions tests/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project>
<Import Project="$([MSBuild]::GetPathOfFileAbove('$(MSBuildThisFile)', '$(MSBuildThisFileDirectory)../'))" />

<PropertyGroup>
<!-- Enable Microsoft.Testing.Platform -->
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>

<!-- Use Microsoft.Testing.Platform exe entry point instead of xUnit.net -->
<UseMicrosoftTestingPlatformRunner>true</UseMicrosoftTestingPlatformRunner>

<!-- Output all output logs to console -->
<TestingPlatformCaptureOutput>false</TestingPlatformCaptureOutput>
</PropertyGroup>

<PropertyGroup>
<!-- Show xUnit.net headers and information -->
<TestingPlatformCommandLineArguments>$(TestingPlatformCommandLineArguments) --xunit-info</TestingPlatformCommandLineArguments>

<!-- Set TestResults output directory -->
<TestingPlatformCommandLineArguments>$(TestingPlatformCommandLineArguments) --results-directory "$(MSBuildThisFileDirectory)TestResults"</TestingPlatformCommandLineArguments>

<!-- Ignore exit code 8 (the test session run zero tests) -->
<TestingPlatformCommandLineArguments>$(TestingPlatformCommandLineArguments) --ignore-exit-code 8</TestingPlatformCommandLineArguments>
</PropertyGroup>
</Project>