From 3269c785e5373f086500bdcc795e54ee501be6e7 Mon Sep 17 00:00:00 2001 From: Anthony Truskinger Date: Wed, 25 Mar 2020 12:10:43 +1000 Subject: [PATCH] Fixes tests for SelfContained/platform-specific builds When a self contained build is done, the `runtimes` folder is not produced. These adjustments cater for that scenerio and also add extra metadata about our build into the app to make it easier to reason about these properties. Also adds Microsoft.DotNet.PlatformAbstractions to Acoustics.Shared because it looks like it exposes some useful platform detection code. Work done for #196 --- build/azure-pipelines-build.yml | 3 ++- src/AP.VersionBuild.targets | 4 +++- src/Acoustics.Shared/Acoustics.Shared.csproj | 1 + src/Acoustics.Shared/AppConfigHelper.cs | 21 ++++++++++++++++++ src/AssemblyMetadata.cs.template | 4 ++++ src/git_version.ps1 | 9 +++++++- tests/Acoustics.Test/RuntimesTests.cs | 22 +++++++++++++++++-- .../Acoustics.Test/TestHelpers/Assertions.cs | 18 ++++++++++----- 8 files changed, 71 insertions(+), 11 deletions(-) diff --git a/build/azure-pipelines-build.yml b/build/azure-pipelines-build.yml index 63326d698..450fdf092 100644 --- a/build/azure-pipelines-build.yml +++ b/build/azure-pipelines-build.yml @@ -124,7 +124,7 @@ jobs: condition: succeededOrFailed() inputs: command: test - projects: tests/AED.Test/AED.Test.csproj + projects: tests/AED.Test/AED.Test.fsproj publishTestResults: true testRunTitle: "AED.Test for $(rid) $(configuration)" arguments: > @@ -138,6 +138,7 @@ jobs: displayName: Run AED.Test - task: DotNetCoreCLI@2 + condition: succeededOrFailed() inputs: command: custom custom: tool diff --git a/src/AP.VersionBuild.targets b/src/AP.VersionBuild.targets index d3f5681b7..85737c4e1 100644 --- a/src/AP.VersionBuild.targets +++ b/src/AP.VersionBuild.targets @@ -3,12 +3,13 @@ - + @@ -20,6 +21,7 @@ + all runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Acoustics.Shared/AppConfigHelper.cs b/src/Acoustics.Shared/AppConfigHelper.cs index 2e1cd9386..55e1b1eff 100644 --- a/src/Acoustics.Shared/AppConfigHelper.cs +++ b/src/Acoustics.Shared/AppConfigHelper.cs @@ -11,10 +11,12 @@ namespace Acoustics.Shared { using System; using System.Collections.Immutable; + using System.Diagnostics; using System.IO; using System.Reflection; using System.Runtime.InteropServices; using log4net; + using Microsoft.DotNet.PlatformAbstractions; using Mono.Unix.Native; using static System.Runtime.InteropServices.OSPlatform; using static System.Runtime.InteropServices.RuntimeInformation; @@ -162,6 +164,19 @@ Architecture.Arm when IsOSPlatform(Linux) => LinuxArm, #pragma warning restore 162 }; + public static string RuntimeIdentifier + { + get + { + // TODO: replace PseudoRuntimeIdentifier with this property once stable + string rid = Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment.GetRuntimeIdentifier(); + Debug.Assert( + rid == PseudoRuntimeIdentifier, + $"Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment.GetRuntimeIdentifier() `{rid}` does not match PseudoRuntimeIdentifier `{PseudoRuntimeIdentifier}`"); + return rid; + } + } + internal static string GetExeFile(string name, bool required = true) { var isWindows = IsOSPlatform(Windows); @@ -256,10 +271,14 @@ internal static string FindProgramInPath(string program) /// private static void CheckOs(ref bool isWindows, ref bool isLinux, ref bool isMacOsX) { + // TODO: theres a new API that should deprecate this + var platform = Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment.OperatingSystemPlatform; + var winDir = Environment.GetEnvironmentVariable("windir"); if (!string.IsNullOrEmpty(winDir) && winDir.Contains(@"\") && Directory.Exists(winDir)) { isWindows = true; + Debug.Assert(platform == Platform.Windows, "Our manual check for the Windows platform disagrees with .NET"); } else if (File.Exists(@"/proc/sys/kernel/ostype")) { @@ -268,6 +287,7 @@ private static void CheckOs(ref bool isWindows, ref bool isLinux, ref bool isMac { // Note: Android gets here too isLinux = true; + Debug.Assert(platform == Platform.Linux, "Our manual check for the Linux platform disagrees with .NET"); } else { @@ -278,6 +298,7 @@ private static void CheckOs(ref bool isWindows, ref bool isLinux, ref bool isMac { // Note: iOS gets here too isMacOsX = true; + Debug.Assert(platform == Platform.Darwin, "Our manual check for the Darwin platform disagrees with .NET"); } else { diff --git a/src/AssemblyMetadata.cs.template b/src/AssemblyMetadata.cs.template index 6b0f0d04e..62d7bf045 100644 --- a/src/AssemblyMetadata.cs.template +++ b/src/AssemblyMetadata.cs.template @@ -41,6 +41,10 @@ namespace AnalysisPrograms public const bool IsDirty = ${is_dirty}; public const string CiBuild = "${build_number}"; + public const bool CompiledAsSelfContained = ${self_contained}; + public const string CompiledRuntimeIdentifer = "${runtime_identifer}"; + + public static readonly Version Version = new Version("${version}"); } } \ No newline at end of file diff --git a/src/git_version.ps1 b/src/git_version.ps1 index 08ca8fa7d..bb3da4d6c 100644 --- a/src/git_version.ps1 +++ b/src/git_version.ps1 @@ -3,7 +3,9 @@ #Requires -Version 6 param( - [string]$configuration = "Release" + [string]$configuration = "Release", + [string]$self_contained, + [string]$runtime_indentifier ) Push-Location @@ -23,6 +25,8 @@ if ($build_type -ieq "Debug") { } } +$self_contained = if ($self_contained -eq 'true') { 'true' } else { 'false' } + $commit_hash = git show -s --format="%H" $branch = git rev-parse --abbrev-ref HEAD @@ -66,6 +70,9 @@ IsDirty=$is_dirty Version=$version InformationalVersion=$informational_version GeneratedMetadata=$metadata_file +CacheWarning=$cache_warning +MsBuildSelfContained=$self_contained +MsBuildRuntimeIdentifer=$runtime_identifer "@ Write-Output $props diff --git a/tests/Acoustics.Test/RuntimesTests.cs b/tests/Acoustics.Test/RuntimesTests.cs index a41cc2445..7e81e7569 100644 --- a/tests/Acoustics.Test/RuntimesTests.cs +++ b/tests/Acoustics.Test/RuntimesTests.cs @@ -32,7 +32,16 @@ public void TestRequiredSqliteLibsCopiedToBuildDir(string rid, string expected) Assert.That.DirectoryExists(buildDir); - Assert.That.FileExists(Path.GetFullPath(Path.Combine(buildDir, "runtimes", expected))); +#pragma warning disable IDE0035, CS0162 + if (BuildMetadata.CompiledAsSelfContained) + { + Assert.That.FileExists(Path.Combine(buildDir, Path.GetFileName(expected))); + } + else + { + Assert.That.FileExists(Path.Combine(buildDir, "runtimes", expected)); + } +#pragma warning restore IDE0035, CS0162 } [RuntimeIdentifierSpecificDataTestMethod] @@ -55,7 +64,16 @@ public void TestRequiredMonoPosixDllCopiedToBuildDir(string rid, string expected Assert.That.DirectoryExists(buildDir); - Assert.That.FileExists(Path.GetFullPath(Path.Combine(buildDir, "runtimes", expected))); +#pragma warning disable IDE0035, CS0162 + if (BuildMetadata.CompiledAsSelfContained) + { + Assert.That.FileExists(Path.Combine(buildDir, Path.GetFileName(expected))); + } + else + { + Assert.That.FileExists(Path.Combine(buildDir, "runtimes", expected)); + } +#pragma warning restore IDE0035, CS0162 } [TestMethod] diff --git a/tests/Acoustics.Test/TestHelpers/Assertions.cs b/tests/Acoustics.Test/TestHelpers/Assertions.cs index d28f30a51..c5a78bbb4 100644 --- a/tests/Acoustics.Test/TestHelpers/Assertions.cs +++ b/tests/Acoustics.Test/TestHelpers/Assertions.cs @@ -214,9 +214,12 @@ public static void DirectoryExists(this Assert assert, DirectoryInfo directory) public static void DirectoryExists(this Assert assert, string path) { - Assert.IsTrue( - Directory.Exists(Path.GetFullPath(path)), - $"Expected path {path} to exist but it could not be found"); + var exists = Directory.Exists(Path.GetFullPath(path)); + if (!exists) + { + PathDiagnostics.PathExistsOrDiff(path, out var diff); + Assert.Fail($"Expected path {path} to exist but it could not be found. Path diagnostics:\n{diff.Messages}"); + } } public static void FileExists(this Assert assert, FileInfo file) @@ -226,9 +229,12 @@ public static void FileExists(this Assert assert, FileInfo file) public static void FileExists(this Assert assert, string path) { - Assert.IsTrue( - File.Exists(Path.GetFullPath(path)), - $"Expected path {path} to exist but it could not be found"); + var exists = File.Exists(Path.GetFullPath(path)); + if (!exists) + { + PathDiagnostics.PathExistsOrDiff(path, out var diff); + Assert.Fail($"Expected path {path} to exist but it could not be found. Path diagnostics:\n{diff.Messages}"); + } } public static void FileNotExists(this Assert assert, FileInfo file)