Skip to content

Commit

Permalink
Merge branch 'stable'
Browse files Browse the repository at this point in the history
* stable:
  (chocolateyGH-268)(specs) BeforeModify failure is ignored
  (maint) remove comment
  (chocolateyGH-268) Revert removed fixes
  (chocolateyGH-268) Rename Legacy Prior to Before Modify
  (maint) formatting
  (chocolateyGH-268) Use MockLogger.contains_message in verification expressions.
  (chocolateyGH-268) Trigger beforeUpgradeAction from the NugetService.
  (chocolateyGH-268) Add before-package-upgrade method to ChocolateyPackageService
  (chocolateyGH-268) Add optional beforeUpgradeAction to upgrade_run method.
  (chocolateyGH-268) Add a before_modify action to the PowershellService
  (chocolateyGH-268)(spec) Added "before modify" integration test scenarios.
  (chocolateyGH-268) Extract method to provide folder information for package.
  (chocolateyGH-268) Add chocolateyBeforeModify.ps1 script to test packages.
  (chocolateyGH-268)(spec) Add scenarios verifying powershell script execution.
  • Loading branch information
ferventcoder committed Apr 9, 2016
2 parents 2ab6082 + bfa6c32 commit 18075dd
Show file tree
Hide file tree
Showing 17 changed files with 180 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@
<None Include="context\installpackage\1.0.0\tools\Casemismatch.exe.ignore">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\installpackage\1.0.0\tools\chocolateyBeforeModify.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\installpackage\1.0.0\tools\chocolateyinstall.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down Expand Up @@ -327,6 +330,9 @@
<None Include="context\testing.packages.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\upgradepackage\1.0.0\tools\chocolateyBeforeModify.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\upgradepackage\1.0.0\tools\chocolateyinstall.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand All @@ -345,6 +351,9 @@
<None Include="context\upgradepackage\1.0.0\upgradepackage.nuspec">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\upgradepackage\1.1.0\tools\chocolateyBeforeModify.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\upgradepackage\1.1.0\tools\chocolateyinstall.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write-Output "$env:PackageName $env:PackageVersion Before Modification"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Write-Output "$env:PackageName $env:PackageVersion Before Modification"

throw "This should not break the upgrade/uninstall"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write-Output "$env:PackageName $env:PackageVersion Before Modification"
19 changes: 13 additions & 6 deletions src/chocolatey.tests.integration/scenarios/InstallScenarios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

namespace chocolatey.tests.integration.scenarios
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -92,13 +93,13 @@ public void should_contain_a_message_that_it_would_have_used_Nuget_to_install_a_
[Fact]
public void should_contain_a_message_that_it_would_have_run_a_powershell_script()
{
bool expectedMessage = false;
foreach (var message in MockLogger.MessagesFor(LogLevel.Info).or_empty_list_if_null())
{
if (message.Contains("chocolateyinstall.ps1")) expectedMessage = true;
}
MockLogger.contains_message("chocolateyinstall.ps1", LogLevel.Info).ShouldBeTrue();
}

expectedMessage.ShouldBeTrue();
[Fact]
public void should_not_contain_a_message_that_it_would_have_run_powershell_modification_script()
{
MockLogger.contains_message("chocolateyBeforeModify.ps1", LogLevel.Info).ShouldBeFalse();
}
}

Expand Down Expand Up @@ -314,6 +315,12 @@ public void should_have_a_version_of_one_dot_zero_dot_zero()
{
packageResult.Version.ShouldEqual("1.0.0");
}

[Fact]
public void should_have_executed_chocolateyInstall_script()
{
MockLogger.contains_message("installpackage v1.0.0 has been installed", LogLevel.Info).ShouldBeTrue();
}
}

[Concern(typeof(ChocolateyInstallCommand))]
Expand Down
24 changes: 18 additions & 6 deletions src/chocolatey.tests.integration/scenarios/UninstallScenarios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ public void should_contain_a_message_that_it_would_have_uninstalled_a_package()
[Fact]
public void should_contain_a_message_that_it_would_have_run_a_powershell_script()
{
bool expectedMessage = false;
foreach (var message in MockLogger.MessagesFor(LogLevel.Info).or_empty_list_if_null())
{
if (message.Contains("chocolateyuninstall.ps1")) expectedMessage = true;
}
MockLogger.contains_message("chocolateyuninstall.ps1").ShouldBeTrue();
}

