Skip to content

Commit

Permalink
(chocolatey#1092) Add tests for dependencies' beforeModify
Browse files Browse the repository at this point in the history
This test added ensures that when we are upgrading a package, both its
own and any dependencies' beforeModify are run so that we can properly
update dependencies which might need to shut down a long-running service
before being upgraded.

(cherry picked from commit 49cc8d7)

# Conflicts:
#	src/chocolatey.tests.integration/chocolatey.tests.integration.csproj
#	src/chocolatey.tests.integration/scenarios/InstallScenarios.cs
#	src/chocolatey.tests.integration/scenarios/UninstallScenarios.cs
#	src/chocolatey.tests.integration/scenarios/UpgradeScenarios.cs
  • Loading branch information
vexx32 authored and gep13 committed Mar 16, 2023
1 parent 29e8f04 commit e9be55b
Show file tree
Hide file tree
Showing 23 changed files with 447 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>hasdependencywithbeforemodify</id>
<version>1.0.0</version>
<title>hasdependencywithbeforemodify</title>
<authors>__REPLACE_AUTHORS_OF_SOFTWARE__</authors>
<owners>__REPLACE_YOUR_NAME__</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>__REPLACE__</description>
<summary>__REPLACE__</summary>
<releaseNotes />
<copyright />
<tags>hasdependencywithbeforemodify admin</tags>
<dependencies>
<dependency id="isdependencywithbeforemodify" version="[1.0.0, 2.0.0.0)" />
</dependencies>
</metadata>
<files>
<file src="tools\**" target="tools" />
</files>
</package>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write-Output "Ran BeforeModify: $env:PackageName $env:PackageVersion"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write-Output "$env:PackageName $env:PackageVersion Installed"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write-Output "$env:PackageName $env:PackageVersion Uninstalled"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This package is used to test out upgrade behaviour with beforeModify scripts in play for the main package and dependencies.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>hasdependencywithbeforemodify</id>
<version>2.0.0</version>
<title>hasdependencywithbeforemodify</title>
<authors>__REPLACE_AUTHORS_OF_SOFTWARE__</authors>
<owners>__REPLACE_YOUR_NAME__</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>__REPLACE__</description>
<summary>__REPLACE__</summary>
<releaseNotes />
<copyright />
<tags>hasdependencywithbeforemodify admin</tags>
<dependencies>
<dependency id="isdependencywithbeforemodify" version="[2.0.0, 3.0.0.0)" />
</dependencies>
</metadata>
<files>
<file src="tools\**" target="tools" />
</files>
</package>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write-Output "Ran BeforeModify: $env:PackageName $env:PackageVersion"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write-Output "$env:PackageName $env:PackageVersion Installed"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write-Output "$env:PackageName $env:PackageVersion Uninstalled"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This package is used to test out upgrade behaviour with beforeModify scripts in play for the main package and dependencies.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>isdependencywithbeforemodify</id>
<version>1.0.0</version>
<title>isdependencywithbeforemodify</title>
<authors>__REPLACE_AUTHORS_OF_SOFTWARE__</authors>
<owners>__REPLACE_YOUR_NAME__</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>__REPLACE__</description>
<summary>__REPLACE__</summary>
<releaseNotes />
<tags>isdependencywithbeforemodify admin</tags>
</metadata>
<files>
<file src="tools\**" target="tools" />
</files>
</package>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write-Output "Ran BeforeModify: $env:PackageName $env:PackageVersion"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write-Output "$env:PackageName $env:PackageVersion Installed"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write-Output "$env:PackageName $env:PackageVersion Uninstalled"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This package is used to test out upgrade behaviour with beforeModify scripts in play for the main package and dependencies.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>isdependencywithbeforemodify</id>
<version>2.0.0</version>
<title>isdependencywithbeforemodify</title>
<authors>__REPLACE_AUTHORS_OF_SOFTWARE__</authors>
<owners>__REPLACE_YOUR_NAME__</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>__REPLACE__</description>
<summary>__REPLACE__</summary>
<releaseNotes />
<tags>isdependencywithbeforemodify admin</tags>
</metadata>
<files>
<file src="tools\**" target="tools" />
</files>
</package>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write-Output "Ran BeforeModify: $env:PackageName $env:PackageVersion"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write-Output "$env:PackageName $env:PackageVersion Installed"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write-Output "$env:PackageName $env:PackageVersion Uninstalled"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This package is used to test out upgrade behaviour with beforeModify scripts in play for the main package and dependencies.
99 changes: 99 additions & 0 deletions src/chocolatey.tests.integration/scenarios/InstallScenarios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5461,5 +5461,104 @@ public override void Context()
Configuration.Version = NonNormalizedVersion;
}
}

