Skip to content

Commit 63abd1a

Browse files
committed
Add tests
1 parent 611d422 commit 63abd1a

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

test/Microsoft.NET.Build.Tests/ReferenceExeTests.cs

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,10 @@ public void ReferencedExeCanRunWhenPublishedWithTrimming(bool referenceExeInCode
323323
}
324324

325325
[RequiresMSBuildVersionTheory("17.0.0.32901")]
326-
[InlineData("xunit")]
327-
[InlineData("mstest")]
328-
public void TestProjectCanReferenceExe(string testTemplateName)
326+
[CombinatorialData]
327+
public void TestProjectCanReferenceExe(
328+
[CombinatorialValues("xunit", "mstest")] string testTemplateName,
329+
bool setSelfContainedProperty)
329330
{
330331
var testConsoleProject = new TestProject("ConsoleApp")
331332
{
@@ -334,6 +335,11 @@ public void TestProjectCanReferenceExe(string testTemplateName)
334335
RuntimeIdentifier = EnvironmentInfo.GetCompatibleRid()
335336
};
336337

338+
if (setSelfContainedProperty)
339+
{
340+
testConsoleProject.SelfContained = "true";
341+
}
342+
337343
var testAsset = _testAssetsManager.CreateTestProject(testConsoleProject, identifier: testTemplateName);
338344

339345
var testProjectDirectory = Path.Combine(testAsset.TestRoot, "TestProject");
@@ -359,6 +365,63 @@ public void TestProjectCanReferenceExe(string testTemplateName)
359365

360366
}
361367

368+
[Theory]
369+
[CombinatorialData]
370+
public void SelfContainedExecutableCannotBeReferencedByNonSelfContainedMTPTestProject(bool setIsTestingPlatformApplicationEarly)
371+
{
372+
// The setup of this test is as follows:
373+
// ConsoleApp is a self-contained executable project.
374+
// MTPTestProject is an executable test project that references ConsoleApp.
375+
// Building MTPTestProject should fail because it references a self-contained executable project.
376+
// A self-contained executable cannot be referenced by a non self-contained executable.
377+
var testConsoleProjectSelfContained = new TestProject("ConsoleApp")
378+
{
379+
IsExe = true,
380+
TargetFrameworks = ToolsetInfo.CurrentTargetFramework,
381+
SelfContained = "true",
382+
};
383+
384+
var testAssetSelfContained = _testAssetsManager.CreateTestProject(testConsoleProjectSelfContained);
385+
386+
var mtpNotSelfContained = new TestProject("MTPTestProject")
387+
{
388+
TargetFrameworks = ToolsetInfo.CurrentTargetFramework,
389+
IsExe = true,
390+
IsTestProject = true,
391+
};
392+
393+
if (setIsTestingPlatformApplicationEarly)
394+
{
395+
mtpNotSelfContained.IsTestingPlatformApplication = true;
396+
}
397+
398+
var testAssetMTP = _testAssetsManager.CreateTestProject(mtpNotSelfContained);
399+
400+
var mtpProjectDirectory = Path.Combine(testAssetMTP.Path, "MTPTestProject");
401+
Assert.True(Directory.Exists(mtpProjectDirectory), $"Expected directory {mtpProjectDirectory} to exist.");
402+
Assert.True(File.Exists(Path.Combine(mtpProjectDirectory, "MTPTestProject.csproj")), $"Expected file MTPTestProject.csproj to exist in {mtpProjectDirectory}.");
403+
404+
if (!setIsTestingPlatformApplicationEarly)
405+
{
406+
File.WriteAllText(Path.Combine(mtpProjectDirectory, "Directory.Build.targets"), """
407+
<Project>
408+
<PropertyGroup>
409+
<IsTestingPlatformApplication>true</IsTestingPlatformApplication>
410+
</PropertyGroup>
411+
</Project>
412+
""");
413+
}
414+
415+
new DotnetCommand(Log, "add", "reference", Path.Combine(testAssetSelfContained.Path, testConsoleProjectSelfContained.Name))
416+
.WithWorkingDirectory(mtpProjectDirectory)
417+
.Execute()
418+
.Should()
419+
.Pass();
420+
421+
var result = new BuildCommand(Log, mtpProjectDirectory).Execute();
422+
result.Should().HaveStdOutContaining("NETSDK1151").And.ExitWith(1);
423+
}
424+
362425
[RequiresMSBuildVersionTheory("17.0.0.32901")]
363426
[InlineData("xunit")]
364427
[InlineData("mstest")]

test/Microsoft.NET.TestFramework/ProjectConstruction/TestProject.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public TestProject([CallerMemberName] string? name = null)
3434
/// </summary>
3535
public bool IsWinExe { get; set; }
3636

37+
public bool IsTestingPlatformApplication { get; set; }
38+
39+
public bool IsTestProject { get; set; }
40+
3741

3842
public string? ProjectSdk { get; set; }
3943

@@ -293,6 +297,16 @@ internal void Create(TestAsset targetTestAsset, string testProjectsSourceFolder)
293297
propertyGroup?.Element(ns + "OutputType")?.SetValue("WinExe");
294298
}
295299

300+
if (IsTestProject)
301+
{
302+
propertyGroup?.Add(new XElement(ns + "IsTestProject", "true"));
303+
}
304+
305+
if (IsTestingPlatformApplication)
306+
{
307+
propertyGroup?.Add(new XElement(ns + "IsTestingPlatformApplication", "true"));
308+
}
309+
296310
if (SelfContained != "")
297311
{
298312
propertyGroup?.Add(new XElement(ns + "SelfContained", string.Equals(SelfContained, "true", StringComparison.OrdinalIgnoreCase) ? "true" : "false"));

0 commit comments

Comments
 (0)