@@ -29,6 +29,9 @@ var testsDirectory = solutionDirectory.Combine("tests");
29
29
var outputDirectory = solutionDirectory . Combine ( "build" ) ;
30
30
var toolsDirectory = solutionDirectory . Combine ( "tools" ) ;
31
31
var toolsHugoDirectory = toolsDirectory . Combine ( "Hugo" ) ;
32
+ var artifactsPackagingTestsDirectory = artifactsDirectory . Combine ( "Packaging.Tests" ) ;
33
+ var localNugetSourceName = "LocalPackages" ;
34
+ var mongoDbDriverPackageName = "MongoDB.Driver" ;
32
35
33
36
var solutionFile = solutionDirectory . CombineWithFilePath ( "CSharpDriver.sln" ) ;
34
37
var solutionFullPath = solutionFile . FullPath ;
@@ -562,6 +565,212 @@ Task("DumpGitVersion")
562
565
Information ( gitVersion . Dump ( ) ) ;
563
566
} ) ;
564
567
568
+ Task( "PreparePackagingTests" )
569
+ . Does ( ( ) =>
570
+ {
571
+ var nugetConfigPath = artifactsDirectory . CombineWithFilePath ( "nuget.config" ) ;
572
+ if ( FileExists ( nugetConfigPath ) ) DeleteFile ( nugetConfigPath ) ;
573
+
574
+ CreateNugetConfig ( nugetConfigPath ) ;
575
+
576
+ void CreateNugetConfig ( FilePath filePath )
577
+ {
578
+ DotNetCoreTool ( filePath , "new nugetconfig" ) ; // create a default nuget.config
579
+
580
+ // <packageSources>
581
+ // <add key="{localNugetSourceName}" value="packages" />
582
+ // </packageSources>
583
+ XmlPoke ( filePath , "/configuration/packageSources/add/@key" , $ "{ localNugetSourceName } ") ;
584
+ XmlPoke ( filePath , $ "/configuration/packageSources/add[@key = '{ localNugetSourceName } ']/@value", "packages" ) ;
585
+ }
586
+ } ) ;
587
+
588
+ Task( "PackagingProjectReferenceTests" )
589
+ . IsDependentOn ( "Build" )
590
+ . DoesForEach (
591
+ GetFiles ( "./**/*.Tests.csproj" ) ,
592
+ testProject =>
593
+ {
594
+ var settings = new DotNetCoreTestSettings
595
+ {
596
+ NoBuild = true ,
597
+ NoRestore = true ,
598
+ Configuration = configuration ,
599
+ ArgumentCustomization = args => args . Append ( "-- RunConfiguration.TargetPlatform=x64" ) ,
600
+ Filter = "Category=\" Packaging\" "
601
+ } ;
602
+
603
+ DotNetCoreTest (
604
+ testProject . FullPath ,
605
+ settings
606
+ ) ;
607
+ } ) ;
608
+
609
+ Task( "PackagingTests" )
610
+ . IsDependentOn ( "PackagingProjectReferenceTests" )
611
+ . IsDependentOn ( "Package" )
612
+ . IsDependentOn ( "PreparePackagingTests" )
613
+ . DoesForEach (
614
+ ( ) =>
615
+ {
616
+ var packagesList = NuGetList (
617
+ new NuGetListSettings {
618
+ AllVersions = true ,
619
+ Prerelease = true ,
620
+ Source = new [ ] { $ "{ localNugetSourceName } " } , // corresponds to artifacts Nuget.config
621
+ WorkingDirectory = artifactsDirectory
622
+ } ) ;
623
+
624
+ foreach ( var package in packagesList )
625
+ {
626
+ Information ( "Found packages {0}, version {1}" , package . Name , package . Version ) ;
627
+ }
628
+ if ( packagesList . Count ( p => p . Name == mongoDbDriverPackageName ) != 1 )
629
+ {
630
+ throw new Exception ( $ "Package { mongoDbDriverPackageName } must be presented and unique.") ;
631
+ }
632
+ var mongoDriverPackageVersion = packagesList . Single ( p => p . Name == mongoDbDriverPackageName ) . Version ;
633
+ Information ( $ "Package version { mongoDriverPackageVersion } ") ;
634
+
635
+ var testDetails = new List < ( string Moniker , string CsprojType , string Version , string Bitness , string Command ) >
636
+ {
637
+ /* Should be considered in https://jira.mongodb.org/browse/CSHARP-3805
638
+ { ("v4.7.2", "NonSdk", mongoDriverPackageVersion) }, */
639
+
640
+ { ( "net472" , "SDK" , mongoDriverPackageVersion , "x64" , Command : "xunit" ) } , // TODO: add x32
641
+ { ( "netcoreapp21" , "SDK" , mongoDriverPackageVersion , "x64" , Command : "xunit" ) } ,
642
+ { ( "netcoreapp30" , "SDK" , mongoDriverPackageVersion , "x64" , Command : "xunit" ) } ,
643
+ { ( "net50" , "SDK" , mongoDriverPackageVersion , "x64" , Command : "xunit" ) }
644
+ } ;
645
+
646
+ // run the same tests on console app
647
+ testDetails . AddRange ( testDetails . ToArray ( ) . Select ( i => ( i . Moniker , i . CsprojType , i . Version , i . Bitness , Command : "console" ) ) ) ;
648
+
649
+ return testDetails ;
650
+ } ,
651
+ ( testDetails ) =>
652
+ {
653
+ var moniker = testDetails . Moniker ;
654
+ var csprojFormat = testDetails . CsprojType ;
655
+ var mongoDriverPackageVersion = testDetails . Version ;
656
+ var command = testDetails . Command ;
657
+
658
+ Information ( $ "Moniker: { moniker } , csproj style: { csprojFormat } ") ;
659
+
660
+ var monikerTestFolder = artifactsPackagingTestsDirectory . Combine ( $ "{ moniker } _{ csprojFormat } _{ command } ") ;
661
+ Information ( $ "Moniker test folder: { monikerTestFolder } ") ;
662
+ EnsureDirectoryExists ( monikerTestFolder ) ;
663
+ CleanDirectory ( monikerTestFolder ) ;
664
+
665
+ var csprojFileName = $ "{ monikerTestFolder . GetDirectoryName ( ) } .csproj";
666
+ var csprojFullPath = monikerTestFolder . CombineWithFilePath ( csprojFileName ) ;
667
+
668
+ switch ( command )
669
+ {
670
+ case "xunit" :
671
+ {
672
+ if ( moniker == "net472" )
673
+ {
674
+ // CSHARP-3806
675
+ return ;
676
+ }
677
+
678
+ Information ( "Creating test project..." ) ;
679
+ DotNetCoreTool ( csprojFullPath , "new xunit" , $ "--target-framework-override { moniker } --language C# ") ;
680
+ Information ( "Created test project" ) ;
681
+
682
+ Information ( $ "Adding FluentAssertions...") ;
683
+ DotNetCoreTool (
684
+ csprojFullPath ,
685
+ "add package FluentAssertions" ,
686
+ $ "--framework { moniker } --version 4.12.0"
687
+ ) ;
688
+ Information ( $ "Added FluentAssertions") ;
689
+
690
+ Information ( $ "Adding test package...") ;
691
+ DotNetCoreTool (
692
+ csprojFullPath ,
693
+ $ "add package { mongoDbDriverPackageName } ",
694
+ $ "--framework { moniker } --version { mongoDriverPackageVersion } "
695
+ ) ;
696
+ Information ( "Added tested package" ) ;
697
+
698
+ DeleteFile ( monikerTestFolder . CombineWithFilePath ( "UnitTest1.cs" ) ) ; // Remove a default unit test
699
+ var packagingTestsDirectory = testsDirectory . Combine ( "MongoDB.Driver.Tests" ) . Combine ( "Packaging" ) ;
700
+ Console . WriteLine ( $ "Original test file { packagingTestsDirectory } ") ;
701
+ var files = GetFiles ( $ "{ packagingTestsDirectory } /*.cs") . ToList ( ) ;
702
+ CopyFiles ( files , monikerTestFolder ) ; // copy tests content
703
+
704
+ Information ( "Running tests..." ) ;
705
+ DotNetCoreTest (
706
+ csprojFullPath . ToString ( ) ,
707
+ new DotNetCoreTestSettings
708
+ {
709
+ Framework = moniker ,
710
+ Configuration = configuration ,
711
+ ArgumentCustomization = args => args . Append ( $ "-- RunConfiguration.TargetPlatform={ testDetails . Bitness } ")
712
+ }
713
+ ) ;
714
+ }
715
+ break ;
716
+ case "console" :
717
+ {
718
+ Information ( "Creating console project..." ) ;
719
+ DotNetCoreTool ( csprojFullPath , "new console" , $ "--target-framework-override { moniker } --language C# ") ;
720
+ Information ( "Created test project" ) ;
721
+
722
+ Information ( $ "Adding tested package...") ;
723
+ DotNetCoreTool (
724
+ csprojFullPath ,
725
+ $ "add package { mongoDbDriverPackageName } ",
726
+ $ "--framework { moniker } --version { mongoDriverPackageVersion } "
727
+ ) ;
728
+ Information ( "Added test package" ) ;
729
+
730
+ // the below two packages are added just to allow using the same code as in xunit
731
+ Information ( $ "Adding FluentAssertions...") ;
732
+ DotNetCoreTool (
733
+ csprojFullPath ,
734
+ "add package FluentAssertions" ,
735
+ $ "--framework { moniker } --version 4.12.0"
736
+ ) ;
737
+ Information ( $ "Added FluentAssertions") ;
738
+
739
+ Information ( $ "Adding xunit...") ;
740
+ DotNetCoreTool (
741
+ csprojFullPath ,
742
+ "add package xunit" ,
743
+ $ "--framework { moniker } --version 2.4.0"
744
+ ) ;
745
+ Information ( $ "Added xunit") ;
746
+
747
+ DeleteFile ( monikerTestFolder . CombineWithFilePath ( "Program.cs" ) ) ; // Remove a default .cs file
748
+ var packagingTestsDirectory = testsDirectory . Combine ( "MongoDB.Driver.Tests" ) . Combine ( "Packaging" ) ;
749
+ Console . WriteLine ( $ "Original test file { packagingTestsDirectory } ") ;
750
+ var files = GetFiles ( $ "{ packagingTestsDirectory } /*.cs") . ToList ( ) ;
751
+ CopyFiles ( files , monikerTestFolder ) ; // copy tests content
752
+
753
+ Information ( "Running console app..." ) ;
754
+ DotNetCoreRun (
755
+ csprojFullPath . ToString ( ) ,
756
+ new DotNetCoreRunSettings
757
+ {
758
+ EnvironmentVariables = new Dictionary < string , string > ( )
759
+ {
760
+ { "DefineConstants" , "CONSOLE_TEST" }
761
+ } ,
762
+ Framework = moniker ,
763
+ Configuration = configuration ,
764
+ ArgumentCustomization = args => args . Append ( $ "-- RunConfiguration.TargetPlatform={ testDetails . Bitness } ")
765
+ }
766
+ ) ;
767
+ }
768
+ break ;
769
+ default : throw new NotSupportedException ( $ "Packaging tests for { testDetails . Command } is not supported.") ;
770
+ }
771
+ } )
772
+ . DeferOnError ( ) ;
773
+
565
774
RunTarget( target ) ;
566
775
567
776
void ThrowIfNotDefaultTarget( string @value )
0 commit comments