Skip to content

Commit

Permalink
Upgrade test project to new design using data tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
wasabii committed Oct 8, 2023
1 parent c891fe3 commit df3c610
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 109 deletions.
1 change: 0 additions & 1 deletion src/IKVM.Maven.Sdk.Tests/IKVM.Maven.Sdk.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

<PropertyGroup>
<TargetFrameworks>net7.0</TargetFrameworks>
<NoWarn>CS1591</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net472;net48;net6.0;net7.0</TargetFrameworks>
<RuntimeIdentifiers>win7-x64;win7-x86;win81-arm;linux-x64;linux-arm;linux-arm64;osx-x64;osx-arm64</RuntimeIdentifiers>
<RuntimeIdentifiers>win-x64;win-x86;win-arm64;linux-x64;linux-arm;linux-arm64;linux-musl-x64;linux-musl-arm;linux-musl-arm64;osx-x64;osx-arm64</RuntimeIdentifiers>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/IKVM.Maven.Sdk.Tests/Project/Exe/ProjectExe.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net472;net48;net6.0;net7.0</TargetFrameworks>
<RuntimeIdentifiers>win7-x64;win7-x86;win81-arm;linux-x64;linux-arm;linux-arm64;osx-x64;osx-arm64</RuntimeIdentifiers>
<RuntimeIdentifiers>win-x64;win-x86;win-arm64;linux-x64;linux-arm;linux-arm64;linux-musl-x64;linux-musl-arm;linux-musl-arm64;osx-x64;osx-arm64</RuntimeIdentifiers>
</PropertyGroup>

<ItemGroup>
Expand Down
226 changes: 120 additions & 106 deletions src/IKVM.Maven.Sdk.Tests/ProjectTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -45,140 +46,153 @@ public override void Initialize(IEventSource eventSource)

}

public TestContext TestContext { get; set; }
public static Dictionary<string, string> Properties { get; set; }

[TestMethod]
public void CanBuildProject()
{
// times out on linux, need to figure this out
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) == false)
return;
public static string TempRoot { get; set; }

var properties = File.ReadAllLines(Path.Combine(Path.GetDirectoryName(typeof(PackProjectTests).Assembly.Location), "IKVM.Maven.Sdk.Tests.properties")).Select(i => i.Split('=', 2)).ToDictionary(i => i[0], i => i[1]);
public static string WorkRoot { get; set; }

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

var ikvmCachePath = Path.Combine(Path.GetTempPath(), "IKVM.Maven.Sdk.Tests_Project", "ikvm", "cache");
if (Directory.Exists(ikvmCachePath))
Directory.Delete(ikvmCachePath, true);
public static string IkvmCachePath { get; set; }

var ikvmExportCachePath = Path.Combine(Path.GetTempPath(), "IKVM.Maven.Sdk.Tests_Project", "ikvm", "expcache");
if (Directory.Exists(ikvmExportCachePath))
Directory.Delete(ikvmExportCachePath, true);
public static string IkvmExportCachePath { get; set; }

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

// temporary directory
TempRoot = Path.Combine(Path.GetTempPath(), "IKVM.Maven.Sdk.Tests", Guid.NewGuid().ToString());
if (Directory.Exists(TempRoot))
Directory.Delete(TempRoot, true);
Directory.CreateDirectory(TempRoot);

// work directory
WorkRoot = Path.Combine(context.TestRunResultsDirectory, "IKVM.Maven.Sdk.Tests", "ProjectTests");
if (Directory.Exists(WorkRoot))
Directory.Delete(WorkRoot, true);
Directory.CreateDirectory(WorkRoot);

// other required sub directories
NuGetPackageRoot = Path.Combine(TempRoot, "nuget", "packages");
IkvmCachePath = Path.Combine(TempRoot, "ikvm", "cache");
IkvmExportCachePath = Path.Combine(TempRoot, "ikvm", "expcache");