public class when_installing_a_package_that_requires_updating_a_dependency : ScenariosBase
{
private const string TargetPackageName = "hasdependencywithbeforemodify";
private const string DependencyName = "isdependencywithbeforemodify";

public override void Context()
{
base.Context();

Scenario.add_packages_to_source_location(Configuration, "{0}.*".format_with(TargetPackageName) + NuGetConstants.PackageExtension);
Scenario.add_packages_to_source_location(Configuration, "{0}.*".format_with(DependencyName) + NuGetConstants.PackageExtension);
Scenario.install_package(Configuration, DependencyName, "1.0.0");

Configuration.PackageNames = Configuration.Input = TargetPackageName;
}

public override void Because()
{
Results = Service.install_run(Configuration);
}

[Fact]
public void should_install_the_package()
{
var packageFile = Path.Combine(Scenario.get_top_level(), "lib", TargetPackageName, "{0}.nupkg".format_with(TargetPackageName));
using (var packageReader = new PackageArchiveReader(packageFile))
{
packageReader.NuspecReader.GetVersion().to_string().ShouldEqual("2.0.0");
}
}

[Fact]
public void should_upgrade_the_minimum_version_dependency()
{
var packageFile = Path.Combine(Scenario.get_top_level(), "lib", DependencyName, "{0}.nupkg".format_with(DependencyName));
using (var packageReader = new PackageArchiveReader(packageFile))
{
packageReader.NuspecReader.GetVersion().to_string().ShouldEqual("2.0.0");
}
}

[Fact]
public void should_contain_a_message_that_everything_installed_successfully()
{
MockLogger.contains_message("installed 2/2", LogLevel.Warn).ShouldBeTrue();
}

[Fact]
[WindowsOnly]
[Platform(Exclude = "Mono")]
public void should_not_run_target_package_beforeModify_for_upgraded_version()
{
MockLogger.contains_message("Ran BeforeModify: {0} {1}".format_with(TargetPackageName, "2.0.0"), LogLevel.Info).ShouldBeFalse();
}

[Fact]
[WindowsOnly]
[Platform(Exclude = "Mono")]
public void should_run_already_installed_dependency_package_beforeModify()
{
MockLogger.contains_message("Ran BeforeModify: {0} {1}".format_with(DependencyName, "1.0.0"), LogLevel.Info).ShouldBeTrue();
}

[Fact]
[WindowsOnly]
[Platform(Exclude = "Mono")]
public void should_not_run_dependency_package_beforeModify_for_upgraded_version()
{
MockLogger.contains_message("Ran BeforeModify: {0} {1}".format_with(DependencyName, "2.0.0"), LogLevel.Info).ShouldBeFalse();
}

[Fact]
public void should_have_a_successful_package_result()
{
foreach (var packageResult in Results)
{
packageResult.Value.Success.ShouldBeTrue();
}
}

[Fact]
public void should_not_have_inconclusive_package_result()
{
foreach (var packageResult in Results)
{
packageResult.Value.Inconclusive.ShouldBeFalse();
}
}

[Fact]
public void should_not_have_warning_package_result()
{
foreach (var packageResult in Results)
{
packageResult.Value.Warning.ShouldBeFalse();
}
}
}
}
}
88 changes: 88 additions & 0 deletions src/chocolatey.tests.integration/scenarios/UninstallScenarios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1527,5 +1527,93 @@ public void should_have_executed_chocolateyUninstall_script()
MockLogger.contains_message("upgradepackage {0} Uninstalled".format_with(NonNormalizedVersion), LogLevel.Info).ShouldBeTrue();
}
}

public class when_uninstalling_a_package_with_remove_dependencies_with_beforeModify : ScenariosBase
{
private const string TargetPackageName = "hasdependencywithbeforemodify";
private const string DependencyName = "isdependencywithbeforemodify";

public override void Context()
{
base.Context();

Scenario.add_packages_to_source_location(Configuration, "{0}.*".format_with(TargetPackageName) + NuGetConstants.PackageExtension);
Scenario.add_packages_to_source_location(Configuration, "{0}.*".format_with(DependencyName) + NuGetConstants.PackageExtension);
Scenario.install_package(Configuration, TargetPackageName, "1.0.0");
Scenario.install_package(Configuration, DependencyName, "1.0.0");

Configuration.PackageNames = Configuration.Input = TargetPackageName;
Configuration.ForceDependencies = true;
}

public override void Because()
{
Results = Service.uninstall_run(Configuration);
}

[Fact]
public void should_uninstall_the_package()
{
var packageFile = Path.Combine(Scenario.get_top_level(), "lib", TargetPackageName, "{0}.nupkg".format_with(TargetPackageName));
File.Exists(packageFile).ShouldBeFalse();
}

[Fact]
public void should_uninstall_the_dependency()
{
var packageFile = Path.Combine(Scenario.get_top_level(), "lib", DependencyName, "{0}.nupkg".format_with(DependencyName));
File.Exists(packageFile).ShouldBeFalse();
}

[Fact]
public void should_contain_a_message_that_everything_uninstalled_successfully()
{
MockLogger.contains_message("uninstalled 2/2", LogLevel.Warn).ShouldBeTrue();
}

[Fact]
[WindowsOnly]
[Platform(Exclude = "Mono")]
public void should_run_target_package_beforeModify()
{
MockLogger.contains_message("Ran BeforeModify: {0} {1}".format_with(TargetPackageName, "1.0.0"), LogLevel.Info).ShouldBeTrue();
}

[Fact]
[WindowsOnly]
[Platform(Exclude = "Mono")]
public void should_run_dependency_package_beforeModify()
{
MockLogger.contains_message("Ran BeforeModify: {0} {1}".format_with(DependencyName, "1.0.0"), LogLevel.Info).ShouldBeTrue();
}

[Fact]
public void should_have_a_successful_package_result()
{
foreach (var packageResult in Results)
{
packageResult.Value.Success.ShouldBeTrue();
}
}

[Fact]
public void should_not_have_inconclusive_package_result()
{
foreach (var packageResult in Results)
{
packageResult.Value.Inconclusive.ShouldBeFalse();
}
}

[Fact]
public void should_not_have_warning_package_result()
{
foreach (var packageResult in Results)
{
packageResult.Value.Warning.ShouldBeFalse();
}

}
}
}
}
Loading

0 comments on commit e9be55b

Please sign in to comment.