Skip to content

Commit

Permalink
(chocolatey#2880) Update NUnit dependencies to v3
Browse files Browse the repository at this point in the history
This commit updates any references to use NUnit v3 as well
as updating the code we have in testing to the equivalent in
the new version of NUnit.
  • Loading branch information
AdmiringWorm committed Nov 2, 2022
1 parent e3a04af commit b96d1eb
Show file tree
Hide file tree
Showing 16 changed files with 423 additions and 391 deletions.
652 changes: 324 additions & 328 deletions recipe.cake

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/chocolatey.tests.integration/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

// Because we use a static variable for collecting log message
// we can not run tests in parallel.
[assembly: NUnit.Framework.NonParallelizable]
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\NUnit3TestAdapter.4.3.0\build\net35\NUnit3TestAdapter.props" Condition="Exists('..\packages\NUnit3TestAdapter.4.3.0\build\net35\NUnit3TestAdapter.props')" />
<Import Project="..\packages\NUnit.3.13.3\build\NUnit.props" Condition="Exists('..\packages\NUnit.3.13.3\build\NUnit.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand All @@ -14,6 +16,8 @@
<FileAlignment>512</FileAlignment>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<OverwriteReadOnlyFiles>true</OverwriteReadOnlyFiles>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -73,12 +77,11 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\lib\Chocolatey-NuGet.Core.2.11.0.20220901\lib\net4\NuGet.Core.dll</HintPath>
</Reference>
<Reference Include="nunit.framework, Version=2.6.4.14350, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath>
<Reference Include="nunit.framework, Version=3.13.3.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.3.13.3\lib\net40\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="Should, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Should.1.1.12.0\lib\Should.dll</HintPath>
<HintPath>..\packages\Should.1.1.20\lib\Should.dll</HintPath>
</Reference>
<Reference Include="SimpleInjector, Version=2.8.3.0, Culture=neutral, PublicKeyToken=984cb50dea722e99, processorArchitecture=MSIL">
<HintPath>..\packages\SimpleInjector.2.8.3\lib\net40-client\SimpleInjector.dll</HintPath>
Expand Down Expand Up @@ -715,6 +718,13 @@
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\NUnit.3.13.3\build\NUnit.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit.3.13.3\build\NUnit.props'))" />
<Error Condition="!Exists('..\packages\NUnit3TestAdapter.4.3.0\build\net35\NUnit3TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit3TestAdapter.4.3.0\build\net35\NUnit3TestAdapter.props'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public override void AfterObservations()
{
base.AfterObservations();
_fileStream.Close();
_fileStream.Dispose();
}

public override void Because()
Expand All @@ -93,13 +94,7 @@ public void should_log_a_warning()
[Platform(Exclude = "Mono")]
public void should_log_a_warning_about_locked_files()
{
bool lockedFiles = false;
foreach (var message in MockLogger.MessagesFor(LogLevel.Warn).or_empty_list_if_null())
{
if (message.Contains("The process cannot access the file")) lockedFiles = true;
}

lockedFiles.ShouldBeTrue();
MockLogger.Verify(l => l.Warn(It.Is<string>(s => s.StartsWith("The process cannot access the file"))), Times.Once);
}

[Fact]
Expand Down
6 changes: 3 additions & 3 deletions src/chocolatey.tests.integration/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
<package id="log4net" version="2.0.12" targetFramework="net40" />
<package id="Microsoft.Web.Xdt" version="2.1.1" targetFramework="net40" />
<package id="Moq" version="4.2.1402.2112" targetFramework="net40" />
<package id="NUnit" version="2.6.4" targetFramework="net40" />
<package id="NUnitTestAdapter" version="2.3.0" targetFramework="net40" />
<package id="NUnit" version="3.13.3" targetFramework="net40" />
<package id="NUnit3TestAdapter" version="4.3.0" targetFramework="net40" />
<package id="Rx-Core" version="2.1.30214.0" targetFramework="net40" />
<package id="Rx-Interfaces" version="2.1.30214.0" targetFramework="net40" />
<package id="Rx-Linq" version="2.1.30214.0" targetFramework="net40" />
<package id="Should" version="1.1.12.0" />
<package id="Should" version="1.1.20" targetFramework="net40" />
<package id="SimpleInjector" version="2.8.3" targetFramework="net40" />
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@ public override void AfterObservations()
{
base.AfterObservations();
fileStream.Close();
fileStream.Dispose();
}

public override void Because()
Expand All @@ -765,15 +766,15 @@ public override void Because()
[Fact]
public void should_install_where_install_location_reports()
{
Directory.Exists(packageResult.InstallLocation).ShouldBeTrue();
DirectoryAssert.Exists(packageResult.InstallLocation);
}

[Fact]
public void should_reinstall_the_package_in_the_lib_directory()
{
var packageDir = Path.Combine(Scenario.get_top_level(), "lib", Configuration.PackageNames);

Directory.Exists(packageDir).ShouldBeTrue();
DirectoryAssert.Exists(packageDir);
}

[Fact]
Expand All @@ -789,7 +790,7 @@ public void should_not_be_able_delete_the_rollback()
{
var packageDir = Path.Combine(Scenario.get_top_level(), "lib-bkp", Configuration.PackageNames);

Directory.Exists(packageDir).ShouldBeTrue();
DirectoryAssert.Exists(packageDir);
}

[Fact]
Expand Down Expand Up @@ -855,6 +856,7 @@ public override void AfterObservations()
{
base.AfterObservations();
fileStream.Close();
fileStream.Dispose();
}

public override void Because()
Expand Down
13 changes: 8 additions & 5 deletions src/chocolatey.tests.integration/scenarios/PackScenarios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ namespace chocolatey.tests.integration.scenarios
using chocolatey.infrastructure.app.commands;
using chocolatey.infrastructure.app.configuration;
using chocolatey.infrastructure.app.services;

using NUnit.Framework;

using Should;

public class PackScenarios
Expand Down Expand Up @@ -67,7 +70,7 @@ public void generated_package_should_be_in_current_directory()
infos[0].ShouldEqual("Attempting to build package from 'myPackage.nuspec'.");
infos[1].ShouldEqual(string.Concat("Successfully created package '", package_path, "'"));

File.Exists(package_path).ShouldBeTrue();
FileAssert.Exists(package_path);
}

