Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
316 changes: 316 additions & 0 deletions docs/decisions/explicit-mstest-packages.md

Large diffs are not rendered by default.

35 changes: 26 additions & 9 deletions eng/bazel/MSTestPackageVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
using System.Text;
using System.Text.Json.Nodes;

if (args.Length != 3)
bool exactFramework = args.Length == 4 && args[3] == "--exact-framework";
if (args.Length != 3 && !exactFramework)
{
throw new ArgumentException("Expected <package-request-template> <platform-version-file> <package-request-output>.");
throw new ArgumentException("Expected <package-request-template> <dependency-version-file> <package-request-output> [--exact-framework].");
}

string version = File.ReadAllText(args[1]).Trim();
Expand All @@ -25,26 +26,42 @@
?? throw new InvalidDataException("Expected a package request object.");
if (request["dependencyGroups"] is not JsonArray groups || groups.Count == 0)
{
throw new InvalidDataException("Expected nonempty platform dependency groups.");
throw new InvalidDataException("Expected nonempty dependency groups.");
}

if (exactFramework)
{
if (request["metadata"]?["id"]?.GetValue<string>() is not ("MSTest.Uwp" or "MSTest.WinUI"))
{
throw new InvalidDataException("Only UI packages may pin the matching framework dependency.");
}

string ownVersionFile = request["versionFile"]?.GetValue<string>()
?? throw new InvalidDataException("The UI package must declare its own version file.");
if (File.ReadAllText(ownVersionFile).Trim() != version)
{
throw new InvalidDataException("The UI package and core dependency versions must match exactly.");
}
}

string dependencyId = exactFramework ? "MSTest.TestFramework" : "Microsoft.Testing.Platform";
foreach (JsonNode? group in groups)
{
if (group?["dependencies"] is not JsonArray dependencies)
{
throw new InvalidDataException("A package dependency group is missing its dependencies.");
}

JsonObject[] platform = dependencies.OfType<JsonObject>()
.Where(dependency => dependency["id"]?.GetValue<string>() == "Microsoft.Testing.Platform")
JsonObject[] bounded = dependencies.OfType<JsonObject>()
.Where(dependency => dependency["id"]?.GetValue<string>() == dependencyId)
.ToArray();
if (platform.Length != 1)
if (bounded.Length != 1)
{
throw new InvalidDataException("Each bounded group must declare exactly one platform dependency.");
throw new InvalidDataException($"Each bounded group must declare exactly one {dependencyId} dependency.");
}

_ = platform[0].Remove("versionFile");
platform[0]["version"] = $"[{version}, {nextMajor}.0.0)";
_ = bounded[0].Remove("versionFile");
bounded[0]["version"] = exactFramework ? $"[{version}]" : $"[{version}, {nextMajor}.0.0)";
}

File.WriteAllText(args[2], request.ToJsonString(), new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
42 changes: 35 additions & 7 deletions eng/bazel/mstest-consumer/NativePackageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,19 @@ public void NativeAssembliesAreLoadedFromConsumer()

string evidence = Environment.GetEnvironmentVariable("TESTFX_BAZEL_CONSUMER_EVIDENCE")
?? throw new InvalidOperationException("The consumer evidence path was not supplied.");
File.WriteAllText(evidence, JsonSerializer.Serialize(new
string reportPath = Path.Join(Path.GetDirectoryName(Path.GetFullPath(evidence))!, "loaded-assemblies-" + Guid.NewGuid().ToString("N") + ".json");
using (FileStream report = File.Open(reportPath, FileMode.CreateNew, FileAccess.Write, FileShare.Read))
{
Runtime = RuntimeInformation.FrameworkDescription,
CoreLib = typeof(object).Assembly.Location,
Framework = expectations.RootElement.GetProperty("Framework").GetString(),
Assemblies = loaded,
}));
JsonSerializer.Serialize(report, new
{
Runtime = RuntimeInformation.FrameworkDescription,
CoreLib = typeof(object).Assembly.Location,
Framework = expectations.RootElement.GetProperty("Framework").GetString(),
Assemblies = loaded,
});
}

File.AppendAllLines(evidence, [reportPath]);
}