// 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", "dev"),
new XAttribute("value", Path.GetFullPath(@"nuget"))),
new XElement("add",
new XAttribute("key", "nuget.org"),
new XAttribute("value", "https://api.nuget.org/v3/index.json")))))
new XAttribute("value", "https://api.nuget.org/v3/index.json")),
new XElement("add",
new XAttribute("key", "ikvm"),
new XAttribute("value", "https://nuget.pkg.github.com/ikvmnet/index.json")),
new XElement("add",
new XAttribute("key", "dev"),
new XAttribute("value", Path.Combine(Path.GetDirectoryName(typeof(ProjectTests).Assembly.Location), @"nuget"))))))
.Save(Path.Combine(@"Project", "nuget.config"));

var manager = new AnalyzerManager();
var analyzer = manager.GetProject(Path.Combine(Path.GetDirectoryName(typeof(PackProjectTests).Assembly.Location), @"Project", "Exe", "ProjectExe.csproj"));
var analyzer = manager.GetProject(Path.Combine(@"Project", "Exe", "ProjectExe.csproj"));
analyzer.AddBuildLogger(new TargetLogger(context));
analyzer.AddBinaryLogger(Path.Combine(WorkRoot, "msbuild.binlog"));
analyzer.SetGlobalProperty("ImportDirectoryBuildProps", "false");
analyzer.SetGlobalProperty("ImportDirectoryBuildTargets", "false");
analyzer.SetGlobalProperty("IkvmCacheDir", ikvmCachePath + Path.DirectorySeparatorChar);
analyzer.SetGlobalProperty("IkvmExportCacheDir", ikvmExportCachePath + Path.DirectorySeparatorChar);
analyzer.SetGlobalProperty("PackageVersion", properties["PackageVersion"]);
analyzer.SetGlobalProperty("RestorePackagesPath", nugetPackageRoot + Path.DirectorySeparatorChar);
analyzer.SetGlobalProperty("IkvmCacheDir", IkvmCachePath + Path.DirectorySeparatorChar);
analyzer.SetGlobalProperty("IkvmExportCacheDir", IkvmExportCachePath + 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");
analyzer.SetGlobalProperty("CreateHardLinksForCopyLocalIfPossible", "true");
analyzer.SetGlobalProperty("CreateHardLinksForPublishFilesIfPossible", "true");
analyzer.SetGlobalProperty("Configuration", "Release");

var options = new EnvironmentOptions();
options.DesignTime = false;
options.Restore = true;
options.TargetsToBuild.Clear();
options.TargetsToBuild.Add("Restore");
analyzer.Build(options).OverallSuccess.Should().Be(true);
}

analyzer.AddBuildLogger(new TargetLogger(TestContext) { Verbosity = LoggerVerbosity.Detailed });
[ClassCleanup]
public static void ClassCleanup()
{
if (Directory.Exists(TempRoot))
Directory.Delete(TempRoot, true);
}

var targets = new[]
{
("net461", "win7-x86"),
("net472", "win7-x86"),
("net48", "win7-x86"),
("net6.0", "win7-x86"),
("net7.0", "win7-x86"),
("net461", "win7-x64"),
("net472", "win7-x64"),
("net48", "win7-x64"),
("net6.0", "win7-x64"),
("net7.0", "win7-x64"),
("net461", "win81-arm"),
("net472", "win81-arm"),
("net48", "win81-arm"),
("net6.0", "win81-arm"),
("net7.0", "win81-arm"),
("net6.0", "linux-x64"),
("net7.0", "linux-x64"),
("net6.0", "linux-arm"),
("net7.0", "linux-arm"),
("net6.0", "linux-arm64"),
("net7.0", "linux-arm64"),
("net6.0", "osx-x64"),
("net7.0", "osx-x64"),
("net6.0", "osx-arm64"),
("net7.0", "osx-arm64"),
};