expectedMessage.ShouldBeTrue();
[Fact]
public void should_contain_a_message_that_it_would_have_run_powershell_modification_script()
{
MockLogger.contains_message("chocolateyBeforeModify.ps1").ShouldBeTrue();
}
}

Expand Down Expand Up @@ -219,6 +219,18 @@ public void config_should_match_package_result_name()
{
packageResult.Name.ShouldEqual(Configuration.PackageNames);
}

[Fact]
public void should_have_executed_chocolateyBeforeModify_script()
{
MockLogger.contains_message("installpackage 1.0.0 Before Modification", LogLevel.Info).ShouldBeTrue();
}

[Fact]
public void should_have_executed_chocolateyUninstall_script()
{
MockLogger.contains_message("installpackage 1.0.0 Uninstalled", LogLevel.Info).ShouldBeTrue();
}
}

[Concern(typeof (ChocolateyUninstallCommand))]
Expand Down
33 changes: 33 additions & 0 deletions src/chocolatey.tests.integration/scenarios/UpgradeScenarios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,39 @@ public void should_match_the_upgrade_version_of_one_dot_one_dot_zero()
{
_packageResult.Version.ShouldEqual("1.1.0");
}

[Fact]
public void should_have_executed_chocolateyBeforeModify_script_for_original_package()
{
MockLogger.contains_message("upgradepackage 1.0.0 Before Modification", LogLevel.Info).ShouldBeTrue();
}

[Fact]
public void should_have_executed_chocolateyBeforeModify_before_chocolateyInstall()
{
MockLogger.MessagesFor(LogLevel.Info).or_empty_list_if_null()
.SkipWhile(p => !p.Contains("upgradepackage 1.0.0 Before Modification"))
.Any(p => p.EndsWith("upgradepackage 1.1.0 Installed"))
.ShouldBeTrue();
}

[Fact]
public void should_not_have_executed_chocolateyUninstall_script_for_original_package()
{
MockLogger.contains_message("upgradepackage 1.0.0 Uninstalled", LogLevel.Info).ShouldBeFalse();
}

[Fact]
public void should_not_have_executed_chocolateyBeforeModify_script_for_new_package()
{
MockLogger.contains_message("upgradepackage 1.1.0 Before Modification", LogLevel.Info).ShouldBeFalse();
}

[Fact]
public void should_have_executed_chocolateyInstall_script_for_new_package()
{
MockLogger.contains_message("upgradepackage 1.1.0 Installed", LogLevel.Info).ShouldBeTrue();
}
}

