Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix some tests by updating expectations for the new world
  • Loading branch information
baronfel committed Nov 17, 2025
commit 2b031cd634ae7a4385b5f79d0492f8a65f4da38b
56 changes: 19 additions & 37 deletions test/Microsoft.NET.Publish.Tests/RuntimeIdentifiersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,55 +259,37 @@ public void PublishRuntimeIdentifierOverridesUseCurrentRuntime()
}

[Theory]
[InlineData("PublishReadyToRun", true)]
[InlineData("PublishSingleFile", true)]
[InlineData("PublishTrimmed", true)]
[InlineData("PublishAot", true)]
[InlineData("PublishReadyToRun", false)]
[InlineData("PublishSingleFile", false)]
[InlineData("PublishTrimmed", false)]
public void SomePublishPropertiesInferSelfContained(string property, bool useFrameworkDependentDefaultTargetFramework)
[InlineData("PublishReadyToRun", ToolsetInfo.CurrentTargetFramework, false)] // R2R doesn't imply self-contained in 8 and above
[InlineData("PublishSingleFile", ToolsetInfo.CurrentTargetFramework, true)] // single-file implies self-contained
[InlineData("PublishTrimmed", ToolsetInfo.CurrentTargetFramework, true)] // trimming implies self-contained
[InlineData("PublishAot", ToolsetInfo.CurrentTargetFramework, true)] // AOT implies self-contained
[InlineData("PublishReadyToRun", "net7.0", true)] // R2R implies self-contained in .NET 7 and below
[InlineData("PublishSingleFile", "net7.0", true)] // single-file implies self-contained
[InlineData("PublishTrimmed", "net7.0", true)] // trimming implies self-contained
public void SomePublishPropertiesInferSelfContained(string property, string targetFramework, bool expectedSelfContainedValue)
{
// Note: there is a bug with PublishAot I think where this test will fail for Aot if the testname is too long. Do not make it longer.
var tfm = useFrameworkDependentDefaultTargetFramework ? ToolsetInfo.CurrentTargetFramework : "net7.0"; // net 7 is the last non FDD default TFM at the time of this PR.
var testProject = new TestProject()
{
IsExe = true,
TargetFrameworks = tfm,
TargetFrameworks = targetFramework,
};
testProject.AdditionalProperties[property] = "true";

testProject.RecordProperties("SelfContained");
var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: $"{property}-{useFrameworkDependentDefaultTargetFramework}");
testProject.RecordPropertiesBeforeTarget("Publish");
var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: $"{property}-{targetFramework}");

var publishCommand = new DotnetPublishCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name));
if (property == "PublishTrimmed" && !useFrameworkDependentDefaultTargetFramework)
{
publishCommand
.Execute()
.Should()
.Fail();
}
else
{
publishCommand
.Execute()
.Should()
.Pass();
}

var properties = testProject.GetPropertyValues(testAsset.TestRoot, targetFramework: tfm, configuration: useFrameworkDependentDefaultTargetFramework ? "Release" : "Debug");

var expectedSelfContainedValue = "true";
if (
(property == "PublishReadyToRun" && useFrameworkDependentDefaultTargetFramework) || // This property should no longer infer SelfContained in net 8
(property == "PublishTrimmed" && !useFrameworkDependentDefaultTargetFramework) // This property did not infer SelfContained until net 8
)
{
expectedSelfContainedValue = "false";
}
publishCommand
.WithWorkingDirectory(Path.Combine(testAsset.TestRoot, testProject.Name))
.Execute($"-bl:{testAsset.Name}-{{}}.binlog")
.Should()
.Pass();

properties["SelfContained"].Should().Be(expectedSelfContainedValue);
// default configuration for publish in <7 is Debug, Release for 7+
var properties = testProject.GetPropertyValues(testAsset.TestRoot, targetFramework: targetFramework, configuration: targetFramework == "net7.0" ? "Debug" : "Release");
bool.Parse(properties["SelfContained"]).Should().Be(expectedSelfContainedValue);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ public TestProject([CallerMemberName] string? name = null)
/// </summary>
public List<string> PropertiesToRecord { get; } = new List<string>();

/// <summary>
/// The target before which to record properties specified in <see cref="PropertiesToRecord"/>.
/// Defaults to "AfterBuild".
/// </summary>
public string TargetToRecordPropertiesBefore { get; private set; } = "AfterBuild";

public IEnumerable<string> TargetFrameworkIdentifiers
{
get
Expand Down Expand Up @@ -252,7 +258,7 @@ internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder)
if(importGroup?.Attribute("Project") is not null)
{
importGroup.Attribute("Project")!.Value = "$(VSINSTALLDIR)\\MSBuild\\Microsoft\\Portable\\$(TargetFrameworkVersion)\\Microsoft.Portable.CSharp.targets";
}
}
}

if(TargetFrameworkVersion is not null)
Expand Down Expand Up @@ -375,12 +381,12 @@ internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder)

propertyGroup?.Add(new XElement(ns + "CustomAfterDirectoryBuildTargets", $"$(CustomAfterDirectoryBuildTargets);{customAfterDirectoryBuildTargetsPath.FullName}"));
propertyGroup?.Add(new XElement(ns + "CustomAfterMicrosoftCommonCrossTargetingTargets", $"$(CustomAfterMicrosoftCommonCrossTargetingTargets);{customAfterDirectoryBuildTargetsPath.FullName}"));

var customAfterDirectoryBuildTargets = new XDocument(new XElement(ns + "Project"));

var target = new XElement(ns + "Target",
new XAttribute("Name", "WritePropertyValues"),
new XAttribute("BeforeTargets", "AfterBuild"));
new XAttribute("BeforeTargets", TargetToRecordPropertiesBefore));

customAfterDirectoryBuildTargets.Root?.Add(target);

Expand Down Expand Up @@ -491,6 +497,15 @@ public void RecordProperties(params string[] propertyNames)
PropertiesToRecord.AddRange(propertyNames);
}

/// <summary>
/// Tells this TestProject to record properties specified in <see cref="PropertiesToRecord"/> before the specified target.
/// By default properties are recorded before the "AfterBuild" target (so after the actual compile+copy targets have run).
/// </summary>
public void RecordPropertiesBeforeTarget(string targetName)
{
TargetToRecordPropertiesBefore = targetName;
}

/// <returns>
/// A dictionary of property keys to property value strings, case sensitive.
/// Only properties added to the <see cref="PropertiesToRecord"/> member will be observed.
Expand Down
Loading