forked from chocolatey/choco
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(chocolateyGH-121) Remove unchanged files on uninstall
When the files that were installed during install are unchanged, they should be removed from the package directory. Any files that have changed should stick around.
- Loading branch information
1 parent
8b22eeb
commit 2fdf3bc
Showing
5 changed files
with
206 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
src/chocolatey.tests/infrastructure.app/services/NugetServiceSpecs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
// Copyright © 2011 - Present RealDimensions Software, LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
namespace chocolatey.tests.infrastructure.app.services | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using Moq; | ||
using NuGet; | ||
using chocolatey.infrastructure.app.domain; | ||
using chocolatey.infrastructure.app.services; | ||
using IFileSystem = chocolatey.infrastructure.filesystem.IFileSystem; | ||
|
||
public class NugetServiceSpecs | ||
{ | ||
public abstract class NugetServiceSpecsBase : TinySpec | ||
{ | ||
protected NugetService service; | ||
protected Mock<IChocolateyPackageInformationService> packageInfoService = new Mock<IChocolateyPackageInformationService>(); | ||
protected Mock<IFileSystem> fileSystem = new Mock<IFileSystem>(); | ||
protected Mock<ILogger> nugetLogger = new Mock<ILogger>(); | ||
protected Mock<IFilesService> filesService = new Mock<IFilesService>(); | ||
protected Mock<IPackage> package = new Mock<IPackage>(); | ||
|
||
public override void Context() | ||
{ | ||
fileSystem.ResetCalls(); | ||
nugetLogger.ResetCalls(); | ||
packageInfoService.ResetCalls(); | ||
filesService.ResetCalls(); | ||
package.ResetCalls(); | ||
|
||
service = new NugetService(fileSystem.Object, nugetLogger.Object, packageInfoService.Object, filesService.Object); | ||
} | ||
} | ||
|
||
public class when_NugetService_removes_installation_files_on_uninstall : NugetServiceSpecsBase | ||
{ | ||
private Action because; | ||
private ChocolateyPackageInformation packageInfo; | ||
private const string filePath = "c:\\tests"; | ||
private IList<PackageFile> packageFiles; | ||
|
||
public override void Context() | ||
{ | ||
base.Context(); | ||
package.Setup(x => x.Id).Returns("bob"); | ||
packageInfo = new ChocolateyPackageInformation(package.Object); | ||
packageInfo.FilesSnapshot = new PackageFiles(); | ||
packageFiles = new List<PackageFile>(); | ||
fileSystem.Setup(x => x.directory_exists(It.IsAny<string>())).Returns(true); | ||
|
||
} | ||
|
||
public override void Because() | ||
{ | ||
because = () => service.remove_installation_files(package.Object, packageInfo); | ||
} | ||
|
||
[Fact] | ||
public void should_do_nothing_if_the_directory_no_longer_exists() | ||
{ | ||
Context(); | ||
fileSystem.ResetCalls(); | ||
fileSystem.Setup(x => x.directory_exists(It.IsAny<string>())).Returns(false); | ||
|
||
var packageFile = new PackageFile { Path = filePath, Checksum = "1234" }; | ||
packageFiles.Add(packageFile); | ||
|
||
packageInfo.FilesSnapshot.Files = packageFiles.ToList(); | ||
|
||
var fileSystemFiles = new List<string>() { filePath }; | ||
|
||
fileSystem.Setup(x => x.get_files(It.IsAny<string>(), It.IsAny<string>(), SearchOption.AllDirectories)).Returns(fileSystemFiles); | ||
filesService.Setup(x => x.get_package_file(It.IsAny<string>())).Returns(packageFile); | ||
|
||
because(); | ||
|
||
filesService.Verify(x => x.get_package_file(It.IsAny<string>()), Times.Never); | ||
fileSystem.Verify(x => x.delete_file(filePath),Times.Never); | ||
} | ||
|
||
[Fact] | ||
public void should_remove_an_unchanged_file() | ||
{ | ||
Context(); | ||
|
||
var packageFile = new PackageFile { Path = filePath, Checksum = "1234" }; | ||
packageFiles.Add(packageFile); | ||
|
||
packageInfo.FilesSnapshot.Files = packageFiles.ToList(); | ||
|
||
var fileSystemFiles = new List<string>() { filePath }; | ||
|
||
|
||
fileSystem.Setup(x => x.get_files(It.IsAny<string>(), It.IsAny<string>(), SearchOption.AllDirectories)).Returns(fileSystemFiles); | ||
|
||
filesService.Setup(x => x.get_package_file(It.IsAny<string>())).Returns(packageFile); | ||
|
||
because(); | ||
|
||
fileSystem.Verify(x => x.delete_file(filePath)); | ||
} | ||
|
||
[Fact] | ||
public void should_not_delete_a_changed_file() | ||
{ | ||
Context(); | ||
|
||
var packageFile = new PackageFile { Path = filePath, Checksum = "1234" }; | ||
var packageFileWithUpdatedChecksum = new PackageFile { Path = filePath, Checksum = "4321" }; | ||
packageFiles.Add(packageFile); | ||
|
||
packageInfo.FilesSnapshot.Files = packageFiles.ToList(); | ||
|
||
var fileSystemFiles = new List<string>() { filePath }; | ||
|
||
|
||
fileSystem.Setup(x => x.get_files(It.IsAny<string>(), It.IsAny<string>(), SearchOption.AllDirectories)).Returns(fileSystemFiles); | ||
|
||
filesService.Setup(x => x.get_package_file(It.IsAny<string>())).Returns(packageFileWithUpdatedChecksum); | ||
|
||
because(); | ||
|
||
fileSystem.Verify(x => x.delete_file(filePath),Times.Never); | ||
} | ||
|
||
[Fact] | ||
public void should_not_delete_an_unfound_file() | ||
{ | ||
Context(); | ||
|
||
var packageFile = new PackageFile { Path = filePath, Checksum = "1234" }; | ||
var packageFileNotInOriginal = new PackageFile { Path ="c:\\files", Checksum = "4321" }; | ||
packageFiles.Add(packageFile); | ||
|
||
packageInfo.FilesSnapshot.Files = packageFiles.ToList(); | ||
|
||
var fileSystemFiles = new List<string>() { filePath }; | ||
|
||
|
||
fileSystem.Setup(x => x.get_files(It.IsAny<string>(), It.IsAny<string>(), SearchOption.AllDirectories)).Returns(fileSystemFiles); | ||
|
||
filesService.Setup(x => x.get_package_file(It.IsAny<string>())).Returns(packageFileNotInOriginal); | ||
|
||
because(); | ||
|
||
fileSystem.Verify(x => x.delete_file(filePath),Times.Never); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters