Skip to content

Commit

Permalink
Fix up test run to output to right places.
Browse files Browse the repository at this point in the history
Use Itemgroup for PreprocessorDefinitions. With optional Value metadata.
  • Loading branch information
wasabii committed Mar 20, 2024
1 parent 88e9601 commit ee1bae1
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 61 deletions.
15 changes: 8 additions & 7 deletions src/IKVM.Clang.Sdk.Tests/IKVM.Clang.Sdk.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@

<PropertyGroup>
<TargetFrameworks>net7.0</TargetFrameworks>
<RunSettingsFilePath>$(MSBuildProjectDirectory)\IKVM.Clang.Sdk.Tests.runsettings</RunSettingsFilePath>
</PropertyGroup>

<ItemGroup>
<None Include="Project\**\*" CopyToOutputDirectory="Always" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.8.0" />
<PackageReference Include="Buildalyzer" Version="4.1.6" />
<PackageReference Include="Microsoft.CodeCoverage" Version="17.4.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Buildalyzer" Version="6.0.4" />
<PackageReference Include="Microsoft.CodeCoverage" Version="17.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="MSTest.TestAdapter" Version="3.2.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.2.2" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' != '' ">
Expand Down
5 changes: 5 additions & 0 deletions src/IKVM.Clang.Sdk.Tests/IKVM.Clang.Sdk.Tests.runsettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<RunSettings>
<MSTest>
<DeleteDeploymentDirectoryAfterTestRunIsComplete>False</DeleteDeploymentDirectoryAfterTestRunIsComplete>
</MSTest>
</RunSettings>
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<ItemGroup>
<Compile Include="hello1.c" />
<Header Include="hello1.h" />
<PreprocessorDefinitions Include="JOE" />
<PreprocessorDefinitions Include="FOO" Value="BAR" />
</ItemGroup>
<Import Sdk="IKVM.Clang.Sdk" Version="$(PackageVersion)" Project="Sdk.targets" />
</Project>
148 changes: 96 additions & 52 deletions src/IKVM.Clang.Sdk.Tests/ProjectTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Xml.Linq;

using Buildalyzer;
Expand Down Expand Up @@ -46,44 +48,106 @@ public override void Initialize(IEventSource eventSource)

public TestContext TestContext { get; set; }

[TestMethod]
public void CanBuildTestProject()
{
var properties = File.ReadAllLines("IKVM.Clang.Sdk.Tests.properties").Select(i => i.Split('=', 2)).ToDictionary(i => i[0], i => i[1]);
public static Dictionary<string, string> Properties { get; set; }

public static string TestRoot { get; set; }

public static string TempRoot { get; set; }

public static string WorkRoot { get; set; }

var nugetPackageRoot = Path.Combine(Path.GetTempPath(), "IKVM.Clang.Sdk.Tests", "nuget", "packages");
if (Directory.Exists(nugetPackageRoot))
Directory.Delete(nugetPackageRoot, true);
Directory.CreateDirectory(nugetPackageRoot);
public static string NuGetPackageRoot { get; set; }

[ClassInitialize]
public static void ClassInitialize(TestContext context)
{
// properties to load into test build
Properties = File.ReadAllLines("IKVM.Clang.Sdk.Tests.properties").Select(i => i.Split('=', 2)).ToDictionary(i => i[0], i => i[1]);

// root of the project collection itself
TestRoot = Path.Combine(Path.GetDirectoryName(typeof(ProjectTests).Assembly.Location), "Project");
context.WriteLine("TestRoot: {0}", TestRoot);

// temporary directory
TempRoot = Path.Combine(Path.GetTempPath(), "IKVM.Clang.Sdk.Tests", Guid.NewGuid().ToString());
if (Directory.Exists(TempRoot))
Directory.Delete(TempRoot, true);
Directory.CreateDirectory(TempRoot);
context.WriteLine("TempRoot: {0}", TempRoot);

// work directory
WorkRoot = Path.Combine(context.TestRunDirectory, "IKVM.Clang.Sdk.Tests", "ProjectTests");
if (Directory.Exists(WorkRoot))
Directory.Delete(WorkRoot, true);
Directory.CreateDirectory(WorkRoot);
context.WriteLine("WorkRoot: {0}", WorkRoot);

// other required sub directories
NuGetPackageRoot = Path.Combine(TempRoot, "nuget", "packages");

// nuget.config file that defines package sources
new XDocument(
new XElement("configuration",
new XElement("config",
new XElement("add",
new XAttribute("key", "globalPackagesFolder"),
new XAttribute("value", nugetPackageRoot))),
new XAttribute("value", NuGetPackageRoot))),
new XElement("packageSources",
new XElement("clear"),
new XElement("add",
new XAttribute("key", "nuget.org"),
new XAttribute("value", "https://api.nuget.org/v3/index.json")),
new XElement("add",
new XAttribute("key", "dev"),
new XAttribute("value", Path.Combine(Path.GetDirectoryName(typeof(ProjectTests).Assembly.Location), @"nuget"))),
new XElement("add",
new XAttribute("key", "nuget.org"),
new XAttribute("value", "https://api.nuget.org/v3/index.json")))))
.Save(Path.Combine(@"Project", "nuget.config"));
new XAttribute("value", Path.Combine(Path.GetDirectoryName(typeof(ProjectTests).Assembly.Location), @"nuget"))))))
.Save(Path.Combine(TestRoot, "nuget.config"));

var manager = new AnalyzerManager();
var analyzer = manager.GetProject(Path.Combine(@"Project", "Executable", "Executable.clangproj"));
analyzer.AddBuildLogger(new TargetLogger(TestContext) { Verbosity = LoggerVerbosity.Detailed });
analyzer.AddBinaryLogger("msbuild.binlog");
var analyzer = manager.GetProject(Path.Combine(TestRoot, "Executable", "Executable.clangproj"));
analyzer.AddBuildLogger(new TargetLogger(context));
analyzer.AddBinaryLogger(Path.Combine(WorkRoot, "msbuild.binlog"));
analyzer.SetGlobalProperty("ImportDirectoryBuildProps", "false");
analyzer.SetGlobalProperty("ImportDirectoryBuildTargets", "false");
analyzer.SetGlobalProperty("PackageVersion", Properties["PackageVersion"]);
analyzer.SetGlobalProperty("RestorePackagesPath", NuGetPackageRoot + Path.DirectorySeparatorChar);
analyzer.SetGlobalProperty("CreateHardLinksForAdditionalFilesIfPossible", "true");
analyzer.SetGlobalProperty("CreateHardLinksForCopyAdditionalFilesIfPossible", "true");
analyzer.SetGlobalProperty("CreateHardLinksForCopyFilesToOutputDirectoryIfPossible", "true");
analyzer.SetGlobalProperty("CreateHardLinksForCopyLocalIfPossible", "true");
analyzer.SetGlobalProperty("CreateHardLinksForPublishFilesIfPossible", "true");
analyzer.SetGlobalProperty("Configuration", "Release");

var options = new EnvironmentOptions();
options.WorkingDirectory = TestRoot;
options.DesignTime = false;
options.Restore = false;
options.TargetsToBuild.Clear();
options.TargetsToBuild.Add("Clean");
options.TargetsToBuild.Add("Restore");
options.Arguments.Add("/v:d");
analyzer.Build(options).OverallSuccess.Should().Be(true);

context.AddResultFile(Path.Combine(WorkRoot, "msbuild.binlog"));
}

[DataTestMethod]
[DataRow(EnvironmentPreference.Core, "x86_64-pc-windows-msvc")]
[DataRow(EnvironmentPreference.Core, "i686-pc-windows-msvc")]
[DataRow(EnvironmentPreference.Core, "aarch64-pc-windows-msvc")]
public void CanBuildTestProject(EnvironmentPreference env, string tid)
{
TestContext.WriteLine("TestRoot: {0}", TestRoot);
TestContext.WriteLine("TempRoot: {0}", TempRoot);
TestContext.WriteLine("WorkRoot: {0}", WorkRoot);

var manager = new AnalyzerManager();
var analyzer = manager.GetProject(Path.Combine(TestRoot, "Executable", "Executable.clangproj"));
analyzer.AddBuildLogger(new TargetLogger(TestContext));
analyzer.AddBinaryLogger(Path.Combine(WorkRoot, $"{tid}-msbuild.binlog"));
analyzer.SetGlobalProperty("ImportDirectoryBuildProps", "false");
analyzer.SetGlobalProperty("ImportDirectoryBuildTargets", "false");
analyzer.SetGlobalProperty("PackageVersion", properties["PackageVersion"]);
analyzer.SetGlobalProperty("RestorePackagesPath", nugetPackageRoot + Path.DirectorySeparatorChar);
analyzer.SetGlobalProperty("PackageVersion", Properties["PackageVersion"]);
analyzer.SetGlobalProperty("RestorePackagesPath", NuGetPackageRoot + Path.DirectorySeparatorChar);
analyzer.SetGlobalProperty("CreateHardLinksForAdditionalFilesIfPossible", "true");
analyzer.SetGlobalProperty("CreateHardLinksForCopyAdditionalFilesIfPossible", "true");
analyzer.SetGlobalProperty("CreateHardLinksForCopyFilesToOutputDirectoryIfPossible", "true");
Expand All @@ -92,39 +156,19 @@ public void CanBuildTestProject()
analyzer.SetGlobalProperty("SkipCopyBuildProduct", "false");
analyzer.SetGlobalProperty("CopyBuildOutputToOutputDirectory", "true");


var o = new EnvironmentOptions();

// first restore and clean
o.TargetsToBuild.Clear();
o.TargetsToBuild.Add("Restore");
o.TargetsToBuild.Add("Clean");
analyzer.Build(o).OverallSuccess.Should().BeTrue();

// do a global build
o.TargetsToBuild.Clear();
o.TargetsToBuild.Add("Build");
analyzer.Build(o).OverallSuccess.Should().BeTrue();

// do individual target builds
var targets = new[]
{
"x86_64-pc-windows-msvc",
"i686-pc-windows-msvc",
"aarch64-pc-windows-msvc",
};

foreach (var target in targets)
{
TestContext.WriteLine("Building with TargetIdentifier {0}.", target);
var options = new EnvironmentOptions();
options.DesignTime = false;
options.Restore = false;
options.GlobalProperties["TargetIdentifier"] = target;
options.TargetsToBuild.Clear();
options.TargetsToBuild.Add("Build");
analyzer.Build(o).OverallSuccess.Should().Be(true);
}
var options = new EnvironmentOptions();
options.WorkingDirectory = TestRoot;
options.Preference = env;
options.DesignTime = false;
options.Restore = false;
options.GlobalProperties["TargetIdentifier"] = tid;
options.TargetsToBuild.Clear();
options.TargetsToBuild.Add("Clean");
options.TargetsToBuild.Add("Build");
options.Arguments.Add("/v:d");
analyzer.Build(options).OverallSuccess.Should().BeTrue();

TestContext.AddResultFile(Path.Combine(WorkRoot, $"{tid}-msbuild.binlog"));
}

}
Expand Down
3 changes: 2 additions & 1 deletion src/IKVM.Clang.Sdk/Sdk/targets/IKVM.Clang.Core.targets
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,8 @@
<_PreprocessorDefinitions Include="%(Compile.PreprocessorDefinitions)" />
<_PreprocessorDefinitions Include="@(PreprocessorDefinitions)" />
<_PreprocessorDefinitions Include="$(PreprocessorDefinitions)" />
<_Args Include="@(_PreprocessorDefinitions->'-D &quot;%(Identity)&quot;'->Replace('\', '\\'))" Condition=" '@(_PreprocessorDefinitions)' != '' " />
<_Args Include="@(_PreprocessorDefinitions->'-D %(Identity)')" Condition=" '@(_PreprocessorDefinitions)' != '' And %(_PreprocessorDefinitions.Value) == '' " />
<_Args Include="@(_PreprocessorDefinitions->'-D %(Identity)=%(Value)')" Condition=" '@(_PreprocessorDefinitions)' != '' And %(_PreprocessorDefinitions.Value) != '' " />

<_AdditionalOptions Remove="@(_AdditionalOptions)" />
<_AdditionalOptions Include="%(Compile.AdditionalCompileOptions)" />
Expand Down
2 changes: 1 addition & 1 deletion src/dist-tests/dist-tests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Build.NoTargets/3.7.0">
<Project Sdk="Microsoft.Build.NoTargets/3.7.0">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
Expand Down

0 comments on commit ee1bae1

Please sign in to comment.