if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
targets = new[]
{
("net6.0", "win7-x86"),
("net7.0", "win7-x86"),
("net6.0", "win7-x64"),
("net7.0", "win7-x64"),
("net6.0", "win81-arm"),
("net7.0", "win81-arm"),
("net6.0", "linux-x64"),
("net7.0", "linux-x64"),
("net6.0", "linux-arm"),
("net7.0", "linux-arm"),
("net6.0", "linux-arm64"),
("net7.0", "linux-arm64"),
("net6.0", "osx-x64"),
("net7.0", "osx-x64"),
("net6.0", "osx-arm64"),
("net7.0", "osx-arm64"),
};
}
public TestContext TestContext { get; set; }

foreach (var (tfm, rid) in targets)
{
TestContext.WriteLine("Publishing with TargetFramework {0} and RuntimeIdentifier {1}.", tfm, rid);

foreach (var configuration in new[] { "Debug", "Release" })
{
var results = analyzer.Build(new EnvironmentOptions()
{
DesignTime = false,
Restore = true,
GlobalProperties =
{
["Configuration"] = configuration,
["TargetFramework"] = tfm,
["RuntimeIdentifier"] = rid,
},
TargetsToBuild =
{
"Build",
"Publish"
}
});

results.OverallSuccess.Should().Be(true);
}
}
[DataTestMethod]
[DataRow("net472", "win-x86")]
[DataRow("net472", "win-x64")]
[DataRow("net48", "win-x86")]
[DataRow("net48", "win-x64")]
[DataRow("net6.0", "win-x86")]
[DataRow("net6.0", "win-x64")]
[DataRow("net6.0", "linux-x64")]
[DataRow("net6.0", "linux-arm")]
[DataRow("net6.0", "linux-arm64")]
[DataRow("net6.0", "linux-musl-x64")]
[DataRow("net6.0", "linux-musl-arm")]
[DataRow("net6.0", "linux-musl-arm64")]
[DataRow("net6.0", "osx-x64")]
[DataRow("net6.0", "osx-arm64")]
[DataRow("net7.0", "win-x86")]
[DataRow("net7.0", "win-x64")]
[DataRow("net7.0", "linux-x64")]
[DataRow("net7.0", "linux-arm")]
[DataRow("net7.0", "linux-arm64")]
[DataRow("net7.0", "linux-musl-x64")]
[DataRow("net7.0", "linux-musl-arm")]
[DataRow("net7.0", "linux-musl-arm64")]
[DataRow("net7.0", "osx-x64")]
[DataRow("net7.0", "osx-arm64")]
public void CanBuildProject(string tfm, string rid)
{
// skip framework tests for non-Windows platforms
if (tfm == "net472" || tfm == "net48")
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) == false)
return;

var manager = new AnalyzerManager();
var analyzer = manager.GetProject(Path.Combine(@"Project", "Exe", "ProjectExe.csproj"));
analyzer.AddBuildLogger(new TargetLogger(TestContext));
analyzer.AddBinaryLogger(Path.Combine(WorkRoot, $"{tfm}-{rid}-msbuild.binlog"));
analyzer.SetGlobalProperty("ImportDirectoryBuildProps", "false");
analyzer.SetGlobalProperty("ImportDirectoryBuildTargets", "false");
analyzer.SetGlobalProperty("IkvmCacheDir", IkvmCachePath + Path.DirectorySeparatorChar);
analyzer.SetGlobalProperty("IkvmExportCacheDir", IkvmExportCachePath + 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");
analyzer.SetGlobalProperty("CreateHardLinksForCopyLocalIfPossible", "true");
analyzer.SetGlobalProperty("CreateHardLinksForPublishFilesIfPossible", "true");
analyzer.SetGlobalProperty("Configuration", "Release");

var options = new EnvironmentOptions();
options.DesignTime = false;
options.Restore = false;
options.GlobalProperties["TargetFramework"] = tfm;
options.GlobalProperties["RuntimeIdentifier"] = rid;
options.TargetsToBuild.Clear();
options.TargetsToBuild.Add("Build");
options.TargetsToBuild.Add("Publish");
analyzer.Build(options).OverallSuccess.Should().Be(true);
}

}
Expand Down

0 comments on commit df3c610

Please sign in to comment.