[Fact]
Expand Down Expand Up @@ -102,7 +105,7 @@ public void generated_package_should_be_in_specified_output_directory()
infos[0].ShouldEqual("Attempting to build package from 'myPackage.nuspec'.");
infos[1].ShouldEqual(string.Concat("Successfully created package '", package_path, "'"));

File.Exists(package_path).ShouldBeTrue();
FileAssert.Exists(package_path);
}

[Fact]
Expand Down Expand Up @@ -146,16 +149,16 @@ public void generated_package_should_be_in_current_directory()
infos[0].ShouldEqual("Attempting to build package from 'myPackage.nuspec'.");
infos[1].ShouldEqual(string.Concat("Successfully created package '", package_path, "'"));

File.Exists(package_path).ShouldBeTrue();
FileAssert.Exists(package_path);
}

[Fact]
public void property_settings_should_be_logged_as_debug_messages()
{
var messages = MockLogger.MessagesFor(LogLevel.Debug);
messages.Count.ShouldEqual(2);
messages.Contains("Setting property 'commitId': 1234abcd");
messages.Contains("Setting property 'version': 0.1.0");
messages.ShouldContain("Setting property 'commitId': 1234abcd");
messages.ShouldContain("Setting property 'version': 0.1.0");
}
}

Expand Down
13 changes: 9 additions & 4 deletions src/chocolatey.tests.integration/scenarios/PinScenarios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ namespace chocolatey.tests.integration.scenarios
using chocolatey.infrastructure.commands;
using chocolatey.infrastructure.results;
using NuGet;

using NUnit.Framework;

using Should;

public class PinScenarios
Expand Down Expand Up @@ -217,11 +220,12 @@ public override void Because()
MockLogger.reset();
}

[ExpectedException(typeof(ApplicationException), ExpectedMessage = "Unable to find package named 'whatisthis' to pin. Please check to ensure it is installed.")]
[Fact]
public void should_throw_an_error_about_not_finding_the_package()
{
Service.run(Configuration);
Assert.That(() => Service.run(Configuration),
Throws.TypeOf<ApplicationException>()
.And.Message.EqualTo("Unable to find package named 'whatisthis' to pin. Please check to ensure it is installed."));
}
}

Expand Down Expand Up @@ -286,11 +290,12 @@ public override void Because()
MockLogger.reset();
}

[ExpectedException(typeof(ApplicationException), ExpectedMessage = "Unable to find package named 'whatisthis' to pin. Please check to ensure it is installed.")]
[Fact]
public void should_throw_an_error_about_not_finding_the_package()
{
Service.run(Configuration);
Assert.That(() => Service.run(Configuration),
Throws.TypeOf<ApplicationException>()
.And.Message.EqualTo("Unable to find package named 'whatisthis' to pin. Please check to ensure it is installed."));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,11 @@ public override void Because()
}

