Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
Expand Down Expand Up @@ -59,7 +60,7 @@ public void It_creates_readytorun_images_for_all_assemblies_except_excluded_ones
"ClassLib");

testProject.AdditionalProperties["PublishReadyToRun"] = "True";
testProject.AdditionalItems["PublishReadyToRunExclude"] = "Classlib.dll";
testProject.AdditionalItems["PublishReadyToRunExclude"] = new Dictionary<string, string> { ["Include"] = "Classlib.dll" };

var testProjectInstance = _testAssetsManager.CreateTestProject(testProject);

Expand Down
63 changes: 41 additions & 22 deletions src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToRunILLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -493,12 +493,13 @@ public void ILLink_respects_feature_settings_from_host_config()
var targetFramework = "net5.0";
var rid = EnvironmentInfo.GetCompatibleRid(targetFramework);

var testProject = CreateTestProjectForILLinkTesting(targetFramework, projectName, referenceProjectName);
// Set up a conditional feature substitution for the "FeatureDisabled" property
AddFeatureDefinition(testProject, referenceProjectName);
var testProject = CreateTestProjectForILLinkTesting(targetFramework, projectName, referenceProjectName,
// Reference the classlib to ensure its XML is processed.
addAssemblyReference: true,
// Set up a conditional feature substitution for the "FeatureDisabled" property
modifyReferencedProject: (referencedProject) => AddFeatureDefinition(referencedProject, referenceProjectName));
var testAsset = _testAssetsManager.CreateTestProject(testProject)
.WithProjectChanges(project => EnableNonFrameworkTrimming(project))
.WithProjectChanges(project => EmbedSubstitutions(project))
// Set a matching RuntimeHostConfigurationOption, with Trim = "true"
.WithProjectChanges(project => AddRuntimeConfigOption(project, trim: true))
.WithProjectChanges(project => AddRootDescriptor(project, $"{referenceProjectName}.xml"));
Expand All @@ -525,12 +526,13 @@ public void ILLink_ignores_host_config_settings_with_link_false()
var targetFramework = "net5.0";
var rid = EnvironmentInfo.GetCompatibleRid(targetFramework);

var testProject = CreateTestProjectForILLinkTesting(targetFramework, projectName, referenceProjectName);
// Set up a conditional feature substitution for the "FeatureDisabled" property
AddFeatureDefinition(testProject, referenceProjectName);
var testProject = CreateTestProjectForILLinkTesting(targetFramework, projectName, referenceProjectName,
// Reference the classlib to ensure its XML is processed.
addAssemblyReference: true,
// Set up a conditional feature substitution for the "FeatureDisabled" property
modifyReferencedProject: (referencedProject) => AddFeatureDefinition(referencedProject, referenceProjectName));
var testAsset = _testAssetsManager.CreateTestProject(testProject)
.WithProjectChanges(project => EnableNonFrameworkTrimming(project))
.WithProjectChanges(project => EmbedSubstitutions(project))
// Set a matching RuntimeHostConfigurationOption, with Trim = "false"
.WithProjectChanges(project => AddRuntimeConfigOption(project, trim: false))
.WithProjectChanges(project => AddRootDescriptor(project, $"{referenceProjectName}.xml"));
Expand Down Expand Up @@ -1116,28 +1118,23 @@ private void EnableNonFrameworkTrimming(XDocument project)

static readonly string substitutionsFilename = "ILLink.Substitutions.xml";

private void EmbedSubstitutions(XDocument project)
{
var ns = project.Root.Name.Namespace;

project.Root.Add(new XElement(ns + "ItemGroup",
new XElement("EmbeddedResource",
new XAttribute("Include", substitutionsFilename),
new XElement("LogicalName", substitutionsFilename))));
}

private void AddFeatureDefinition(TestProject testProject, string referenceAssemblyName)
private void AddFeatureDefinition(TestProject testProject, string assemblyName)
{
// Add a feature definition that replaces the FeatureDisabled property when DisableFeature is true.
testProject.EmbeddedResources[substitutionsFilename] = $@"
<linker>
<assembly fullname=""{referenceAssemblyName}"" feature=""DisableFeature"" featurevalue=""true"">
<assembly fullname=""{assemblyName}"" feature=""DisableFeature"" featurevalue=""true"">
<type fullname=""ClassLib"">
<method signature=""System.Boolean get_FeatureDisabled()"" body=""stub"" value=""true"" />
</type>
</assembly>
</linker>
";

testProject.AdditionalItems["EmbeddedResource"] = new Dictionary<string, string> {
["Include"] = substitutionsFilename,
["LogicalName"] = substitutionsFilename
};
}

private void AddRuntimeConfigOption(XDocument project, bool trim)
Expand Down Expand Up @@ -1221,7 +1218,9 @@ private TestProject CreateTestProjectForILLinkTesting(
string referenceProjectName = null,
bool usePackageReference = true,
[CallerMemberName] string callingMethod = "",
string referenceProjectIdentifier = "")
string referenceProjectIdentifier = "",
Action<TestProject> modifyReferencedProject = null,
bool addAssemblyReference = false)
{
var testProject = new TestProject()
{
Expand All @@ -1239,11 +1238,24 @@ public static void Main()
{
Console.WriteLine(""Hello world"");
}
}
";

if (addAssemblyReference)
{
testProject.SourceFiles[$"{mainProjectName}.cs"] += @"
public static void UseClassLib()
{
ClassLib.UsedMethod();
}
}";
} else {
testProject.SourceFiles[$"{mainProjectName}.cs"] += @"}";
}

if (referenceProjectName == null)
{
if (addAssemblyReference)
throw new ArgumentException("Adding an assembly reference requires a project to reference.");
return testProject;
}

Expand All @@ -1258,8 +1270,13 @@ public static void Main()
};
referenceProject.SourceFiles[$"{referenceProjectName}.cs"] = @"
using System;

public class ClassLib
{
public static void UsedMethod()
{
}

public void UnusedMethod()
{
}
Expand All @@ -1283,6 +1300,8 @@ public static void FeatureImplementation()
}
}
";
if (modifyReferencedProject != null)
modifyReferencedProject(referenceProject);

if (usePackageReference)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public TestProject([CallerMemberName] string name = null)

public Dictionary<string, string> AdditionalProperties { get; } = new Dictionary<string, string>();

public Dictionary<string, string> AdditionalItems { get; } = new Dictionary<string, string>();
public Dictionary<string, Dictionary<string, string>> AdditionalItems { get; } = new Dictionary<string, Dictionary<string, string>>();

public IEnumerable<string> TargetFrameworkIdentifiers
{
Expand Down Expand Up @@ -230,9 +230,10 @@ internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder,
additionalItemGroup = new XElement(ns + "ItemGroup");
projectXml.Root.Add(packageReferenceItemGroup);
}
additionalItemGroup.Add(new XElement(
ns + additionalItem.Key,
new XAttribute("Include", additionalItem.Value)));
var item = new XElement(ns + additionalItem.Key);
foreach (var attribute in additionalItem.Value)
item.Add(new XAttribute(attribute.Key, attribute.Value));
additionalItemGroup.Add(item);
}
}

Expand Down