Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ private string ResolveJsonFileName(string template)
}

private static string GetTargetFrameworkMoniker()
=> TargetFrameworkParser.GetShortTargetFramework(
Assembly.GetEntryAssembly()?.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkDisplayName)
?? TargetFrameworkParser.GetShortTargetFramework(RuntimeInformation.FrameworkDescription)
=> TargetFrameworkParser.GetShortTargetFrameworkIncludingPlatform(Assembly.GetEntryAssembly())
?? "unknown";

// CTRF `osPlatform` is the short Node-style platform identifier (e.g. "win32",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,72 @@ internal static class TargetFrameworkParser

return frameworkDescription;
}

/// <summary>
/// Resolves the short target framework moniker of <paramref name="entryAssembly"/>, including the
/// OS-platform component (e.g. <c>net8.0-windows10.0.18362.0</c>) when the assembly was built for an
/// OS-specific TFM.
Comment thread
Evangelink marked this conversation as resolved.
Resolved
/// </summary>
/// <remarks>
/// A plain <c>net8.0</c> build and a <c>net8.0-windows10.0.18362.0</c> build carry the exact same
/// <see cref="TargetFrameworkAttribute"/> (<c>.NETCoreApp,Version=v8.0</c>) and produce the same
/// <see cref="RuntimeInformation.FrameworkDescription"/>, so the short TFM alone cannot tell them apart.
/// The only runtime-visible signal is <c>System.Runtime.Versioning.TargetPlatformAttribute</c>, which the
/// SDK emits for any platform-specific TFM (including non-OS / custom platform identifiers such as Uno's
/// <c>browserwasm</c>). Appending it here keeps report file names unique per build so two modules of the
/// same assembly no longer overwrite each other's report.
/// </remarks>
public static string? GetShortTargetFrameworkIncludingPlatform(Assembly? entryAssembly)
{
string? shortTargetFramework = GetShortTargetFramework(entryAssembly?.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkDisplayName);

// FrameworkDisplayName can be missing, empty, or whitespace for some assemblies (e.g. when the SDK
// emits a TargetFrameworkAttribute without a display name for a custom TargetFrameworkIdentifier such
// as Uno's net8.0-browserwasm). In that case GetShortTargetFramework echoes the empty value back rather
// than returning null, so a plain null-coalesce would not fall back. Treat null/empty/whitespace alike
// and fall back to the runtime description so the base moniker stays meaningful (e.g. net8.0) and we
// never produce a dangling "-platform" name.
if (RoslynString.IsNullOrWhiteSpace(shortTargetFramework))
{
shortTargetFramework = GetShortTargetFramework(RuntimeInformation.FrameworkDescription);
}

return BuildTargetFrameworkMoniker(shortTargetFramework, GetTargetPlatformName(entryAssembly));
}

/// <summary>
/// Combines a short target framework (e.g. <c>net8.0</c>) with an optional OS-platform name
/// (e.g. <c>Windows10.0.18362.0</c>) into a full moniker (e.g. <c>net8.0-windows10.0.18362.0</c>).
/// </summary>
internal static string? BuildTargetFrameworkMoniker(string? shortTargetFramework, string? targetPlatformName)
=> RoslynString.IsNullOrEmpty(shortTargetFramework) || RoslynString.IsNullOrEmpty(targetPlatformName)
? shortTargetFramework
: $"{shortTargetFramework}-{targetPlatformName.ToLowerInvariant()}";
Comment thread
Copilot marked this conversation as resolved.
Resolved

