From 3242525d7e9046c3dec4b5a70f044d4679766433 Mon Sep 17 00:00:00 2001 From: jsakamoto Date: Thu, 3 Feb 2022 13:11:28 +0900 Subject: [PATCH] modernize unit tests --- test/BuildProjectABTest.cs | 35 ++-- test/BuildTest.cs | 166 +++++++++--------- test/GlobalUsings.cs | 5 + test/Lib/ExecutableFile.cs | 54 ------ test/Lib/Shell.cs | 56 ------ test/Lib/WorkSpace.cs | 24 --- ...um.WebDriver.GeckoDriver.NuPkg.Test.csproj | 10 +- test/global.json | 2 +- 8 files changed, 115 insertions(+), 237 deletions(-) create mode 100644 test/GlobalUsings.cs delete mode 100644 test/Lib/ExecutableFile.cs delete mode 100644 test/Lib/Shell.cs delete mode 100644 test/Lib/WorkSpace.cs diff --git a/test/BuildProjectABTest.cs b/test/BuildProjectABTest.cs index 6a6fb4e..ca2888c 100644 --- a/test/BuildProjectABTest.cs +++ b/test/BuildProjectABTest.cs @@ -1,25 +1,24 @@ -using System.IO; -using NUnit.Framework; -using Selenium.WebDriver.GeckoDriver.NuPkg.Test.Lib; +namespace Selenium.WebDriver.GeckoDriver.NuPkg.Test; -namespace Selenium.WebDriver.GeckoDriver.NuPkg.Test +[Parallelizable(ParallelScope.All)] +public class BuildProjectABTest { - [Parallelizable(ParallelScope.All)] - public class BuildProjectABTest + [Test] + public async Task Output_of_ProjectB_Contains_DriverFile_Test() { - [Test] - public void Output_of_ProjectB_Contains_DriverFile_Test() - { - using var workSpace = new WorkSpace(copyFrom: "ProjectAB"); + var unitTestProjectDir = FileIO.FindContainerDirToAncestor("*.csproj"); + using var workDir = WorkDirectory.CreateCopyFrom(Path.Combine(unitTestProjectDir, "ProjectAB"), item => item.Name is not "obj" and not "bin"); - var devenv = @"C:\Program Files\Microsoft Visual Studio\2022\Preview\Common7\IDE\devenv.exe"; - Shell.Run(workSpace, "nuget", "restore").Is(0); - Shell.Run(workSpace, devenv, "ProjectAB.sln", "/Build").Is(0); + var devenvExe = @"C:\Program Files\Microsoft Visual Studio\2022\Preview\Common7\IDE\devenv.exe"; + var nuget = await XProcess.Start("nuget", "restore", workDir).WaitForExitAsync(); + nuget.ExitCode.Is(0, message: nuget.Output); - var outDir = Path.Combine(workSpace, "ProjectB", "bin", "Debug", "net472"); - var driverFullPath1 = Path.Combine(outDir, "geckodriver"); - var driverFullPath2 = Path.Combine(outDir, "geckodriver.exe"); - (File.Exists(driverFullPath1) || File.Exists(driverFullPath2)).IsTrue(); - } + var devenv = await XProcess.Start(devenvExe, "ProjectAB.sln /Build", workDir).WaitForExitAsync(); + devenv.ExitCode.Is(0, message: devenv.Output); + + var outDir = Path.Combine(workDir, "ProjectB", "bin", "Debug", "net472"); + var driverFullPath1 = Path.Combine(outDir, "geckodriver"); + var driverFullPath2 = Path.Combine(outDir, "geckodriver.exe"); + (File.Exists(driverFullPath1) || File.Exists(driverFullPath2)).IsTrue(); } } diff --git a/test/BuildTest.cs b/test/BuildTest.cs index 957b9fe..60fafdc 100644 --- a/test/BuildTest.cs +++ b/test/BuildTest.cs @@ -1,105 +1,107 @@ -using System.IO; -using NUnit.Framework; -using Selenium.WebDriver.GeckoDriver.NuPkg.Test.Lib; -using static Selenium.WebDriver.GeckoDriver.NuPkg.Test.Lib.ExecutableFile; +namespace Selenium.WebDriver.GeckoDriver.NuPkg.Test; -namespace Selenium.WebDriver.GeckoDriver.NuPkg.Test +[Parallelizable(ParallelScope.All)] +public class BuildTest { - [Parallelizable(ParallelScope.All)] - public class BuildTest + public static object[][] Runtimes => new object[][]{ + new object[] { "win7-x86", "geckodriver.exe", Format.PE32 }, + new object[] { "win-x64", "geckodriver.exe", Format.PE64 }, + new object[] { "osx.10.12-x64", "geckodriver", Format.MachO }, + new object[] { "linux-x64", "geckodriver", Format.ELF }, + }; + + [Test] + [TestCaseSource(nameof(Runtimes))] + public async Task BuildWithRuntimeIdentifier_Test(string rid, string driverFileName, Format executableFileFormat) { - public static object[][] Runtimes => new object[][]{ - new object[] { "win7-x86", "geckodriver.exe", Format.PE32 }, - new object[] { "win-x64", "geckodriver.exe", Format.PE64 }, - new object[] { "osx.10.12-x64", "geckodriver", Format.MachO }, - new object[] { "linux-x64", "geckodriver", Format.ELF }, - }; + var unitTestProjectDir = FileIO.FindContainerDirToAncestor("*.csproj"); + using var workDir = WorkDirectory.CreateCopyFrom(Path.Combine(unitTestProjectDir, "Project"), item => item.Name is not "obj" and not "bin"); - [Test] - [TestCaseSource(nameof(Runtimes))] - public void BuildWithRuntimeIdentifier_Test(string rid, string driverFileName, Format executableFileFormat) - { - using var workSpace = new WorkSpace(copyFrom: "Project"); + var dotnet = await XProcess.Start("dotnet", $"build -r {rid} -o out", workDir).WaitForExitAsync(); + dotnet.ExitCode.Is(0, message: dotnet.Output); - var exitCode = Shell.Run(workSpace, "dotnet", "build", "-r", rid, "-o", "out"); - exitCode.Is(0); + var driverFullPath = Path.Combine(workDir, "out", driverFileName); + File.Exists(driverFullPath).IsTrue(); - var driverFullPath = Path.Combine(workSpace, "out", driverFileName); - File.Exists(driverFullPath).IsTrue(); + DetectFormat(driverFullPath).Is(executableFileFormat); + } - DetectFormat(driverFullPath).Is(executableFileFormat); - } + [Test] + [TestCaseSource(nameof(Runtimes))] + public async Task PublishWithRuntimeIdentifier_NoPublish_Test(string rid, string driverFileName, Format _) + { + var unitTestProjectDir = FileIO.FindContainerDirToAncestor("*.csproj"); + using var workDir = WorkDirectory.CreateCopyFrom(Path.Combine(unitTestProjectDir, "Project"), item => item.Name is not "obj" and not "bin"); - [Test] - [TestCaseSource(nameof(Runtimes))] - public void PublishWithRuntimeIdentifier_NoPublish_Test(string rid, string driverFileName, Format _) - { - using var workSpace = new WorkSpace(copyFrom: "Project"); + var dotnet = await XProcess.Start("dotnet", $"publish -r {rid} -o out", workDir).WaitForExitAsync(); + dotnet.ExitCode.Is(0, message: dotnet.Output); - var exitCode = Shell.Run(workSpace, "dotnet", "publish", "-r", rid, "-o", "out"); - exitCode.Is(0); + var driverFullPath = Path.Combine(workDir, "out", driverFileName); + File.Exists(driverFullPath).IsFalse(); + } - var driverFullPath = Path.Combine(workSpace, "out", driverFileName); - File.Exists(driverFullPath).IsFalse(); - } + [Test] + [TestCaseSource(nameof(Runtimes))] + public async Task PublishWithRuntimeIdentifier_with_MSBuildProp_Test(string rid, string driverFileName, Format executableFileFormat) + { + var unitTestProjectDir = FileIO.FindContainerDirToAncestor("*.csproj"); + using var workDir = WorkDirectory.CreateCopyFrom(Path.Combine(unitTestProjectDir, "Project"), item => item.Name is not "obj" and not "bin"); - [Test] - [TestCaseSource(nameof(Runtimes))] - public void PublishWithRuntimeIdentifier_with_MSBuildProp_Test(string rid, string driverFileName, Format executableFileFormat) - { - using var workSpace = new WorkSpace(copyFrom: "Project"); + var dotnet = await XProcess.Start("dotnet", $"publish -r {rid} -o out -p:PublishGeckoDriver=true", workDir).WaitForExitAsync(); + dotnet.ExitCode.Is(0, message: dotnet.Output); - var exitCode = Shell.Run(workSpace, "dotnet", "publish", "-r", rid, "-o", "out", "-p:PublishGeckoDriver=true"); - exitCode.Is(0); + var driverFullPath = Path.Combine(workDir, "out", driverFileName); + File.Exists(driverFullPath).IsTrue(); - var driverFullPath = Path.Combine(workSpace, "out", driverFileName); - File.Exists(driverFullPath).IsTrue(); + DetectFormat(driverFullPath).Is(executableFileFormat); + } - DetectFormat(driverFullPath).Is(executableFileFormat); - } + [Test] + [TestCaseSource(nameof(Runtimes))] + public async Task PublishWithRuntimeIdentifier_with_DefineConstants_Test(string rid, string driverFileName, Format executableFileFormat) + { + var unitTestProjectDir = FileIO.FindContainerDirToAncestor("*.csproj"); + using var workDir = WorkDirectory.CreateCopyFrom(Path.Combine(unitTestProjectDir, "Project"), item => item.Name is not "obj" and not "bin"); - [Test] - [TestCaseSource(nameof(Runtimes))] - public void PublishWithRuntimeIdentifier_with_DefineConstants_Test(string rid, string driverFileName, Format executableFileFormat) - { - using var workSpace = new WorkSpace(copyFrom: "Project"); + var dotnet = await XProcess.Start("dotnet", $"publish -r {rid} -o out -p:DefineConstants=_PUBLISH_GECKODRIVER", workDir).WaitForExitAsync(); + dotnet.ExitCode.Is(0, message: dotnet.Output); - var exitCode = Shell.Run(workSpace, "dotnet", "publish", "-r", rid, "-o", "out", "-p:DefineConstants=_PUBLISH_GECKODRIVER"); - exitCode.Is(0); + var driverFullPath = Path.Combine(workDir, "out", driverFileName); + File.Exists(driverFullPath).IsTrue(); - var driverFullPath = Path.Combine(workSpace, "out", driverFileName); - File.Exists(driverFullPath).IsTrue(); + DetectFormat(driverFullPath).Is(executableFileFormat); + } - DetectFormat(driverFullPath).Is(executableFileFormat); - } + [Test] + public async Task Publish_with_SingleFileEnabled_Test() + { + var rid = "win-x64"; + var driverFileName = "geckodriver.exe"; + var executableFileFormat = Format.PE64; + + var unitTestProjectDir = FileIO.FindContainerDirToAncestor("*.csproj"); + using var workDir = WorkDirectory.CreateCopyFrom(Path.Combine(unitTestProjectDir, "Project"), item => item.Name is not "obj" and not "bin"); + var publishCommand = new[] { + "dotnet", "publish", "-r", rid, "-o", "out", + "-c:Release", + "-p:PublishGeckoDriver=true", + "-p:PublishSingleFile=true", + "-p:SelfContained=false" + }; - [Test] - public void Publish_with_SingleFileEnabled_Test() + // IMPORTANT: 2nd time of publishing, sometimes lost driver file in the published folder, so we have to validate it.. + for (var i = 0; i < 2; i++) { - var rid = "win-x64"; - var driverFileName = "geckodriver.exe"; - var executableFileFormat = Format.PE64; - - using var workSpace = new WorkSpace(copyFrom: "Project"); - var publishCommand = new[] { - "dotnet", "publish", "-r", rid, "-o", "out", - "-c:Release", - "-p:PublishGeckoDriver=true", - "-p:PublishSingleFile=true", - "-p:SelfContained=false" - }; - - // IMPORTANT: 2nd time of publishing, sometimes lost driver file in the published folder, so we have to validate it.. - for (var i = 0; i < 2; i++) - { - var exitCode = Shell.Run(workSpace, publishCommand); - exitCode.Is(0); - - var driverFullPath = Path.Combine(workSpace, "out", driverFileName); - File.Exists(driverFullPath).IsTrue(); - - DetectFormat(driverFullPath).Is(executableFileFormat); - } + var dotnet = await XProcess.Start( + filename: publishCommand.First(), + arguments: String.Join(' ', publishCommand.Skip(1)), + workDir).WaitForExitAsync(); + dotnet.ExitCode.Is(0, message: dotnet.Output); + + var driverFullPath = Path.Combine(workDir, "out", driverFileName); + File.Exists(driverFullPath).IsTrue(); + + DetectFormat(driverFullPath).Is(executableFileFormat); } } } diff --git a/test/GlobalUsings.cs b/test/GlobalUsings.cs new file mode 100644 index 0000000..7322746 --- /dev/null +++ b/test/GlobalUsings.cs @@ -0,0 +1,5 @@ +global using NUnit.Framework; +global using Toolbelt; +global using Toolbelt.Diagnostics; +global using Format = Toolbelt.ExecutableFileFormatType; +global using static Toolbelt.ExecutableFileFormat; diff --git a/test/Lib/ExecutableFile.cs b/test/Lib/ExecutableFile.cs deleted file mode 100644 index a9f92c1..0000000 --- a/test/Lib/ExecutableFile.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.IO; -using System.Linq; -using System.Text; - -namespace Selenium.WebDriver.GeckoDriver.NuPkg.Test.Lib -{ - public class ExecutableFile - { - public enum Format - { - Unknown, - PE32, - PE64, - ELF, - MachO - } - - internal static Format DetectFormat(string path) - { - const int maxHeaderBytesLength = 4; - if (new FileInfo(path).Length < maxHeaderBytesLength) return Format.Unknown; - using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read)) - { - var headerBytes = new byte[maxHeaderBytesLength]; - stream.Read(headerBytes, 0, headerBytes.Length); - if (headerBytes.Take(2).SequenceEqual(new[] { (byte)'M', (byte)'Z' })) - { - var buff = new byte[4]; - const int offsetOfNEHeaderOffset = 60; - stream.Seek(offsetOfNEHeaderOffset, SeekOrigin.Begin); - stream.Read(buff, 0, 4); - var posOfNEHeader = BitConverter.ToInt32(buff, 0); - - stream.Seek(posOfNEHeader, SeekOrigin.Begin); - stream.Read(buff, 0, 4); - if (!buff.Take(4).SequenceEqual(new byte[] { (byte)'P', (byte)'E', 0, 0 })) - return Format.Unknown; - - stream.Read(buff, 0, 2); - if (buff.Take(2).SequenceEqual(new byte[] { 0x4c, 0x01 })) - return Format.PE32; - if (buff.Take(2).SequenceEqual(new byte[] { 0x64, 0x86 })) - return Format.PE64; - - return Format.Unknown; - } - if (headerBytes.Take(4).SequenceEqual(new byte[] { 0x7f, (byte)'E', (byte)'L', (byte)'F' })) return Format.ELF; - if (headerBytes.Take(4).SequenceEqual(new byte[] { 0xcf, 0xfa, 0xed, 0xfe })) return Format.MachO; - } - return Format.Unknown; - } - } -} diff --git a/test/Lib/Shell.cs b/test/Lib/Shell.cs deleted file mode 100644 index 5996151..0000000 --- a/test/Lib/Shell.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using System.Diagnostics; -using System.IO; -using System.Linq; - -namespace Selenium.WebDriver.GeckoDriver.NuPkg.Test.Lib -{ - public static class Shell - { - public static int ErrorLevel { get; set; } - - public static bool Exists(string dir, string wildCard) - { - return Directory.GetFiles(dir, wildCard, SearchOption.TopDirectoryOnly).Any(); - } - - public static void DeleteDir(string dir) - { - if (Directory.Exists(dir)) Directory.Delete(dir, recursive: true); - } - - public static void XcopyDir(string srcDir, string dstDir) - { - Directory.CreateDirectory(dstDir); - - var srcFileNames = Directory.GetFiles(srcDir); - foreach (var srcFileName in srcFileNames) - { - var dstFileName = Path.Combine(dstDir, Path.GetFileName(srcFileName)); - File.Copy(srcFileName, dstFileName); - } - - var srcSubDirs = Directory.GetDirectories(srcDir); - foreach (var srcSubDir in srcSubDirs) - { - var dstSubDir = Path.Combine(dstDir, Path.GetFileName(srcSubDir)); - XcopyDir(srcSubDir, dstSubDir); - } - } - - public static int Run(string workDir, params string[] args) - { - var pi = new ProcessStartInfo - { - WorkingDirectory = workDir, - FileName = args.First(), - Arguments = string.Join(" ", args.Skip(1)), - UseShellExecute = false, - }; - var process = Process.Start(pi); - process.WaitForExit(); - ErrorLevel = process.ExitCode; - return process.ExitCode; - } - } -} diff --git a/test/Lib/WorkSpace.cs b/test/Lib/WorkSpace.cs deleted file mode 100644 index c102f36..0000000 --- a/test/Lib/WorkSpace.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using System.IO; - -namespace Selenium.WebDriver.GeckoDriver.NuPkg.Test.Lib -{ - public class WorkSpace : IDisposable - { - public string WorkDir { get; } - - public WorkSpace(string copyFrom) - { - this.WorkDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Guid.NewGuid().ToString("N")); - var srcDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, copyFrom); - Shell.XcopyDir(srcDir, WorkDir); - } - - public void Dispose() - { - try { Shell.DeleteDir(WorkDir); } catch { } - } - - public static implicit operator string(WorkSpace workSpace) => workSpace.WorkDir; - } -} diff --git a/test/Selenium.WebDriver.GeckoDriver.NuPkg.Test.csproj b/test/Selenium.WebDriver.GeckoDriver.NuPkg.Test.csproj index ae34278..a1ed3f0 100644 --- a/test/Selenium.WebDriver.GeckoDriver.NuPkg.Test.csproj +++ b/test/Selenium.WebDriver.GeckoDriver.NuPkg.Test.csproj @@ -1,15 +1,21 @@  - net5.0 + net6.0 false + enable + enable + nullable - + + + + diff --git a/test/global.json b/test/global.json index 8dbfee4..f9e1f10 100644 --- a/test/global.json +++ b/test/global.json @@ -1,6 +1,6 @@ { "sdk": { - "version": "5.0.0", + "version": "6.0.0", "allowPrerelease": false, "rollForward": "latestMinor" }