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