@@ -323,9 +323,10 @@ public void ReferencedExeCanRunWhenPublishedWithTrimming(bool referenceExeInCode
323
323
}
324
324
325
325
[ 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 )
329
330
{
330
331
var testConsoleProject = new TestProject ( "ConsoleApp" )
331
332
{
@@ -334,6 +335,11 @@ public void TestProjectCanReferenceExe(string testTemplateName)
334
335
RuntimeIdentifier = EnvironmentInfo . GetCompatibleRid ( )
335
336
} ;
336
337
338
+ if ( setSelfContainedProperty )
339
+ {
340
+ testConsoleProject . SelfContained = "true" ;
341
+ }
342
+
337
343
var testAsset = _testAssetsManager . CreateTestProject ( testConsoleProject , identifier : testTemplateName ) ;
338
344
339
345
var testProjectDirectory = Path . Combine ( testAsset . TestRoot , "TestProject" ) ;
@@ -359,10 +365,111 @@ public void TestProjectCanReferenceExe(string testTemplateName)
359
365
360
366
}
361
367
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 mtpNotSelfContained = new TestProject ( "MTPTestProject" )
385
+ {
386
+ TargetFrameworks = ToolsetInfo . CurrentTargetFramework ,
387
+ IsExe = true ,
388
+ } ;
389
+
390
+ mtpNotSelfContained . AdditionalProperties [ "IsTestProject" ] = "true" ;
391
+
392
+ if ( setIsTestingPlatformApplicationEarly )
393
+ {
394
+ mtpNotSelfContained . AdditionalProperties [ "IsTestingPlatformApplication" ] = "true" ;
395
+ }
396
+
397
+ mtpNotSelfContained . ReferencedProjects . Add ( testConsoleProjectSelfContained ) ;
398
+
399
+ var testAssetMTP = _testAssetsManager . CreateTestProject ( mtpNotSelfContained ) ;
400
+
401
+ var mtpProjectDirectory = Path . Combine ( testAssetMTP . Path , "MTPTestProject" ) ;
402
+
403
+ if ( ! setIsTestingPlatformApplicationEarly )
404
+ {
405
+ File . WriteAllText ( Path . Combine ( mtpProjectDirectory , "Directory.Build.targets" ) , """
406
+ <Project>
407
+ <PropertyGroup>
408
+ <IsTestingPlatformApplication>true</IsTestingPlatformApplication>
409
+ </PropertyGroup>
410
+ </Project>
411
+ """ ) ;
412
+ }
413
+
414
+ var result = new BuildCommand ( Log , mtpProjectDirectory ) . Execute ( ) ;
415
+ result . Should ( ) . Fail ( ) . And . HaveStdOutContaining ( "NETSDK1151" ) ;
416
+ }
417
+
418
+ [ Theory ]
419
+ [ CombinatorialData ]
420
+ public void MTPNonSelfContainedExecutableCannotBeReferencedBySelfContained ( bool setIsTestingPlatformApplicationEarly )
421
+ {
422
+ // The setup of this test is as follows:
423
+ // ConsoleApp is a self-contained executable project, which references a non-self-contained MTP executable test project.
424
+ // Building ConsoleApp should fail because it references a non-self-contained MTP executable project.
425
+ // A non self-contained executable cannot be referenced by a self-contained executable.
426
+ var testConsoleProjectSelfContained = new TestProject ( "ConsoleApp" )
427
+ {
428
+ IsExe = true ,
429
+ TargetFrameworks = ToolsetInfo . CurrentTargetFramework ,
430
+ SelfContained = "true" ,
431
+ } ;
432
+
433
+ var mtpNotSelfContained = new TestProject ( "MTPTestProject" )
434
+ {
435
+ TargetFrameworks = ToolsetInfo . CurrentTargetFramework ,
436
+ IsExe = true ,
437
+ } ;
438
+
439
+ mtpNotSelfContained . AdditionalProperties [ "IsTestProject" ] = "true" ;
440
+
441
+ if ( setIsTestingPlatformApplicationEarly )
442
+ {
443
+ mtpNotSelfContained . AdditionalProperties [ "IsTestingPlatformApplication" ] = "true" ;
444
+ }
445
+
446
+ testConsoleProjectSelfContained . ReferencedProjects . Add ( mtpNotSelfContained ) ;
447
+
448
+ var testAssetSelfContained = _testAssetsManager . CreateTestProject ( testConsoleProjectSelfContained ) ;
449
+
450
+ if ( ! setIsTestingPlatformApplicationEarly )
451
+ {
452
+ File . WriteAllText ( Path . Combine ( testAssetSelfContained . TestRoot , mtpNotSelfContained . Name , "Directory.Build.targets" ) , """
453
+ <Project>
454
+ <PropertyGroup>
455
+ <IsTestingPlatformApplication>true</IsTestingPlatformApplication>
456
+ </PropertyGroup>
457
+ </Project>
458
+ """ ) ;
459
+ }
460
+
461
+ var consoleAppDirectory = Path . Combine ( testAssetSelfContained . Path , testConsoleProjectSelfContained . Name ) ;
462
+
463
+ var result = new BuildCommand ( Log , consoleAppDirectory ) . Execute ( ) ;
464
+ result . Should ( ) . HaveStdOutContaining ( "NETSDK1150" ) . And . ExitWith ( 1 ) ;
465
+ }
466
+
362
467
[ RequiresMSBuildVersionTheory ( "17.0.0.32901" ) ]
363
- [ InlineData ( "xunit" ) ]
364
- [ InlineData ( "mstest" ) ]
365
- public void ExeProjectCanReferenceTestProject ( string testTemplateName )
468
+ [ CombinatorialData ]
469
+ public void ExeProjectCanReferenceTestProject (
470
+ [ CombinatorialValues ( "xunit" , "mstest" ) ] string testTemplateName ,
471
+ bool setSelfContainedProperty ,
472
+ bool buildWithSelfContainedFromCommandLine )
366
473
{
367
474
var testConsoleProject = new TestProject ( "ConsoleApp" )
368
475
{
@@ -371,6 +478,11 @@ public void ExeProjectCanReferenceTestProject(string testTemplateName)
371
478
RuntimeIdentifier = EnvironmentInfo . GetCompatibleRid ( )
372
479
} ;
373
480
481
+ if ( setSelfContainedProperty )
482
+ {
483
+ testConsoleProject . SelfContained = "true" ;
484
+ }
485
+
374
486
var testAsset = _testAssetsManager . CreateTestProject ( testConsoleProject , identifier : testTemplateName ) ;
375
487
376
488
var testProjectDirectory = Path . Combine ( testAsset . TestRoot , "TestProject" ) ;
@@ -391,7 +503,47 @@ public void ExeProjectCanReferenceTestProject(string testTemplateName)
391
503
. Should ( )
392
504
. Pass ( ) ;
393
505
394
- new BuildCommand ( Log , consoleProjectDirectory )
506
+ new BuildCommand ( testAsset )
507
+ . WithWorkingDirectory ( consoleProjectDirectory )
508
+ . Execute ( buildWithSelfContainedFromCommandLine ? [ "-p:SelfContained=true" ] : Array . Empty < string > ( ) )
509
+ . Should ( )
510
+ . Pass ( ) ;
511
+ }
512
+
513
+ [ Theory ]
514
+ [ CombinatorialData ]
515
+ public void MTPCanBeBuiltAsSelfContained ( bool setIsTestingPlatformApplicationEarly )
516
+ {
517
+ var mtpSelfContained = new TestProject ( "MTPTestProject" )
518
+ {
519
+ TargetFrameworks = ToolsetInfo . CurrentTargetFramework ,
520
+ IsExe = true ,
521
+ SelfContained = "true" ,
522
+ } ;
523
+
524
+ mtpSelfContained . AdditionalProperties [ "IsTestProject" ] = "true" ;
525
+
526
+ if ( setIsTestingPlatformApplicationEarly )
527
+ {
528
+ mtpSelfContained . AdditionalProperties [ "IsTestingPlatformApplication" ] = "true" ;
529
+ }
530
+
531
+ var testAssetMTP = _testAssetsManager . CreateTestProject ( mtpSelfContained ) ;
532
+
533
+ var mtpProjectDirectory = Path . Combine ( testAssetMTP . Path , mtpSelfContained . Name ) ;
534
+
535
+ if ( ! setIsTestingPlatformApplicationEarly )
536
+ {
537
+ File . WriteAllText ( Path . Combine ( mtpProjectDirectory , "Directory.Build.targets" ) , """
538
+ <Project>
539
+ <PropertyGroup>
540
+ <IsTestingPlatformApplication>true</IsTestingPlatformApplication>
541
+ </PropertyGroup>
542
+ </Project>
543
+ """ ) ;
544
+ }
545
+
546
+ new BuildCommand ( Log , mtpProjectDirectory )
395
547
. Execute ( )
396
548
. Should ( )
397
549
. Pass ( ) ;
0 commit comments