|
| 1 | +using System.Collections.Immutable; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.Diagnostics.CodeAnalysis; |
| 4 | +using System.Runtime.CompilerServices; |
| 5 | +using System.Text; |
| 6 | +using System.Text.RegularExpressions; |
| 7 | +using System.Xml.Linq; |
| 8 | +using Microsoft.CodeAnalysis; |
| 9 | +using Microsoft.CodeAnalysis.CSharp.Testing; |
| 10 | +using Microsoft.CodeAnalysis.Options; |
| 11 | +using Microsoft.CodeAnalysis.Testing; |
| 12 | +using Microsoft.CodeAnalysis.Text; |
| 13 | +using Verify = Microsoft.CodeAnalysis.CSharp.Testing.CSharpSourceGeneratorTest<Mobile.BuildTools.AppSettings.Generators.AppSettingsGenerator, Microsoft.CodeAnalysis.Testing.DefaultVerifier>; |
| 14 | + |
| 15 | +namespace Mobile.BuildTools.AppSettings.Tests; |
| 16 | + |
| 17 | +public class AppSettingsGeneratorFixture |
| 18 | +{ |
| 19 | + [Fact] |
| 20 | + public async Task AddsSimpleProperty() |
| 21 | + { |
| 22 | + await Test(); |
| 23 | + } |
| 24 | + |
| 25 | + private static Task Test([CallerMemberName] string? method = null) |
| 26 | + { |
| 27 | + Assert.NotNull(method); |
| 28 | + var expected = new DirectoryInfo(Path.Combine(Environment.CurrentDirectory, "Expected", method)); |
| 29 | + var expectedFiles = expected.GetFiles("*.g.cs").Select(x => x.Name).ToArray(); |
| 30 | + var sources = new DirectoryInfo(Path.Combine(Environment.CurrentDirectory, "Sources", method)); |
| 31 | + var sourceFiles = sources.GetFiles("*").Select(x => x.Name).ToArray(); |
| 32 | + return Test(expectedFiles, sourceFiles, method); |
| 33 | + } |
| 34 | + |
| 35 | + private static Task Test(string[] generatedSources, string[] additionalFiles, [CallerMemberName] string? method = null) |
| 36 | + { |
| 37 | + Assert.NotNull(method); |
| 38 | + var test = new Verify |
| 39 | + { |
| 40 | + ReferenceAssemblies = ReferenceAssemblies.Net.Net80, |
| 41 | + CompilerDiagnostics = CompilerDiagnostics.All, |
| 42 | + TestState = |
| 43 | + { |
| 44 | + OutputKind = OutputKind.DynamicallyLinkedLibrary |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + AddMSBuildProperties(test, ("MSBuildProjectName", test.TestState.Name), |
| 49 | + ("RootNamespace", test.TestState.AssemblyName), |
| 50 | + ("Configuration", "Debug"), |
| 51 | + ("TargetFrameworkIdentifier", "net8.0")); |
| 52 | + |
| 53 | + test.TestState.GeneratedSources.AddRange(GetGeneratedFiles(generatedSources, method)); |
| 54 | + test.TestState.AdditionalFiles.AddRange(GetAdditionalFiles(additionalFiles, method)); |
| 55 | + |
| 56 | + return test.RunAsync(); |
| 57 | + } |
| 58 | + |
| 59 | + private static void AddMSBuildProperties(Verify test, params(string, string)[] buildProperties) |
| 60 | + { |
| 61 | + var sb = new StringBuilder(); |
| 62 | + sb.AppendLine("is_global = true"); |
| 63 | + foreach(var (property, value) in buildProperties) |
| 64 | + { |
| 65 | + sb.AppendLine($"build_property.{property} = {value}"); |
| 66 | + } |
| 67 | + |
| 68 | + test.TestState.AnalyzerConfigFiles.Add(("/.editorconfig", sb.ToString())); |
| 69 | + } |
| 70 | + |
| 71 | + private static SourceFileCollection GetAdditionalFiles(string[] sourceFiles, string method) |
| 72 | + { |
| 73 | + var files = new SourceFileCollection(); |
| 74 | + foreach(var file in sourceFiles) |
| 75 | + { |
| 76 | + files.Add(GetAdditionalFile(file, method)); |
| 77 | + } |
| 78 | + |
| 79 | + if (!sourceFiles.Contains(Constants.BuildToolsEnvironmentSettings)) |
| 80 | + { |
| 81 | + files.Add(GetAdditionalFile(Constants.BuildToolsEnvironmentSettings, method)); |
| 82 | + } |
| 83 | + |
| 84 | + if (!sourceFiles.Contains(Constants.BuildToolsConfigFileName)) |
| 85 | + { |
| 86 | + files.Add(GetAdditionalFile(Constants.BuildToolsConfigFileName, method)); |
| 87 | + } |
| 88 | + |
| 89 | + return files; |
| 90 | + } |
| 91 | + |
| 92 | + private static (string, SourceText) GetAdditionalFile(string sourceFile, string method) |
| 93 | + { |
| 94 | + var path = Path.Combine(Environment.CurrentDirectory, "Sources", method, sourceFile); |
| 95 | + Assert.True(File.Exists(path)); |
| 96 | + |
| 97 | + return (path, GetSourceText(path)); |
| 98 | + } |
| 99 | + |
| 100 | + private static SourceFileCollection GetGeneratedFiles(string[] generatedSources, string method) |
| 101 | + { |
| 102 | + var files = new SourceFileCollection(); |
| 103 | + foreach(var file in generatedSources) |
| 104 | + { |
| 105 | + files.Add(GetGeneratedFile(file, method)); |
| 106 | + } |
| 107 | + |
| 108 | + return files; |
| 109 | + } |
| 110 | + |
| 111 | + private static (string, SourceText) GetGeneratedFile(string filename, string method) |
| 112 | + { |
| 113 | + var path = Path.Combine(Environment.CurrentDirectory, "Expected", method, filename); |
| 114 | + Assert.True(File.Exists(path), $"Path does not exist: '{path}'"); |
| 115 | + |
| 116 | + return (path, GetSourceText(path)); |
| 117 | + } |
| 118 | + |
| 119 | + private static SourceText GetSourceText(string path) |
| 120 | + { |
| 121 | + using var reader = new StreamReader(path); |
| 122 | + var source = /*Regex.Replace(*/reader.ReadToEnd()/*, @"\r\n|\n\r|\n|\r", Environment.NewLine)*/; |
| 123 | + return SourceText.From(source, Encoding.UTF8); |
| 124 | + } |
| 125 | +} |
0 commit comments