Skip to content

Commit 3abb20a

Browse files
committed
chore: adding tests
1 parent 8e0f018 commit 3abb20a

File tree

6 files changed

+230
-1
lines changed

6 files changed

+230
-1
lines changed

Mobile.BuildTools.sln

+16-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mobile.BuildTools.Core", "s
4141
EndProject
4242
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mobile.BuildTools.AppManifests", "src\Mobile.BuildTools.AppManifests\Mobile.BuildTools.AppManifests.csproj", "{3A49F4A6-9282-4BBD-84C7-296CD11B5091}"
4343
EndProject
44-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mobile.BuildTools.Configuration.MSBuild", "src\Mobile.BuildTools.Configuration.MSBuild\Mobile.BuildTools.Configuration.MSBuild.csproj", "{5523AC86-C50C-40D4-8427-08E00E8C033A}"
44+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mobile.BuildTools.Configuration.MSBuild", "src\Mobile.BuildTools.Configuration.MSBuild\Mobile.BuildTools.Configuration.MSBuild.csproj", "{5523AC86-C50C-40D4-8427-08E00E8C033A}"
45+
EndProject
46+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mobile.BuildTools.AppSettings.Tests", "tests\Mobile.BuildTools.AppSettings.Tests\Mobile.BuildTools.AppSettings.Tests.csproj", "{915FBF46-619A-44FF-A8A5-6B0681CD2D79}"
4547
EndProject
4648
Global
4749
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -197,6 +199,18 @@ Global
197199
{5523AC86-C50C-40D4-8427-08E00E8C033A}.Release|x64.Build.0 = Release|Any CPU
198200
{5523AC86-C50C-40D4-8427-08E00E8C033A}.Release|x86.ActiveCfg = Release|Any CPU
199201
{5523AC86-C50C-40D4-8427-08E00E8C033A}.Release|x86.Build.0 = Release|Any CPU
202+
{915FBF46-619A-44FF-A8A5-6B0681CD2D79}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
203+
{915FBF46-619A-44FF-A8A5-6B0681CD2D79}.Debug|Any CPU.Build.0 = Debug|Any CPU
204+
{915FBF46-619A-44FF-A8A5-6B0681CD2D79}.Debug|x64.ActiveCfg = Debug|Any CPU
205+
{915FBF46-619A-44FF-A8A5-6B0681CD2D79}.Debug|x64.Build.0 = Debug|Any CPU
206+
{915FBF46-619A-44FF-A8A5-6B0681CD2D79}.Debug|x86.ActiveCfg = Debug|Any CPU
207+
{915FBF46-619A-44FF-A8A5-6B0681CD2D79}.Debug|x86.Build.0 = Debug|Any CPU
208+
{915FBF46-619A-44FF-A8A5-6B0681CD2D79}.Release|Any CPU.ActiveCfg = Release|Any CPU
209+
{915FBF46-619A-44FF-A8A5-6B0681CD2D79}.Release|Any CPU.Build.0 = Release|Any CPU
210+
{915FBF46-619A-44FF-A8A5-6B0681CD2D79}.Release|x64.ActiveCfg = Release|Any CPU
211+
{915FBF46-619A-44FF-A8A5-6B0681CD2D79}.Release|x64.Build.0 = Release|Any CPU
212+
{915FBF46-619A-44FF-A8A5-6B0681CD2D79}.Release|x86.ActiveCfg = Release|Any CPU
213+
{915FBF46-619A-44FF-A8A5-6B0681CD2D79}.Release|x86.Build.0 = Release|Any CPU
200214
EndGlobalSection
201215
GlobalSection(SolutionProperties) = preSolution
202216
HideSolutionNode = FALSE
@@ -214,6 +228,7 @@ Global
214228
{A5578B7E-63D4-4532-8094-09BDE2B5574A} = {C7456F1C-2B86-4C40-A228-72209A7355A0}
215229
{3A49F4A6-9282-4BBD-84C7-296CD11B5091} = {C7456F1C-2B86-4C40-A228-72209A7355A0}
216230
{5523AC86-C50C-40D4-8427-08E00E8C033A} = {C7456F1C-2B86-4C40-A228-72209A7355A0}
231+
{915FBF46-619A-44FF-A8A5-6B0681CD2D79} = {2510D213-21AF-4B25-888D-E63163356A4F}
217232
EndGlobalSection
218233
GlobalSection(ExtensibilityGlobals) = postSolution
219234
SolutionGuid = {429C8CA0-DB8C-4CF5-9627-187B9CC47A99}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by Mobile.BuildTools. For more information please visit
4+
// https://mobilebuildtools.com or to file an issue please see
5+
// https://github.com/dansiegel/Mobile.BuildTools
6+
//
7+
// Changes to this file may cause incorrect behavior and will be lost when
8+
// the code is regenerated.
9+
//
10+
// When I wrote this, only God and I understood what I was doing
11+
// Now, God only knows.
12+
//
13+
// NOTE: This file should be excluded from source control.
14+
// </auto-generated>
15+
//------------------------------------------------------------------------------
16+
17+
using System;
18+
using GeneratedCodeAttribute = System.CodeDom.Compiler.GeneratedCodeAttribute;
19+
20+
namespace TestProject.Helpers
21+
{
22+
internal static partial class AppSettings
23+
{
24+
public const string AProperty =
25+
"Hello World";
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
<IsTestProject>true</IsTestProject>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<Compile Remove="Expected/**/*.cs;Sources/**/*.cs" />
14+
<None Include="Expected/**;Sources/**" CopyToOutputDirectory="Always" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<PackageReference Include="Microsoft.CodeAnalysis.Common" />
19+
<PackageReference Include="coverlet.collector">
20+
<PrivateAssets>all</PrivateAssets>
21+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
22+
</PackageReference>
23+
<PackageReference Include="GitHubActionsTestLogger">
24+
<PrivateAssets>all</PrivateAssets>
25+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
26+
</PackageReference>
27+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing" />
28+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" />
29+
<PackageReference Include="Microsoft.NET.Test.Sdk" />
30+
<PackageReference Include="xunit" />
31+
<PackageReference Include="xunit.runner.visualstudio" />
32+
</ItemGroup>
33+
34+
<ItemGroup>
35+
<ProjectReference Include="..\..\src\Mobile.BuildTools.AppSettings\Mobile.BuildTools.AppSettings.csproj" />
36+
</ItemGroup>
37+
38+
<ItemGroup>
39+
<Using Include="Xunit" />
40+
</ItemGroup>
41+
42+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"Environment": {
3+
"AProperty": "Hello World"
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"$schema": "https://mobilebuildtools.com/schemas/v2/buildtools.schema.json",
3+
"appSettings": {
4+
"TestProject": [
5+
{
6+
"properties": [
7+
{
8+
"name": "AProperty",
9+
"type": "String"
10+
}
11+
]
12+
}
13+
]
14+
}
15+
}

0 commit comments

Comments
 (0)