/// <summary>
/// Reads the OS-platform name from <c>System.Runtime.Versioning.TargetPlatformAttribute</c> on
/// <paramref name="entryAssembly"/>, or <see langword="null"/> when the assembly targets no specific OS.
/// </summary>
internal static string? GetTargetPlatformName(Assembly? entryAssembly)
{
if (entryAssembly is null)
{
return null;
}

// TargetPlatformAttribute only exists in the .NET 5+ BCL, so read it by full type name through
// CustomAttributeData to keep this method compiling for netstandard2.0 / net462 consumers.
foreach (CustomAttributeData attribute in entryAssembly.GetCustomAttributesData())
{
if (string.Equals(attribute.AttributeType.FullName, "System.Runtime.Versioning.TargetPlatformAttribute", StringComparison.Ordinal)
&& attribute.ConstructorArguments.Count == 1
&& attribute.ConstructorArguments[0].Value is string platformName
&& platformName.Length > 0)
Comment thread
Copilot marked this conversation as resolved.
Resolved
{
return platformName;
}
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public static Dictionary<string, string> GetStandardReplacements(string processN
string? asmName = Assembly.GetEntryAssembly()?.GetName().Name;
replacements["asm"] = asmName ?? "unknown";

string? tfm = TargetFrameworkParser.GetShortTargetFramework(Assembly.GetEntryAssembly()?.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkDisplayName)
?? TargetFrameworkParser.GetShortTargetFramework(RuntimeInformation.FrameworkDescription);
string? tfm = TargetFrameworkParser.GetShortTargetFrameworkIncludingPlatform(Assembly.GetEntryAssembly());
replacements["tfm"] = tfm ?? "unknown";

return replacements;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace Microsoft.Testing.Extensions;
internal static class TargetFrameworkMonikerHelper
{
public static string GetTargetFrameworkMoniker()
=> TargetFrameworkParser.GetShortTargetFramework(Assembly.GetEntryAssembly()?.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkDisplayName)
?? TargetFrameworkParser.GetShortTargetFramework(RuntimeInformation.FrameworkDescription)
=> TargetFrameworkParser.GetShortTargetFrameworkIncludingPlatform(Assembly.GetEntryAssembly())
?? "unknown";
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#if NET
using System.Reflection.Emit;
#endif

using Microsoft.Testing.Platform.OutputDevice;

namespace Microsoft.Testing.Platform.UnitTests;
Expand Down Expand Up @@ -36,4 +40,121 @@ public void ParseNETCore(string longName, string expectedShortName)
[TestMethod]
public void ParseNET(string longName, string expectedShortName)
=> Assert.AreEqual(expectedShortName, TargetFrameworkParser.GetShortTargetFramework(longName));

[TestMethod]
public void BuildTargetFrameworkMoniker_WithoutPlatform_ReturnsShortTargetFramework()
=> Assert.AreEqual("net8.0", TargetFrameworkParser.BuildTargetFrameworkMoniker("net8.0", null));

[TestMethod]
public void BuildTargetFrameworkMoniker_WithEmptyPlatform_ReturnsShortTargetFramework()
=> Assert.AreEqual("net8.0", TargetFrameworkParser.BuildTargetFrameworkMoniker("net8.0", string.Empty));

[TestMethod]
public void BuildTargetFrameworkMoniker_WithPlatform_AppendsLowercasedPlatform()
=> Assert.AreEqual("net8.0-windows10.0.18362.0", TargetFrameworkParser.BuildTargetFrameworkMoniker("net8.0", "Windows10.0.18362.0"));

[TestMethod]
public void BuildTargetFrameworkMoniker_WithNullShortTargetFramework_ReturnsNull()
=> Assert.IsNull(TargetFrameworkParser.BuildTargetFrameworkMoniker(null, "Windows10.0.18362.0"));

[TestMethod]
public void BuildTargetFrameworkMoniker_WithEmptyShortTargetFramework_DoesNotEmitDanglingPlatform()
=> Assert.AreEqual(string.Empty, TargetFrameworkParser.BuildTargetFrameworkMoniker(string.Empty, "BrowserWasm1.0"));

[TestMethod]
public void GetTargetPlatformName_WithNullAssembly_ReturnsNull()
=> Assert.IsNull(TargetFrameworkParser.GetTargetPlatformName(null));

#if NET
[TestMethod]
public void GetTargetPlatformName_WithTargetPlatformAttribute_ReturnsPlatformName()
{
Assembly dynamicAssembly = CreateAssemblyWithTargetPlatform("Windows10.0.18362.0");
Assert.AreEqual("Windows10.0.18362.0", TargetFrameworkParser.GetTargetPlatformName(dynamicAssembly));
}

[TestMethod]
public void GetTargetPlatformName_WithoutTargetPlatformAttribute_ReturnsNull()
{
Assembly dynamicAssembly = CreateAssemblyWithoutTargetPlatform();
Assert.IsNull(TargetFrameworkParser.GetTargetPlatformName(dynamicAssembly));
}

[TestMethod]
public void GetTargetPlatformName_WithEmptyPlatformValue_ReturnsNull()
{
Assembly dynamicAssembly = CreateAssemblyWithTargetPlatform(string.Empty);
Assert.IsNull(TargetFrameworkParser.GetTargetPlatformName(dynamicAssembly));
}

[TestMethod]
public void GetShortTargetFrameworkIncludingPlatform_WithTargetPlatformAttribute_AppendsLowercasedPlatform()
{
Assembly dynamicAssembly = CreateAssemblyWithTargetPlatform("Windows10.0.18362.0");

// The dynamic assembly carries no TargetFrameworkAttribute, so the parser falls back to
// RuntimeInformation.FrameworkDescription. The point of this test is to verify the platform
// discriminator gets appended (and lowercased) on top of whatever the .NET runtime reports.
string? result = TargetFrameworkParser.GetShortTargetFrameworkIncludingPlatform(dynamicAssembly);

Assert.IsNotNull(result);
Assert.EndsWith("-windows10.0.18362.0", result);
}

[TestMethod]
public void GetShortTargetFrameworkIncludingPlatform_WithoutTargetPlatformAttribute_ReturnsShortTargetFrameworkOnly()
{
Assembly dynamicAssembly = CreateAssemblyWithoutTargetPlatform();

string? result = TargetFrameworkParser.GetShortTargetFrameworkIncludingPlatform(dynamicAssembly);

Assert.IsNotNull(result);
Assert.DoesNotContain("-", result);
}

[TestMethod]
public void GetShortTargetFrameworkIncludingPlatform_WithEmptyFrameworkDisplayName_FallsBackToRuntimeBase()
{
// Simulates a build whose TargetFrameworkAttribute carries an empty FrameworkDisplayName (as can happen
// for a custom TargetFrameworkIdentifier such as Uno's net8.0-browserwasm). The base must fall back to
// the runtime description (e.g. net8.0) instead of leaving a dangling "-browserwasm1.0" name.
Assembly dynamicAssembly = CreateAssemblyWithFrameworkDisplayNameAndTargetPlatform(string.Empty, "BrowserWasm1.0");

string? result = TargetFrameworkParser.GetShortTargetFrameworkIncludingPlatform(dynamicAssembly);

Assert.IsNotNull(result);
Assert.StartsWith("net", result);
Assert.EndsWith("-browserwasm1.0", result);
}

private static Assembly CreateAssemblyWithTargetPlatform(string platformName)
{
var name = new AssemblyName($"TestAssembly_{Guid.NewGuid():N}");
var builder = AssemblyBuilder.DefineDynamicAssembly(name, AssemblyBuilderAccess.RunAndCollect);

ConstructorInfo ctor = typeof(System.Runtime.Versioning.TargetPlatformAttribute).GetConstructor([typeof(string)])!;
builder.SetCustomAttribute(new CustomAttributeBuilder(ctor, [platformName]));
return builder;
}

private static Assembly CreateAssemblyWithFrameworkDisplayNameAndTargetPlatform(string frameworkDisplayName, string platformName)
{
var name = new AssemblyName($"TestAssembly_{Guid.NewGuid():N}");
var builder = AssemblyBuilder.DefineDynamicAssembly(name, AssemblyBuilderAccess.RunAndCollect);

ConstructorInfo frameworkCtor = typeof(System.Runtime.Versioning.TargetFrameworkAttribute).GetConstructor([typeof(string)])!;
PropertyInfo displayNameProperty = typeof(System.Runtime.Versioning.TargetFrameworkAttribute).GetProperty(nameof(System.Runtime.Versioning.TargetFrameworkAttribute.FrameworkDisplayName))!;
builder.SetCustomAttribute(new CustomAttributeBuilder(frameworkCtor, [".NETCoreApp,Version=v8.0"], [displayNameProperty], [frameworkDisplayName]));

ConstructorInfo platformCtor = typeof(System.Runtime.Versioning.TargetPlatformAttribute).GetConstructor([typeof(string)])!;
builder.SetCustomAttribute(new CustomAttributeBuilder(platformCtor, [platformName]));
return builder;
}

private static Assembly CreateAssemblyWithoutTargetPlatform()
{
var name = new AssemblyName($"TestAssembly_{Guid.NewGuid():N}");
return AssemblyBuilder.DefineDynamicAssembly(name, AssemblyBuilderAccess.RunAndCollect);
}
#endif
}