[Fact]
[ExpectedException(typeof(ApplicationException))]
public void should_throw_an_error_that_it_is_not_allowed()
{
Results = Service.uninstall_run(Configuration);
Action m = () => Service.uninstall_run(Configuration);

m.ShouldThrow<ApplicationException>();
}
}

Expand Down Expand Up @@ -434,6 +435,7 @@ public override void AfterObservations()
{
base.AfterObservations();
fileStream.Close();
fileStream.Dispose();
}

public override void Because()
Expand All @@ -455,7 +457,7 @@ public void should_not_be_able_delete_the_rollback()
{
var packageDir = Path.Combine(Scenario.get_top_level(), "lib-bkp", Configuration.PackageNames);

Directory.Exists(packageDir).ShouldBeTrue();
DirectoryAssert.Exists(packageDir);
}

[Fact]
Expand Down Expand Up @@ -508,6 +510,7 @@ public override void AfterObservations()
{
base.AfterObservations();
fileStream.Close();
fileStream.Dispose();
}

public override void Because()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1216,10 +1216,11 @@ public override void Because()
}

[Fact]
[ExpectedException(typeof(ApplicationException))]
public void should_throw_an_error_that_it_is_not_allowed()
{
Results = Service.upgrade_run(Configuration);
Action m = () => Service.upgrade_run(Configuration);

m.ShouldThrow<ApplicationException>();
}
}

Expand Down Expand Up @@ -1343,6 +1344,7 @@ public override void AfterObservations()
{
base.AfterObservations();
fileStream.Close();
fileStream.Dispose();
}

public override void Because()
Expand Down Expand Up @@ -1378,7 +1380,7 @@ public void should_not_be_able_delete_the_rollback()
{
var packageDir = Path.Combine(Scenario.get_top_level(), "lib-bkp", Configuration.PackageNames);

Directory.Exists(packageDir).ShouldBeTrue();
DirectoryAssert.Exists(packageDir);
}

[Fact]
Expand Down Expand Up @@ -1451,6 +1453,7 @@ public override void AfterObservations()
{
base.AfterObservations();
fileStream.Close();
fileStream.Dispose();
}

public override void Because()
Expand Down
4 changes: 4 additions & 0 deletions src/chocolatey.tests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

// Because we use a static variable for collecting log message
// we can not run tests in parallel.
[assembly: NUnit.Framework.NonParallelizable]
19 changes: 6 additions & 13 deletions src/chocolatey.tests/TinySpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace chocolatey.tests
using NUnit.Framework;
using chocolatey.infrastructure.app;
using chocolatey.infrastructure.logging;
using System.IO;

// ReSharper disable InconsistentNaming

Expand All @@ -32,21 +33,22 @@ public class NUnitSetup

private static readonly string InstallLocationVariable = Environment.GetEnvironmentVariable(ApplicationParameters.ChocolateyInstallEnvironmentVariableName);

[SetUp]
[OneTimeSetUp]
public virtual void BeforeEverything()
{
Environment.SetEnvironmentVariable(ApplicationParameters.ChocolateyInstallEnvironmentVariableName, string.Empty);
MockLogger = new MockLogger();
Log.InitializeWith(MockLogger);
// do not log trace messages
ILogExtensions.LogTraceMessages = false;
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
}

public virtual void before_everything()
{
}

[TearDown]
[OneTimeTearDown]
public void AfterEverything()
{
Environment.SetEnvironmentVariable(ApplicationParameters.ChocolateyInstallEnvironmentVariableName, InstallLocationVariable);
Expand All @@ -61,7 +63,7 @@ public MockLogger MockLogger
get { return NUnitSetup.MockLogger; }
}

[TestFixtureSetUp]
[OneTimeSetUp]
public void Setup()
{
if (MockLogger != null) MockLogger.reset();
Expand Down Expand Up @@ -94,7 +96,7 @@ public virtual void AfterEachSpec()
{
}

[TestFixtureTearDown]
[OneTimeTearDown]
public void TearDown()
{
AfterObservations();
Expand Down Expand Up @@ -167,14 +169,5 @@ public IntegrationAttribute()
}
}

public class ExpectedExceptionAttribute : NUnit.Framework.ExpectedExceptionAttribute
{
public ExpectedExceptionAttribute(Type exceptionType) : base(exceptionType)
{}

public ExpectedExceptionAttribute(string exceptionName) : base(exceptionName)
{}
}

// ReSharper restore InconsistentNaming
}
Loading

0 comments on commit b96d1eb

Please sign in to comment.