public void ShouldNotAddNewPackageWithButUpdateCurrentWithHigherVersion() { // Arrange var mockLogger = new Mock <ILogger>(); var mockVersionExisting = new Mock <IVersion>(); var mockVersionNew = new Mock <IVersion>(); var packageRepository = new MockPackageRepository(mockLogger.Object); // Act mockVersionExisting.SetupGet(v => v.Major).Returns(1); mockVersionExisting.SetupGet(v => v.Minor).Returns(1); mockVersionExisting.SetupGet(v => v.Patch).Returns(1); mockVersionExisting.SetupGet(v => v.VersionType).Returns(VersionType.alpha); mockVersionNew.SetupGet(v => v.Major).Returns(100); mockVersionNew.SetupGet(v => v.Minor).Returns(100); mockVersionNew.SetupGet(v => v.Patch).Returns(100); mockVersionNew.SetupGet(v => v.VersionType).Returns(VersionType.final); var existingPackage = new Package("Pesho", mockVersionExisting.Object); var newPackage = new Package("Pesho", mockVersionNew.Object); // Act packageRepository.Add(existingPackage); packageRepository.Add(newPackage); // Assert Assert.AreEqual(1, packageRepository.Packages.Count); }
public void GetUpdatesReturnsDistinctSetOfPackages() { // Arrange var package_10 = PackageUtility.CreatePackage("A", "1.0"); var package_11 = PackageUtility.CreatePackage("A", "1.1"); var package_12 = PackageUtility.CreatePackage("A", "1.2"); var repo1 = new MockPackageRepository(); repo1.Add(package_12); repo1.Add(package_11); var repo2 = new MockPackageRepository(); repo2.Add(package_12); var aggregateRepository = new AggregateRepository(new[] { repo1, repo2 }); // Act var updates = aggregateRepository.GetUpdates(new[] { package_10 }, includePrerelease: false, includeAllVersions: true); // Assert Assert.Equal(2, updates.Count()); Assert.Same(package_11, updates.ElementAt(0)); Assert.Same(package_12, updates.ElementAt(1)); }
public void TestRecentSwitchWorkCorrectly() { // Arrange var packageA = PackageUtility.CreatePackage("A", "1.0"); var packageB = PackageUtility.CreatePackage("B", "2.0"); var packageC = PackageUtility.CreatePackage("C", "3.0"); var repository = new MockPackageRepository(); repository.Add(packageA); repository.Add(packageB); repository.Add(packageC); var cmdlet = BuildCmdlet(false, repository); cmdlet.Recent = true; // Act var packages = cmdlet.GetResults().OfType <IPackage>().ToList(); // Assert Assert.AreEqual(3, packages.Count); Assert.AreSame(packageA, packages[0]); Assert.AreSame(packageB, packages[1]); Assert.AreSame(packageC, packages[2]); }
public void ShouldNotAddPackageWhenAlreadyExists() { // Arrange var mockLogger = new Mock <ILogger>(); var mockPackage = new Mock <IPackage>(); var packageRepository = new MockPackageRepository(mockLogger.Object); // Act packageRepository.Add(mockPackage.Object); packageRepository.Add(mockPackage.Object); // Assert Assert.AreEqual(1, packageRepository.Packages.Count); }
public void SelfUpdateNewVersionDoesUpdatesExe() { // Arrange var factory = new Mock <IPackageRepositoryFactory>(); var sourceProvider = new Mock <IPackageSourceProvider>(); var repository = new MockPackageRepository(); IPackage package = PackageUtility.CreatePackage("NuGet.CommandLine", "3.0", tools: new[] { "NuGet.exe" }); repository.Add(package); factory.Setup(m => m.CreateRepository(It.IsAny <string>())).Returns(repository); ConsoleInfo consoleInfo = GetConsoleInfo(); var updateCmd = new MockUpdateCommand(factory.Object, sourceProvider.Object); updateCmd.Console = consoleInfo.Console; // Act updateCmd.SelfUpdate(@"c:\NuGet.exe", new SemanticVersion("2.0")); // Assert Assert.True(updateCmd.MovedFiles.ContainsKey(@"c:\NuGet.exe")); Assert.Equal(@"c:\NuGet.exe.old", updateCmd.MovedFiles[@"c:\NuGet.exe"]); Assert.True(updateCmd.UpdatedFiles.ContainsKey(@"c:\NuGet.exe")); Assert.Equal(@"tools\NuGet.exe", updateCmd.UpdatedFiles[@"c:\NuGet.exe"]); }
public void UpdatePackageAddsPackagesToSharedPackageRepositoryWhenReferencesAreAdded() { // Arrange var localRepository = new MockPackageRepository(); var sourceRepository = new MockPackageRepository(); var constraintProvider = NullConstraintProvider.Instance; var pathResolver = new DefaultPackagePathResolver(NullFileSystem.Instance); var projectSystem = new MockProjectSystem(); var package_A10 = PackageUtility.CreatePackage("A", "1.0", content: new[] { "1.txt" }); var package_A12 = PackageUtility.CreatePackage("A", "1.2", content: new[] { "1.txt" }); localRepository.Add(package_A10); sourceRepository.Add(package_A12); var sharedRepository = new Mock <ISharedPackageRepository>(MockBehavior.Strict); sharedRepository.Setup(s => s.AddPackage(package_A12)).Verifiable(); var repositoryFactory = new Mock <IPackageRepositoryFactory>(); repositoryFactory.Setup(s => s.CreateRepository(It.IsAny <string>())).Returns(sourceRepository); var packageSourceProvider = new Mock <IPackageSourceProvider>(); packageSourceProvider.Setup(s => s.LoadPackageSources()).Returns(new[] { new PackageSource("foo-source") }); var updateCommand = new UpdateCommand(repositoryFactory.Object, packageSourceProvider.Object); // Act updateCommand.UpdatePackages(localRepository, sharedRepository.Object, sourceRepository, constraintProvider, pathResolver, projectSystem); // Assert sharedRepository.Verify(); }
public void EnsurePackageDownloadsUsesMachineCacheIfAvailable() { // Arrange var hashBytes = new byte[] { 1, 2, 3, 4 }; var hash = Convert.ToBase64String(hashBytes); var zipPackage = PackageUtility.CreatePackage("A", "1.2"); var hashProvider = new Mock <IHashProvider>(MockBehavior.Strict); hashProvider.Setup(h => h.CalculateHash(It.IsAny <byte[]>())).Returns(hashBytes); var mockRepository = new MockPackageRepository(); mockRepository.Add(zipPackage); var servicePackage = new DataServicePackage { Id = "A", Version = "1.2", PackageHash = hash, HashProvider = hashProvider.Object, }; // Act servicePackage.EnsurePackage(mockRepository); // Assert Assert.Equal(zipPackage, servicePackage._package); }
public void UpdatePackageWillAskForEachFileWhenThereAreFileConflict() { // Arrange var localRepository = new MockPackageRepository(); var sourceRepository = new MockPackageRepository(); var constraintProvider = NullConstraintProvider.Instance; var fileSystem = new MockFileSystem(); var pathResolver = new DefaultPackagePathResolver(fileSystem); var projectSystem = new MockProjectSystem(); projectSystem.AddFile("one.txt", "this is one"); projectSystem.AddFile("two.txt", "this is two"); var packages = new List <IPackage>(); var package_A10 = PackageUtility.CreatePackage("A", "1.0", content: new[] { "1.txt" }); var package_A12 = PackageUtility.CreatePackage("A", "1.2", content: new[] { "one.txt", "two.txt" }); localRepository.Add(package_A10); sourceRepository.Add(package_A12); var sharedRepository = new Mock <ISharedPackageRepository>(MockBehavior.Strict); sharedRepository.Setup(s => s.AddPackage(package_A12)).Callback <IPackage>(p => packages.Add(p)); sharedRepository.Setup(s => s.GetPackages()).Returns(packages.AsQueryable()); var repositoryFactory = new Mock <IPackageRepositoryFactory>(); repositoryFactory.Setup(s => s.CreateRepository(It.IsAny <string>())).Returns(sourceRepository); var packageSourceProvider = new Mock <IPackageSourceProvider>(); packageSourceProvider.Setup(s => s.LoadPackageSources()).Returns(new[] { new PackageSource("foo-source") }); var answers = new[] { FileConflictResolution.Overwrite, FileConflictResolution.Ignore, }; int cursor = 0; var console = new Mock <IConsole>(); console.Setup(c => c.ResolveFileConflict(It.IsAny <string>())).Returns(() => answers[cursor++]); var updateCommand = new UpdateCommand() { RepositoryFactory = repositoryFactory.Object, SourceProvider = packageSourceProvider.Object, Console = console.Object, }; // Act updateCommand.UpdatePackages(localRepository, fileSystem, sharedRepository.Object, sourceRepository, constraintProvider, pathResolver, projectSystem); // Assert Assert.True(localRepository.Exists("A", new SemanticVersion("1.2"))); Assert.Equal("content\\one.txt", projectSystem.ReadAllText("one.txt")); Assert.Equal("this is two", projectSystem.ReadAllText("two.txt")); Assert.Equal(2, cursor); }
public void ReinstallPackagesRestoresPackageWithTheSameVersion() { // Arrange var localRepository = new Mock<MockPackageRepository>() { CallBase = true }.As<ISharedPackageRepository>().Object; var sourceRepository = new MockPackageRepository(); var projectSystem = new MockProjectSystem(); var pathResolver = new DefaultPackagePathResolver(projectSystem); var projectManager = new ProjectManager(localRepository, pathResolver, projectSystem, new MockPackageRepository()); var packageManager = new VsPackageManager( TestUtils.GetSolutionManager(), sourceRepository, new Mock<IFileSystemProvider>().Object, projectSystem, localRepository, new Mock<IDeleteOnRestartManager>().Object, new Mock<VsPackageInstallerEvents>().Object); var packageA = PackageUtility.CreatePackage("A", "1.2", new[] { "content.txt" }); sourceRepository.Add(packageA); localRepository.AddPackage(packageA); projectManager.LocalRepository.AddPackage(packageA); // Act packageManager.ReinstallPackage(projectManager, "A", updateDependencies: true, allowPrereleaseVersions: true, logger: null); // Assert Assert.True(packageManager.LocalRepository.Exists("A", new SemanticVersion("1.2"))); Assert.True(projectManager.LocalRepository.Exists("A", new SemanticVersion("1.2"))); }
public void FindPackagesReturnsPackagesWithTermInPackageTagOrDescriptionOrId() { // Arrange var term = "TAG"; var repo = new MockPackageRepository(); repo.Add(CreateMockPackage("A", "1.0", "Description", " TAG ")); repo.Add(CreateMockPackage("B", "2.0", "Description", "Tags")); repo.Add(CreateMockPackage("C", "1.0", "This description has tags in it")); repo.Add(CreateMockPackage("D", "1.0", "Description")); repo.Add(CreateMockPackage("TagCloud", "1.0", "Description")); // Act var packages = repo.GetPackages().Find(term.Split()).ToList(); // Assert Assert.AreEqual(3, packages.Count); Assert.AreEqual("A", packages[0].Id); Assert.AreEqual("C", packages[1].Id); Assert.AreEqual("TagCloud", packages[2].Id); }
public void AddPackageWhenCorrectIsAddedPackage() { // Arrange var mockLogger = new Mock <ILogger>(); var mockPackage = new Mock <IPackage>(); var packageRepository = new MockPackageRepository(mockLogger.Object); // Act packageRepository.Add(mockPackage.Object); // Assert Assert.AreEqual(1, packageRepository.Packages.Count); }
public void ShouldUpdatePackageWithLowerVersion() { // Arrange var mockLogger = new Mock <ILogger>(); var mockVersionExisting = new Mock <IVersion>(); var mockVersionNew = new Mock <IVersion>(); var packageRepository = new MockPackageRepository(mockLogger.Object); // Act mockVersionExisting.SetupGet(v => v.Major).Returns(1); mockVersionExisting.SetupGet(v => v.Minor).Returns(1); mockVersionExisting.SetupGet(v => v.Patch).Returns(1); mockVersionExisting.SetupGet(v => v.VersionType).Returns(VersionType.alpha); mockVersionNew.SetupGet(v => v.Major).Returns(100); mockVersionNew.SetupGet(v => v.Minor).Returns(100); mockVersionNew.SetupGet(v => v.Patch).Returns(100); mockVersionNew.SetupGet(v => v.VersionType).Returns(VersionType.final); var existingPackage = new Package("Pesho", mockVersionExisting.Object); var newPackage = new Package("Pesho", mockVersionNew.Object); // Act packageRepository.Add(existingPackage); packageRepository.Add(newPackage); IVersion version = existingPackage.Version; foreach (var item in packageRepository.Packages) { version = item.Version; } // Assert Assert.AreEqual(newPackage.Version, version); }
public void UpdatePackageThrowsIfPackageNotInstalled() { // Arrange var localRepository = new MockPackageRepository(); var sourceRepository = new MockPackageRepository(); var projectSystem = new MockProjectSystem(); PackageManager packageManager = new PackageManager(sourceRepository, new DefaultPackagePathResolver(projectSystem), projectSystem, localRepository); IPackage A20 = PackageUtility.CreatePackage("A", "2.0"); sourceRepository.Add(A20); // Act ExceptionAssert.Throws <InvalidOperationException>(() => packageManager.UpdatePackage("A", updateDependencies: true), "Unable to find package 'A'."); }
public void ReinstallPackagesSkipsReinstallingIfPackageDoesNotExistAndLogWarning() { // Arrange var localRepository = new Mock<MockPackageRepository>() { CallBase = true }.As<ISharedPackageRepository>().Object; var sourceRepository = new MockPackageRepository(); var projectSystem = new MockProjectSystem(); var pathResolver = new DefaultPackagePathResolver(projectSystem); var projectManager = new ProjectManager(localRepository, pathResolver, projectSystem, new MockPackageRepository()); var installerEvents = new Mock<VsPackageInstallerEvents>(MockBehavior.Strict); int eventCount = 0; RegisterInstallerEvents(installerEvents, _ => eventCount++); var packageManager = new VsPackageManager( TestUtils.GetSolutionManager(), sourceRepository, new Mock<IFileSystemProvider>().Object, projectSystem, localRepository, new Mock<IDeleteOnRestartManager>().Object, installerEvents.Object); var packageA = PackageUtility.CreatePackage("A", "1.2", new[] { "content.txt" }); sourceRepository.Add(packageA); localRepository.AddPackage(packageA); projectManager.LocalRepository.AddPackage(packageA); // remove package from source repository to simulate missing package condition sourceRepository.Remove(packageA); var logger = new Mock<ILogger>(); logger.Setup(s => s.Log( MessageLevel.Warning, "Skipped reinstalling package '{0}' in project '{1}' because the package does not exist in the package source.", "A 1.2", "x:\\MockFileSystem") ).Verifiable(); // Act packageManager.ReinstallPackage(projectManager, "A", updateDependencies: true, allowPrereleaseVersions: true, logger: logger.Object); // Assert logger.Verify(); Assert.Equal(0, eventCount); Assert.True(packageManager.LocalRepository.Exists("A", new SemanticVersion("1.2"))); Assert.True(projectManager.LocalRepository.Exists("A", new SemanticVersion("1.2"))); }
public void UpdatePackageOverwriteAllFilesWhenFileConflictActionSetToOverwrite() { // Arrange var localRepository = new MockPackageRepository(); var sourceRepository = new MockPackageRepository(); var constraintProvider = NullConstraintProvider.Instance; var fileSystem = new MockFileSystem(); var pathResolver = new DefaultPackagePathResolver(fileSystem); var projectSystem = new MockProjectSystem(); projectSystem.AddFile("one.txt", "this is one"); projectSystem.AddFile("two.txt", "this is two"); var packages = new List<IPackage>(); var package_A10 = PackageUtility.CreatePackage("A", "1.0", content: new[] { "1.txt" }); var package_A12 = PackageUtility.CreatePackage("A", "1.2", content: new[] { "one.txt", "two.txt" }); localRepository.Add(package_A10); sourceRepository.Add(package_A12); var sharedRepository = new Mock<ISharedPackageRepository>(MockBehavior.Strict); sharedRepository.Setup(s => s.AddPackage(package_A12)).Callback<IPackage>(p => packages.Add(p)); sharedRepository.Setup(s => s.GetPackages()).Returns(packages.AsQueryable()); var repositoryFactory = new Mock<IPackageRepositoryFactory>(); repositoryFactory.Setup(s => s.CreateRepository(It.IsAny<string>())).Returns(sourceRepository); var packageSourceProvider = new Mock<IPackageSourceProvider>(); packageSourceProvider.Setup(s => s.LoadPackageSources()).Returns(new[] { new PackageSource("foo-source") }); var console = new Mock<IConsole>(); console.Setup(c => c.ResolveFileConflict(It.IsAny<string>())).Returns(FileConflictResolution.Ignore); var updateCommand = new UpdateCommand() { RepositoryFactory = repositoryFactory.Object, SourceProvider = packageSourceProvider.Object, Console = console.Object, FileConflictAction = FileConflictAction.Overwrite }; // Act updateCommand.UpdatePackages(localRepository, fileSystem, sharedRepository.Object, sourceRepository, constraintProvider, pathResolver, projectSystem); // Assert Assert.True(localRepository.Exists("A", new SemanticVersion("1.2"))); Assert.Equal("content\\one.txt", projectSystem.ReadAllText("one.txt")); Assert.Equal("content\\two.txt", projectSystem.ReadAllText("two.txt")); }
public void ExistsReturnsTrueIfAnyRepositoryContainsPackage() { // Arrange var package = PackageUtility.CreatePackage("Abc"); var repo1 = new MockPackageRepository(); var repo2 = new MockPackageRepository(); repo2.Add(package); var aggregateRepository = new AggregateRepository(new[] { repo1, repo2 }); // Act var exists = aggregateRepository.Exists("Abc", new SemanticVersion("1.0")); // Assert Assert.True(exists); }
public void SelfUpdateNoNuGetExeInNuGetExePackageThrows() { // Arrange var factory = new Mock<IPackageRepositoryFactory>(); var sourceProvider = new Mock<IPackageSourceProvider>(); var repository = new MockPackageRepository(); repository.Add(PackageUtility.CreatePackage("NuGet.CommandLine", "3.0")); factory.Setup(m => m.CreateRepository(It.IsAny<string>())).Returns(repository); ConsoleInfo consoleInfo = GetConsoleInfo(); var updateCmd = new UpdateCommand(factory.Object, sourceProvider.Object); updateCmd.Console = consoleInfo.Console; updateCmd.Console = consoleInfo.Console; // Act & Assert ExceptionAssert.Throws<CommandLineException>(() => updateCmd.SelfUpdate("c:\foo.exe", new SemanticVersion("2.0")), "Invalid NuGet.CommandLine package. Unable to locate NuGet.exe within the package."); }
public void UpdatePackageDoesNothingIfNoUpdatesAvailable() { // Arrange var localRepository = new MockPackageRepository(); var sourceRepository = new MockPackageRepository(); var projectSystem = new MockProjectSystem(); PackageManager packageManager = new PackageManager(sourceRepository, new DefaultPackagePathResolver(projectSystem), projectSystem, localRepository); IPackage A10 = PackageUtility.CreatePackage("A", "1.0"); localRepository.Add(A10); // Act packageManager.UpdatePackage("A", updateDependencies: true); // Assert Assert.IsTrue(localRepository.Exists("A", new Version("1.0"))); }
public void SelfUpdateNoNuGetExeInNuGetExePackageThrows() { // Arrange var factory = new Mock <IPackageRepositoryFactory>(); var repository = new MockPackageRepository(); repository.Add(PackageUtility.CreatePackage("NuGet.CommandLine", "3.0")); factory.Setup(m => m.CreateRepository(It.IsAny <PackageSource>())).Returns(repository); ConsoleInfo consoleInfo = GetConsoleInfo(); var updateCmd = new UpdateCommand(factory.Object); updateCmd.Console = consoleInfo.Console; updateCmd.Console = consoleInfo.Console; // Act & Assert ExceptionAssert.Throws <CommandLineException>(() => updateCmd.SelfUpdate("c:\foo.exe", new Version("2.0")), "Invalid NuGet.CommandLine package. Unable to locate NuGet.exe within the package."); }
public void SelfUpdateOlderVersionDoesNotUpdate() { // Arrange var factory = new Mock<IPackageRepositoryFactory>(); var sourceProvider = new Mock<IPackageSourceProvider>(); var repository = new MockPackageRepository(); repository.Add(PackageUtility.CreatePackage("NuGet.CommandLine", "1.0")); factory.Setup(m => m.CreateRepository(It.IsAny<string>())).Returns(repository); ConsoleInfo consoleInfo = GetConsoleInfo(); var updateCmd = new UpdateCommand(factory.Object, sourceProvider.Object); updateCmd.Console = consoleInfo.Console; // Act updateCmd.SelfUpdate("c:\foo.exe", new SemanticVersion("2.0")); // Assert Assert.Equal("NuGet.exe is up to date.", consoleInfo.WrittenLines[0]); }
public void SelfUpdateOlderVersionDoesNotUpdate() { // Arrange var factory = new Mock <IPackageRepositoryFactory>(); var repository = new MockPackageRepository(); repository.Add(PackageUtility.CreatePackage("NuGet.CommandLine", "1.0")); factory.Setup(m => m.CreateRepository(It.IsAny <PackageSource>())).Returns(repository); ConsoleInfo consoleInfo = GetConsoleInfo(); var updateCmd = new UpdateCommand(factory.Object); updateCmd.Console = consoleInfo.Console; // Act updateCmd.SelfUpdate("c:\foo.exe", new Version("2.0")); // Assert Assert.AreEqual("NuGet.exe is up to date.", consoleInfo.WrittenLines[0]); }
public void UpdatePackageUninstallsPackageAndInstallsNewPackage() { // Arrange var localRepository = new MockPackageRepository(); var sourceRepository = new MockPackageRepository(); var projectSystem = new MockProjectSystem(); PackageManager packageManager = new PackageManager(sourceRepository, new DefaultPackagePathResolver(projectSystem), projectSystem, localRepository); IPackage A10 = PackageUtility.CreatePackage("A", "1.0"); IPackage A20 = PackageUtility.CreatePackage("A", "2.0"); localRepository.Add(A10); sourceRepository.Add(A20); // Act packageManager.UpdatePackage("A", updateDependencies: true); // Assert Assert.IsFalse(localRepository.Exists("A", new Version("1.0"))); Assert.IsTrue(localRepository.Exists("A", new Version("2.0"))); }
public void SelfUpdateOlderVersionDoesNotUpdate() { // Arrange var factory = new Mock <IPackageRepositoryFactory>(); var repository = new MockPackageRepository(); repository.Add(PackageUtility.CreatePackage("NuGet.CommandLine", "1.0")); factory.Setup(m => m.CreateRepository(It.IsAny <string>())).Returns(repository); ConsoleInfo consoleInfo = GetConsoleInfo(); var selfUpdater = new SelfUpdater(factory.Object) { Console = consoleInfo.Console }; // Act selfUpdater.SelfUpdate("c:\foo.exe", new SemanticVersion("2.0")); // Assert Assert.Equal("NuGet.exe is up to date.", consoleInfo.WrittenLines[0]); }
public void SelfUpdateNewVersionUpdatesExe() { // Arrange var factory = new Mock<IPackageRepositoryFactory>(); var repository = new MockPackageRepository(); IPackage package = PackageUtility.CreatePackage("NuGet.CommandLine", "3.0", tools: new[] { "NuGet.exe" }); repository.Add(package); factory.Setup(m => m.CreateRepository(It.IsAny<string>())).Returns(repository); ConsoleInfo consoleInfo = GetConsoleInfo(); var selfUpdater = new MockSelfUpdater(factory.Object); selfUpdater.Console = consoleInfo.Console; // Act selfUpdater.SelfUpdate(@"c:\NuGet.exe", new SemanticVersion("2.0")); // Assert Assert.True(selfUpdater.MovedFiles.ContainsKey(@"c:\NuGet.exe")); Assert.Equal(@"c:\NuGet.exe.old", selfUpdater.MovedFiles[@"c:\NuGet.exe"]); Assert.True(selfUpdater.UpdatedFiles.ContainsKey(@"c:\NuGet.exe")); Assert.Equal(@"tools\NuGet.exe", selfUpdater.UpdatedFiles[@"c:\NuGet.exe"]); }
public void UpdatePackageAddsPackagesToSharedPackageRepositoryWhenReferencesAreAdded() { // Arrange var localRepository = new MockPackageRepository(); var sourceRepository = new MockPackageRepository(); var constraintProvider = NullConstraintProvider.Instance; var fileSystem = new MockFileSystem(); var pathResolver = new DefaultPackagePathResolver(fileSystem); var projectSystem = new MockProjectSystem(); var packages = new List<IPackage>(); var package_A10 = PackageUtility.CreatePackage("A", "1.0", content: new[] { "1.txt" }); var package_A12 = PackageUtility.CreatePackage("A", "1.2", content: new[] { "1.txt" }); localRepository.Add(package_A10); sourceRepository.Add(package_A12); var sharedRepository = new Mock<ISharedPackageRepository>(MockBehavior.Strict); sharedRepository.SetupSet(s => s.PackageSaveMode = PackageSaveModes.Nupkg); sharedRepository.Setup(s => s.AddPackage(package_A12)).Callback<IPackage>(p => packages.Add(p)).Verifiable(); sharedRepository.Setup(s => s.GetPackages()).Returns(packages.AsQueryable()); var repositoryFactory = new Mock<IPackageRepositoryFactory>(); repositoryFactory.Setup(s => s.CreateRepository(It.IsAny<string>())).Returns(sourceRepository); var packageSourceProvider = new Mock<IPackageSourceProvider>(); packageSourceProvider.Setup(s => s.LoadPackageSources()).Returns(new[] { new PackageSource("foo-source") }); var updateCommand = new UpdateCommand() { RepositoryFactory = repositoryFactory.Object, SourceProvider = packageSourceProvider.Object }; // Act updateCommand.UpdatePackages(localRepository, fileSystem, sharedRepository.Object, sourceRepository, constraintProvider, pathResolver, projectSystem); // Assert sharedRepository.Verify(); }
public void InstallSolutionLevelPackageAddEntryToSolutionPackagesConfig(bool allowPrerelease) { // Arrange // Source repository has A -> B, where B is a project-level package and A is a meta-package. // We want to make sure A is added to the packages.config of the project, and NOT packages.config of the solution var localRepository = new Mock<MockPackageRepository>() { CallBase = true }.As<ISharedPackageRepository>(); var sourceRepository = new MockPackageRepository(); var packageA = PackageUtility.CreatePackage("A", "2.0", tools: new[] { "install.ps1" }); sourceRepository.Add(packageA); var projectSystem = new MockProjectSystem(); var pathResolver = new DefaultPackagePathResolver(projectSystem); var packageManager = new VsPackageManager( TestUtils.GetSolutionManager(), sourceRepository, new Mock<IFileSystemProvider>().Object, projectSystem, localRepository.Object, new Mock<IRecentPackageRepository>().Object, new Mock<VsPackageInstallerEvents>().Object); var projectRepository = new MockProjectPackageRepository(localRepository.Object); var projectManager = new ProjectManager(localRepository.Object, pathResolver, projectSystem, projectRepository); localRepository.Setup(r => r.IsReferenced(It.IsAny<string>(), It.IsAny<SemanticVersion>())). Returns((string id, SemanticVersion version) => projectRepository.Exists(id, version)); // Act packageManager.InstallPackage( projectManager, "A", new SemanticVersion("2.0"), ignoreDependencies: false, allowPrereleaseVersions: allowPrerelease, logger: null); // Assert Assert.True(!projectRepository.Exists("A", new SemanticVersion("2.0"))); // assert that packages.config for solution-level is created. localRepository.Verify(r => r.AddPackageReferenceEntry("A", new SemanticVersion("2.0")), Times.Once()); }
public void CallingEnableCurrentSolutionDownloadPrereleasePackagesButDoNotUnlistedPackage() { // Arrange string tempSolutionPath = CreateTempFolder(); // setup SolutionManager var solutionManager = new Mock<ISolutionManager>(); solutionManager.Setup(p => p.IsSolutionOpen).Returns(true); solutionManager.Setup(p => p.SolutionDirectory).Returns(tempSolutionPath); // setup file system var fileSystem = new PhysicalFileSystem(tempSolutionPath); var fileSystemProvider = new Mock<IFileSystemProvider>(); fileSystemProvider.Setup(p => p.GetFileSystem(tempSolutionPath)).Returns(fileSystem); var nugetFolderFileSystem = new PhysicalFileSystem(tempSolutionPath + "\\.nuget"); fileSystemProvider.Setup(p => p.GetFileSystem(tempSolutionPath + "\\.nuget")).Returns(nugetFolderFileSystem); // default app settings var defaultAppSettings = new Mock<ISettings>(); defaultAppSettings.Setup(s => s.GetValue("packageRestore", "enabled")).Returns("false"); // setup DTE var dte = new Mock<DTE>(); var projectItems = new Mock<ProjectItems>(); var solutionFolder = new Mock<Project>(); solutionFolder.Setup(s => s.Name).Returns(".nuget"); solutionFolder.SetupGet(s => s.ProjectItems).Returns(projectItems.Object); var solution = new Mock<Solution>(); solution.As<Solution2>().Setup(p => p.AddSolutionFolder(".nuget")).Returns(solutionFolder.Object); var projects = new MockProjects(new Project[0]); solution.As<Solution2>().Setup(s => s.Projects).Returns(projects); dte.SetupGet(p => p.Solution).Returns(solution.Object); // setup package repository var packageRepository = new MockPackageRepository(); packageRepository.Add(PackageUtility.CreatePackage( "NuGet.Build", version: "1.0", tools: new string[] { "NuGet.targets" }, dependencies: new PackageDependency[] { new PackageDependency("NuGet.CommandLine") })); // this package contains 'invalid.targets' in the tools folder. // it shouldn't be installed because it is unlisted. packageRepository.Add(PackageUtility.CreatePackage( "NuGet.Build", version: "2.0", tools: new string[] { "invalid.targets" }, dependencies: new PackageDependency[] { new PackageDependency("NuGet.CommandLine") }, listed: false)); // this verify that we accepts prerelease packages packageRepository.Add(PackageUtility.CreatePackage( "NuGet.CommandLine", version: "1.0-alpha", tools: new string[] { "NuGet.exe" })); var packageRepositoryFactory = new Mock<IPackageRepositoryFactory>(); packageRepositoryFactory.Setup(p => p.CreateRepository(NuGetConstants.DefaultFeedUrl)).Returns(packageRepository); var packageSourceProvider = new Mock<IPackageSourceProvider>(); packageSourceProvider.Setup(p => p.LoadPackageSources()).Returns(new[] { new PackageSource(NuGetConstants.DefaultFeedUrl) }); var packageRestore = CreateInstance( dte.Object, solutionManager.Object, fileSystemProvider.Object, packageRepositoryFactory.Object, packageSourceProvider: packageSourceProvider.Object, settings: defaultAppSettings.Object); // Act packageRestore.EnableCurrentSolutionForRestore(fromActivation: false); // Assert // verify that the files are copied to the .nuget sub folder under solution Assert.True(Directory.Exists(Path.Combine(tempSolutionPath, ".nuget"))); Assert.True(File.Exists(Path.Combine(tempSolutionPath, ".nuget\\NuGet.exe"))); Assert.True(File.Exists(Path.Combine(tempSolutionPath, ".nuget\\NuGet.targets"))); // verify that solution folder 'nuget' is added to solution solution.As<Solution2>().Verify(p => p.AddSolutionFolder(".nuget")); projectItems.Verify(p => p.AddFromFile(tempSolutionPath + "\\.nuget\\NuGet.exe")); projectItems.Verify(p => p.AddFromFile(tempSolutionPath + "\\.nuget\\NuGet.targets")); // verify that the Source Control mode is disabled var settings = new Settings(nugetFolderFileSystem); Assert.True(settings.IsSourceControlDisabled()); // verify that package restore consent is not set defaultAppSettings.Verify( s => s.SetValue("packageRestore", "enabled", It.Is<string>(v => v == "true" || v == "1")), Times.Never()); }
public void ResolvingDependencyForUpdateWithConflictingDependents() { // Arrange var localRepository = new MockPackageRepository(); var sourceRepository = new MockPackageRepository(); // A 1.0 -> B [1.0] IPackage A10 = PackageUtility.CreatePackage("A", "1.0", dependencies: new List<PackageDependency> { PackageDependency.CreateDependency("B", "[1.0]") }, content: new[] { "a1" }); // A 2.0 -> B (any version) IPackage A20 = PackageUtility.CreatePackage("A", "2.0", dependencies: new List<PackageDependency> { new PackageDependency("B") }, content: new[] { "a2" }); IPackage B10 = PackageUtility.CreatePackage("B", "1.0", content: new[] { "b1" }); IPackage B101 = PackageUtility.CreatePackage("B", "1.0.1", content: new[] { "b101" }); IPackage B20 = PackageUtility.CreatePackage("B", "2.0", content: new[] { "a2" }); localRepository.Add(A10); localRepository.Add(B10); sourceRepository.AddPackage(A10); sourceRepository.AddPackage(A20); sourceRepository.AddPackage(B10); sourceRepository.AddPackage(B101); sourceRepository.AddPackage(B20); IPackageOperationResolver resolver = new UpdateWalker(localRepository, sourceRepository, new DependentsWalker(localRepository), NullConstraintProvider.Instance, NullLogger.Instance, updateDependencies: true, allowPrereleaseVersions: false) { AcceptedTargets = PackageTargets.Project }; // Act var packages = resolver.ResolveOperations(B101).ToList(); // Assert Assert.Equal(4, packages.Count); AssertOperation("A", "1.0", PackageAction.Uninstall, packages[0]); AssertOperation("B", "1.0", PackageAction.Uninstall, packages[1]); AssertOperation("A", "2.0", PackageAction.Install, packages[2]); AssertOperation("B", "1.0.1", PackageAction.Install, packages[3]); }
public void FindPackagesReturnsPackagesWithTermInPackageTagOrDescriptionOrId() { // Arrange var term = "TAG"; var repo = new MockPackageRepository(); repo.Add(CreateMockPackage("A", "1.0", "Description", " TAG ")); repo.Add(CreateMockPackage("B", "2.0", "Description", "Tags")); repo.Add(CreateMockPackage("C", "1.0", "This description has tags in it")); repo.Add(CreateMockPackage("D", "1.0", "Description")); repo.Add(CreateMockPackage("TagCloud", "1.0", "Description")); // Act var packages = repo.GetPackages().Find(term).ToList(); // Assert Assert.Equal(3, packages.Count); Assert.Equal("A", packages[0].Id); Assert.Equal("C", packages[1].Id); Assert.Equal("TagCloud", packages[2].Id); }
public void ReinstallPackagesSkipRestallingForOneProjectButProceedWithTheOther() { // Arrange var localRepositoryMock = new Mock<MockPackageRepository>() { CallBase = true }.As<ISharedPackageRepository>(); var localRepository = localRepositoryMock.Object; var sourceRepository = new MockPackageRepository(); var projectSystem1 = new MockProjectSystem(new FrameworkName(".NETFramework, Version=v3.0")); var pathResolver1 = new DefaultPackagePathResolver(projectSystem1); var packageReferenceRepository1 = new PackageReferenceRepository(projectSystem1, localRepository); var projectManager1 = new ProjectManager(localRepository, pathResolver1, projectSystem1, packageReferenceRepository1); var projectSystem2 = new MockProjectSystem(new FrameworkName(".NETCore, Version=v4.5")); var pathResolver2 = new DefaultPackagePathResolver(projectSystem2); var packageReferenceRepository2 = new PackageReferenceRepository(projectSystem2, localRepository); var projectManager2 = new ProjectManager(localRepository, pathResolver2, projectSystem2, packageReferenceRepository2); var project1 = TestUtils.GetProject("Project1"); var project2 = TestUtils.GetProject("Project2"); var packageManager = new MockVsPackageManager( TestUtils.GetSolutionManager(projects: new[] { project1, project2 }), sourceRepository, new Mock<IFileSystemProvider>().Object, projectSystem2, localRepository, new Mock<IDeleteOnRestartManager>().Object, new Mock<VsPackageInstallerEvents>().Object); packageManager.RegisterProjectManager(project1, projectManager1); packageManager.RegisterProjectManager(project2, projectManager2); var packageA = PackageUtility.CreatePackage( "A", "1.2-alpha", new[] { "net30\\content.txt", "silverlight40\\content4.txt" }, new[] { "lib\\net30\\ref.dll", "lib\\silverlight40\\refsl.dll" }); var packageB = PackageUtility.CreatePackage( "B", "2.0", new[] { "winrt45\\hello.txt", "sl4-wp71\\world.txt" }, new[] { "lib\\winrt45\\comma.dll", "lib\\sl4-wp71\\dude.dll" }); sourceRepository.Add(packageA); localRepository.AddPackage(packageA); localRepository.AddPackage(packageB); // install package A -> project 1 // and package B -> project 2 packageManager.InstallPackage(projectManager1, "A", new SemanticVersion("1.2-alpha"), ignoreDependencies: false, allowPrereleaseVersions: true, logger: null); packageManager.InstallPackage(projectManager2, "B", new SemanticVersion("2.0"), ignoreDependencies: false, allowPrereleaseVersions: true, logger: null); // now change project's target framework to silverlight projectSystem1.ChangeTargetFramework(new FrameworkName("Silverlight, Version=v4.0")); projectSystem2.ChangeTargetFramework(new FrameworkName("Silverlight, Version=v4.0, Profile=WindowsPhone71")); localRepositoryMock.Setup(p => p.IsReferenced("A", new SemanticVersion("1.2-alpha"))).Returns((string id, SemanticVersion version) => projectManager1.LocalRepository.Exists(id, version)); localRepositoryMock.Setup(p => p.IsReferenced("B", new SemanticVersion("2.0"))).Returns((string id, SemanticVersion version) => projectManager2.LocalRepository.Exists(id, version)); var logger = new Mock<ILogger>(); logger.Setup(s => s.Log( MessageLevel.Warning, "Skipped reinstalling package '{0}' in project '{1}' because the package does not exist in the package source.", "B 2.0", "Project2") ).Verifiable(); // Act packageManager.ReinstallPackages(updateDependencies: true, allowPrereleaseVersions: false, logger: logger.Object, eventListener: NullPackageOperationEventListener.Instance); // Assert logger.Verify(); Assert.True(packageManager.LocalRepository.Exists("A", new SemanticVersion("1.2-alpha"))); Assert.True(projectManager1.LocalRepository.Exists("A", new SemanticVersion("1.2-alpha"))); Assert.False(projectSystem1.FileExists("content.txt")); Assert.True(projectSystem1.FileExists("content4.txt")); Assert.False(projectSystem1.ReferenceExists("ref.dll")); Assert.True(projectSystem1.ReferenceExists("refsl.dll")); Assert.True(packageManager.LocalRepository.Exists("B", new SemanticVersion("2.0"))); Assert.True(projectManager2.LocalRepository.Exists("B", new SemanticVersion("2.0"))); Assert.True(projectSystem2.FileExists("hello.txt")); Assert.False(projectSystem2.FileExists("world.txt")); Assert.True(projectSystem2.ReferenceExists("comma.dll")); Assert.False(projectSystem2.ReferenceExists("dude.dll")); }
public void ReinstallPackagesDoesNotThrowWithNewDependencyWhenProjectFrameworkChangesIfAllowPrereleaseParameterIsFalseAndPackageVersionIsPrerelease() { // Arrange var localRepository = new Mock<MockPackageRepository>() { CallBase = true }.As<ISharedPackageRepository>().Object; var sourceRepository = new MockPackageRepository(); var projectSystem = new MockProjectSystem(new FrameworkName(".NETFramework, Version=v4.0")); var pathResolver = new DefaultPackagePathResolver(projectSystem); var packageReferenceRepository = new PackageReferenceRepository(projectSystem, localRepository); var projectManager = new ProjectManager(localRepository, pathResolver, projectSystem, packageReferenceRepository); var packageManager = new VsPackageManager( TestUtils.GetSolutionManager(), sourceRepository, new Mock<IFileSystemProvider>().Object, projectSystem, localRepository, new Mock<IDeleteOnRestartManager>().Object, new Mock<VsPackageInstallerEvents>().Object); var packageA = PackageUtility.CreatePackageWithDependencySets( "A", "1.2-alpha", new[] { "contentA.txt" }, dependencySets: new PackageDependencySet[] { new PackageDependencySet(new FrameworkName(".NETFramework, Version=v4.0"), new [] { new PackageDependency("B")}), new PackageDependencySet(new FrameworkName("Silverlight, Version=v5.0"), new [] { new PackageDependency("C")}) }); var packageB = PackageUtility.CreatePackage( "B", "1.0", new[] { "contentB.txt" }); var packageC = PackageUtility.CreatePackage( "C", "2.0-beta", new[] { "contentC.txt" }); sourceRepository.Add(packageA); sourceRepository.Add(packageB); sourceRepository.Add(packageC); packageManager.InstallPackage(projectManager, "A", new SemanticVersion("1.2-alpha"), ignoreDependencies: false, allowPrereleaseVersions: true, logger: null); Assert.True(packageManager.LocalRepository.Exists("A")); Assert.True(projectManager.LocalRepository.Exists("A")); Assert.True(packageManager.LocalRepository.Exists("B")); Assert.True(projectManager.LocalRepository.Exists("B")); Assert.False(packageManager.LocalRepository.Exists("C")); Assert.False(projectManager.LocalRepository.Exists("C")); Assert.True(projectSystem.FileExists("contentA.txt")); Assert.True(projectSystem.FileExists("contentB.txt")); Assert.False(projectSystem.FileExists("contentC.txt")); // now change project's target framework to silverilght projectSystem.ChangeTargetFramework(new FrameworkName("Silverlight, Version=v5.0")); // Act packageManager.ReinstallPackage(projectManager, "A", updateDependencies: true, allowPrereleaseVersions: false, logger: null); // Assert Assert.True(packageManager.LocalRepository.Exists("A")); Assert.True(projectManager.LocalRepository.Exists("A")); Assert.False(packageManager.LocalRepository.Exists("B")); Assert.False(projectManager.LocalRepository.Exists("B")); Assert.True(packageManager.LocalRepository.Exists("C")); Assert.True(projectManager.LocalRepository.Exists("C")); }
public void ReinstallPackagesWithDependenciesSkipIfDependencyPackageIsMissingFromSource() { // Arrange var localRepository = new Mock<MockPackageRepository>() { CallBase = true }.As<ISharedPackageRepository>().Object; var sourceRepository = new MockPackageRepository(); var projectSystem = new MockProjectSystem(); var pathResolver = new DefaultPackagePathResolver(projectSystem); var projectManager = new ProjectManager(localRepository, pathResolver, projectSystem, new MockPackageRepository()); var packageManager = new VsPackageManager( TestUtils.GetSolutionManager(), sourceRepository, new Mock<IFileSystemProvider>().Object, projectSystem, localRepository, new Mock<IDeleteOnRestartManager>().Object, new Mock<VsPackageInstallerEvents>().Object); var packageA = PackageUtility.CreatePackage("A", "1.2-alpha", new[] { "content.txt" }, dependencies: new[] { new PackageDependency("B") }); var packageB = PackageUtility.CreatePackage("B", "2.0.0", new[] { "hello.txt" }); sourceRepository.Add(packageA); localRepository.AddPackage(packageA); projectManager.LocalRepository.AddPackage(packageA); //sourceRepository.Add(packageB); localRepository.AddPackage(packageB); projectManager.LocalRepository.AddPackage(packageB); var logger = new Mock<ILogger>(); logger.Setup(s => s.Log( MessageLevel.Warning, "Skipped reinstalling package '{0}' in project '{1}' because the package does not exist in the package source.", "B 2.0.0", "x:\\MockFileSystem") ).Verifiable(); // Act packageManager.ReinstallPackages(projectManager, updateDependencies: false, allowPrereleaseVersions: false, logger: logger.Object); // Assert logger.Verify(); Assert.True(packageManager.LocalRepository.Exists("A", new SemanticVersion("1.2-alpha"))); Assert.True(projectManager.LocalRepository.Exists("A", new SemanticVersion("1.2-alpha"))); Assert.True(packageManager.LocalRepository.Exists("B", new SemanticVersion("2.0.0"))); Assert.True(projectManager.LocalRepository.Exists("B", new SemanticVersion("2.0.0"))); }
public void CallingEnableCurrentSolutionDoNotDownloadPackageIfPresentInLocalCache() { // Arrange string tempSolutionPath = CreateTempFolder(); // setup SolutionManager var solutionManager = new Mock <ISolutionManager>(); solutionManager.Setup(p => p.IsSolutionOpen).Returns(true); solutionManager.Setup(p => p.SolutionDirectory).Returns(tempSolutionPath); // setup file system var fileSystem = new PhysicalFileSystem(tempSolutionPath); var fileSystemProvider = new Mock <IFileSystemProvider>(MockBehavior.Strict); fileSystemProvider.Setup(p => p.GetFileSystem(tempSolutionPath)).Returns(fileSystem); var nugetFolderFileSystem = new PhysicalFileSystem(tempSolutionPath + "\\.nuget"); fileSystemProvider.Setup(p => p.GetFileSystem(tempSolutionPath + "\\.nuget")).Returns(nugetFolderFileSystem); // setup DTE var dte = new Mock <DTE>(); var projectItems = new Mock <ProjectItems>(); var solutionFolder = new Mock <Project>(); solutionFolder.Setup(s => s.Name).Returns(".nuget"); solutionFolder.SetupGet(s => s.ProjectItems).Returns(projectItems.Object); var solution = new Mock <Solution>(); solution.As <Solution2>().Setup(p => p.AddSolutionFolder(".nuget")).Returns(solutionFolder.Object); var projects = new MockProjects(new Project[0]); solution.As <Solution2>().Setup(s => s.Projects).Returns(projects); dte.SetupGet(p => p.Solution).Returns(solution.Object); // setup package repository var packageRepository = new MockPackageRepository(); var packageA = new Mock <IPackage>(MockBehavior.Strict); packageA.Setup(p => p.Id).Returns("NuGet.Build"); packageA.Setup(p => p.Version).Returns(new SemanticVersion("1.0")); packageA.Setup(p => p.IsLatestVersion).Returns(true); packageA.Setup(p => p.Listed).Returns(true); var packageB = new Mock <IPackage>(MockBehavior.Strict); packageB.Setup(p => p.Id).Returns("NuGet.CommandLine"); packageB.Setup(p => p.Version).Returns(new SemanticVersion("2.0")); packageB.Setup(p => p.IsLatestVersion).Returns(true); packageB.Setup(p => p.Listed).Returns(true); packageRepository.AddPackage(packageA.Object); packageRepository.AddPackage(packageB.Object); var packageRepositoryFactory = new Mock <IPackageRepositoryFactory>(MockBehavior.Strict); packageRepositoryFactory.Setup(p => p.CreateRepository("x:\\nugetsource")).Returns(packageRepository); var packageSourceProvider = new Mock <IVsPackageSourceProvider>(); packageSourceProvider.Setup(p => p.LoadPackageSources()).Returns(new[] { new PackageSource("x:\\nugetsource"), new PackageSource("y:\\me", "unabled", isEnabled: false), }); var localCache = new MockPackageRepository(); localCache.Add(PackageUtility.CreatePackage( "NuGet.Build", version: "1.0", tools: new string[] { "NuGet.targets" }, dependencies: new PackageDependency[] { new PackageDependency("NuGet.CommandLine") }, listed: true)); localCache.Add(PackageUtility.CreatePackage( "NuGet.CommandLine", version: "2.0", tools: new string[] { "NuGet.exe" }, listed: true)); var packageRestore = CreateInstance( dte.Object, solutionManager.Object, fileSystemProvider.Object, packageRepositoryFactory.Object, localCache: localCache, packageSourceProvider: packageSourceProvider.Object); // Act packageRestore.EnableCurrentSolutionForRestore(fromActivation: false); // Assert packageA.Verify(p => p.GetFiles(), Times.Never()); packageB.Verify(p => p.GetFiles(), Times.Never()); }
public void CallingEnableCurrentSolutionDownloadPrereleasePackagesButDoNotUnlistedPackage() { // Arrange string tempSolutionPath = CreateTempFolder(); // setup SolutionManager var solutionManager = new Mock <ISolutionManager>(); solutionManager.Setup(p => p.IsSolutionOpen).Returns(true); solutionManager.Setup(p => p.SolutionDirectory).Returns(tempSolutionPath); // setup file system var fileSystem = new PhysicalFileSystem(tempSolutionPath); var fileSystemProvider = new Mock <IFileSystemProvider>(); fileSystemProvider.Setup(p => p.GetFileSystem(tempSolutionPath)).Returns(fileSystem); var nugetFolderFileSystem = new PhysicalFileSystem(tempSolutionPath + "\\.nuget"); fileSystemProvider.Setup(p => p.GetFileSystem(tempSolutionPath + "\\.nuget")).Returns(nugetFolderFileSystem); // default app settings var defaultAppSettings = new Mock <ISettings>(); defaultAppSettings.Setup(s => s.GetValue("packageRestore", "enabled")).Returns("false"); // setup DTE var dte = new Mock <DTE>(); var projectItems = new Mock <ProjectItems>(); var solutionFolder = new Mock <Project>(); solutionFolder.Setup(s => s.Name).Returns(".nuget"); solutionFolder.SetupGet(s => s.ProjectItems).Returns(projectItems.Object); var solution = new Mock <Solution>(); solution.As <Solution2>().Setup(p => p.AddSolutionFolder(".nuget")).Returns(solutionFolder.Object); var projects = new MockProjects(new Project[0]); solution.As <Solution2>().Setup(s => s.Projects).Returns(projects); dte.SetupGet(p => p.Solution).Returns(solution.Object); // setup package repository var packageRepository = new MockPackageRepository(); packageRepository.Add(PackageUtility.CreatePackage( "NuGet.Build", version: "1.0", tools: new string[] { "NuGet.targets" }, dependencies: new PackageDependency[] { new PackageDependency("NuGet.CommandLine") })); // this package contains 'invalid.targets' in the tools folder. // it shouldn't be installed because it is unlisted. packageRepository.Add(PackageUtility.CreatePackage( "NuGet.Build", version: "2.0", tools: new string[] { "invalid.targets" }, dependencies: new PackageDependency[] { new PackageDependency("NuGet.CommandLine") }, listed: false)); // this verify that we accepts prerelease packages packageRepository.Add(PackageUtility.CreatePackage( "NuGet.CommandLine", version: "1.0-alpha", tools: new string[] { "NuGet.exe" })); var packageRepositoryFactory = new Mock <IPackageRepositoryFactory>(); packageRepositoryFactory.Setup(p => p.CreateRepository(NuGetConstants.DefaultFeedUrl)).Returns(packageRepository); var packageSourceProvider = new Mock <IVsPackageSourceProvider>(); packageSourceProvider.Setup(p => p.LoadPackageSources()).Returns(new[] { new PackageSource(NuGetConstants.DefaultFeedUrl) }); var packageRestore = CreateInstance( dte.Object, solutionManager.Object, fileSystemProvider.Object, packageRepositoryFactory.Object, packageSourceProvider: packageSourceProvider.Object, settings: defaultAppSettings.Object); // Act packageRestore.EnableCurrentSolutionForRestore(fromActivation: false); // Assert // verify that the files are copied to the .nuget sub folder under solution Assert.True(Directory.Exists(Path.Combine(tempSolutionPath, ".nuget"))); Assert.True(File.Exists(Path.Combine(tempSolutionPath, ".nuget\\NuGet.exe"))); Assert.True(File.Exists(Path.Combine(tempSolutionPath, ".nuget\\NuGet.targets"))); // verify that solution folder 'nuget' is added to solution solution.As <Solution2>().Verify(p => p.AddSolutionFolder(".nuget")); projectItems.Verify(p => p.AddFromFile(tempSolutionPath + "\\.nuget\\NuGet.exe")); projectItems.Verify(p => p.AddFromFile(tempSolutionPath + "\\.nuget\\NuGet.targets")); // verify that the Source Control mode is disabled var settings = new Settings(nugetFolderFileSystem); Assert.True(settings.IsSourceControlDisabled()); // verify that package restore consent is not set defaultAppSettings.Verify( s => s.SetValue("packageRestore", "enabled", It.Is <string>(v => v == "true" || v == "1")), Times.Never()); }
public void ExecuteMethodInstallPackagesWithInitScript() { // source repo has A, B, C // solution repo has A // project repo has C // install B // Arrange var packageA = PackageUtility.CreatePackage("A", "1.0"); var packageB = PackageUtility.CreatePackage("B", "2.0", content: new string[] { "hello world" }, tools: new string[] { "init.ps1" }); var packageC = PackageUtility.CreatePackage("C", "3.0"); var solutionRepository = new MockPackageRepository(); solutionRepository.AddPackage(packageA); var sourceRepository = new MockPackageRepository(); sourceRepository.AddPackage(packageA); sourceRepository.AddPackage(packageB); sourceRepository.AddPackage(packageC); var localRepository = new MockPackageRepository(); localRepository.Add(packageC); var projectManager = CreateProjectManager(localRepository, solutionRepository); var project = new Mock<Project>(); project.Setup(p => p.Properties.Item("TargetFrameworkMoniker").Value).Returns(".NETFramework, Version=4.0"); var scriptExecutor = new Mock<IScriptExecutor>(); var packageManager = new Mock<IVsPackageManager>(); packageManager.Setup(p => p.SourceRepository).Returns(sourceRepository); packageManager.Setup(p => p.LocalRepository).Returns(solutionRepository); packageManager.Setup(p => p.InstallPackage(projectManager, packageB, It.IsAny<IEnumerable<PackageOperation>>(), false, false, It.IsAny<ILogger>())). Raises(p => p.PackageInstalled += null, packageManager, new PackageOperationEventArgs(packageB, null, "")); packageManager.Setup(p => p.GetProjectManager(It.Is<Project>(s => s == project.Object))).Returns(projectManager); var solutionManager = new Mock<ISolutionManager>(); solutionManager.Setup(s => s.GetProject(It.IsAny<string>())).Returns(project.Object); var provider = CreateOnlineProvider(packageManager.Object, null, null, null, project.Object, scriptExecutor.Object, solutionManager.Object); var extensionTree = provider.ExtensionsTree; var firstTreeNode = (SimpleTreeNode)extensionTree.Nodes[0]; firstTreeNode.Repository.AddPackage(packageA); firstTreeNode.Repository.AddPackage(packageB); firstTreeNode.Repository.AddPackage(packageC); provider.SelectedNode = firstTreeNode; ManualResetEvent manualEvent = new ManualResetEvent(false); provider.ExecuteCompletedCallback = delegate { try { // Assert // init.ps1 should be executed scriptExecutor.Verify(p => p.Execute(It.IsAny<string>(), PowerShellScripts.Init, packageB, null, null, It.IsAny<ILogger>()), Times.Once()); // InstallPackage() should get called packageManager.Verify(p => p.InstallPackage( projectManager, It.IsAny<IPackage>(), It.IsAny<IEnumerable<PackageOperation>>(), false, false, It.IsAny<ILogger>()), Times.Once()); } finally { manualEvent.Set(); } }; var extensionB = new PackageItem(provider, packageB); // Act provider.Execute(extensionB); // do not allow the method to return manualEvent.WaitOne(); }
public void ExecuteMethodInvokesScript() { // source repo has A, B, C // solution repo has A // project repo has C // install B // Arrange var packageA = PackageUtility.CreatePackage("A", "1.0"); var packageB = PackageUtility.CreatePackage("B", "2.0", content: new string[] { "hello world" }, tools: new string[] { "install.ps1", "uninstall.ps1" }); var packageC = PackageUtility.CreatePackage("C", "3.0"); var solutionRepository = new MockPackageRepository(); solutionRepository.AddPackage(packageA); var sourceRepository = new MockPackageRepository(); sourceRepository.AddPackage(packageA); sourceRepository.AddPackage(packageB); sourceRepository.AddPackage(packageC); var localRepository = new MockPackageRepository(); localRepository.Add(packageC); var projectManager = CreateProjectManager(localRepository, solutionRepository); var project = new Mock<Project>(); var scriptExecutor = new Mock<IScriptExecutor>(); var packageManager = new Mock<IVsPackageManager>(); packageManager.Setup(p => p.SourceRepository).Returns(sourceRepository); packageManager.Setup(p => p.LocalRepository).Returns(solutionRepository); packageManager.Setup(p => p.InstallPackage( projectManager, It.IsAny<IPackage>(), It.IsAny<IEnumerable<PackageOperation>>(), false, It.IsAny<ILogger>())).Callback( () => { solutionRepository.AddPackage(packageB); projectManager.AddPackageReference(packageB.Id, packageB.Version); }); var provider = CreateOnlineProvider(packageManager.Object, projectManager, null, null, project.Object, scriptExecutor.Object); var extensionTree = provider.ExtensionsTree; var firstTreeNode = (SimpleTreeNode)extensionTree.Nodes[0]; firstTreeNode.Repository.AddPackage(packageA); firstTreeNode.Repository.AddPackage(packageB); firstTreeNode.Repository.AddPackage(packageC); provider.SelectedNode = firstTreeNode; ManualResetEvent manualEvent = new ManualResetEvent(false); provider.ExecuteCompletedCallback = delegate { try { // Assert scriptExecutor.Verify(p => p.Execute(It.IsAny<string>(), PowerShellScripts.Install, packageB, project.Object, It.IsAny<ILogger>()), Times.Once()); } finally { manualEvent.Set(); } }; var extensionB = new PackageItem(provider, packageB, null); // Act provider.Execute(extensionB); // do not allow the method to return manualEvent.WaitOne(); }
public void CallingEnableCurrentSolutionSetupEverythingCorrectly() { // Arrange string tempSolutionPath = CreateTempFolder(); // setup SolutionManager var solutionManager = new Mock<ISolutionManager>(); solutionManager.Setup(p => p.IsSolutionOpen).Returns(true); solutionManager.Setup(p => p.SolutionDirectory).Returns(tempSolutionPath); // setup file system var fileSystem = new PhysicalFileSystem(tempSolutionPath); var fileSystemProvider = new Mock<IFileSystemProvider>(); fileSystemProvider.Setup(p => p.GetFileSystem(tempSolutionPath)).Returns(fileSystem); var nugetFolderFileSystem = new PhysicalFileSystem(tempSolutionPath + "\\.nuget"); fileSystemProvider.Setup(p => p.GetFileSystem(tempSolutionPath + "\\.nuget")).Returns(nugetFolderFileSystem); // setup DTE var dte = new Mock<DTE>(); var projectItems = new Mock<ProjectItems>(); var solutionFolder = new Mock<Project>(); solutionFolder.Setup(s => s.Name).Returns(".nuget"); solutionFolder.SetupGet(s => s.ProjectItems).Returns(projectItems.Object); var solution = new Mock<Solution>(); solution.As<Solution2>().Setup(p => p.AddSolutionFolder(".nuget")).Returns(solutionFolder.Object); var projects = new MockProjects(new Project[0]); solution.As<Solution2>().Setup(s => s.Projects).Returns(projects); dte.SetupGet(p => p.Solution).Returns(solution.Object); // setup package repository var packageRepository = new MockPackageRepository(); packageRepository.Add(PackageUtility.CreatePackage( "NuGet.Build", version: "1.0", tools: new string[] { "NuGet.targets" }, dependencies: new PackageDependency[] { new PackageDependency("NuGet.CommandLine") })); packageRepository.Add(PackageUtility.CreatePackage( "NuGet.CommandLine", version: "1.0", tools: new string[] { "NuGet.exe" })); var packageRepositoryFactory = new Mock<IPackageRepositoryFactory>(); packageRepositoryFactory.Setup(p => p.CreateRepository(NuGetConstants.DefaultFeedUrl)).Returns(packageRepository); var packageRestore = CreateInstance( dte.Object, solutionManager.Object, fileSystemProvider.Object, packageRepositoryFactory.Object); // Act packageRestore.EnableCurrentSolutionForRestore(quietMode: true); // Assert // verify that the files are copied to the .nuget sub folder under solution Assert.True(Directory.Exists(Path.Combine(tempSolutionPath, ".nuget"))); Assert.True(File.Exists(Path.Combine(tempSolutionPath, ".nuget\\NuGet.exe"))); Assert.True(File.Exists(Path.Combine(tempSolutionPath, ".nuget\\NuGet.targets"))); // verify that solution folder 'nuget' is added to solution solution.As<Solution2>().Verify(p => p.AddSolutionFolder(".nuget")); projectItems.Verify(p => p.AddFromFile(tempSolutionPath + "\\.nuget\\NuGet.exe")); projectItems.Verify(p => p.AddFromFile(tempSolutionPath + "\\.nuget\\NuGet.targets")); // verify that the Source Control mode is disabled var settings = new Settings(nugetFolderFileSystem); Assert.True(settings.IsSourceControlDisabled()); // clean up Directory.Delete(tempSolutionPath, recursive: true); }
public void ExecuteMethodInstallPackagesWithInitScript() { // source repo has A, B, C // solution repo has A // project repo has C // install B // Arrange var packageA = PackageUtility.CreatePackage("A", "1.0"); var packageB = PackageUtility.CreatePackage("B", "2.0", content: new string[] { "hello world" }, tools: new string[] { "init.ps1" }); var packageC = PackageUtility.CreatePackage("C", "3.0"); var solutionRepository = new MockPackageRepository(); solutionRepository.AddPackage(packageA); var sourceRepository = new MockPackageRepository(); sourceRepository.AddPackage(packageA); sourceRepository.AddPackage(packageB); sourceRepository.AddPackage(packageC); var localRepository = new MockPackageRepository(); localRepository.Add(packageC); var projectManager = CreateProjectManager(localRepository, solutionRepository); var project = new Mock <Project>(); project.Setup(p => p.Properties.Item("TargetFrameworkMoniker").Value).Returns(".NETFramework, Version=4.0"); var scriptExecutor = new Mock <IScriptExecutor>(); var packageManager = new Mock <IVsPackageManager>(); packageManager.Setup(p => p.SourceRepository).Returns(sourceRepository); packageManager.Setup(p => p.LocalRepository).Returns(solutionRepository); packageManager.Setup(p => p.InstallPackage(projectManager, packageB, It.IsAny <IEnumerable <PackageOperation> >(), false, false, It.IsAny <ILogger>())). Raises(p => p.PackageInstalled += null, packageManager, new PackageOperationEventArgs(packageB, null, "")); packageManager.Setup(p => p.GetProjectManager(It.Is <Project>(s => s == project.Object))).Returns(projectManager); var solutionManager = new Mock <ISolutionManager>(); solutionManager.Setup(s => s.GetProject(It.IsAny <string>())).Returns(project.Object); var provider = CreateOnlineProvider(packageManager.Object, null, null, null, project.Object, scriptExecutor.Object, solutionManager.Object); var extensionTree = provider.ExtensionsTree; var firstTreeNode = (SimpleTreeNode)extensionTree.Nodes[0]; firstTreeNode.Repository.AddPackage(packageA); firstTreeNode.Repository.AddPackage(packageB); firstTreeNode.Repository.AddPackage(packageC); provider.SelectedNode = firstTreeNode; ManualResetEvent manualEvent = new ManualResetEvent(false); provider.ExecuteCompletedCallback = delegate { try { // Assert // init.ps1 should be executed scriptExecutor.Verify(p => p.Execute(It.IsAny <string>(), PowerShellScripts.Init, packageB, null, null, It.IsAny <ILogger>()), Times.Once()); // InstallPackage() should get called packageManager.Verify(p => p.InstallPackage( projectManager, It.IsAny <IPackage>(), It.IsAny <IEnumerable <PackageOperation> >(), false, false, It.IsAny <ILogger>()), Times.Once()); } finally { manualEvent.Set(); } }; var extensionB = new PackageItem(provider, packageB); // Act provider.Execute(extensionB); // do not allow the method to return manualEvent.WaitOne(); }
public void CallingEnableCurrentSolutionWillAddPackagesToMachineCache() { // Arrange string tempSolutionPath = CreateTempFolder(); // setup SolutionManager var solutionManager = new Mock <ISolutionManager>(); solutionManager.Setup(p => p.IsSolutionOpen).Returns(true); solutionManager.Setup(p => p.SolutionDirectory).Returns(tempSolutionPath); // setup file system var fileSystem = new PhysicalFileSystem(tempSolutionPath); var fileSystemProvider = new Mock <IFileSystemProvider>(); fileSystemProvider.Setup(p => p.GetFileSystem(tempSolutionPath)).Returns(fileSystem); var nugetFolderFileSystem = new PhysicalFileSystem(tempSolutionPath + "\\.nuget"); fileSystemProvider.Setup(p => p.GetFileSystem(tempSolutionPath + "\\.nuget")).Returns(nugetFolderFileSystem); // setup DTE var dte = new Mock <DTE>(); var projectItems = new Mock <ProjectItems>(); var solutionFolder = new Mock <Project>(); solutionFolder.Setup(s => s.Name).Returns(".nuget"); solutionFolder.SetupGet(s => s.ProjectItems).Returns(projectItems.Object); var solution = new Mock <Solution>(); solution.As <Solution2>().Setup(p => p.AddSolutionFolder(".nuget")).Returns(solutionFolder.Object); var projects = new MockProjects(new Project[0]); solution.As <Solution2>().Setup(s => s.Projects).Returns(projects); dte.SetupGet(p => p.Solution).Returns(solution.Object); // setup package repository var packageRepository = new MockPackageRepository(); packageRepository.Add(PackageUtility.CreatePackage( "NuGet.Build", version: "1.0", tools: new string[] { "NuGet.targets" }, dependencies: new PackageDependency[] { new PackageDependency("NuGet.CommandLine") })); packageRepository.Add(PackageUtility.CreatePackage( "NuGet.CommandLine", version: "2.0", tools: new string[] { "NuGet.exe" })); var packageRepositoryFactory = new Mock <IPackageRepositoryFactory>(); packageRepositoryFactory.Setup(p => p.CreateRepository("x:\\nugetsource")).Returns(packageRepository); var packageSourceProvider = new Mock <IVsPackageSourceProvider>(); packageSourceProvider.Setup(p => p.LoadPackageSources()).Returns(new[] { new PackageSource("x:\\nugetsource") }); var localCache = new MockPackageRepository(); var packageRestore = CreateInstance( dte.Object, solutionManager.Object, fileSystemProvider.Object, packageRepositoryFactory.Object, localCache: localCache, packageSourceProvider: packageSourceProvider.Object); // Act packageRestore.EnableCurrentSolutionForRestore(fromActivation: false); // Assert var cachePackages = localCache.GetPackages().ToList(); Assert.Equal(2, cachePackages.Count); Assert.Equal("NuGet.Build", cachePackages[0].Id); Assert.Equal(new SemanticVersion("1.0"), cachePackages[0].Version); Assert.Equal("NuGet.CommandLine", cachePackages[1].Id); Assert.Equal(new SemanticVersion("2.0"), cachePackages[1].Version); }
public void EnsurePackageDownloadsUsesMachineCacheIfAvailable() { // Arrange var hashBytes = new byte[] { 1, 2, 3, 4 }; var hash = Convert.ToBase64String(hashBytes); var zipPackage = PackageUtility.CreatePackage("A", "1.2"); var hashProvider = new Mock<IHashProvider>(MockBehavior.Strict); hashProvider.Setup(h => h.CalculateHash(It.IsAny<byte[]>())).Returns(hashBytes); var mockRepository = new MockPackageRepository(); mockRepository.Add(zipPackage); var servicePackage = new DataServicePackage { Id = "A", Version = "1.2", PackageHash = hash, HashProvider = hashProvider.Object, }; // Act servicePackage.EnsurePackage(mockRepository); // Assert Assert.Equal(zipPackage, servicePackage._package); }
public void UpdatePackageThrowsIfPackageNotInstalled() { // Arrange var localRepository = new MockPackageRepository(); var sourceRepository = new MockPackageRepository(); var projectSystem = new MockProjectSystem(); PackageManager packageManager = new PackageManager(sourceRepository, new DefaultPackagePathResolver(projectSystem), projectSystem, localRepository); IPackage A20 = PackageUtility.CreatePackage("A", "2.0"); sourceRepository.Add(A20); // Act ExceptionAssert.Throws<InvalidOperationException>(() => packageManager.UpdatePackage("A", updateDependencies: true, allowPrereleaseVersions: false), "Unable to find package 'A'."); }
public void ReinstallPackagesRestoresAllPackagesInAProjectWithNewContentIfProjectFrameworkChanges() { // Arrange var localRepository = new Mock<MockPackageRepository>() { CallBase = true }.As<ISharedPackageRepository>().Object; var sourceRepository = new MockPackageRepository(); var projectSystem = new MockProjectSystem(new FrameworkName(".NETFramework, Version=v3.0")); var pathResolver = new DefaultPackagePathResolver(projectSystem); var packageReferenceRepository = new PackageReferenceRepository(projectSystem, localRepository); var projectManager = new ProjectManager(localRepository, pathResolver, projectSystem, packageReferenceRepository); var packageManager = new VsPackageManager( TestUtils.GetSolutionManager(), sourceRepository, new Mock<IFileSystemProvider>().Object, projectSystem, localRepository, new Mock<IDeleteOnRestartManager>().Object, new Mock<VsPackageInstallerEvents>().Object); var packageA = PackageUtility.CreatePackage( "A", "1.2-alpha", new[] { "net30\\content.txt", "silverlight40\\content4.txt" }, new[] { "lib\\net30\\ref.dll", "lib\\silverlight40\\refsl.dll" }); var packageB = PackageUtility.CreatePackage( "B", "2.0", new[] { "net30\\contentB.txt", "silverlight40\\content4B.txt" }, new[] { "lib\\net30\\refB.dll", "lib\\silverlight40\\refslB.dll" }); sourceRepository.Add(packageA); sourceRepository.Add(packageB); packageManager.InstallPackage(projectManager, "A", new SemanticVersion("1.2-alpha"), ignoreDependencies: false, allowPrereleaseVersions: true, logger: null); packageManager.InstallPackage(projectManager, "B", new SemanticVersion("2.0"), ignoreDependencies: false, allowPrereleaseVersions: true, logger: null); Assert.True(packageManager.LocalRepository.Exists("A", new SemanticVersion("1.2-alpha"))); Assert.True(projectManager.LocalRepository.Exists("A", new SemanticVersion("1.2-alpha"))); Assert.True(projectSystem.FileExists("content.txt")); Assert.False(projectSystem.FileExists("content4.txt")); Assert.True(projectSystem.ReferenceExists("ref.dll")); Assert.False(projectSystem.ReferenceExists("refsl.dll")); Assert.True(packageManager.LocalRepository.Exists("B")); Assert.True(projectManager.LocalRepository.Exists("B")); Assert.True(projectSystem.FileExists("contentB.txt")); Assert.False(projectSystem.FileExists("content4B.txt")); Assert.True(projectSystem.ReferenceExists("refB.dll")); Assert.False(projectSystem.ReferenceExists("refslB.dll")); // now change project's target framework to silverlight projectSystem.ChangeTargetFramework(new FrameworkName("Silverlight, Version=v4.0")); // Act packageManager.ReinstallPackages(projectManager, updateDependencies: true, allowPrereleaseVersions: false, logger: null); // Assert Assert.True(packageManager.LocalRepository.Exists("A", new SemanticVersion("1.2-alpha"))); Assert.True(projectManager.LocalRepository.Exists("A", new SemanticVersion("1.2-alpha"))); Assert.False(projectSystem.FileExists("content.txt")); Assert.True(projectSystem.FileExists("content4.txt")); Assert.False(projectSystem.ReferenceExists("ref.dll")); Assert.True(projectSystem.ReferenceExists("refsl.dll")); Assert.True(packageManager.LocalRepository.Exists("B")); Assert.True(projectManager.LocalRepository.Exists("B")); Assert.False(projectSystem.FileExists("contentB.txt")); Assert.True(projectSystem.FileExists("content4B.txt")); Assert.False(projectSystem.ReferenceExists("refB.dll")); Assert.True(projectSystem.ReferenceExists("refslB.dll")); }
public void UpdatePackageDoesNothingIfNoUpdatesAvailable() { // Arrange var localRepository = new MockPackageRepository(); var sourceRepository = new MockPackageRepository(); var projectSystem = new MockProjectSystem(); PackageManager packageManager = new PackageManager(sourceRepository, new DefaultPackagePathResolver(projectSystem), projectSystem, localRepository); IPackage A10 = PackageUtility.CreatePackage("A", "1.0"); localRepository.Add(A10); // Act packageManager.UpdatePackage("A", updateDependencies: true, allowPrereleaseVersions: false); // Assert Assert.True(localRepository.Exists("A", new SemanticVersion("1.0"))); }
public void ReinstallPackagesRestoresPackageInAllProjectsWithNewContentIfSourcePackageChanges() { // Arrange var localRepositoryMock = new Mock<MockPackageRepository>() { CallBase = true }.As<ISharedPackageRepository>(); var localRepository = localRepositoryMock.Object; var sourceRepository = new MockPackageRepository(); var projectSystem1 = new MockProjectSystem(); var pathResolver1 = new DefaultPackagePathResolver(projectSystem1); var packageReferenceRepository1 = new PackageReferenceRepository(projectSystem1, localRepository); var projectManager1 = new ProjectManager(localRepository, pathResolver1, projectSystem1, packageReferenceRepository1); var projectSystem2 = new MockProjectSystem(); var pathResolver2 = new DefaultPackagePathResolver(projectSystem2); var packageReferenceRepository2 = new PackageReferenceRepository(projectSystem2, localRepository); var projectManager2 = new ProjectManager(localRepository, pathResolver2, projectSystem2, packageReferenceRepository2); var project1 = TestUtils.GetProject("Project1"); var project2 = TestUtils.GetProject("Project2"); var packageManager = new MockVsPackageManager( TestUtils.GetSolutionManager(projects: new[] { project1, project2 }), sourceRepository, new Mock<IFileSystemProvider>().Object, projectSystem2, localRepository, new Mock<IDeleteOnRestartManager>().Object, new Mock<VsPackageInstallerEvents>().Object); packageManager.RegisterProjectManager(project1, projectManager1); packageManager.RegisterProjectManager(project2, projectManager2); var packageA = PackageUtility.CreatePackage( "A", "1.2-alpha", new[] { "content.txt" }, new[] { "lib\\ref.dll" }); var packageA2 = PackageUtility.CreatePackage( "A", "1.2-alpha", new[] { "foo.txt" }, new[] { "lib\\bar.dll" }); var packageB = PackageUtility.CreatePackage( "B", "2.0", new[] { "hello.txt" }, new[] { "lib\\comma.dll" }); var packageB2 = PackageUtility.CreatePackage( "B", "2.0", new[] { "world.txt" }, new[] { "lib\\dude.dll" }); sourceRepository.Add(packageA); sourceRepository.Add(packageB); // install package A -> project 1 // and package B -> project 2 packageManager.InstallPackage(projectManager1, "A", new SemanticVersion("1.2-alpha"), ignoreDependencies: false, allowPrereleaseVersions: true, logger: null); packageManager.InstallPackage(projectManager2, "B", new SemanticVersion("2.0"), ignoreDependencies: false, allowPrereleaseVersions: true, logger: null); Assert.True(packageManager.LocalRepository.Exists("A", new SemanticVersion("1.2-alpha"))); Assert.True(projectManager1.LocalRepository.Exists("A", new SemanticVersion("1.2-alpha"))); Assert.True(projectSystem1.FileExists("content.txt")); Assert.True(projectSystem1.ReferenceExists("ref.dll")); Assert.True(packageManager.LocalRepository.Exists("B", new SemanticVersion("2.0"))); Assert.True(projectManager2.LocalRepository.Exists("B", new SemanticVersion("2.0"))); Assert.True(projectSystem2.FileExists("hello.txt")); Assert.True(projectSystem2.ReferenceExists("comma.dll")); // now change the package A and B to different packages sourceRepository.RemovePackage(packageA); sourceRepository.RemovePackage(packageB); sourceRepository.AddPackage(packageA2); sourceRepository.AddPackage(packageB2); localRepositoryMock.Setup(p => p.IsReferenced("A", new SemanticVersion("1.2-alpha"))).Returns((string id, SemanticVersion version) => projectManager1.LocalRepository.Exists(id, version)); localRepositoryMock.Setup(p => p.IsReferenced("B", new SemanticVersion("2.0"))).Returns((string id, SemanticVersion version) => projectManager2.LocalRepository.Exists(id, version)); // Act packageManager.ReinstallPackages(updateDependencies: true, allowPrereleaseVersions: false, logger: NullLogger.Instance, eventListener: NullPackageOperationEventListener.Instance); // Assert Assert.True(packageManager.LocalRepository.Exists("A", new SemanticVersion("1.2-alpha"))); Assert.True(projectManager1.LocalRepository.Exists("A", new SemanticVersion("1.2-alpha"))); Assert.False(projectSystem1.FileExists("content.txt")); Assert.True(projectSystem1.FileExists("foo.txt")); Assert.False(projectSystem1.ReferenceExists("ref.dll")); Assert.True(projectSystem1.ReferenceExists("bar.dll")); Assert.True(packageManager.LocalRepository.Exists("B", new SemanticVersion("2.0"))); Assert.True(projectManager2.LocalRepository.Exists("B", new SemanticVersion("2.0"))); Assert.False(projectSystem2.FileExists("hello.txt")); Assert.True(projectSystem2.FileExists("world.txt")); Assert.False(projectSystem2.ReferenceExists("comma.dll")); Assert.True(projectSystem2.ReferenceExists("dude.dll")); }
public void CallingEnableCurrentSolutionWillAddPackagesToMachineCache() { // Arrange string tempSolutionPath = CreateTempFolder(); // setup SolutionManager var solutionManager = new Mock<ISolutionManager>(); solutionManager.Setup(p => p.IsSolutionOpen).Returns(true); solutionManager.Setup(p => p.SolutionDirectory).Returns(tempSolutionPath); // setup file system var fileSystem = new PhysicalFileSystem(tempSolutionPath); var fileSystemProvider = new Mock<IFileSystemProvider>(); fileSystemProvider.Setup(p => p.GetFileSystem(tempSolutionPath)).Returns(fileSystem); var nugetFolderFileSystem = new PhysicalFileSystem(tempSolutionPath + "\\.nuget"); fileSystemProvider.Setup(p => p.GetFileSystem(tempSolutionPath + "\\.nuget")).Returns(nugetFolderFileSystem); // setup DTE var dte = new Mock<DTE>(); var projectItems = new Mock<ProjectItems>(); var solutionFolder = new Mock<Project>(); solutionFolder.Setup(s => s.Name).Returns(".nuget"); solutionFolder.SetupGet(s => s.ProjectItems).Returns(projectItems.Object); var solution = new Mock<Solution>(); solution.As<Solution2>().Setup(p => p.AddSolutionFolder(".nuget")).Returns(solutionFolder.Object); var projects = new MockProjects(new Project[0]); solution.As<Solution2>().Setup(s => s.Projects).Returns(projects); dte.SetupGet(p => p.Solution).Returns(solution.Object); // setup package repository var packageRepository = new MockPackageRepository(); packageRepository.Add(PackageUtility.CreatePackage( "NuGet.Build", version: "1.0", tools: new string[] { "NuGet.targets" }, dependencies: new PackageDependency[] { new PackageDependency("NuGet.CommandLine") })); packageRepository.Add(PackageUtility.CreatePackage( "NuGet.CommandLine", version: "2.0", tools: new string[] { "NuGet.exe" })); var packageRepositoryFactory = new Mock<IPackageRepositoryFactory>(); packageRepositoryFactory.Setup(p => p.CreateRepository("x:\\nugetsource")).Returns(packageRepository); var packageSourceProvider = new Mock<IPackageSourceProvider>(); packageSourceProvider.Setup(p => p.LoadPackageSources()).Returns(new[] { new PackageSource("x:\\nugetsource") }); var localCache = new MockPackageRepository(); var packageRestore = CreateInstance( dte.Object, solutionManager.Object, fileSystemProvider.Object, packageRepositoryFactory.Object, localCache: localCache, packageSourceProvider: packageSourceProvider.Object); // Act packageRestore.EnableCurrentSolutionForRestore(fromActivation: false); // Assert var cachePackages = localCache.GetPackages().ToList(); Assert.Equal(2, cachePackages.Count); Assert.Equal("NuGet.Build", cachePackages[0].Id); Assert.Equal(new SemanticVersion("1.0"), cachePackages[0].Version); Assert.Equal("NuGet.CommandLine", cachePackages[1].Id); Assert.Equal(new SemanticVersion("2.0"), cachePackages[1].Version); }
public void CallingEnableCurrentSolutionDoNotDownloadPackageIfPresentInLocalCache() { // Arrange string tempSolutionPath = CreateTempFolder(); // setup SolutionManager var solutionManager = new Mock<ISolutionManager>(); solutionManager.Setup(p => p.IsSolutionOpen).Returns(true); solutionManager.Setup(p => p.SolutionDirectory).Returns(tempSolutionPath); // setup file system var fileSystem = new PhysicalFileSystem(tempSolutionPath); var fileSystemProvider = new Mock<IFileSystemProvider>(MockBehavior.Strict); fileSystemProvider.Setup(p => p.GetFileSystem(tempSolutionPath)).Returns(fileSystem); var nugetFolderFileSystem = new PhysicalFileSystem(tempSolutionPath + "\\.nuget"); fileSystemProvider.Setup(p => p.GetFileSystem(tempSolutionPath + "\\.nuget")).Returns(nugetFolderFileSystem); // setup DTE var dte = new Mock<DTE>(); var projectItems = new Mock<ProjectItems>(); var solutionFolder = new Mock<Project>(); solutionFolder.Setup(s => s.Name).Returns(".nuget"); solutionFolder.SetupGet(s => s.ProjectItems).Returns(projectItems.Object); var solution = new Mock<Solution>(); solution.As<Solution2>().Setup(p => p.AddSolutionFolder(".nuget")).Returns(solutionFolder.Object); var projects = new MockProjects(new Project[0]); solution.As<Solution2>().Setup(s => s.Projects).Returns(projects); dte.SetupGet(p => p.Solution).Returns(solution.Object); // setup package repository var packageRepository = new MockPackageRepository(); var packageA = new Mock<IPackage>(MockBehavior.Strict); packageA.Setup(p => p.Id).Returns("NuGet.Build"); packageA.Setup(p => p.Version).Returns(new SemanticVersion("1.0")); packageA.Setup(p => p.IsLatestVersion).Returns(true); packageA.Setup(p => p.Listed).Returns(true); var packageB = new Mock<IPackage>(MockBehavior.Strict); packageB.Setup(p => p.Id).Returns("NuGet.CommandLine"); packageB.Setup(p => p.Version).Returns(new SemanticVersion("2.0")); packageB.Setup(p => p.IsLatestVersion).Returns(true); packageB.Setup(p => p.Listed).Returns(true); packageRepository.AddPackage(packageA.Object); packageRepository.AddPackage(packageB.Object); var packageRepositoryFactory = new Mock<IPackageRepositoryFactory>(MockBehavior.Strict); packageRepositoryFactory.Setup(p => p.CreateRepository("x:\\nugetsource")).Returns(packageRepository); var packageSourceProvider = new Mock<IPackageSourceProvider>(); packageSourceProvider.Setup(p => p.LoadPackageSources()).Returns(new[] { new PackageSource("x:\\nugetsource"), new PackageSource("y:\\me", "unabled", isEnabled: false), }); var localCache = new MockPackageRepository(); localCache.Add(PackageUtility.CreatePackage( "NuGet.Build", version: "1.0", tools: new string[] { "NuGet.targets" }, dependencies: new PackageDependency[] { new PackageDependency("NuGet.CommandLine") }, listed: true)); localCache.Add(PackageUtility.CreatePackage( "NuGet.CommandLine", version: "2.0", tools: new string[] { "NuGet.exe" }, listed: true)); var packageRestore = CreateInstance( dte.Object, solutionManager.Object, fileSystemProvider.Object, packageRepositoryFactory.Object, localCache: localCache, packageSourceProvider: packageSourceProvider.Object); // Act packageRestore.EnableCurrentSolutionForRestore(fromActivation: false); // Assert packageA.Verify(p => p.GetFiles(), Times.Never()); packageB.Verify(p => p.GetFiles(), Times.Never()); }
public void TestRecentSwitchWorkCorrectly() { // Arrange var packageA = PackageUtility.CreatePackage("A", "1.0"); var packageB = PackageUtility.CreatePackage("B", "2.0"); var packageC = PackageUtility.CreatePackage("C", "3.0"); var repository = new MockPackageRepository(); repository.Add(packageA); repository.Add(packageB); repository.Add(packageC); var cmdlet = BuildCmdlet(false, repository); cmdlet.Recent = true; // Act var packages = cmdlet.GetResults().OfType<IPackage>().ToList(); // Assert Assert.AreEqual(3, packages.Count); Assert.AreSame(packageA, packages[0]); Assert.AreSame(packageB, packages[1]); Assert.AreSame(packageC, packages[2]); }
public void InstallMetaPackageDoesNotAddEntryToSolutionPackagesConfig2(bool skipAssemblyReferences) { // Arrange // Source repository has A -> B, where B is a project-level package and A is a meta-package. // We want to make sure A is added to the packages.config of the project, and NOT packages.config of the solution var localRepository = new Mock<MockPackageRepository>() { CallBase = true }.As<ISharedPackageRepository>(); var sourceRepository = new MockPackageRepository(); var packageB = PackageUtility.CreatePackage("B", "1.0", new string[] { "hello.txt" }); var packageA = PackageUtility.CreatePackage("A", "2.0", new string[] { "world" }, dependencies: new[] { new PackageDependency("B") }); sourceRepository.Add(packageA); sourceRepository.Add(packageB); var projectSystem = new MockProjectSystem(); var pathResolver = new DefaultPackagePathResolver(projectSystem); var packageManager = new VsPackageManager( TestUtils.GetSolutionManager(), sourceRepository, new Mock<IFileSystemProvider>().Object, projectSystem, localRepository.Object, new Mock<IDeleteOnRestartManager>().Object, new Mock<VsPackageInstallerEvents>().Object); var projectRepository = new MockProjectPackageRepository(localRepository.Object); var projectManager = new ProjectManager(localRepository.Object, pathResolver, projectSystem, projectRepository); localRepository.Setup(r => r.IsReferenced(It.IsAny<string>(), It.IsAny<SemanticVersion>())). Returns((string id, SemanticVersion version) => projectRepository.Exists(id, version)); // Act packageManager.InstallPackage( projectManager, "A", new SemanticVersion("2.0"), ignoreDependencies: false, allowPrereleaseVersions: true, skipAssemblyReferences: skipAssemblyReferences, logger: null); // Assert Assert.True(projectRepository.Exists("A", new SemanticVersion("2.0"))); Assert.True(projectRepository.Exists("B", new SemanticVersion("1.0"))); // assert that packages.config for solution-level is not created. Assert.False(localRepository.Object.IsSolutionReferenced("A", new SemanticVersion("2.0"))); Assert.False(localRepository.Object.IsSolutionReferenced("B", new SemanticVersion("1.0"))); }
public void InstallSolutionLevelPackageAddEntryToSolutionPackagesConfig2(bool skipAssemblyReferences) { // Arrange // Source repository has A -> B, where B is a project-level package and A is a meta-package. // We want to make sure A is added to the packages.config of the project, and NOT packages.config of the solution var localRepository = new MockSharedPackageRepository(); var sourceRepository = new MockPackageRepository(); var packageA = PackageUtility.CreatePackage("A", "2.0", tools: new[] { "install.ps1" }); sourceRepository.Add(packageA); var projectSystem = new MockProjectSystem(); var pathResolver = new DefaultPackagePathResolver(projectSystem); var packageManager = new VsPackageManager( TestUtils.GetSolutionManager(), sourceRepository, new Mock<IFileSystemProvider>().Object, projectSystem, localRepository, new Mock<IDeleteOnRestartManager>().Object, new Mock<VsPackageInstallerEvents>().Object); var projectRepository = new MockProjectPackageRepository(localRepository); var projectManager = new ProjectManager(localRepository, pathResolver, projectSystem, projectRepository); // Act packageManager.InstallPackage( projectManager, "A", new SemanticVersion("2.0"), ignoreDependencies: false, allowPrereleaseVersions: true, skipAssemblyReferences: skipAssemblyReferences, logger: null); // Assert Assert.True(!projectRepository.Exists("A", new SemanticVersion("2.0"))); // assert that packages.config for solution-level is created. Assert.True(localRepository.IsSolutionReferenced("A", new SemanticVersion("2.0"))); }
public void ResolvingDependencyForUpdateThatHasAnUnsatisfiedConstraint() { // Arrange var localRepository = new MockPackageRepository(); var sourceRepository = new MockPackageRepository(); var constraintProvider = new Mock<IPackageConstraintProvider>(); constraintProvider.Setup(m => m.GetConstraint("B")).Returns(VersionUtility.ParseVersionSpec("[1.4]")); constraintProvider.Setup(m => m.Source).Returns("foo"); IPackage A10 = PackageUtility.CreatePackage("A", "1.0", dependencies: new List<PackageDependency> { PackageDependency.CreateDependency("B", "1.5") }); IPackage A20 = PackageUtility.CreatePackage("A", "2.0", dependencies: new List<PackageDependency> { PackageDependency.CreateDependency("B", "2.0") }); IPackage B15 = PackageUtility.CreatePackage("B", "1.5"); IPackage B20 = PackageUtility.CreatePackage("B", "2.0"); localRepository.Add(A10); localRepository.Add(B15); sourceRepository.AddPackage(A10); sourceRepository.AddPackage(A20); sourceRepository.AddPackage(B15); sourceRepository.AddPackage(B20); IPackageOperationResolver resolver = new InstallWalker(localRepository, sourceRepository, constraintProvider.Object, null, NullLogger.Instance, ignoreDependencies: false, allowPrereleaseVersions: false); // Act & Assert ExceptionAssert.Throws<InvalidOperationException>(() => resolver.ResolveOperations(A20), "Unable to resolve dependency 'B (\u2265 2.0)'.'B' has an additional constraint (= 1.4) defined in foo."); }
public void ExecuteMethodInvokesScript() { // source repo has A, B, C // solution repo has A // project repo has C // install B // Arrange var packageA = PackageUtility.CreatePackage("A", "1.0"); var packageB = PackageUtility.CreatePackage("B", "2.0", content: new string[] { "hello world" }, tools: new string[] { "install.ps1", "uninstall.ps1" }); var packageC = PackageUtility.CreatePackage("C", "3.0"); var solutionRepository = new MockPackageRepository(); solutionRepository.AddPackage(packageA); var sourceRepository = new MockPackageRepository(); sourceRepository.AddPackage(packageA); sourceRepository.AddPackage(packageB); sourceRepository.AddPackage(packageC); var localRepository = new MockPackageRepository(); localRepository.Add(packageC); var projectManager = CreateProjectManager(localRepository, solutionRepository); var project = new Mock <Project>(); var scriptExecutor = new Mock <IScriptExecutor>(); var packageManager = new Mock <IVsPackageManager>(); packageManager.Setup(p => p.SourceRepository).Returns(sourceRepository); packageManager.Setup(p => p.LocalRepository).Returns(solutionRepository); packageManager.Setup(p => p.InstallPackage( projectManager, It.IsAny <IPackage>(), It.IsAny <IEnumerable <PackageOperation> >(), false, It.IsAny <ILogger>())).Callback( () => { solutionRepository.AddPackage(packageB); projectManager.AddPackageReference(packageB.Id, packageB.Version); }); var provider = CreateOnlineProvider(packageManager.Object, projectManager, null, null, project.Object, scriptExecutor.Object); var extensionTree = provider.ExtensionsTree; var firstTreeNode = (SimpleTreeNode)extensionTree.Nodes[0]; firstTreeNode.Repository.AddPackage(packageA); firstTreeNode.Repository.AddPackage(packageB); firstTreeNode.Repository.AddPackage(packageC); provider.SelectedNode = firstTreeNode; ManualResetEvent manualEvent = new ManualResetEvent(false); provider.ExecuteCompletedCallback = delegate { try { // Assert scriptExecutor.Verify(p => p.Execute(It.IsAny <string>(), PowerShellScripts.Install, packageB, project.Object, It.IsAny <ILogger>()), Times.Once()); } finally { manualEvent.Set(); } }; var extensionB = new PackageItem(provider, packageB, null); // Act provider.Execute(extensionB); // do not allow the method to return manualEvent.WaitOne(); }