@@ -531,6 +531,74 @@ public void ItCanBuildWithMicrosoftBuildArtifactsSdk()
531
531
new FileInfo ( Path . Combine ( testAsset . Path , "MSBuildSdk" , "obj" , "Debug" , ToolsetInfo . CurrentTargetFramework , "MSBuildSdk.dll" ) ) . Should ( ) . Exist ( ) ;
532
532
533
533
}
534
+
535
+ [ Fact ]
536
+ public void PublishingRegistersWrittenFilesForProperCleanup ( )
537
+ {
538
+ var testProject = new TestProject ( )
539
+ {
540
+ IsExe = true ,
541
+ UseArtifactsOutput = true ,
542
+ } ;
543
+
544
+ var testAsset = _testAssetsManager . CreateTestProject ( testProject ) ;
545
+
546
+ // Now add a Directory.Build.props file setting UseArtifactsOutput to true
547
+ File . WriteAllText ( Path . Combine ( testAsset . Path , "Directory.Build.props" ) , """
548
+ <Project>
549
+ <PropertyGroup>
550
+ <UseArtifactsOutput>true</UseArtifactsOutput>
551
+ </PropertyGroup>
552
+ </Project>
553
+ """ ) ;
554
+
555
+ var projectDir = Path . Combine ( testAsset . Path , testAsset . TestProject . Name ) ;
556
+
557
+ // publish the app
558
+ // we publish self-contained so that we include hostfxr.dll.
559
+ // if we don't clean up this file, when we build in Release,
560
+ // the generated exe will pick up the hostfxr and fail to run.
561
+ // so the only way to successfully run the exe is to clean up
562
+ // the hostfxr.dll and other self-contained files.
563
+ new DotnetPublishCommand ( Log )
564
+ . WithWorkingDirectory ( projectDir )
565
+ . Execute ( "--self-contained" )
566
+ . Should ( )
567
+ . Pass ( ) ;
568
+
569
+ var outputDir = new DirectoryInfo ( OutputPathCalculator . FromProject ( testAsset . Path , testProject ) . GetOutputDirectory ( configuration : "release" ) ) ;
570
+ outputDir . Should ( ) . Exist ( ) . And . HaveFile ( "hostfxr.dll" ) ;
571
+ LocateAndRunApp ( outputDir ) ;
572
+
573
+ var publishDir = new DirectoryInfo ( OutputPathCalculator . FromProject ( testAsset . Path , testProject ) . GetPublishDirectory ( configuration : "release" ) ) ;
574
+ publishDir . Should ( ) . Exist ( ) . And . HaveFile ( "hostfxr.dll" ) ;
575
+ LocateAndRunApp ( publishDir ) ;
576
+
577
+ // now build the app in Release configuration.
578
+ // now self-contained, so that we are forced to clean up the runtime
579
+ // files that were published.
580
+ new DotnetBuildCommand ( Log )
581
+ . WithWorkingDirectory ( projectDir )
582
+ . Execute ( "-c" , "Release" )
583
+ . Should ( )
584
+ . Pass ( ) ;
585
+ outputDir . Should ( ) . Exist ( ) ;
586
+ outputDir . Should ( ) . NotHaveFiles ( [ "hostfxr.dll" ] ) ;
587
+ LocateAndRunApp ( outputDir ) ;
588
+
589
+ void LocateAndRunApp ( DirectoryInfo root )
590
+ {
591
+ var appBinaryName = RuntimeInformation . IsOSPlatform ( OSPlatform . Windows )
592
+ ? $ "{ testProject . Name } .exe"
593
+ : testProject . Name ;
594
+ root . Should ( ) . HaveFiles ( [ appBinaryName ] ) ;
595
+ var binary = root . GetFiles ( appBinaryName ) . First ( ) ;
596
+ new RunExeCommand ( Log , binary . FullName )
597
+ . Execute ( )
598
+ . Should ( )
599
+ . Pass ( ) ;
600
+ }
601
+ }
534
602
}
535
603
536
604
namespace ArtifactsTestExtensions
0 commit comments