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