Skip to content

Commit

Permalink
modernize unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jsakamoto committed Feb 3, 2022
1 parent 4eb6399 commit 3242525
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 237 deletions.
35 changes: 17 additions & 18 deletions test/BuildProjectABTest.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
166 changes: 84 additions & 82 deletions test/BuildTest.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
5 changes: 5 additions & 0 deletions test/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -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;
54 changes: 0 additions & 54 deletions test/Lib/ExecutableFile.cs

This file was deleted.

56 changes: 0 additions & 56 deletions test/Lib/Shell.cs

This file was deleted.

24 changes: 0 additions & 24 deletions test/Lib/WorkSpace.cs

This file was deleted.

10 changes: 8 additions & 2 deletions test/Selenium.WebDriver.GeckoDriver.NuPkg.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<WarningsAsErrors>nullable</WarningsAsErrors>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ChainingAssertion-NUnit.Bin" Version="1.8.0.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="Toolbelt.WorkDirectory" Version="1.0.0" />
<PackageReference Include="XProcess" Version="1.2.0" />
<PackageReference Include="Toolbelt.ExecutableFileFormatDetector" Version="1.0.0" />
</ItemGroup>


Expand Down
Loading

0 comments on commit 3242525

Please sign in to comment.