Skip to content

Commit

Permalink
Fixes tests for SelfContained/platform-specific builds
Browse files Browse the repository at this point in the history
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
  • Loading branch information
atruskie committed Mar 25, 2020
1 parent 8b8b9e9 commit 3269c78
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 11 deletions.
3 changes: 2 additions & 1 deletion build/azure-pipelines-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: >
Expand All @@ -138,6 +138,7 @@ jobs:
displayName: Run AED.Test

- task: DotNetCoreCLI@2
condition: succeededOrFailed()
inputs:
command: custom
custom: tool
Expand Down
4 changes: 3 additions & 1 deletion src/AP.VersionBuild.targets
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

<!-- Register our task that as something to run before standard build target -->
<Target Name="APVersionBeforeBuild" BeforeTargets="PrepareForBuild">
<Exec Command="pwsh –NonInteractive -noprofile $(ProjectDir)../git_version.ps1 -configuration $(Configuration)"
<Exec Command="pwsh –NonInteractive -noprofile $(ProjectDir)../git_version.ps1 -configuration $(Configuration) -self_contained $(SelfContained) -runtime_identifier $(RuntimeIdentifier)"
ConsoleToMSBuild="True"
EchoOff="false"
StandardOutputImportance="low" >
<Output TaskParameter="ConsoleOutput" ItemName="VersionMetadata" />
</Exec>
<Message Text="[APVersionBeforeBuild] %(VersionMetadata.Indentity)" Importance="High" />
<!-- batch through returned metadata, then use ✨MAGIC✨ to convert subexpression to string, then split on key/value -->
<!-- and finally create each property -->
<CreateProperty Value="$([System.String]::Copy(`%(VersionMetadata.Identity)`).Split('=')[1].Trim())">
Expand All @@ -20,6 +21,7 @@
<Message Text="[APVersionBeforeBuild] Using assembly version with: $(Version)!" Importance="High" />
<Message Text="[APVersionBeforeBuild] InformationalVersion: $(InformationalVersion)" Importance="High" />
<Message Text="[APVersionBeforeBuild] BuildDate: $(BuildDate)" Importance="High" />
<Message Text="[APVersionBeforeBuild] SelfContained: $(MsBuildSelfContained), Build RuntimeIdentifier:$(MsBuildRuntimeIdentifer)" Importance="High" />
<Message Text="[APVersionBeforeBuild] git_version.ps1 stdout: %(VersionMetadata.Identity)" Importance="High" Condition="$(GeneratedMetadata) == ''"/>

<Error Text="AP Version metadata generation failed. Check you have PowerShell 6+ &amp; Git installed and available on the system PATH."
Expand Down
1 change: 1 addition & 0 deletions src/Acoustics.Shared/Acoustics.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.DotNet.PlatformAbstractions" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.2" />
<PackageReference Include="Mono.Posix.NETStandard" Version="1.0.0" />
Expand Down
21 changes: 21 additions & 0 deletions src/Acoustics.Shared/AppConfigHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -256,10 +271,14 @@ internal static string FindProgramInPath(string program)
/// </summary>
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"))
{
Expand All @@ -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
{
Expand All @@ -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
{
Expand Down
4 changes: 4 additions & 0 deletions src/AssemblyMetadata.cs.template
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
}
}
9 changes: 8 additions & 1 deletion src/git_version.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#Requires -Version 6

param(
[string]$configuration = "Release"
[string]$configuration = "Release",
[string]$self_contained,
[string]$runtime_indentifier
)

Push-Location
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
22 changes: 20 additions & 2 deletions tests/Acoustics.Test/RuntimesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand Down
18 changes: 12 additions & 6 deletions tests/Acoustics.Test/TestHelpers/Assertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 3269c78

Please sign in to comment.