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.
  • Loading branch information
vexx32 committed Mar 13, 2023
1 parent 9f5d9cc commit 49cc8d7
Show file tree
Hide file tree
Showing 24 changed files with 499 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,54 @@
<None Include="context\badpackage\2.0\tools\chocolateyUninstall.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\dependenciesbeforemodify\hasdependencywithbeforemodify\1.0.0\hasdependencywithbeforemodify.nuspec">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\dependenciesbeforemodify\hasdependencywithbeforemodify\1.0.0\tools\chocolateyBeforeModify.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\dependenciesbeforemodify\hasdependencywithbeforemodify\1.0.0\tools\chocolateyinstall.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\dependenciesbeforemodify\hasdependencywithbeforemodify\1.0.0\tools\chocolateyuninstall.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\dependenciesbeforemodify\hasdependencywithbeforemodify\2.0.0\hasdependencywithbeforemodify.nuspec">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\dependenciesbeforemodify\hasdependencywithbeforemodify\2.0.0\tools\chocolateyBeforeModify.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\dependenciesbeforemodify\hasdependencywithbeforemodify\2.0.0\tools\chocolateyinstall.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\dependenciesbeforemodify\hasdependencywithbeforemodify\2.0.0\tools\chocolateyuninstall.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\dependenciesbeforemodify\isdependencywithbeforemodify\1.0.0\isdependencywithbeforemodify.nuspec">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\dependenciesbeforemodify\isdependencywithbeforemodify\1.0.0\tools\chocolateyBeforeModify.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\dependenciesbeforemodify\isdependencywithbeforemodify\1.0.0\tools\chocolateyinstall.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\dependenciesbeforemodify\isdependencywithbeforemodify\1.0.0\tools\chocolateyuninstall.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\dependenciesbeforemodify\isdependencywithbeforemodify\2.0.0\isdependencywithbeforemodify.nuspec">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\dependenciesbeforemodify\isdependencywithbeforemodify\2.0.0\tools\chocolateyBeforeModify.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\dependenciesbeforemodify\isdependencywithbeforemodify\2.0.0\tools\chocolateyinstall.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\dependenciesbeforemodify\isdependencywithbeforemodify\2.0.0\tools\chocolateyuninstall.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\dependencies\conflictingdependency\1.0.1\conflictingdependency.nuspec">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down Expand Up @@ -661,6 +709,10 @@
<None Include="context\upgradepackage\1.1.0\tools\graphical.exe">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<Content Include="context\dependenciesbeforemodify\hasdependencywithbeforemodify\1.0.0\tools\purpose.txt" />
<Content Include="context\dependenciesbeforemodify\hasdependencywithbeforemodify\2.0.0\tools\purpose.txt" />
<Content Include="context\dependenciesbeforemodify\isdependencywithbeforemodify\1.0.0\tools\purpose.txt" />
<Content Include="context\dependenciesbeforemodify\isdependencywithbeforemodify\2.0.0\tools\purpose.txt" />
<Content Include="context\exactpackage\exactpackage.dontfind\1.0.0\tools\purpose.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
Expand Down
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.
95 changes: 95 additions & 0 deletions src/chocolatey.tests.integration/scenarios/InstallScenarios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4013,5 +4013,100 @@ public void should_not_have_executed_beforemodify_hook_script()
MockLogger.contains_message("pre-beforemodify-all.ps1 hook ran for portablepackage 1.0.0", LogLevel.Info).ShouldBeFalse();
}
}

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) + Constants.PackageExtension);
Scenario.add_packages_to_source_location(Configuration, "{0}.*".format_with(DependencyName) + Constants.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));
var package = new OptimizedZipPackage(packageFile);
package.Version.Version.to_string().ShouldEqual("2.0.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));
var package = new OptimizedZipPackage(packageFile);
package.Version.Version.to_string().ShouldEqual("2.0.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();
}
}
}
}
}
86 changes: 86 additions & 0 deletions src/chocolatey.tests.integration/scenarios/UninstallScenarios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1316,5 +1316,91 @@ public void should_have_executed_post_beforemodify_hook_script()
}
}

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) + Constants.PackageExtension);
Scenario.add_packages_to_source_location(Configuration, "{0}.*".format_with(DependencyName) + Constants.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 49cc8d7

Please sign in to comment.