-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expose import tree as MSBuildImportedProject items #13681
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
drewnoakes
wants to merge
6
commits into
dotnet:main
Choose a base branch
from
drewnoakes:imported-project-items
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
095c2c7
Expose import tree as MSBuildImportedProject items
drewnoakes af0d0b0
Address PR feedback: escaping, null safety, allocation reuse
drewnoakes 044bf49
Fix doc comment, add global property test
drewnoakes bce42f6
Add documentation and XSD entries for MSBuildImportedProject
drewnoakes 469405e
Extract helper method, add constants for item/metadata names
drewnoakes 3806108
Move item synthesis from ProjectInstance to Evaluator
drewnoakes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
252 changes: 252 additions & 0 deletions
252
src/Build.UnitTests/Instance/ProjectInstance_ImportedProjectItems_Tests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,252 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using Microsoft.Build.Definition; | ||
| using Microsoft.Build.Evaluation; | ||
| using Microsoft.Build.Execution; | ||
| using Microsoft.Build.Unittest; | ||
| using Microsoft.Build.UnitTests; | ||
| using Shouldly; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Build.UnitTests.OM.Instance | ||
| { | ||
| /// <summary> | ||
| /// Tests for the MSBuildImportedProject items synthesized from the import closure. | ||
| /// </summary> | ||
| public class ProjectInstance_ImportedProjectItems_Tests | ||
| { | ||
| private readonly ITestOutputHelper _output; | ||
|
|
||
| public ProjectInstance_ImportedProjectItems_Tests(ITestOutputHelper output) | ||
| { | ||
| _output = output; | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ImportedProjectItemsNotCreatedWithoutOptIn() | ||
| { | ||
| using TestEnvironment env = TestEnvironment.Create(_output); | ||
|
|
||
| string importContent = "<Project />"; | ||
| var importFile = env.CreateFile("import.targets", importContent); | ||
|
|
||
| string projectContent = $""" | ||
| <Project> | ||
| <Import Project="{importFile.Path}" /> | ||
| </Project> | ||
| """; | ||
| var projectFile = env.CreateFile("test.proj", projectContent); | ||
|
|
||
| using var collection = new ProjectCollection(); | ||
| var project = new Project(projectFile.Path, globalProperties: null, toolsVersion: null, collection); | ||
|
|
||
| // Items should not exist on the Project | ||
| project.GetItems("MSBuildImportedProject").Count.ShouldBe(0); | ||
|
|
||
| // Nor on ProjectInstance | ||
| ProjectInstance instance = project.CreateProjectInstance(); | ||
| instance.GetItems("MSBuildImportedProject").Count.ShouldBe(0); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ImportedProjectItemsCreatedWhenPropertyIsSet() | ||
| { | ||
| using TestEnvironment env = TestEnvironment.Create(_output); | ||
|
|
||
| string importContent = "<Project />"; | ||
| var importFile = env.CreateFile("import.targets", importContent); | ||
|
|
||
| string projectContent = $""" | ||
| <Project> | ||
| <PropertyGroup> | ||
| <MSBuildProvideImportedProjects>true</MSBuildProvideImportedProjects> | ||
| </PropertyGroup> | ||
| <Import Project="{importFile.Path}" /> | ||
| </Project> | ||
| """; | ||
| var projectFile = env.CreateFile("test.proj", projectContent); | ||
|
|
||
| using var collection = new ProjectCollection(); | ||
| var project = new Project(projectFile.Path, globalProperties: null, toolsVersion: null, collection); | ||
|
|
||
| // Items should be visible on Project (created during evaluation) | ||
| var projectItems = project.GetItems("MSBuildImportedProject").ToList(); | ||
| projectItems.Count.ShouldBe(1); | ||
| projectItems[0].EvaluatedInclude.ShouldBe(importFile.Path); | ||
| projectItems[0].GetMetadataValue("ImportingProjectPath").ShouldBe(projectFile.Path); | ||
| projectItems[0].GetMetadataValue("Sdk").ShouldBeEmpty(); | ||
|
|
||
| // And also on ProjectInstance | ||
| ProjectInstance instance = project.CreateProjectInstance(); | ||
| var instanceItems = instance.GetItems("MSBuildImportedProject").ToList(); | ||
| instanceItems.Count.ShouldBe(1); | ||
| instanceItems[0].EvaluatedInclude.ShouldBe(importFile.Path); | ||
| instanceItems[0].GetMetadataValue("ImportingProjectPath").ShouldBe(projectFile.Path); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ImportedProjectItemsHaveCorrectImportingPath() | ||
| { | ||
| using TestEnvironment env = TestEnvironment.Create(_output); | ||
|
|
||
| string import2Content = "<Project />"; | ||
| var import2File = env.CreateFile("import2.targets", import2Content); | ||
|
|
||
| string import1Content = $""" | ||
| <Project> | ||
| <Import Project="{import2File.Path}" /> | ||
| </Project> | ||
| """; | ||
| var import1File = env.CreateFile("import1.targets", import1Content); | ||
|
|
||
| string projectContent = $""" | ||
| <Project> | ||
| <PropertyGroup> | ||
| <MSBuildProvideImportedProjects>true</MSBuildProvideImportedProjects> | ||
| </PropertyGroup> | ||
| <Import Project="{import1File.Path}" /> | ||
| </Project> | ||
| """; | ||
| var projectFile = env.CreateFile("test.proj", projectContent); | ||
|
|
||
| using var collection = new ProjectCollection(); | ||
| var project = new Project(projectFile.Path, globalProperties: null, toolsVersion: null, collection); | ||
| ProjectInstance instance = project.CreateProjectInstance(); | ||
|
|
||
| var items = instance.GetItems("MSBuildImportedProject").ToList(); | ||
| items.Count.ShouldBe(2); | ||
|
|
||
| // project -> import1 | ||
| var item1 = items.First(i => i.EvaluatedInclude == import1File.Path); | ||
| item1.GetMetadataValue("ImportingProjectPath").ShouldBe(projectFile.Path); | ||
|
|
||
| // import1 -> import2 | ||
| var item2 = items.First(i => i.EvaluatedInclude == import2File.Path); | ||
| item2.GetMetadataValue("ImportingProjectPath").ShouldBe(import1File.Path); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ImportedProjectItemsExcludeRootProject() | ||
| { | ||
| using TestEnvironment env = TestEnvironment.Create(_output); | ||
|
|
||
| string projectContent = """ | ||
| <Project> | ||
| <PropertyGroup> | ||
| <MSBuildProvideImportedProjects>true</MSBuildProvideImportedProjects> | ||
| </PropertyGroup> | ||
| </Project> | ||
| """; | ||
| var projectFile = env.CreateFile("test.proj", projectContent); | ||
|
|
||
| using var collection = new ProjectCollection(); | ||
| var project = new Project(projectFile.Path, globalProperties: null, toolsVersion: null, collection); | ||
| ProjectInstance instance = project.CreateProjectInstance(); | ||
|
|
||
| instance.GetItems("MSBuildImportedProject").Count.ShouldBe(0); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ImportedProjectItemsAvailableToTargets() | ||
| { | ||
| using TestEnvironment env = TestEnvironment.Create(_output); | ||
|
|
||
| string importContent = "<Project />"; | ||
| var importFile = env.CreateFile("import.targets", importContent); | ||
|
|
||
| string projectContent = $""" | ||
| <Project> | ||
| <PropertyGroup> | ||
| <MSBuildProvideImportedProjects>true</MSBuildProvideImportedProjects> | ||
| </PropertyGroup> | ||
| <Import Project="{importFile.Path}" /> | ||
| <Target Name="ShowImports"> | ||
| <Message Text="Import: %(MSBuildImportedProject.Identity) from %(MSBuildImportedProject.ImportingProjectPath)" Importance="High" /> | ||
| </Target> | ||
| </Project> | ||
| """; | ||
| var projectFile = env.CreateFile("test.proj", projectContent); | ||
|
|
||
| using var collection = new ProjectCollection(); | ||
| var project = new Project(projectFile.Path, globalProperties: null, toolsVersion: null, collection); | ||
| ProjectInstance instance = project.CreateProjectInstance(); | ||
|
|
||
| var mockLogger = new MockLogger(_output); | ||
| instance.Build(["ShowImports"], [mockLogger]).ShouldBeTrue(); | ||
| mockLogger.AssertLogContains($"Import: {importFile.Path} from {projectFile.Path}"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ImportedProjectItemsHaveSdkMetadataForSdkImports() | ||
| { | ||
| using TestEnvironment env = TestEnvironment.Create(_output); | ||
|
|
||
| string testSdkDirectory = env.CreateFolder().Path; | ||
| File.WriteAllText(Path.Combine(testSdkDirectory, "Sdk.props"), "<Project />"); | ||
| File.WriteAllText(Path.Combine(testSdkDirectory, "Sdk.targets"), "<Project />"); | ||
|
|
||
| var projectOptions = SdkUtilities.CreateProjectOptionsWithResolver( | ||
| new SdkUtilities.FileBasedMockSdkResolver(new Dictionary<string, string> | ||
| { | ||
| { "MyTestSdk", testSdkDirectory }, | ||
| })); | ||
|
|
||
| string projectContent = """ | ||
| <Project Sdk="MyTestSdk"> | ||
| <PropertyGroup> | ||
| <MSBuildProvideImportedProjects>true</MSBuildProvideImportedProjects> | ||
| </PropertyGroup> | ||
| </Project> | ||
| """; | ||
|
|
||
| using ProjectRootElementFromString projectRootElementFromString = new(projectContent); | ||
| Project project = Project.FromProjectRootElement( | ||
| projectRootElementFromString.Project, | ||
| projectOptions); | ||
| ProjectInstance instance = project.CreateProjectInstance(); | ||
|
|
||
| var items = instance.GetItems("MSBuildImportedProject").ToList(); | ||
| items.Count.ShouldBe(2); // Sdk.props and Sdk.targets | ||
|
|
||
| // Both should have Sdk metadata set to "MyTestSdk" | ||
| foreach (var item in items) | ||
| { | ||
| item.GetMetadataValue("Sdk").ShouldBe("MyTestSdk"); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ImportedProjectItemsCreatedWhenSetViaGlobalProperty() | ||
| { | ||
| using TestEnvironment env = TestEnvironment.Create(_output); | ||
|
|
||
| string importContent = "<Project />"; | ||
| var importFile = env.CreateFile("import.targets", importContent); | ||
|
|
||
| string projectContent = $""" | ||
| <Project> | ||
| <Import Project="{importFile.Path}" /> | ||
| </Project> | ||
| """; | ||
| var projectFile = env.CreateFile("test.proj", projectContent); | ||
|
|
||
| var globalProperties = new Dictionary<string, string> | ||
| { | ||
| { "MSBuildProvideImportedProjects", "true" }, | ||
| }; | ||
|
|
||
| using var collection = new ProjectCollection(); | ||
| var project = new Project(projectFile.Path, globalProperties, toolsVersion: null, collection); | ||
| ProjectInstance instance = project.CreateProjectInstance(); | ||
|
|
||
| var items = instance.GetItems("MSBuildImportedProject").ToList(); | ||
| items.Count.ShouldBe(1); | ||
| items[0].EvaluatedInclude.ShouldBe(importFile.Path); | ||
| items[0].GetMetadataValue("ImportingProjectPath").ShouldBe(projectFile.Path); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.