forked from mongodb/mongo-csharp-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.cake
824 lines (727 loc) · 31.4 KB
/
build.cake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
#addin nuget:?package=Cake.FileHelpers&version=4.0.1
#addin nuget:?package=Cake.Git&version=1.0.1
#addin nuget:?package=Cake.Incubator&version=6.0.0
#tool dotnet:?package=GitVersion.Tool&version=5.6.9
#tool nuget:?package=JunitXml.TestLogger&version=3.0.98
using System;
using System.Text.RegularExpressions;
using System.Linq;
using Cake.Common.Tools.DotNetCore.DotNetCoreVerbosity;
using Path = Cake.Core.IO.Path;
const string defaultTarget = "Default";
var target = Argument("target", defaultTarget);
var configuration = Argument("configuration", "Release");
var gitVersion = GitVersion();
var solutionDirectory = MakeAbsolute(Directory("./"));
var artifactsDirectory = solutionDirectory.Combine("artifacts");
var artifactsBinDirectory = artifactsDirectory.Combine("bin");
var artifactsDocsDirectory = artifactsDirectory.Combine("docs");
var artifactsDocsApiDocsDirectory = artifactsDocsDirectory.Combine("ApiDocs-" + gitVersion.LegacySemVer);
var artifactsDocsRefDocsDirectory = artifactsDocsDirectory.Combine("RefDocs-" + gitVersion.LegacySemVer);
var artifactsPackagesDirectory = artifactsDirectory.Combine("packages");
var docsDirectory = solutionDirectory.Combine("Docs");
var docsApiDirectory = docsDirectory.Combine("Api");
var srcDirectory = solutionDirectory.Combine("src");
var testsDirectory = solutionDirectory.Combine("tests");
var outputDirectory = solutionDirectory.Combine("build");
var toolsDirectory = solutionDirectory.Combine("tools");
var toolsHugoDirectory = toolsDirectory.Combine("Hugo");
var artifactsPackagingTestsDirectory = artifactsDirectory.Combine("Packaging.Tests");
var mongoDbDriverPackageName = "MongoDB.Driver";
var solutionFile = solutionDirectory.CombineWithFilePath("CSharpDriver.sln");
var solutionFullPath = solutionFile.FullPath;
var srcProjectNames = new[]
{
"MongoDB.Bson",
"MongoDB.Driver.Core",
"MongoDB.Driver",
"MongoDB.Driver.Legacy",
"MongoDB.Driver.GridFS"
};
Task("Default")
.IsDependentOn("Test");
Task("Release")
.IsDependentOn("Build")
.IsDependentOn("Test")
.IsDependentOn("Docs")
.IsDependentOn("Package");
Task("Restore")
.Does(() =>
{
// disable parallel restore to work around apparent bugs in restore
var restoreSettings = new DotNetCoreRestoreSettings
{
DisableParallel = true
};
DotNetCoreRestore(solutionFullPath, restoreSettings);
});
Task("Build")
.IsDependentOn("Restore")
.Does<BuildConfig>((buildConfig) =>
{
var settings = new DotNetCoreBuildSettings
{
NoRestore = true,
Configuration = configuration,
EnvironmentVariables = new Dictionary<string, string>
{
{ "Version", gitVersion.LegacySemVer },
{ "SourceRevisionId", gitVersion.Sha }
}
};
if (buildConfig.IsReleaseMode)
{
Console.WriteLine("Build continuousIntegration is enabled");
settings.MSBuildSettings = new DotNetCoreMSBuildSettings();
// configure deterministic build for better compatibility with debug symbols (used in Package/Build tasks). Affects: *.nupkg
settings.MSBuildSettings.SetContinuousIntegrationBuild(continuousIntegrationBuild: true);
}
DotNetCoreBuild(solutionFullPath, settings);
});
Task("BuildArtifacts")
.IsDependentOn("Build")
.Does(() =>
{
foreach (var targetFramework in new[] { "net472", "netstandard2.0", "netstandard2.1" })
{
var toDirectory = artifactsBinDirectory.Combine(targetFramework);
CleanDirectory(toDirectory);
var projects = new[] { "MongoDB.Bson", "MongoDB.Driver.Core", "MongoDB.Driver", "MongoDB.Driver.Legacy", "MongoDB.Driver.GridFS" };
foreach (var project in projects)
{
var fromDirectory = srcDirectory.Combine(project).Combine("bin").Combine(configuration).Combine(targetFramework);
var fileNames = new List<string>();
foreach (var extension in new[] { "dll", "pdb", "xml" })
{
var fileName = $"{project}.{extension}";
fileNames.Add(fileName);
}
// add additional files needed by Sandcastle
if (targetFramework == "net472" && project == "MongoDB.Driver.Core")
{
fileNames.Add("DnsClient.dll");
fileNames.Add("MongoDB.Libmongocrypt.dll");
fileNames.Add("SharpCompress.dll");
}
foreach (var fileName in fileNames)
{
var fromFile = fromDirectory.CombineWithFilePath(fileName);
var toFile = toDirectory.CombineWithFilePath(fileName);
CopyFile(fromFile, toFile);
}
}
}
});
Task("Test")
.IsDependentOn("Build")
.DoesForEach(
items: GetFiles("./**/*.Tests.csproj").Where(name => !name.ToString().Contains("Atlas")),
action: (BuildConfig buildConfig, Path testProject) =>
{
if (Environment.GetEnvironmentVariable("MONGODB_API_VERSION") != null &&
testProject.ToString().Contains("Legacy"))
{
return; // Legacy tests are exempt from Version API testing
}
var testWithDefaultGuidRepresentationMode = Environment.GetEnvironmentVariable("TEST_WITH_DEFAULT_GUID_REPRESENTATION_MODE");
if (testWithDefaultGuidRepresentationMode != null)
{
Console.WriteLine($"TEST_WITH_DEFAULT_GUID_REPRESENTATION_MODE={testWithDefaultGuidRepresentationMode}");
}
var testWithDefaultGuidRepresentation = Environment.GetEnvironmentVariable("TEST_WITH_DEFAULT_GUID_REPRESENTATION");
if (testWithDefaultGuidRepresentation != null)
{
Console.WriteLine($"TEST_WITH_DEFAULT_GUID_REPRESENTATION={testWithDefaultGuidRepresentation}");
}
var mongoX509ClientCertificatePath = Environment.GetEnvironmentVariable("MONGO_X509_CLIENT_CERTIFICATE_PATH");
if (mongoX509ClientCertificatePath != null)
{
Console.WriteLine($"MONGO_X509_CLIENT_CERTIFICATE_PATH={mongoX509ClientCertificatePath}");
}
var mongoX509ClientCertificatePassword = Environment.GetEnvironmentVariable("MONGO_X509_CLIENT_CERTIFICATE_PASSWORD");
if (mongoX509ClientCertificatePassword != null)
{
Console.WriteLine($"MONGO_X509_CLIENT_CERTIFICATE_PASSWORD={mongoX509ClientCertificatePassword}");
}
var settings = new DotNetCoreTestSettings
{
NoBuild = true,
NoRestore = true,
Configuration = configuration,
Loggers = CreateLoggers(),
ArgumentCustomization = args => args.Append("-- RunConfiguration.TargetPlatform=x64"),
Framework = buildConfig.Framework
};
DotNetCoreTest(
testProject.FullPath,
settings
);
})
.DeferOnError();
Task("TestNet472").IsDependentOn("Test");
Task("TestNetStandard20").IsDependentOn("Test");
Task("TestNetStandard21").IsDependentOn("Test");
Task("TestAwsAuthentication")
.IsDependentOn("Build")
.DoesForEach(
GetFiles("./**/MongoDB.Driver.Tests.csproj"),
testProject =>
{
DotNetCoreTest(
testProject.FullPath,
new DotNetCoreTestSettings {
NoBuild = true,
NoRestore = true,
Configuration = configuration,
ArgumentCustomization = args => args.Append("-- RunConfiguration.TargetPlatform=x64"),
Filter = "Category=\"AwsMechanism\""
}
);
});
Task("TestPlainAuthentication")
.IsDependentOn("Build")
.DoesForEach(
GetFiles("./**/MongoDB.Driver.Tests.csproj"),
testProject =>
{
DotNetCoreTest(
testProject.FullPath,
new DotNetCoreTestSettings {
NoBuild = true,
NoRestore = true,
Configuration = configuration,
ArgumentCustomization = args => args.Append("-- RunConfiguration.TargetPlatform=x64"),
Filter = "Category=\"PlainMechanism\""
}
);
});
// currently we are not running this Task on Evergreen (only locally occassionally)
Task("TestAllGuidRepresentations")
.IsDependentOn("Build")
.DoesForEach(
GetFiles("./**/*.Tests.csproj")
// .Where(name => name.ToString().Contains("Bson.Tests")) // uncomment to only test Bson
.Where(name => !name.ToString().Contains("Atlas")),
testProject =>
{
var modes = new string[][]
{
new[] { "V2", "Unspecified" },
new[] { "V2", "JavaLegacy" },
new[] { "V2", "Standard" },
new[] { "V2", "PythonLegacy" },
new[] { "V2", "CSharpLegacy" },
new[] { "V3", "Unspecified" }
};
foreach (var mode in modes)
{
var testWithGuidRepresentationMode = mode[0];
var testWithGuidRepresentation = mode[1];
Console.WriteLine($"TEST_WITH_DEFAULT_GUID_REPRESENTATION_MODE={testWithGuidRepresentationMode}");
Console.WriteLine($"TEST_WITH_DEFAULT_GUID_REPRESENTATION={testWithGuidRepresentation}");
DotNetCoreTest(
testProject.FullPath,
new DotNetCoreTestSettings {
NoBuild = true,
NoRestore = true,
Configuration = configuration,
ArgumentCustomization = args => args.Append("-- RunConfiguration.TargetPlatform=x64"),
EnvironmentVariables = new Dictionary<string, string>
{
{ "TEST_WITH_DEFAULT_GUID_REPRESENTATION_MODE", testWithGuidRepresentationMode },
{ "TEST_WITH_DEFAULT_GUID_REPRESENTATION", testWithGuidRepresentation }
}
}
);
}
});
Task("TestAtlasConnectivity")
.IsDependentOn("Build")
.DoesForEach(
GetFiles("./**/AtlasConnectivity.Tests.csproj"),
testProject =>
{
DotNetCoreTest(
testProject.FullPath,
new DotNetCoreTestSettings {
NoBuild = true,
NoRestore = true,
Configuration = configuration,
ArgumentCustomization = args => args.Append("-- RunConfiguration.TargetPlatform=x64")
}
);
});
Task("TestAtlasDataLake")
.IsDependentOn("Build")
.DoesForEach(
GetFiles("./**/MongoDB.Driver.Tests.csproj"),
testProject =>
{
DotNetCoreTest(
testProject.FullPath,
new DotNetCoreTestSettings {
NoBuild = true,
NoRestore = true,
Configuration = configuration,
ArgumentCustomization = args => args.Append("-- RunConfiguration.TargetPlatform=x64"),
Filter = "Category=\"AtlasDataLake\""
}
);
});
Task("TestOcsp")
.IsDependentOn("Build")
.DoesForEach(
GetFiles("./**/MongoDB.Driver.Tests.csproj"),
testProject =>
{
DotNetCoreTest(
testProject.FullPath,
new DotNetCoreTestSettings {
NoBuild = true,
NoRestore = true,
Configuration = configuration,
ArgumentCustomization = args => args
.Append("--filter FullyQualifiedName~OcspIntegrationTests")
.Append("-- RunConfiguration.TargetPlatform=x64")
}
);
});
Task("TestGssapi")
.IsDependentOn("Build")
.DoesForEach(
items: GetFiles("./**/MongoDB.Driver.Tests.csproj"),
action: (BuildConfig buildConfig, Path testProject) =>
{
var settings = new DotNetCoreTestSettings
{
NoBuild = true,
NoRestore = true,
Configuration = configuration,
ArgumentCustomization = args => args.Append("-- RunConfiguration.TargetPlatform=x64"),
Filter = "Category=\"GssapiMechanism\"",
Framework = buildConfig.Framework
};
DotNetCoreTest(
testProject.FullPath,
settings
);
});
Task("TestGssapiNet472").IsDependentOn("TestGssapi");
Task("TestGssapiNetStandard20").IsDependentOn("TestGssapi");
Task("TestGssapiNetStandard21").IsDependentOn("TestGssapi");
Task("TestServerless")
.IsDependentOn("Build")
.DoesForEach(
items: GetFiles("./**/MongoDB.Driver.Tests.csproj"),
action: (BuildConfig buildConfig, Path testProject) =>
{
var settings = new DotNetCoreTestSettings
{
NoBuild = true,
NoRestore = true,
Configuration = configuration,
ArgumentCustomization = args => args.Append("-- RunConfiguration.TargetPlatform=x64"),
Filter = "Category=\"Serverless\"",
Framework = buildConfig.Framework
};
DotNetCoreTest(
testProject.FullPath,
settings
);
});
Task("TestServerlessNet472").IsDependentOn("TestServerless");
Task("TestServerlessNetStandard20").IsDependentOn("TestServerless");
Task("TestServerlessNetStandard21").IsDependentOn("TestServerless");
Task("TestLoadBalanced")
.IsDependentOn("Build")
.DoesForEach(
items: GetFiles("./**/*.Tests.csproj"),
action: (BuildConfig buildConfig, Path testProject) =>
{
var settings = new DotNetCoreTestSettings
{
NoBuild = true,
NoRestore = true,
Configuration = configuration,
ArgumentCustomization = args => args.Append("-- RunConfiguration.TargetPlatform=x64"),
Filter = "Category=\"SupportLoadBalancing\"",
Framework = buildConfig.Framework
};
DotNetCoreTest(
testProject.FullPath,
settings
);
});
Task("TestLoadBalancedNetStandard20").IsDependentOn("TestLoadBalanced");
Task("TestLoadBalancedNetStandard21").IsDependentOn("TestLoadBalanced");
Task("TestCsfleWithMockedKms")
.IsDependentOn("Build")
.DoesForEach(
items: GetFiles("./**/*.Tests.csproj"),
action: (BuildConfig buildConfig, Path testProject) =>
{
var settings = new DotNetCoreTestSettings
{
NoBuild = true,
NoRestore = true,
Configuration = configuration,
Loggers = CreateLoggers(),
ArgumentCustomization = args => args.Append("-- RunConfiguration.TargetPlatform=x64"),
Filter = "Category=\"CSFLE\"",
Framework = buildConfig.Framework
};
DotNetCoreTest(
testProject.FullPath,
settings
);
});
Task("TestCsfleWithMockedKmsnet472").IsDependentOn("TestCsfleWithMockedKms");
Task("TestCsfleWithMockedKmsNetStandard20").IsDependentOn("TestCsfleWithMockedKms");
Task("TestCsfleWithMockedKmsNetStandard21").IsDependentOn("TestCsfleWithMockedKms");
Task("Docs")
.IsDependentOn("ApiDocs")
.IsDependentOn("RefDocs");
Task("ApiDocs")
.IsDependentOn("BuildArtifacts")
.Does(() =>
{
EnsureDirectoryExists(artifactsDocsApiDocsDirectory);
CleanDirectory(artifactsDocsApiDocsDirectory);
var shfbprojFile = docsApiDirectory.CombineWithFilePath("CSharpDriverDocs.shfbproj");
var preliminary = false; // TODO: compute
MSBuild(shfbprojFile, new MSBuildSettings
{
Configuration = "Release"
}
.WithProperty("OutputPath", artifactsDocsApiDocsDirectory.ToString())
.WithProperty("CleanIntermediate", "True")
.WithProperty("Preliminary", preliminary ? "True" : "False")
.WithProperty("HelpFileVersion", gitVersion.LegacySemVer)
);
var lowerCaseIndexFile = artifactsDocsApiDocsDirectory.CombineWithFilePath("index.html");
var upperCaseIndexFile = artifactsDocsApiDocsDirectory.CombineWithFilePath("Index.html");
MoveFile(upperCaseIndexFile, lowerCaseIndexFile);
var chmFile = artifactsDocsApiDocsDirectory.CombineWithFilePath("CSharpDriverDocs.chm");
var artifactsDocsChmFile = artifactsDocsDirectory.CombineWithFilePath("CSharpDriverDocs.chm");
CopyFile(chmFile, artifactsDocsChmFile);
});
Task("RefDocs")
.Does(() =>
{
EnsureDirectoryExists(toolsHugoDirectory);
CleanDirectory(toolsHugoDirectory);
var url = "https://github.com/spf13/hugo/releases/download/v0.13/hugo_0.13_windows_amd64.zip";
var hugoZipFile = toolsHugoDirectory.CombineWithFilePath("hugo_0.13_windows_amd64.zip");
DownloadFile(url, hugoZipFile);
Unzip(hugoZipFile, toolsHugoDirectory);
var hugoExe = toolsHugoDirectory.CombineWithFilePath("hugo_0.13_windows_amd64.exe");
var landingDirectory = docsDirectory.Combine("landing");
var landingPublicDirectory = landingDirectory.Combine("public");
CleanDirectory(landingPublicDirectory);
var processSettings = new ProcessSettings
{
WorkingDirectory = landingDirectory
};
StartProcess(hugoExe, processSettings);
var referenceDirectory = docsDirectory.Combine("reference");
var referencePublicDirectory = referenceDirectory.Combine("public");
CleanDirectory(referencePublicDirectory);
processSettings = new ProcessSettings
{
WorkingDirectory = referenceDirectory
};
StartProcess(hugoExe, processSettings);
EnsureDirectoryExists(artifactsDocsRefDocsDirectory);
CleanDirectory(artifactsDocsRefDocsDirectory);
CopyDirectory(landingPublicDirectory, artifactsDocsRefDocsDirectory);
var artifactsReferencePublicDirectory = artifactsDocsRefDocsDirectory.Combine(gitVersion.Major + "." + gitVersion.Minor);
CopyDirectory(referencePublicDirectory, artifactsReferencePublicDirectory);
});
Task("Package")
.IsDependentOn("PackageNugetPackages");
Task("PackageNugetPackages")
.IsDependentOn("Build")
.Does(() =>
{
EnsureDirectoryExists(artifactsPackagesDirectory);
CleanDirectory(artifactsPackagesDirectory);
var projects = new[]
{
"MongoDB.Bson",
"MongoDB.Driver.Core",
"MongoDB.Driver",
"MongoDB.Driver.GridFS",
"MongoDB.Driver.Legacy"
};
foreach (var project in projects)
{
var projectPath = $"{srcDirectory}\\{project}\\{project}.csproj";
var settings = new DotNetCorePackSettings
{
Configuration = configuration,
OutputDirectory = artifactsPackagesDirectory,
NoBuild = true, // SetContinuousIntegrationBuild is enabled for nupkg on the Build step
IncludeSymbols = true,
MSBuildSettings = new DotNetCoreMSBuildSettings()
// configure deterministic build for better compatibility with debug symbols (used in Package/Build tasks). Affects: *.snupkg
.SetContinuousIntegrationBuild(continuousIntegrationBuild: true)
.WithProperty("PackageVersion", gitVersion.LegacySemVer)
};
DotNetCorePack(projectPath, settings);
}
});
Task("PushToNuGet")
.Does(() =>
{
var nugetApiKey = EnvironmentVariable("NUGETAPIKEY");
if (nugetApiKey == null)
{
throw new Exception("NUGETAPIKEY environment variable missing");
}
var packageFiles = new List<FilePath>();
var projects = new[]
{
"MongoDB.Bson",
"MongoDB.Driver.Core",
"MongoDB.Driver",
"MongoDB.Driver.GridFS",
"mongocsharpdriver" // the Nuget package name for MongoDB.Driver.Legacy
};
foreach (var project in projects)
{
var packageFileName = $"{project}.{gitVersion.LegacySemVer}.nupkg";
var packageFile = artifactsPackagesDirectory.CombineWithFilePath(packageFileName);
packageFiles.Add(packageFile);
}
NuGetPush(packageFiles, new NuGetPushSettings
{
ApiKey = nugetApiKey,
Source = "https://api.nuget.org/v3/index.json"
});
});
Task("DumpGitVersion")
.Does(() =>
{
Information(gitVersion.Dump());
});
Task("TestsPackagingProjectReference")
.IsDependentOn("Build")
.DoesForEach(
GetFiles("./**/*.Tests.csproj"),
testProject =>
{
var settings = new DotNetCoreTestSettings
{
NoBuild = true,
NoRestore = true,
Configuration = configuration,
ArgumentCustomization = args => args.Append("-- RunConfiguration.TargetPlatform=x64"),
Filter = "Category=\"Packaging\""
};
DotNetCoreTest(
testProject.FullPath,
settings
);
});
Task("TestsPackaging")
.IsDependentOn("TestsPackagingProjectReference")
.IsDependentOn("Package")
.DoesForEach(
() =>
{
var monikers = new[] { "net472", "netcoreapp21", "netcoreapp30", "net50" };
var csprojTypes = new[] { "SDK" };
var processorArchitectures = new[] { "x64" };
var projectTypes = new[] { "xunit", "console" };
return
from moniker in monikers
from csprojType in csprojTypes
from processorArchitecture in processorArchitectures
from projectType in projectTypes
select new { Moniker = moniker, CsprojType = csprojType, ProcessorArchitecture = processorArchitecture, ProjectType = projectType };
},
(testDetails) =>
{
var moniker = testDetails.Moniker;
var csprojFormat = testDetails.CsprojType;
var projectType = testDetails.ProjectType;
var processorArchitecture = testDetails.ProcessorArchitecture;
var localNugetSourceName = "LocalPackages";
Information($"Moniker: {moniker}, csproj style: {csprojFormat}");
var monikerTestFolder = artifactsPackagingTestsDirectory.Combine($"{moniker}_{csprojFormat}_{projectType}_{processorArchitecture}");
Information($"Moniker test folder: {monikerTestFolder}");
EnsureDirectoryExists(monikerTestFolder);
CleanDirectory(monikerTestFolder);
var csprojFileName = $"{monikerTestFolder.GetDirectoryName()}.csproj";
var csprojFullPath = monikerTestFolder.CombineWithFilePath(csprojFileName);
switch (projectType)
{
case "xunit":
{
if (moniker == "net472")
{
// CSHARP-3806
return;
}
Information("Creating test project...");
DotNetCoreTool(csprojFullPath, "new xunit", $"--target-framework-override {moniker} --language C# ");
Information("Created test project");
// the below two packages are added just to allow using the same code as in xunit
Information($"Adding FluentAssertions...");
DotNetCoreTool(
csprojFullPath,
"add package FluentAssertions",
$"--framework {moniker} --version 4.12.0"
);
Information($"Added FluentAssertions");
var mongoDriverPackageVersion = ConfigureAndGetTestedDriverVersion(monikerTestFolder, localNugetSourceName);
Information($"Adding test package...");
DotNetCoreTool(
csprojFullPath,
$"add package {mongoDbDriverPackageName}",
$"--framework {moniker} --version {mongoDriverPackageVersion}"
);
Information("Added tested package");
DeleteFile(monikerTestFolder.CombineWithFilePath("UnitTest1.cs")); // Remove a default unit test
var packagingTestsDirectory = testsDirectory.Combine("MongoDB.Driver.Tests").Combine("Packaging");
Console.WriteLine($"Original test file {packagingTestsDirectory}");
var files = GetFiles($"{packagingTestsDirectory}/*.cs").ToList();
CopyFiles(files, monikerTestFolder); // copy tests content
Information("Running tests...");
DotNetCoreTest(
csprojFullPath.ToString(),
new DotNetCoreTestSettings
{
Framework = moniker,
Configuration = configuration,
ArgumentCustomization = args => args.Append($"-- RunConfiguration.TargetPlatform={processorArchitecture}")
}
);
}
break;
case "console":
{
if (moniker == "netcoreapp21")
{
// https://github.com/dotnet/sdk/issues/8662
// The described solution works but it's tricky to implement it via scripts
return;
}
Information("Creating console project...");
DotNetCoreTool(csprojFullPath, "new console", $"--target-framework-override {moniker} --language C# ");
Information("Created test project");
// the below two packages are added just to allow using the same code as in xunit
Information($"Adding FluentAssertions...");
DotNetCoreTool(
csprojFullPath,
"add package FluentAssertions",
$"--framework {moniker} --version 4.12.0"
);
Information($"Added FluentAssertions");
Information($"Adding xunit...");
DotNetCoreTool(
csprojFullPath,
"add package xunit",
$"--framework {moniker} --version 2.4.0"
);
Information($"Added xunit");
var mongoDriverPackageVersion = ConfigureAndGetTestedDriverVersion(monikerTestFolder, localNugetSourceName);
Information($"Adding tested package...");
DotNetCoreTool(
csprojFullPath,
$"add package {mongoDbDriverPackageName}",
$"--framework {moniker} --version {mongoDriverPackageVersion}"
);
Information("Added test package");
DeleteFile(monikerTestFolder.CombineWithFilePath("Program.cs")); // Remove a default .cs file
var packagingTestsDirectory = testsDirectory.Combine("MongoDB.Driver.Tests").Combine("Packaging");
Console.WriteLine($"Original test file {packagingTestsDirectory}");
var files = GetFiles($"{packagingTestsDirectory}/*.cs").ToList();
CopyFiles(files, monikerTestFolder); // copy tests content
Information("Running console app...");
DotNetCoreRun(
csprojFullPath.ToString(),
new DotNetCoreRunSettings
{
EnvironmentVariables = new Dictionary<string, string>()
{
{ "DefineConstants", "CONSOLE_TEST" },
{ "PlatformTarget", processorArchitecture }
},
Framework = moniker,
Configuration = configuration
}
);
}
break;
default: throw new NotSupportedException($"Packaging tests for {projectType} is not supported.");
}
string ConfigureAndGetTestedDriverVersion(DirectoryPath directoryPath, string localNugetSourceName)
{
CreateNugetConfig(directoryPath, localNugetSourceName);
var packagesList = NuGetList(
new NuGetListSettings {
AllVersions = true,
Prerelease = true,
Source = new [] { $"{localNugetSourceName}" }, // corresponds to artifacts Nuget.config
WorkingDirectory = directoryPath
});
foreach(var package in packagesList)
{
Information("Found package {0}, version {1}", package.Name, package.Version);
}
if (packagesList.Count(p => p.Name == mongoDbDriverPackageName) != 1)
{
throw new Exception($"Package {mongoDbDriverPackageName} must be presented and unique.");
}
var mongoDriverPackageVersion = packagesList.Single(p => p.Name == mongoDbDriverPackageName).Version;
Information($"Package version {mongoDriverPackageVersion}");
return mongoDriverPackageVersion;
void CreateNugetConfig(DirectoryPath directoryPath, string localNugetSourceName)
{
var nugetConfigPath = directoryPath.CombineWithFilePath("nuget.config");
if (FileExists(nugetConfigPath)) DeleteFile(nugetConfigPath);
DotNetCoreTool(nugetConfigPath, "new nugetconfig"); // create a default nuget.config
// <packageSources>
// <add key="{localNugetSourceName}" value="..\..\packages" />
// </packageSources>
XmlPoke(nugetConfigPath, "/configuration/packageSources/add/@key", $"{localNugetSourceName}");
XmlPoke(nugetConfigPath, $"/configuration/packageSources/add[@key = '{localNugetSourceName}']/@value", @"..\..\packages");
}
}
})
.DeferOnError();
Setup<BuildConfig>(
setupContext =>
{
var lowerTarget = target.ToLowerInvariant();
var framework = lowerTarget switch
{
string s when s.StartsWith("test") && s.EndsWith("net472") => "net472",
string s when s.StartsWith("test") && s.EndsWith("netstandard20") => "netcoreapp2.1",
string s when s.StartsWith("test") && s.EndsWith("netstandard21") => "netcoreapp3.1",
_ => null
};
var isReleaseMode = lowerTarget.StartsWith("package") || lowerTarget == "release";
Console.WriteLine($"Framework: {framework ?? "null (not set)"}, IsReleaseMode: {isReleaseMode}");
return new BuildConfig(isReleaseMode, framework);
});
RunTarget(target);
public class BuildConfig
{
public bool IsReleaseMode { get; }
public string Framework { get; }
public BuildConfig(bool isReleaseMode, string framework)
{
IsReleaseMode = isReleaseMode;
Framework = framework;
}
}
string[] CreateLoggers()
{
var testResultsFile = outputDirectory.Combine("test-results").Combine($"TEST-{target.ToLowerInvariant()}-{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}.xml");
// Evergreen CI server requires JUnit output format to display test results
var junitLogger = $"junit;LogFilePath={testResultsFile};FailureBodyFormat=Verbose";
var consoleLogger = "console;verbosity=detailed";
return new []{ junitLogger, consoleLogger };
}