Skip to content

Commit cbcf3c2

Browse files
authored
Properly test the Source Generator. (#22)
Signed-off-by: AraHaan <seandhunt_7@yahoo.com>
1 parent 3ecc56a commit cbcf3c2

File tree

5 files changed

+106
-67
lines changed

5 files changed

+106
-67
lines changed

NuGet.config

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
<configuration>
22
<packageSources>
3-
<add key="dotnet-core" value="https://dotnetfeed.blob.core.windows.net/dotnet-core/index.json" />
4-
<add key="dotnet5" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5/nuget/v3/index.json" />
5-
<add key="dotnet5-transport" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5-transport/nuget/v3/index.json" />
63
<add key="dotnet6" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet6/nuget/v3/index.json" />
74
<add key="dotnet6-transport" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet6-transport/nuget/v3/index.json" />
8-
<add key="dotnet-public" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-public/nuget/v3/index.json" />
9-
<add key="dotnet-eng" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-eng/nuget/v3/index.json" />
105
<add key="dotnet-tools" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json" />
116
<add key="dotnet-tools-transport" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools-transport/nuget/v3/index.json" />
127
<add key="xunit-prereleases" value="https://www.myget.org/F/xunit/api/v3/index.json" />

src/GitBuildInfo.SourceGenerator.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@
4040
</ItemDefinitionGroup>
4141

4242
<ItemGroup>
43-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.10.0-3.21256.6" />
44-
<PackageReference Include="Nullable" Version="1.3.0" />
45-
<PackageReference Include="System.Text.Json" Version="6.0.0-preview.5.21255.7" />
43+
<PackageReference Include="Microsoft.CodeAnalysis" Version="*-*" />
44+
<PackageReference Include="Nullable" Version="*-*" />
45+
<PackageReference Include="System.Text.Json" Version="*-*" />
4646
</ItemGroup>
4747

4848
<Import Project="$(MSBuildProjectName).targets" />

tests/GeneratorTests.cs

Lines changed: 0 additions & 52 deletions
This file was deleted.

tests/GitBuildInfo.SourceGenerator.Tests.csproj

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@
33
<PropertyGroup>
44
<TargetFramework>net6.0</TargetFramework>
55
<IsPackable>false</IsPackable>
6+
<Nullable>enable</Nullable>
67
</PropertyGroup>
78

89
<ItemGroup>
9-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.10.0-3.21256.6" />
10-
<PackageReference Include="System.Text.Json" Version="6.0.0-preview.5.21255.7" />
11-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
12-
<PackageReference Include="xunit" Version="2.4.1" />
13-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
10+
<PackageReference Include="Microsoft.CodeAnalysis" Version="*-*" />
11+
<PackageReference Include="System.Text.Json" Version="*-*" />
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="*-*" />
13+
<PackageReference Include="xunit" Version="*-*" />
14+
<PackageReference Include="xunit.runner.visualstudio" Version="*-*">
1415
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1516
<PrivateAssets>all</PrivateAssets>
1617
</PackageReference>
17-
<PackageReference Include="coverlet.collector" Version="3.0.3">
18+
<PackageReference Include="coverlet.collector" Version="*-*">
1819
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1920
<PrivateAssets>all</PrivateAssets>
2021
</PackageReference>
21-
<PackageReference Include="coverlet.msbuild" Version="3.0.3">
22+
<PackageReference Include="coverlet.msbuild" Version="*-*">
2223
<PrivateAssets>all</PrivateAssets>
2324
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2425
</PackageReference>

tests/SourceGeneratorTests.cs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
namespace GitBuildInfo.SourceGenerator.Tests
2+
{
3+
using System;
4+
using System.Collections.Immutable;
5+
using System.Linq;
6+
using System.Threading;
7+
using GitBuildInfo.SourceGenerator;
8+
using Microsoft.CodeAnalysis;
9+
using Microsoft.CodeAnalysis.CSharp;
10+
using Microsoft.CodeAnalysis.Text;
11+
using Xunit;
12+
13+
public class SourceGeneratorTests
14+
{
15+
[Fact]
16+
public void TestGeneratingNonGeneric()
17+
{
18+
var result = DoTest("TestNamespace.Test", false);
19+
Assert.Equal(@"// <autogenerated/>
20+
using Elskom.Generic.Libs;
21+
using TestNamespace;
22+
23+
[assembly: GitInformationAttribute(""fbgtgretgtre"", ""vfdbttregter"", ""vsdfvfdsv"", typeof(Test))]
24+
", result);
25+
}
26+
27+
[Fact]
28+
public void TestGeneratingGeneric()
29+
{
30+
var result = DoTest("TestNamespace.Test", true);
31+
Assert.Equal(@"// <autogenerated/>
32+
using Elskom.Generic.Libs;
33+
using TestNamespace;
34+
35+
[assembly: GitInformationAttribute(""fbgtgretgtre"", ""vfdbttregter"", ""vsdfvfdsv"", typeof(Test<>))]
36+
", result);
37+
}
38+
39+
[Fact]
40+
public void TestGeneratingFailure()
41+
=> Assert.Throws<InvalidOperationException>(() => DoTest(string.Empty, false));
42+
43+
// the values on GitInfo are not valid in terms of results
44+
// that would normally be from git.
45+
private static string DoTest(string assemblyType, bool generic)
46+
=> TestGenerate(
47+
$@"{{
48+
""$schema"": ""https://raw.githubusercontent.com/Elskom/GitBuildInfo.SourceGenerator/main/settings.schema.json"",
49+
""AssemblyType"": ""{assemblyType}"",
50+
""IsGeneric"": {generic.ToString().ToLowerInvariant()},
51+
}}",
52+
@"{
53+
""GitHead"": ""fbgtgretgtre"",
54+
""CommitHash"": ""vfdbttregter"",
55+
""GitBranch"": ""vsdfvfdsv"",
56+
}");
57+
58+
private static string TestGenerate(string optionsText, string gitInfoText)
59+
{
60+
var compilation = CSharpCompilation.Create(
61+
"TestAssembly",
62+
Array.Empty<SyntaxTree>(),
63+
Array.Empty<MetadataReference>(),
64+
new CSharpCompilationOptions(
65+
OutputKind.DynamicallyLinkedLibrary));
66+
var driver = CSharpGeneratorDriver.Create(new SourceGenerator())
67+
.AddAdditionalTexts(
68+
ImmutableArray.Create<AdditionalText>(
69+
new CustomAdditionalText("GitBuildInfo.json", optionsText),
70+
new CustomAdditionalText("GitInfo.json", gitInfoText)));
71+
_ = driver.RunGeneratorsAndUpdateCompilation(compilation, out var outputCompilation, out var generateDiagnostics);
72+
Assert.False(generateDiagnostics.Any(d => d.Severity == DiagnosticSeverity.Error), $"Failed: {generateDiagnostics.FirstOrDefault()?.GetMessage()}");
73+
return outputCompilation.SyntaxTrees.Last().ToString();
74+
}
75+
76+
private class CustomAdditionalText : AdditionalText
77+
{
78+
private readonly string _text;
79+
80+
public override string Path { get; }
81+
82+
public CustomAdditionalText(string path, string text)
83+
{
84+
Path = path;
85+
_text = text;
86+
}
87+
88+
public override SourceText? GetText(CancellationToken cancellationToken = default)
89+
{
90+
cancellationToken.ThrowIfCancellationRequested();
91+
return SourceText.From(_text);
92+
}
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)