[TestMethod]
Expand All @@ -80,9 +86,31 @@ public void KoreanSatellitesAreCopiedAndLoadable()
}

[TestMethod]
public void PackagedTargetsProvideExtensionsAndParallelization()
public void PackagedTargetsProvideCoreAndParallelization()
{
Assert.AreEqual("MSTest.TestFramework", typeof(TestContext).Assembly.GetName().Name);
Assert.AreSame(typeof(Assert).Assembly, typeof(TestContext).Assembly);
Assert.AreSame(typeof(Assert).Assembly, typeof(DeploymentItemAttribute).Assembly);
Assert.IsTrue(File.Exists(Path.Join(AppContext.BaseDirectory, "MSTest.TestFramework.Extensions.dll")), "The core package must provide the native compatibility facade.");
Assembly compatibility = Assembly.Load("MSTest.TestFramework.Extensions");
Assert.AreSame(typeof(TestContext), compatibility.GetType(typeof(TestContext).FullName!));
Assert.AreSame(typeof(DeploymentItemAttribute), compatibility.GetType(typeof(DeploymentItemAttribute).FullName!));
Assert.IsFalse(compatibility.DefinedTypes.Any(type => type.IsPublic || type.IsNestedPublic));
Assert.IsTrue(compatibility.GetForwardedTypes().All(type => type.Assembly == typeof(Assert).Assembly));
string[] directories =
[
nameof(TestContext.TestRunDirectory),
nameof(TestContext.DeploymentDirectory),
nameof(TestContext.ResultsDirectory),
nameof(TestContext.TestRunResultsDirectory),
nameof(TestContext.TestResultsDirectory),
nameof(TestContext.TestTempDirectory),
];
foreach (string directory in directories)
{
Assert.IsNotNull(typeof(TestContext).GetProperty(directory), directory);
}

ParallelizeAttribute? parallelize = typeof(NativePackageTests).Assembly.GetCustomAttribute<ParallelizeAttribute>();
Assert.IsNotNull(parallelize);
Assert.AreEqual(ExecutionScope.MethodLevel, parallelize.Scope);
Expand Down
85 changes: 73 additions & 12 deletions eng/bazel/mstest-packages.bzl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""The portable MSTest package layouts, with explicitly owned bundled payloads."""
"""MSTest package layouts, with explicitly owned core and platform payloads."""

load(
"@rules_dotnet//dotnet/private:providers.bzl",
Expand All @@ -7,9 +7,15 @@ load(
"NuGetPackInfo",
)
load("@rules_dotnet//dotnet/private/rules/nuget_pack:layout.bzl", "extra_files_layout")
load(":defs.bzl", "PORTABLE_FRAMEWORKS")
load(":defs.bzl", "PORTABLE_FRAMEWORKS", "STANDARD_FRAMEWORKS")
load(":packages.bzl", "package_build_assets", "package_version")

def mstest_package_tfm(framework):
"""Match NuGet's Windows folder normalization without shortening classic UAP."""
if "-windows" in framework and len(framework.split("-windows")[1].split(".")) == 4:
return framework.removesuffix(".0")
return framework

def _frameworks_impl(_settings, attr):
return {
framework: {"@rules_dotnet//dotnet:target_framework": framework}
Expand Down Expand Up @@ -50,6 +56,7 @@ def _package_impl(ctx):
groups = []
version_files = [ctx.file.version_file]
platform_version_file = None
core_version_file = None
dependencies = []
for target in ctx.attr.deps:
package = target[NuGetPackInfo]
Expand All @@ -60,6 +67,10 @@ def _package_impl(ctx):
if not version_file:
fail("The native platform dependency must carry its generated version file.")
platform_version_file = version_file
if ctx.attr.exact_core_dependency and package.package_id == "MSTest.TestFramework":
if not version_file:
fail("The native core dependency must carry its generated version file.")
core_version_file = version_file
dependencies.append(struct(
id = package.package_id,
version = package.version or "",
Expand All @@ -69,19 +80,24 @@ def _package_impl(ctx):

if ctx.attr.bound_platform and not platform_version_file:
fail("A bounded package must declare the native platform dependency.")
if ctx.attr.exact_core_dependency and not core_version_file:
fail("An exact core package must declare the native framework dependency.")
if core_version_file and platform_version_file:
fail("Core pinning and platform bounds must use separate package contracts.")
if len(ctx.attr.directories) != len(ctx.attr.resource_directories):
fail("Each assembly directory needs an explicit satellite-resource policy.")
for framework in ctx.attr.target_frameworks:
targets = ctx.split_attr.libraries.get(framework, [])
if len(targets) != len(ctx.attr.directories):
fail("{} is missing native assembly outputs for {}".format(ctx.label, framework))
for index, target in enumerate(targets):
directory = ctx.attr.directories[index].replace("{tfm}", framework)
package_tfm = mstest_package_tfm(framework)
directory = ctx.attr.directories[index].replace("{tfm}", package_tfm)
resource_directory = ctx.attr.resource_directories[index]
# Adapter satellites are shared once, using the lowest portable TFM.
# Ordinary adapter satellites are shared once, from its first portable TFM.
if "{tfm}" not in resource_directory and framework != ctx.attr.target_frameworks[0]:
resource_directory = ""
name = _add_assembly(entries, target, framework, directory, resource_directory.replace("{tfm}", framework))
name = _add_assembly(entries, target, framework, directory, resource_directory.replace("{tfm}", package_tfm))
names[name] = True

external_dependencies = []
Expand All @@ -92,7 +108,7 @@ def _package_impl(ctx):
if info.nuget_info == None:
fail("{} is not an external NuGet dependency".format(target.label))
external_dependencies.append(struct(id = info.name, version = info.version, exclude = ["Build", "Analyzers"]))
groups.append(struct(targetFramework = framework, dependencies = dependencies + external_dependencies))
groups.append(struct(targetFramework = mstest_package_tfm(framework), dependencies = dependencies + external_dependencies))

for target in ctx.attr.task_libraries:
_add_assembly(entries, target, "netstandard2.0", "_MSBuildTasks/netstandard2.0", "_MSBuildTasks/netstandard2.0")
Expand All @@ -115,7 +131,8 @@ def _package_impl(ctx):
nupkg = ctx.actions.declare_file(ctx.label.name + "/" + ctx.label.name + ".nupkg")
nuspec = ctx.actions.declare_file(ctx.label.name + "/" + ctx.label.name + ".nuspec")
request = ctx.actions.declare_file(ctx.label.name + "/nuget_pack_request.json")
template = ctx.actions.declare_file(ctx.label.name + "/nuget_pack_request.template.json") if platform_version_file else request
range_version_file = core_version_file or platform_version_file
template = ctx.actions.declare_file(ctx.label.name + "/nuget_pack_request.template.json") if range_version_file else request
ctx.actions.write(template, json.encode(struct(
output = nupkg.path,
nuspecOutput = nuspec.path,
Expand All @@ -131,13 +148,18 @@ def _package_impl(ctx):
dependencyGroups = groups,
files = [struct(source = by_path[path].path, target = path) for path in sorted(by_path.keys())],
)))
if platform_version_file:
if range_version_file:
arguments = [template.path, range_version_file.path, request.path]
inputs = [template, range_version_file]
if core_version_file:
arguments.append("--exact-framework")
inputs.append(ctx.file.version_file)
ctx.actions.run(
executable = ctx.executable._range_tool,
arguments = [template.path, platform_version_file.path, request.path],
inputs = [template, platform_version_file],
arguments = arguments,
inputs = depset(inputs),
outputs = [request],
mnemonic = "TestFxPlatformDependencyRange",
mnemonic = "TestFxFrameworkDependencyRange" if core_version_file else "TestFxPlatformDependencyRange",
)
ctx.actions.run(
executable = ctx.attr._packer[DefaultInfo].files_to_run,
Expand Down Expand Up @@ -175,6 +197,7 @@ _package = rule(
"dependency_excludes": attr.string_list_dict(),
"nuget_deps": attr.label_list(providers = [DotnetAssemblyRuntimeInfo], cfg = _frameworks),
"bound_platform": attr.bool(),
"exact_core_dependency": attr.bool(),
"package_id": attr.string(mandatory = True),
"description": attr.string(mandatory = True),
"readme": attr.label(allow_single_file = True, mandatory = True),
Expand All @@ -191,7 +214,7 @@ def _asset(files, name, source, destination):
files[":" + name] = destination

def native_mstest_packages():
"""Declare portable packages using the explicit compatibility task wrappers."""
"""Declare ordinary packages and opt-in modern Windows platform packages."""
package_version(name = "mstest_package_version", property_name = "VersionPrefix")
framework_project = "src/TestFramework/TestFramework"
adapter_project = "src/Adapter/MSTest.TestAdapter"
Expand All @@ -212,6 +235,7 @@ def native_mstest_packages():
description = "Provides the libraries for writing tests with MSTest. MSTest is Microsoft supported Test Framework.",
readme = framework_project + "/PACKAGE.md",
libraries = [":framework", ":framework_compatibility"],
target_frameworks = STANDARD_FRAMEWORKS + ["net462"],
directories = ["lib/{tfm}", "lib/{tfm}"],
resource_directories = ["lib/{tfm}", ""],
files = framework_files,
Expand Down Expand Up @@ -256,6 +280,7 @@ def native_mstest_packages():
description = "Discovers and runs MSTest tests using VSTest or Microsoft Testing Platform. MSTest is Microsoft supported Test Framework.",
readme = adapter_project + "/PACKAGE.md",
libraries = [":adapter", ":platform_services"],
target_frameworks = PORTABLE_FRAMEWORKS,
directories = ["buildTransitive/{tfm}", "buildTransitive/{tfm}"],
resource_directories = ["buildTransitive/_localization", "buildTransitive/_localization"],
files = adapter_files,
Expand All @@ -273,3 +298,39 @@ def native_mstest_packages():
name = "mstest_package_closure",
srcs = [":mstest_packages", ":packages", ":analyzers_package"],
)

platform_packages = []
for flavor, package_id, framework in [
("uwp", "MSTest.Uwp", "net10.0-windows10.0.17763.0"),
("winui", "MSTest.WinUI", "net10.0-windows10.0.18362.0"),
]:
name = "mstest_" + flavor + "_package"
project = "src/TestFramework/" + package_id
tfm = mstest_package_tfm(framework)
files = {
adapter_project + "/buildTransitive/common/MSTest.TestAdapter.targets": "buildTransitive/" + tfm + "/" + package_id + ".targets",
adapter_project + "/buildTransitive/Parallelize.targets": "buildTransitive/" + tfm + "/Parallelize.targets",
"src/TestFramework/PlatformPackage/MSTest.Platform.props": "buildTransitive/" + tfm + "/" + package_id + ".props",
}
_package(
name = name,
package_id = package_id,
version_file = ":mstest_package_version",
description = "Provides UI-thread test attributes and matching MSTest adapters for " + package_id + ".",
readme = project + "/PACKAGE.md",
libraries = [":mstest_" + flavor, ":adapter_" + flavor, ":platform_services_" + flavor],
target_frameworks = [framework],
directories = ["lib/{tfm}", "buildTransitive/{tfm}", "buildTransitive/{tfm}"],
resource_directories = ["lib/{tfm}", "buildTransitive/{tfm}/_localization", "buildTransitive/{tfm}/_localization"],
files = files,
deps = [":framework_package", ":trx_abstractions_package", ":telemetry_package", ":platform_msbuild_package"],
exact_core_dependency = True,
nuget_deps = ["@paket.source//microsoft.testplatform.objectmodel"],
target_compatible_with = ["@platforms//os:windows"],
)
platform_packages.append(":" + name)
native.filegroup(
name = "mstest_platform_package_closure",
srcs = [":mstest_package_closure"] + platform_packages,
target_compatible_with = ["@platforms//os:windows"],
)
2 changes: 1 addition & 1 deletion eng/bazel/sdk-packages.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,6 @@ def sdk_packages():
testfx_test(
name = "sdk_package_tests",
binary = ":sdk_package_tests_app",
args = ["--minimum-expected-tests", "12"],
args = ["--minimum-expected-tests", "15"],
size = "medium",
)
Loading
Loading