[Concern(typeof (ChocolateyUpgradeCommand))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,9 @@ public ConcurrentDictionary<string, PackageResult> upgrade_run(ChocolateyConfigu

get_environment_before(config, allowLogging: true);

var packageUpgrades = perform_source_runner_function(config, r => r.upgrade_run(config, action));
var beforeUpgradeAction = new Action<PackageResult>(packageResult => before_package_upgrade(packageResult, config));

var packageUpgrades = perform_source_runner_function(config, r => r.upgrade_run(config, action, beforeUpgradeAction));

var upgradeFailures = packageUpgrades.Count(p => !p.Value.Success);
var upgradeWarnings = packageUpgrades.Count(p => p.Value.Warning);
Expand Down Expand Up @@ -591,12 +593,21 @@ public ConcurrentDictionary<string, PackageResult> upgrade_run(ChocolateyConfigu
return packageUpgrades;
}

private void before_package_upgrade(PackageResult packageResult, ChocolateyConfiguration config)
{
_powershellService.before_modify(config, packageResult);
}

public void uninstall_noop(ChocolateyConfiguration config)
{
Action<PackageResult> action = null;
if (config.SourceType == SourceType.normal)
{
action = (pkg) => _powershellService.uninstall_noop(pkg);
action = (pkg) =>
{
_powershellService.before_modify_noop(pkg);
_powershellService.uninstall_noop(pkg);
};
}

perform_source_runner_action(config, r => r.uninstall_noop(config, action));
Expand Down Expand Up @@ -660,6 +671,11 @@ public void handle_package_uninstall(PackageResult packageResult, ChocolateyConf
packageResult.InstallLocation += ".{0}".format_with(packageResult.Package.Version.to_string());
}

if (!config.SkipPackageInstallProvider)
{
_powershellService.before_modify(config, packageResult);
}

_shimgenService.uninstall(config, packageResult);

if (!config.SkipPackageInstallProvider)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public ConcurrentDictionary<string, PackageResult> upgrade_noop(ChocolateyConfig
return new ConcurrentDictionary<string, PackageResult>(StringComparer.InvariantCultureIgnoreCase);
}

public ConcurrentDictionary<string, PackageResult> upgrade_run(ChocolateyConfiguration config, Action<PackageResult> continueAction)
public ConcurrentDictionary<string, PackageResult> upgrade_run(ChocolateyConfiguration config, Action<PackageResult> continueAction, Action<PackageResult> beforeUpgradeAction)
{
throw new NotImplementedException("{0} does not implement upgrade".format_with(APP_NAME));
}
Expand Down
14 changes: 14 additions & 0 deletions src/chocolatey/infrastructure.app/services/IPowershellService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,19 @@ public interface IPowershellService
/// <param name="packageResult">The package result.</param>
/// <returns>true if the chocolateyUninstall.ps1 was found, even if it has failures</returns>
bool uninstall(ChocolateyConfiguration configuration, PackageResult packageResult);

/// <summary>
/// Noops the specified package before modify operation.
/// </summary>
/// <param name="packageResult">The package result.</param>
void before_modify_noop(PackageResult packageResult);

/// <summary>
/// Runs any before modification script on the specified package.
/// </summary>
/// <param name="configuration">The configuration</param>
/// <param name="packageResult">The package result.</param>
/// <returns>true if the chocolateyBeforeModify.ps1 was found, even if it has failures</returns>
bool before_modify(ChocolateyConfiguration configuration, PackageResult packageResult);
}
}
5 changes: 3 additions & 2 deletions src/chocolatey/infrastructure.app/services/ISourceRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,15 @@ public interface ISourceRunner
/// <param name="config">The configuration.</param>
/// <param name="continueAction">The action to continue with for each noop test upgrade.</param>
ConcurrentDictionary<string, PackageResult> upgrade_noop(ChocolateyConfiguration config, Action<PackageResult> continueAction);

/// <summary>
/// Upgrades packages from NuGet related feeds
/// </summary>
/// <param name="config">The configuration.</param>
/// <param name="continueAction">The action to continue with when upgrade is successful.</param>
/// <param name="beforeUpgradeAction">The action (if any) to run on any currently installed package before triggering the upgrade.</param>
/// <returns>results of installs</returns>
ConcurrentDictionary<string, PackageResult> upgrade_run(ChocolateyConfiguration config, Action<PackageResult> continueAction);
ConcurrentDictionary<string, PackageResult> upgrade_run(ChocolateyConfiguration config, Action<PackageResult> continueAction, Action<PackageResult> beforeUpgradeAction = null);

/// <summary>
/// Run uninstall in noop mode
Expand Down
60 changes: 30 additions & 30 deletions src/chocolatey/infrastructure.app/services/NugetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -500,12 +500,12 @@ public ConcurrentDictionary<string, PackageResult> upgrade_noop(ChocolateyConfig
return upgrade_run(config, continueAction, performAction: false);
}

public ConcurrentDictionary<string, PackageResult> upgrade_run(ChocolateyConfiguration config, Action<PackageResult> continueAction)
public ConcurrentDictionary<string, PackageResult> upgrade_run(ChocolateyConfiguration config, Action<PackageResult> continueAction, Action<PackageResult> beforeUpgradeAction = null)
{
return upgrade_run(config, continueAction, performAction: true);
return upgrade_run(config, continueAction, performAction: true, beforeUpgradeAction: beforeUpgradeAction);
}

public ConcurrentDictionary<string, PackageResult> upgrade_run(ChocolateyConfiguration config, Action<PackageResult> continueAction, bool performAction)
public ConcurrentDictionary<string, PackageResult> upgrade_run(ChocolateyConfiguration config, Action<PackageResult> continueAction, bool performAction, Action<PackageResult> beforeUpgradeAction = null)
{
_fileSystem.create_directory_if_not_exists(ApplicationParameters.PackagesLocation);
var packageInstalls = new ConcurrentDictionary<string, PackageResult>(StringComparer.InvariantCultureIgnoreCase);
Expand Down Expand Up @@ -710,6 +710,12 @@ public ConcurrentDictionary<string, PackageResult> upgrade_run(ChocolateyConfigu
{
ensure_package_files_have_compatible_attributes(config, installedPackage, pkgInfo);
rename_legacy_package_version(config, installedPackage, pkgInfo);
if (beforeUpgradeAction != null)
{
var currentPackageResult = new PackageResult(installedPackage, get_install_directory(config, installedPackage));
beforeUpgradeAction(currentPackageResult);
}

backup_existing_version(config, installedPackage, pkgInfo);
remove_shim_directors(config, installedPackage, pkgInfo);
if (config.Force && (installedPackage.Version == availablePackage.Version))
Expand Down Expand Up @@ -744,16 +750,30 @@ public ConcurrentDictionary<string, PackageResult> upgrade_run(ChocolateyConfigu
return packageInstalls;
}

public void ensure_package_files_have_compatible_attributes(ChocolateyConfiguration config, IPackage installedPackage, ChocolateyPackageInformation pkgInfo)
private string get_install_directory(ChocolateyConfiguration config, IPackage installedPackage)
{
var installDirectory = _fileSystem.combine_paths(ApplicationParameters.PackagesLocation, installedPackage.Id);
var pathResolver = NugetCommon.GetPathResolver(config, NugetCommon.GetNuGetFileSystem(config, _nugetLogger));
var installDirectory = pathResolver.GetInstallPath(installedPackage);
if (!_fileSystem.directory_exists(installDirectory))
{
var pathResolver = new ChocolateyPackagePathResolver(NugetCommon.GetNuGetFileSystem(config, _nugetLogger), useSideBySidePaths: true);
installDirectory = pathResolver.GetInstallPath(installedPackage);
if (!_fileSystem.directory_exists(installDirectory)) return;
var chocoPathResolver = pathResolver as ChocolateyPackagePathResolver;
if (chocoPathResolver != null)
{
chocoPathResolver.UseSideBySidePaths = !chocoPathResolver.UseSideBySidePaths;
installDirectory = chocoPathResolver.GetInstallPath(installedPackage);
}

if (!_fileSystem.directory_exists(installDirectory)) return null;
}

return installDirectory;
}

public void ensure_package_files_have_compatible_attributes(ChocolateyConfiguration config, IPackage installedPackage, ChocolateyPackageInformation pkgInfo)
{
var installDirectory = get_install_directory(config, installedPackage);
if (!_fileSystem.directory_exists(installDirectory)) return;

_filesService.ensure_compatible_file_attributes(installDirectory, config);
}

Expand All @@ -780,17 +800,7 @@ public void backup_existing_version(ChocolateyConfiguration config, IPackage ins
{
_fileSystem.create_directory_if_not_exists(ApplicationParameters.PackageBackupLocation);

var pathResolver = NugetCommon.GetPathResolver(config, NugetCommon.GetNuGetFileSystem(config, _nugetLogger));
var pkgInstallPath = pathResolver.GetInstallPath(installedPackage);
if (!_fileSystem.directory_exists(pkgInstallPath))
{
var chocoPathResolver = pathResolver as ChocolateyPackagePathResolver;
if (chocoPathResolver != null)
{
chocoPathResolver.UseSideBySidePaths = !chocoPathResolver.UseSideBySidePaths;
pkgInstallPath = chocoPathResolver.GetInstallPath(installedPackage);
}
}
var pkgInstallPath = get_install_directory(config, installedPackage);

if (_fileSystem.directory_exists(pkgInstallPath))
{
Expand Down Expand Up @@ -885,17 +895,7 @@ public void backup_changed_files(string packageInstallPath, ChocolateyConfigurat
/// <param name="pkgInfo">The package information.</param>
private void remove_shim_directors(ChocolateyConfiguration config, IPackage installedPackage, ChocolateyPackageInformation pkgInfo)
{
var pathResolver = NugetCommon.GetPathResolver(config, NugetCommon.GetNuGetFileSystem(config, _nugetLogger));
var pkgInstallPath = pathResolver.GetInstallPath(installedPackage);
if (!_fileSystem.directory_exists(pkgInstallPath))
{
var chocoPathResolver = pathResolver as ChocolateyPackagePathResolver;
if (chocoPathResolver != null)
{
chocoPathResolver.UseSideBySidePaths = !chocoPathResolver.UseSideBySidePaths;
pkgInstallPath = chocoPathResolver.GetInstallPath(installedPackage);
}
}
var pkgInstallPath = get_install_directory(config, installedPackage);

if (_fileSystem.directory_exists(pkgInstallPath))
{
Expand Down
Loading

0 comments on commit 18075dd

Please sign in to comment.