From 3e52161bd7fcb134396249dee6c84e129f4b752a Mon Sep 17 00:00:00 2001 From: Charlie Poole Date: Tue, 14 Sep 2021 03:04:15 -0700 Subject: [PATCH 1/2] Move constant definitions to the files that use them; add utilities.cake --- build.cake | 46 ++++++++++++++++++++++++++++++-------------- build.ps1 | 2 +- cake/constants.cake | 37 ----------------------------------- cake/packaging.cake | 19 ------------------ cake/parameters.cake | 24 +++++++++++++++++++---- cake/utilities.cake | 30 +++++++++++++++++++++++++++++ nunit.v2.driver.sln | 2 +- 7 files changed, 84 insertions(+), 76 deletions(-) delete mode 100644 cake/constants.cake create mode 100644 cake/utilities.cake diff --git a/build.cake b/build.cake index 53f364e..377032d 100644 --- a/build.cake +++ b/build.cake @@ -3,24 +3,28 @@ #tool nuget:?package=NUnit.ConsoleRunner&version=3.11.1 #tool nuget:?package=NUnit.ConsoleRunner&version=3.10.0 +//////////////////////////////////////////////////////////////////// +// PROJECT-SPECIFIC CONSTANTS +////////////////////////////////////////////////////////////////////// + +const string SOLUTION_FILE = "nunit.v2.driver.sln"; +const string NUGET_ID = "NUnit.Extension.NUnitV2Driver"; +const string CHOCO_ID = "nunit-extension-nunit-v2-driver"; +const string DEFAULT_VERSION = "3.9.0"; +const string DEFAULT_CONFIGURATION = "Release"; + +// Load scripts after defining constants +#load cake/parameters.cake + ////////////////////////////////////////////////////////////////////// // ARGUMENTS ////////////////////////////////////////////////////////////////////// -// NOTE: These two constants are set here because constants.cake -// isn't loaded until after the arguments are parsed. -// -// Since GitVersion is only used when running under -// Windows, the default version should be updated to the -// next version after each release. -const string DEFAULT_VERSION = "3.9.0"; -const string DEFAULT_CONFIGURATION = "Release"; - var target = Argument("target", "Default"); -// Load additional cake files here since some of them -// depend on the arguments provided. -#load cake/parameters.cake +// Additional arguments defined in the cake scripts: +// --configuration +// --version ////////////////////////////////////////////////////////////////////// // SETUP AND TEARDOWN @@ -55,9 +59,19 @@ Task("DumpSettings") Task("Clean") .Does((parameters) => { + Information("Cleaning " + parameters.OutputDirectory); CleanDirectory(parameters.OutputDirectory); }); +Task("CleanAll") + .Does((parameters) => + { + Information("Cleaning all output directories"); + CleanDirectory(parameters.ProjectDirectory + "bin/"); + + Information("Deleting object directories"); + DeleteObjectDirectories(parameters); + }); ////////////////////////////////////////////////////////////////////// // INITIALIZE FOR BUILD @@ -68,7 +82,11 @@ Task("NuGetRestore") { NuGetRestore(SOLUTION_FILE, new NuGetRestoreSettings() { - Source = PACKAGE_SOURCES + Source = new string[] + { + "https://www.nuget.org/api/v2", + "https://www.myget.org/F/nunit/api/v2" + } }); }); @@ -108,7 +126,7 @@ Task("Test") .IsDependentOn("Build") .Does((parameters) => { - NUnit3(parameters.OutputDirectory + UNIT_TEST_ASSEMBLY); + NUnit3(parameters.OutputDirectory + "nunit.v2.driver.tests.dll"); }); ////////////////////////////////////////////////////////////////////// diff --git a/build.ps1 b/build.ps1 index 18b8560..3f52e6e 100644 --- a/build.ps1 +++ b/build.ps1 @@ -44,7 +44,7 @@ Param( [string]$Target = "Default", [string]$Configuration = "Release", [ValidateSet("Quiet", "Minimal", "Normal", "Verbose", "Diagnostic")] - [string]$Verbosity = "Verbose", + [string]$Verbosity = "Normal", [switch]$Experimental, [Alias("DryRun","Noop")] [switch]$WhatIf, diff --git a/cake/constants.cake b/cake/constants.cake deleted file mode 100644 index d50dce2..0000000 --- a/cake/constants.cake +++ /dev/null @@ -1,37 +0,0 @@ -// This file contains both constants and static readonly values, which -// are used as constants. The latter must not depend in any way on the -// contents of other cake files, which are loaded after this one. - -// Files -const string SOLUTION_FILE = "nunit.v2.driver.sln"; -const string UNIT_TEST_ASSEMBLY = "nunit.v2.driver.tests.dll"; - -// Packaging -const string NUGET_ID = "NUnit.Extension.NUnitV2Driver"; -const string CHOCO_ID = "nunit-extension-nunit-v2-driver"; - -// Package sources for nuget restore - used in build.cake -static readonly string[] PACKAGE_SOURCES = new string[] -{ - "https://www.nuget.org/api/v2", - "https://www.myget.org/F/nunit/api/v2" -}; - -// The following items are only used in parameters.cake - -// URLs for uploading packages -private const string MYGET_PUSH_URL = "https://www.myget.org/F/nunit/api/v2"; -private const string NUGET_PUSH_URL = "https://api.nuget.org/v3/index.json"; -private const string CHOCO_PUSH_URL = "https://push.chocolatey.org/"; - -// Environment Variable names holding API keys -private const string MYGET_API_KEY = "MYGET_API_KEY"; -private const string NUGET_API_KEY = "NUGET_API_KEY"; -private const string CHOCO_API_KEY = "CHOCO_API_KEY"; -private const string GITHUB_ACCESS_TOKEN = "GITHUB_ACCESS_TOKEN"; - -// Pre-release labels that we publish -private static readonly string[] LABELS_WE_PUBLISH_ON_MYGET = { "dev", "pre" }; -private static readonly string[] LABELS_WE_PUBLISH_ON_NUGET = { "alpha", "beta", "rc" }; -private static readonly string[] LABELS_WE_PUBLISH_ON_CHOCOLATEY = { "alpha", "beta", "rc" }; -private static readonly string[] LABELS_WE_RELEASE_ON_GITHUB = { "alpha", "beta", "rc" }; diff --git a/cake/packaging.cake b/cake/packaging.cake index 8bcd409..e7bb925 100644 --- a/cake/packaging.cake +++ b/cake/packaging.cake @@ -104,22 +104,3 @@ public void BuildChocolateyPackage(BuildParameters parameters) } }); } - -private void PushNuGetPackage(FilePath package, string apiKey, string url) -{ - CheckPackageExists(package); - NuGetPush(package, new NuGetPushSettings() { ApiKey = apiKey, Source = url }); -} - -private void PushChocolateyPackage(FilePath package, string apiKey, string url) -{ - CheckPackageExists(package); - ChocolateyPush(package, new ChocolateyPushSettings() { ApiKey = apiKey, Source = url }); -} - -private void CheckPackageExists(FilePath package) -{ - if (!FileExists(package)) - throw new InvalidOperationException( - $"Package not found: {package.GetFilename()}.\nCode may have changed since package was last built."); -} diff --git a/cake/parameters.cake b/cake/parameters.cake index 78bfe5d..a2de0cf 100644 --- a/cake/parameters.cake +++ b/cake/parameters.cake @@ -1,13 +1,30 @@ -#load "./constants.cake" -#load "./versioning.cake" +#load "./versioning.cake" #load "./packaging.cake" #load "./package-checks.cake" #load "./package-tests.cake" #load "./test-results.cake" #load "./test-reports.cake" +#load "./utilities.cake" using System; +// URLs for uploading packages +private const string MYGET_PUSH_URL = "https://www.myget.org/F/nunit/api/v2"; +private const string NUGET_PUSH_URL = "https://api.nuget.org/v3/index.json"; +private const string CHOCO_PUSH_URL = "https://push.chocolatey.org/"; + +// Environment Variable names holding API keys +private const string MYGET_API_KEY = "MYGET_API_KEY"; +private const string NUGET_API_KEY = "NUGET_API_KEY"; +private const string CHOCO_API_KEY = "CHOCO_API_KEY"; +private const string GITHUB_ACCESS_TOKEN = "GITHUB_ACCESS_TOKEN"; + +// Pre-release labels that we publish +private static readonly string[] LABELS_WE_PUBLISH_ON_MYGET = { "dev", "pre" }; +private static readonly string[] LABELS_WE_PUBLISH_ON_NUGET = { "alpha", "beta", "rc" }; +private static readonly string[] LABELS_WE_PUBLISH_ON_CHOCOLATEY = { "alpha", "beta", "rc" }; +private static readonly string[] LABELS_WE_RELEASE_ON_GITHUB = { "alpha", "beta", "rc" }; + public class BuildParameters { private ISetupContext _context; @@ -63,8 +80,7 @@ public class BuildParameters public string ProjectDirectory { get; } public string OutputDirectory => ProjectDirectory + "bin/" + Configuration + "/"; - public string Net20OutputDirectory => OutputDirectory + "net20/"; - public string NetCore21OutputDirectory => OutputDirectory + "netcoreapp2.1/"; + public string SourceDirectory => ProjectDirectory + "src/"; public string PackageDirectory => ProjectDirectory + "output/"; public string ToolsDirectory => ProjectDirectory + "tools/"; public string NuGetInstallDirectory => ToolsDirectory + NUGET_ID + "/"; diff --git a/cake/utilities.cake b/cake/utilities.cake new file mode 100644 index 0000000..e3da86b --- /dev/null +++ b/cake/utilities.cake @@ -0,0 +1,30 @@ +////////////////////////////////////////////////////////////////////// +// GLOBALLY ACCESSIBLE UTILITY METHODS +////////////////////////////////////////////////////////////////////// + +public void DeleteObjectDirectories(BuildParameters parameters) +{ + string pattern = parameters.SourceDirectory + "**/obj/"; + + foreach (var dir in GetDirectories(pattern)) + DeleteDirectory(dir, new DeleteDirectorySettings() { Recursive = true }); +} + +private void PushNuGetPackage(FilePath package, string apiKey, string url) +{ + CheckPackageExists(package); + NuGetPush(package, new NuGetPushSettings() { ApiKey = apiKey, Source = url }); +} + +private void PushChocolateyPackage(FilePath package, string apiKey, string url) +{ + CheckPackageExists(package); + ChocolateyPush(package, new ChocolateyPushSettings() { ApiKey = apiKey, Source = url }); +} + +private void CheckPackageExists(FilePath package) +{ + if (!FileExists(package)) + throw new InvalidOperationException( + $"Package not found: {package.GetFilename()}.\nCode may have changed since package was last built."); +} diff --git a/nunit.v2.driver.sln b/nunit.v2.driver.sln index 1640b95..8374401 100644 --- a/nunit.v2.driver.sln +++ b/nunit.v2.driver.sln @@ -26,13 +26,13 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "nunit.v2.driver.tests", "sr EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "cake", "cake", "{0E1320F6-992A-48FA-871B-5FD018BD8BE8}" ProjectSection(SolutionItems) = preProject - cake\constants.cake = cake\constants.cake cake\package-checks.cake = cake\package-checks.cake cake\package-tests.cake = cake\package-tests.cake cake\packaging.cake = cake\packaging.cake cake\parameters.cake = cake\parameters.cake cake\test-reports.cake = cake\test-reports.cake cake\test-results.cake = cake\test-results.cake + cake\utilities.cake = cake\utilities.cake cake\versioning.cake = cake\versioning.cake EndProjectSection EndProject From d5e311c3f6d0aab573834a9e6fa517d3082d85f9 Mon Sep 17 00:00:00 2001 From: Charlie Poole Date: Tue, 14 Sep 2021 03:29:21 -0700 Subject: [PATCH 2/2] Separate definition of package tests from the test runner --- build.cake | 25 +++++++++++++++++++++++-- cake/package-tests.cake | 25 ++----------------------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/build.cake b/build.cake index 377032d..177a5e0 100644 --- a/build.cake +++ b/build.cake @@ -169,7 +169,7 @@ Task("TestNuGetPackage") .IsDependentOn("InstallNuGetPackage") .Does((parameters) => { - new NuGetPackageTester(parameters).RunPackageTests(); + new NuGetPackageTester(parameters).RunPackageTests(PackageTests); }); Task("BuildChocolateyPackage") @@ -215,9 +215,30 @@ Task("TestChocolateyPackage") //using (var writer = new StreamWriter(runnerDir + "/choco.engine.addins")) // writer.WriteLine("../../nunit-extension-*/tools/"); - new ChocolateyPackageTester(parameters).RunPackageTests(); + new ChocolateyPackageTester(parameters).RunPackageTests(PackageTests); }); +PackageTest[] PackageTests = new PackageTest[] +{ + new PackageTest() + { + Description = "Integration Tests using NUnit V2", + Arguments = "bin/Release/v2-tests/v2-test-assembly.dll", + TestConsoleVersions = new string[] { "3.12.0", "3.11.1", "3.10.0" }, + ExpectedResult = new ExpectedResult("Passed") + { + Total = 75, + Passed = 75, + Failed = 0, + Warnings = 0, + Inconclusive = 0, + Skipped = 0, + Assemblies = new[] { new ExpectedAssemblyResult( + System.IO.Path.GetFullPath("bin/Release/v2-tests/v2-test-assembly.dll"), "net-2.0") } + } + } +}; + ////////////////////////////////////////////////////////////////////// // PUBLISH ////////////////////////////////////////////////////////////////////// diff --git a/cake/package-tests.cake b/cake/package-tests.cake index 4fdba02..ba72f4c 100644 --- a/cake/package-tests.cake +++ b/cake/package-tests.cake @@ -27,38 +27,17 @@ public abstract class PackageTester { _parameters = parameters; _context = parameters.Context; - - PackageTests.Add(new PackageTest() - { - Description = "Integration Tests using NUnit V2", - Arguments = "bin/Release/v2-tests/v2-test-assembly.dll", - TestConsoleVersions = new string[] { "3.12.0", "3.11.1", "3.10.0" }, - ExpectedResult = new ExpectedResult("Passed") - { - Total = 75, - Passed = 75, - Failed = 0, - Warnings = 0, - Inconclusive = 0, - Skipped = 0, - Assemblies = new[] { new ExpectedAssemblyResult( - System.IO.Path.GetFullPath("bin/Release/v2-tests/v2-test-assembly.dll"), "net-2.0") } - } - }); } protected abstract string PackageName { get; } protected abstract string PackageUnderTest { get; } public abstract string InstallDirectory { get; } - public PackageCheck[] PackageChecks { get; set; } - public List PackageTests = new List(); - - public void RunPackageTests() + public void RunPackageTests(IList packageTests) { var reporter = new ResultReporter(PackageName); - foreach (var packageTest in PackageTests) + foreach (var packageTest in packageTests) { foreach (var consoleVersion in packageTest.TestConsoleVersions) {