Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
cklutz committed Oct 21, 2024
1 parent 68b1218 commit ac18b7b
Showing 4 changed files with 37 additions and 40 deletions.
11 changes: 1 addition & 10 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -11,16 +11,7 @@
*.userprefs

# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
build/
bld/
[Bb]in/
[Oo]bj/
artifacts/

# Visual Studo 2015 cache/options directory
.vs/
3 changes: 3 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<Project>
<PropertyGroup>
<UseArtifactsOutput>true</UseArtifactsOutput>
<ArtifactsPath>$(MSBuildThisFileDirectory)artifacts</ArtifactsPath>

<TargetFrameworks>net8.0;net481</TargetFrameworks>

<Nullable Condition="'$(TargetFramework)' == 'net8.0'">enable</Nullable>
1 change: 1 addition & 0 deletions LockCheck.sln
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{018E2998-2375-4819-9B5A-554C4F46BC1F}"
ProjectSection(SolutionItems) = preProject
test\test-docker-linux.ps1 = test\test-docker-linux.ps1
test\test-linux.sh = test\test-linux.sh
test\test-windows.ps1 = test\test-windows.ps1
EndProjectSection
EndProject
62 changes: 32 additions & 30 deletions test/LockCheck.Tests/Tooling/TestHelper.cs
Original file line number Diff line number Diff line change
@@ -3,9 +3,11 @@
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using LockCheck.Windows;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace LockCheck.Tests.Tooling
@@ -202,14 +204,13 @@ private static string GetClientFullPath(bool target64Bit, out string? hostExecut
//
// Assume that LockCheck.Tests (this assembly) is located as follows:
//
// C:\...\test\LockCheck.Tests\bin\<Configuration>\<TargetFramework>[\<RID>] (=> AppContext.BaseDirectory)
// C:\...\artifacts\bin\LockCheck.Tests\<Pivot[_RID]> (=> AppContext.BaseDirectory)
//
// Note that sometimes this path ends with the RID, sometimes it doesn't. That depends
// on how the build/tests are invoked. So we have to cater for both possibilities.
// Depending on how we build (VS vs. dotnet build vs. publish), the <Pivot> might end with the <RID> or not.
//
// Assume further, that LCTestTarget projects are located as follows:
//
// C:\...\test\LCTestTarget.<TargetRID>\bin\<Configuration>\<TargetFramework>\<TargetRid>\LCTestTarget<Extension>
// C:\...\artifacts\bin\LCTestTarget.<TargetRID>\<Pivot_TargetRid>\LCTestTarget<Extension>
//
// So, we want to run LCTestTarget from the same <TargetFramework> and <Configuration> as this assembly,
// but obviously using the <TargetRid> requested.
@@ -219,43 +220,44 @@ private static string GetClientFullPath(bool target64Bit, out string? hostExecut
// Assume that the project (directory name) matches the assembly name.
string projectName = typeof(TestHelper).Assembly.GetName().Name!;

// Get the part of the path before the project directory (e.g. "C:\...\test")
if (!TryGetPathBeforeLastSegment(AppContext.BaseDirectory, projectName, out var baseDir))
// Get the part of the path before the project directory (e.g. "C:\...\artifacts\bin")
if (!TryGetPathBeforeLastSegment(AppContext.BaseDirectory, projectName, out var artifactsBaseBinDir))
{
throw new InvalidOperationException($"Failed to get project base directory from '{AppContext.BaseDirectory}' and '{projectName}'.");
}

// Get the part of the path after the project directory (e.g. "\bin\<Configuration>\<TargetFramework>[\<RID>]")
if (!TryGetPathAfterLastSegment(AppContext.BaseDirectory, projectName, out var outputDirString))
// Get the part of the path after the project directory (e.g. "<Pivot[_RID]>")
if (!TryGetPathAfterLastSegment(AppContext.BaseDirectory, projectName, out var pivot))
{
throw new InvalidOperationException($"Failed to get output directory from '{AppContext.BaseDirectory}' and '{projectName}'.");
}

// See if the path after the project directory ends with the RID (of this assembly)..
var outputDir = outputDirString.AsSpan().Trim(separators);
var currentRid = GetCurrentRuntimeIdentifier().AsSpan();
if (outputDir.ToString().EndsWith(currentRid.ToString()))
// Split "pivot" into parts (might be either like "<configuration>_<tfm>_<rid>" or "<configuration>_<tfm>"
var pivotSpan = pivot.AsSpan().Trim(separators);
int pos = pivotSpan.LastIndexOf('_');
Debug.Assert(pos != -1);
if (MemoryExtensions.Equals(pivotSpan.Slice(pos + 1), GetCurrentRuntimeIdentifier(), StringComparison.OrdinalIgnoreCase))
{
// Remove the RID from the path, so that next we can always assume the same segment
// position for <Configuration> and <TargetFramework>.
outputDir = outputDir.Slice(0, outputDir.Length - currentRid.Length).Trim(separators);
// Strip off current RID of LockCheck.Tests - we don't need it.
pivotSpan = pivotSpan.Slice(0, pos);
// Readjust position to delimiter between configuration and tfm
pos = pivotSpan.LastIndexOf('_');
Debug.Assert(pos != -1);
}

var configuration = pivotSpan.Slice(0, pos);
var targetFramework = pivotSpan.Slice(pos + 1);

// Now, outputDir should look like this bin\<Configuration>\<TargetFramework>
int pos = outputDir.LastIndexOfAny(separators);
Debug.Assert(pos != -1);
var targetFramework = outputDir.Slice(pos + 1);
outputDir = outputDir.Slice(0, pos).Trim(separators);
pos = outputDir.LastIndexOfAny(separators);
var configuration = outputDir.Slice(pos + 1);
#if NET
var targetPivot = $"{configuration}_{targetFramework}_{targetRid}";
#else
var targetPivot = string.Concat(configuration.ToString(), "_", targetFramework.ToString(), "_", targetRid);
#endif

string clientFullPath = Path.Combine(
baseDir.ToString(),
artifactsBaseBinDir,
$"LCTestTarget.{targetRid}",
"bin",
configuration.ToString(),
targetFramework.ToString(),
targetRid,
targetPivot,
$"LCTestTarget{extension}");

clientFullPath = Path.GetFullPath(clientFullPath);
@@ -432,7 +434,7 @@ internal static bool TryGetPathAfterLastSegment(string path, string segment, [No
return false;
}

internal static string GetCurrentRuntimeIdentifier()
internal static ReadOnlySpan<char> GetCurrentRuntimeIdentifier()
{
#if NET
return RuntimeInformation.RuntimeIdentifier;
@@ -441,11 +443,11 @@ internal static string GetCurrentRuntimeIdentifier()

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return $"win-{RuntimeInformation.ProcessArchitecture.ToString().ToLowerInvariant()}";
return $"win-{RuntimeInformation.ProcessArchitecture.ToString().ToLowerInvariant()}".AsSpan();
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return $"linux-{RuntimeInformation.ProcessArchitecture.ToString().ToLowerInvariant()}";
return $"linux-{RuntimeInformation.ProcessArchitecture.ToString().ToLowerInvariant()}".AsSpan();
}

throw new InvalidOperationException($"Cannot get RID for '{RuntimeInformation.OSDescription}' and '{RuntimeInformation.ProcessArchitecture}'.");

0 comments on commit ac18b7b

Please sign in to comment.