Skip to content

Fix test projects which reference self-contained Exes #17594

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

Merged
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
Expand Up @@ -1060,6 +1060,13 @@ Copyright (c) .NET Foundation. All rights reserved.
<ShouldBeValidatedAsExecutableReference>false</ShouldBeValidatedAsExecutableReference>
</PropertyGroup>

<PropertyGroup Condition="'$(IsTestProject)' == 'true' And '$(ValidateExecutableReferencesMatchSelfContained)' == ''">
<!-- Don't generate an error if a test project references a self-contained Exe. Test projects
use an OutputType of Exe but will usually call APIs in a referenced Exe rather than try
to run it. -->
<ValidateExecutableReferencesMatchSelfContained>false</ValidateExecutableReferencesMatchSelfContained>
</PropertyGroup>

<UsingTask TaskName="ValidateExecutableReferences" AssemblyFile="$(MicrosoftNETBuildTasksAssembly)" />

<Target Name="ValidateExecutableReferences"
Expand Down
36 changes: 36 additions & 0 deletions src/Tests/Microsoft.NET.Build.Tests/ReferenceExeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,5 +259,41 @@ public void ReferencedExeCanRunWhenPublishedWithTrimming()

RunTest();
}

[Theory]
[InlineData("xunit")]
[InlineData("mstest")]
public void TestProjectCanReferenceExe(string testTemplateName)
{
var testConsoleProject = new TestProject("ConsoleApp")
{
IsExe = true,
TargetFrameworks = "net5.0",
RuntimeIdentifier = EnvironmentInfo.GetCompatibleRid()
};

var testAsset = _testAssetsManager.CreateTestProject(testConsoleProject, identifier: testTemplateName);

var testProjectDirectory = Path.Combine(testAsset.TestRoot, "TestProject");
Directory.CreateDirectory(testProjectDirectory);

new DotnetCommand(Log, "new", testTemplateName)
.WithWorkingDirectory(testProjectDirectory)
.Execute()
.Should()
.Pass();

new DotnetCommand(Log, "add", "reference", ".." + Path.DirectorySeparatorChar + testConsoleProject.Name)
.WithWorkingDirectory(testProjectDirectory)
.Execute()
.Should()
.Pass();

new BuildCommand(Log, testProjectDirectory)
.Execute()
.Should()
.Pass();

}
}
}