Inheritance: MockFileSystem, IProjectSystem
        public void InstallPackageSetOperationToInstall3()
        {
            // Arrange
            var localRepository = new MockSharedPackageRepository();
            var sourceRepository = new MockPackageRepository();
            var projectSystem = new MockProjectSystem();
            var pathResolver = new DefaultPackagePathResolver(projectSystem);
            var projectManager = new ProjectManager(localRepository, pathResolver, new MockProjectSystem(), new MockPackageRepository());
            var packageManager = new VsPackageManager(TestUtils.GetSolutionManager(), sourceRepository, new Mock<IFileSystemProvider>().Object, projectSystem, localRepository, new Mock<IDeleteOnRestartManager>().Object, new Mock<VsPackageInstallerEvents>().Object);

            var package = PackageUtility.CreatePackage("foo", "1.0", new[] { "hello" });
            sourceRepository.AddPackage(package);

            var package2 = PackageUtility.CreatePackage("bar", "2.0", new[] { "world" });
            sourceRepository.AddPackage(package2);

            // Act
            packageManager.InstallPackage(
                projectManager,
                package2,
                new PackageOperation[] { 
                    new PackageOperation(package, PackageAction.Install),
                    new PackageOperation(package2, PackageAction.Install),
                },
                ignoreDependencies: false, 
                allowPrereleaseVersions: false, 
                logger: NullLogger.Instance);

            // Assert
            Assert.Equal("Install", sourceRepository.LastOperation);
            Assert.Equal("bar", sourceRepository.LastMainPackageId);
            Assert.Equal("2.0", sourceRepository.LastMainPackageVersion);
        }
Beispiel #2
0
        public void AddPackageReferenceAppliesPackageReferencesCorrectly2()
        {
            // Arrange            
            var sourceRepository = new MockPackageRepository();
            var projectSystem = new MockProjectSystem(new FrameworkName(".NETFramework, Version=4.5"));
            var projectManager = new ProjectManager(sourceRepository, new DefaultPackagePathResolver(projectSystem), projectSystem, new MockPackageRepository());
            IPackage packageA = PackageUtility.CreatePackage(
                "A",
                "1.0",
                assemblyReferences: new[] { "lib\\net35\\a.dll", "lib\\net35\\b.dll" });
            sourceRepository.AddPackage(packageA);

            Mock<IPackage> mockPackageA = Mock.Get<IPackage>(packageA);
            mockPackageA.Setup(m => m.PackageAssemblyReferences).Returns(
                    new PackageReferenceSet[] { 
                        new PackageReferenceSet(VersionUtility.ParseFrameworkName("net40"), new [] { "a.dll" }),
                        new PackageReferenceSet(VersionUtility.ParseFrameworkName("net45"), new [] { "b.dll" })
                    }
                );

            // Act
            projectManager.AddPackageReference("A");

            // Assert
            Assert.True(projectManager.LocalRepository.Exists("A"));
            Assert.False(projectSystem.ReferenceExists("a.dll"));
            Assert.True(projectSystem.ReferenceExists("b.dll"));
        }
        public void AddPackageReferenceLoadPackagesProjectNameConfigIfPresent()
        {
            // Arrange            
            var sourceRepository = new MockPackageRepository();
            var projectSystem = new MockProjectSystem(
                new FrameworkName(".NETFramework, Version=4.5"));
            projectSystem.ProjectName = "CoolProject";
            projectSystem.AddFile("packages.CoolProject.config", @"<?xml version=""1.0"" encoding=""utf-8""?>
<packages>
  <package id=""luan"" version=""3.0"" />
</packages>");

            var projectManager = new ProjectManager(sourceRepository, new DefaultPackagePathResolver(projectSystem), projectSystem, new MockPackageRepository());
            IPackage packageA = PackageUtility.CreatePackage(
                "A",
                "1.0",
                assemblyReferences: new[] { "lib\\a.dll"});
            sourceRepository.AddPackage(packageA);

            // Act
            projectManager.AddPackageReference("A");

            // Assert
            Assert.True(projectManager.LocalRepository.Exists("A"));
            Assert.True(projectManager.Project.FileExists("packages.CoolProject.config"));
            Assert.False(projectManager.Project.FileExists("packages.config"));
        }
Beispiel #4
0
        public void UninstallPackageExecutesUninstallScript()
        {
            // Arrange
            var activeRepository = new MockPackageRepository();
            var localRepository = new Mock<MockPackageRepository>() { CallBase = true };
            localRepository.As<ISharedPackageRepository>();
            var projectRepository = new MockProjectPackageRepository(localRepository.Object);
            var project = TestUtils.GetProject("Foo");
            var projectSystem = new MockProjectSystem();
            var projectManager = new ProjectManager(activeRepository, new DefaultPackagePathResolver(new MockFileSystem()), projectSystem, projectRepository);
            var package = PackageUtility.CreatePackage("A");
            var scriptExecutor = new Mock<IScriptExecutor>(MockBehavior.Strict);
            scriptExecutor.Setup(s => s.Execute(@"C:\MockFileSystem\A.1.0", "uninstall.ps1", package, project, NullLogger.Instance)).Returns(true).Verifiable();
            var packageManager = new Mock<VsPackageManager>(TestUtils.GetSolutionManager(), activeRepository, new Mock<IFileSystemProvider>().Object, new MockFileSystem(), localRepository.Object,
                new Mock<IRecentPackageRepository>().Object, new Mock<VsPackageInstallerEvents>().Object) { CallBase = true };
            var packageManagerFactory = new Mock<IVsPackageManagerFactory>();
            packageManagerFactory.Setup(m => m.CreatePackageManager(activeRepository, false, false)).Returns(packageManager.Object);
            packageManager.Setup(m => m.GetProjectManager(project)).Returns(projectManager).Verifiable();
            var packageUninstaller = new VsPackageUninstaller(packageManagerFactory.Object, activeRepository, scriptExecutor.Object);


            // Act
            localRepository.Object.AddPackage(package);
            packageUninstaller.UninstallPackage(project, "A", removeDependencies: true);

            // Assert
            scriptExecutor.Verify();
            Assert.False(localRepository.Object.Contains(package));
        }
        public void AddPackageReferenceWhenNewVersionOfPackageAlreadyReferencedThrows()
        {
            // Arrange            
            var sourceRepository = new MockPackageRepository();
            var projectSystem = new MockProjectSystem();
            var projectManager = new ProjectManager(sourceRepository, new DefaultPackagePathResolver(projectSystem), projectSystem, new MockPackageRepository());
            IPackage packageA10 = PackageUtility.CreatePackage("A", "1.0",
                                                                dependencies: new List<PackageDependency> { 
                                                                    new PackageDependency("B")
                                                                }, content: new[] { "foo" });
            IPackage packageA20 = PackageUtility.CreatePackage("A", "2.0",
                                                                dependencies: new List<PackageDependency> { 
                                                                    new PackageDependency("B")
                                                                }, content: new[] { "foo" });
            IPackage packageB10 = PackageUtility.CreatePackage("B", "1.0", content: new[] { "foo" });
            projectManager.LocalRepository.AddPackage(packageA20);
            projectManager.LocalRepository.AddPackage(packageB10);

            sourceRepository.AddPackage(packageA20);
            sourceRepository.AddPackage(packageB10);
            sourceRepository.AddPackage(packageA10);
            sourceRepository.AddPackage(packageA20);
            sourceRepository.AddPackage(packageB10);

            // Act & Assert
            ExceptionAssert.Throws<InvalidOperationException>(() => projectManager.AddPackageReference("A", SemanticVersion.Parse("1.0")), @"Already referencing a newer version of 'A'.");
        }
        public void InstallPackageDoesNotUseFallbackRepository()
        {
            // Arrange
            var localRepository = new Mock<MockPackageRepository>() { CallBase = true }.As<ISharedPackageRepository>().Object;
            var sourceRepository = new MockPackageRepository();
            var projectRepository = new MockProjectPackageRepository(localRepository);
            var fileSystem = new MockFileSystem();
            var projectSystem = new MockProjectSystem();
            var pathResolver = new DefaultPackagePathResolver(new MockProjectSystem());
            var project = TestUtils.GetProject("Foo");
            var projectManager = new ProjectManager(localRepository, pathResolver, projectSystem, projectRepository);
            var scriptExecutor = new Mock<IScriptExecutor>();
            var packageManager = new Mock<VsPackageManager>(TestUtils.GetSolutionManager(), sourceRepository, fileSystem, localRepository, new Mock<IRecentPackageRepository>().Object, 
                new Mock<VsPackageInstallerEvents>().Object) { CallBase = true };
            var packageManagerFactory = new Mock<IVsPackageManagerFactory>();
            packageManagerFactory.Setup(m => m.CreatePackageManager(It.IsAny<IPackageRepository>(), false, false)).Returns(packageManager.Object);
            packageManagerFactory.Setup(m => m.CreatePackageManager()).Throws(new Exception("A"));
            packageManagerFactory.Setup(m => m.CreatePackageManager(It.IsAny<IPackageRepository>(), true, It.IsAny<bool>())).Throws(new Exception("B"));
            packageManager.Setup(m => m.GetProjectManager(project)).Returns(projectManager);

            var package = NuGet.Test.PackageUtility.CreatePackage("foo", "1.0", new[] { "hello" }, tools: new[] { "init.ps1", "install.ps1" });
            sourceRepository.AddPackage(package);
            var installer = new VsPackageInstaller(packageManagerFactory.Object, scriptExecutor.Object, new Mock<IPackageRepositoryFactory>().Object);

            // Act
            installer.InstallPackage(sourceRepository, project, "foo", new SemanticVersion("1.0"), ignoreDependencies: false);

            // Assert
            packageManagerFactory.Verify(m => m.CreatePackageManager(It.IsAny<IPackageRepository>(), false, false), Times.Once());
            packageManagerFactory.Verify(m => m.CreatePackageManager(), Times.Never());
            packageManagerFactory.Verify(m => m.CreatePackageManager(It.IsAny<IPackageRepository>(), true, It.IsAny<bool>()), Times.Never());
        }
Beispiel #7
0
        public void XdtTransformOnXmlNodeWithAttributes()
        {
            // Arrange
            var mockProjectSystem = new MockProjectSystem();
            var mockRepository = new MockPackageRepository();
            mockProjectSystem.AddFile("test.xml",
@"<a attrib=""b""/>".AsStream());
            var projectManager = new ProjectManager(mockRepository, new DefaultPackagePathResolver(new MockProjectSystem()), mockProjectSystem, new MockPackageRepository());

            var package = new Mock<IPackage>();
            package.Setup(m => m.Id).Returns("A");
            package.Setup(m => m.Version).Returns(new SemanticVersion("1.0"));
            package.Setup(m => m.Listed).Returns(true);

            var file = new Mock<IPackageFile>();
            file.Setup(m => m.Path).Returns(@"content\test.xml.install.xdt");
            file.Setup(m => m.EffectivePath).Returns("test.xml.install.xdt");
            file.Setup(m => m.GetStream()).Returns(() =>
@"<a xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform""><test xdt:Transform=""InsertIfMissing""/></a>".AsStream());            

            package.Setup(m => m.GetFiles()).Returns(new[] { file.Object });
            mockRepository.AddPackage(package.Object);

            // Act
            projectManager.AddPackageReference("A");

            // Assert
            Assert.True(mockProjectSystem.FileExists("test.xml"));
            var actual = mockProjectSystem.OpenFile("test.xml").ReadToEnd();
            Assert.Equal("<a attrib=\"b\">\t<test/></a>", actual);
        }
Beispiel #8
0
        public void InstallPackageRunsInitAndInstallScripts()
        {
            // Arrange
            var localRepository = new Mock<MockPackageRepository>() { CallBase = true }.As<ISharedPackageRepository>().Object;
            var sourceRepository = new MockPackageRepository();
            var projectRepository = new MockProjectPackageRepository(localRepository);
            var fileSystem = new MockFileSystem();
            var projectSystem = new MockProjectSystem();
            var pathResolver = new DefaultPackagePathResolver(new MockProjectSystem());
            var project = TestUtils.GetProject("Foo");
            var projectManager = new ProjectManager(localRepository, pathResolver, projectSystem, projectRepository);
            var scriptExecutor = new Mock<IScriptExecutor>();
            var packageManager = new Mock<VsPackageManager>(TestUtils.GetSolutionManager(), sourceRepository, new Mock<IFileSystemProvider>().Object, fileSystem, localRepository, new Mock<IRecentPackageRepository>().Object, 
                new Mock<VsPackageInstallerEvents>().Object) { CallBase = true };
            var packageManagerFactory = new Mock<IVsPackageManagerFactory>();
            packageManagerFactory.Setup(m => m.CreatePackageManager(It.IsAny<IPackageRepository>(), false, false)).Returns(packageManager.Object);
            packageManager.Setup(m => m.GetProjectManager(project)).Returns(projectManager);

            var package = NuGet.Test.PackageUtility.CreatePackage("foo", "1.0", new[] { "hello" }, tools: new[] { "init.ps1", "install.ps1" });
            sourceRepository.AddPackage(package);
            var installer = new VsPackageInstaller(packageManagerFactory.Object, scriptExecutor.Object, new Mock<IPackageRepositoryFactory>().Object, new Mock<IVsCommonOperations>().Object, new Mock<ISolutionManager>().Object);

            // Act
            installer.InstallPackage(sourceRepository, project, "foo", new SemanticVersion("1.0"), ignoreDependencies: false, skipAssemblyReferences: false);

            // Assert
            scriptExecutor.Verify(e => e.Execute(It.IsAny<string>(), PowerShellScripts.Init, It.IsAny<IPackage>(), It.IsAny<Project>(), It.IsAny<ILogger>()), Times.Once());
            scriptExecutor.Verify(e => e.Execute(It.IsAny<string>(), PowerShellScripts.Install, It.IsAny<IPackage>(), It.IsAny<Project>(), It.IsAny<ILogger>()), Times.Once());
        }
        public void InstallMetadataPackageWithReadMeInstallsIntoProjectAndPackageManager()
        {
            // 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, new MockProjectSystem(), 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.0", dependencies: new[] { new PackageDependency("B") });
            var mockPackageA = Mock.Get(packageA);
            mockPackageA.Setup(m => m.GetFiles()).Returns(PackageUtility.CreateFiles(new[] { "readme.txt" }));

            var packageB = PackageUtility.CreatePackage("B", "1.0", content: new[] { "hello.txt" });
            sourceRepository.AddPackage(packageA);
            sourceRepository.AddPackage(packageB);

            // Act
            packageManager.InstallPackage(projectManager, "A", new SemanticVersion("1.0"), ignoreDependencies: false, allowPrereleaseVersions: false, logger: NullLogger.Instance);

            // Assert
            Assert.True(packageManager.LocalRepository.Exists(packageA));
            Assert.True(projectManager.LocalRepository.Exists(packageA));

            Assert.True(packageManager.LocalRepository.Exists(packageB));
            Assert.True(projectManager.LocalRepository.Exists(packageB));
        }
Beispiel #10
0
        public void InstallPackageWithOperationsExecuteAllOperations()
        {
            // 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, new MockProjectSystem(), new MockPackageRepository());
            var packageManager = new VsPackageManager(TestUtils.GetSolutionManager(), sourceRepository, projectSystem, localRepository, new Mock<IRecentPackageRepository>().Object);

            var package = PackageUtility.CreatePackage("foo", "1.0", new[] { "hello" }, dependencies: new PackageDependency[] { new PackageDependency("bar") });
            sourceRepository.AddPackage(package);

            var package2 = PackageUtility.CreatePackage("bar", "2.0", new[] { "world" });
            sourceRepository.AddPackage(package2);

            var package3 = PackageUtility.CreatePackage("awesome", "1.0", new[] { "candy" });
            localRepository.AddPackage(package3);

            var operations = new PackageOperation[] {
                 new PackageOperation(package, PackageAction.Install),
                 new PackageOperation(package2, PackageAction.Install),
                 new PackageOperation(package3, PackageAction.Uninstall)
             };

            // Act
            packageManager.InstallPackage(projectManager, package, operations, ignoreDependencies: false, logger: NullLogger.Instance);

            // Assert
            Assert.IsTrue(packageManager.LocalRepository.Exists(package));
            Assert.IsTrue(packageManager.LocalRepository.Exists(package2));
            Assert.IsTrue(!packageManager.LocalRepository.Exists(package3));
            Assert.IsTrue(projectManager.LocalRepository.Exists(package));
            Assert.IsTrue(projectManager.LocalRepository.Exists(package2));
        }
Beispiel #11
0
        public void InstallPackageAddsAllFilesToFileSystem()
        {
            // Arrange
            var projectSystem = new MockProjectSystem();
            var sourceRepository = new MockPackageRepository();
            var pathResolver = new DefaultPackagePathResolver(projectSystem);
            var packageManager = new PackageManager(sourceRepository, pathResolver, projectSystem, 
                new SharedPackageRepository(pathResolver, projectSystem, projectSystem));

            IPackage packageA = PackageUtility.CreatePackage("A", "1.0",
                                                             new[] { "contentFile", @"sub\contentFile" },
                                                             new[] { @"lib\reference.dll" },
                                                             new[] { @"readme.txt" });

            sourceRepository.AddPackage(packageA);

            // Act
            Install("A", new NullProjectManager(packageManager));

            // Assert
            Assert.Equal(0, projectSystem.References.Count);
            Assert.Equal(5, projectSystem.Paths.Count);
            Assert.True(projectSystem.FileExists(@"A.1.0\content\contentFile"));
            Assert.True(projectSystem.FileExists(@"A.1.0\content\sub\contentFile"));
            Assert.True(projectSystem.FileExists(@"A.1.0\lib\reference.dll"));
            Assert.True(projectSystem.FileExists(@"A.1.0\tools\readme.txt"));
            Assert.True(projectSystem.FileExists(@"A.1.0\A.1.0.nupkg"));
        }
        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")));
        }
Beispiel #13
0
        public void AddPackageReferenceAddingPackageWithDuplicateReferenceOverwritesReference()
        {
            // Arrange
            var projectSystem = new MockProjectSystem();
            var localRepository = new MockPackageRepository();
            var mockRepository = new MockPackageRepository();
            var projectManager = new ProjectManager(mockRepository, new DefaultPackagePathResolver(projectSystem), projectSystem, localRepository);
            var packageA = PackageUtility.CreatePackage("A", "1.0",
                                                        assemblyReferences: new[] { "reference.dll" });
            var packageB = PackageUtility.CreatePackage("B", "1.0",
                                                        assemblyReferences: new[] { "reference.dll" });

            mockRepository.AddPackage(packageA);
            mockRepository.AddPackage(packageB);

            // Act
            projectManager.AddPackageReference("A");
            projectManager.AddPackageReference("B");

            // Assert
            Assert.AreEqual(0, projectSystem.Paths.Count);
            Assert.AreEqual(1, projectSystem.References.Count);
            Assert.IsTrue(projectSystem.References.ContainsKey(@"reference.dll"));
            Assert.IsTrue(projectSystem.References.ContainsValue(@"B.1.0\reference.dll"));
            Assert.IsTrue(localRepository.Exists("A"));
            Assert.IsTrue(localRepository.Exists("B"));
        }
        public void UninstallPackageDoesNotRemoveDependenciesIfFlagIsFalse()
        {
            // Arrange
            var activeRepository = new MockPackageRepository();
            var localRepository = new Mock<MockPackageRepository>() { CallBase = true };
            localRepository.As<ISharedPackageRepository>();
            var projectRepository = new MockProjectPackageRepository(localRepository.Object);
            var project = TestUtils.GetProject("Foo");
            var projectSystem = new MockProjectSystem(new FrameworkName(".NETCore, Version=4.5"));
            var projectManager = new ProjectManager(activeRepository, new DefaultPackagePathResolver(new MockFileSystem()), projectSystem, projectRepository);
            var packageA = PackageUtility.CreatePackage("A", dependencies: new[] { new PackageDependency("B") });
            var packageB = PackageUtility.CreatePackage("B");
            var scriptExecutor = new Mock<IScriptExecutor>(MockBehavior.Strict);
            scriptExecutor.Setup(s => s.Execute(@"C:\MockFileSystem\A.1.0", "uninstall.ps1", packageA, project, new FrameworkName(".NETCore, Version=4.5"), NullLogger.Instance)).Returns(true).Verifiable();
            var packageManager = new Mock<VsPackageManager>(TestUtils.GetSolutionManager(), activeRepository, new Mock<IFileSystemProvider>().Object, new MockFileSystem(), localRepository.Object,
                new Mock<IDeleteOnRestartManager>().Object, new Mock<VsPackageInstallerEvents>().Object) { CallBase = true };
            var packageManagerFactory = new Mock<IVsPackageManagerFactory>();
            packageManagerFactory.Setup(m => m.CreatePackageManager(activeRepository, false)).Returns(packageManager.Object);
            packageManager.Setup(m => m.GetProjectManager(project)).Returns(projectManager).Verifiable();
            var packageUninstaller = new VsPackageUninstaller(packageManagerFactory.Object, activeRepository, scriptExecutor.Object);


            // Act
            localRepository.Object.AddPackage(packageA);
            localRepository.Object.AddPackage(packageB);
            packageUninstaller.UninstallPackage(project, "A", removeDependencies: false);

            // Assert
            scriptExecutor.Verify();
            Assert.False(localRepository.Object.Contains(packageA));
            Assert.True(localRepository.Object.Contains(packageB));
        }
Beispiel #15
0
        public void ReThrowWithMeaningfulErrorMessageWhenXdtFileHasSyntaxError()
        {
            // Arrange
            var mockProjectSystem = new MockProjectSystem();
            var mockRepository = new MockPackageRepository();
            mockProjectSystem.AddFile("web.config",
@"<configuration>
    <system.web>
        <compilation debug=""true"" />
    </system.web>
</configuration>
".AsStream());
            var projectManager = new ProjectManager(mockRepository, new DefaultPackagePathResolver(new MockProjectSystem()), mockProjectSystem, new MockPackageRepository());

            var package = new Mock<IPackage>();
            package.Setup(m => m.Id).Returns("A");
            package.Setup(m => m.Version).Returns(new SemanticVersion("1.0"));
            package.Setup(m => m.Listed).Returns(true);

            var file = new Mock<IPackageFile>();
            file.Setup(m => m.Path).Returns(@"content\web.config.install.xdt");
            file.Setup(m => m.EffectivePath).Returns("web.config.install.xdt");
            file.Setup(m => m.GetStream()).Returns(() =>
@"<configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform"">
    <system.web>
        <compilation xd:Locator=""Condition('@debug=true')"" debug=""false"" xdt:Transform=""Replace"" />
    </system.web>
</configuration>".AsStream());

            var file2 = new Mock<IPackageFile>();
            file2.Setup(m => m.Path).Returns(@"content\web.config.uninstall.xdt");
            file2.Setup(m => m.EffectivePath).Returns("web.config.uninstall.xdt");
            file2.Setup(m => m.GetStream()).Returns(() =>
@"<configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform"">
    <system.web>
        <compilation xdt:Locator=""Match(debug)"" debug=""false"" xdt:Transform=""Remove"" />
    </system.web>
</configuration>".AsStream());

            package.Setup(m => m.GetFiles()).Returns(new[] { file.Object, file2.Object });
            mockRepository.AddPackage(package.Object);

            // Act 
            ExceptionAssert.Throws<InvalidDataException>(
                () => projectManager.AddPackageReference("A"),
                @"An error occurred while applying transformation to 'web.config' in project 'x:\MockFileSystem': 'xd' is an undeclared prefix. Line 3, position 22.");

            // Assert
            Assert.False(mockProjectSystem.FileExists("web.config.install.xdt"));
            Assert.False(mockProjectSystem.FileExists("web.config.uninstall.xdt"));
            Assert.True(mockProjectSystem.FileExists("web.config"));
            Assert.Equal(
@"<configuration>
    <system.web>
        <compilation debug=""true"" />
    </system.web>
</configuration>
", mockProjectSystem.OpenFile("web.config").ReadToEnd());
        }
Beispiel #16
0
        public void AddPackageWithXdtTransformFile()
        {
            // Arrange
            var mockProjectSystem = new MockProjectSystem();
            var mockRepository = new MockPackageRepository();
            mockProjectSystem.AddFile("web.config",
@"<configuration>
    <system.web>
        <compilation debug=""true"" />
    </system.web>
</configuration>
".AsStream());
            var projectManager = new ProjectManager(mockRepository, new DefaultPackagePathResolver(new MockProjectSystem()), mockProjectSystem, new MockPackageRepository());

            var package = new Mock<IPackage>();
            package.Setup(m => m.Id).Returns("A");
            package.Setup(m => m.Version).Returns(new SemanticVersion("1.0"));
            package.Setup(m => m.Listed).Returns(true);
            
            var file = new Mock<IPackageFile>();
            file.Setup(m => m.Path).Returns(@"content\web.config.install.xdt");
            file.Setup(m => m.EffectivePath).Returns("web.config.install.xdt");
            file.Setup(m => m.GetStream()).Returns(() =>
@"<configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform"">
    <system.web>
        <compilation xdt:Locator=""Condition('@debug=true')"" debug=""false"" xdt:Transform=""Replace"" />
    </system.web>
</configuration>".AsStream());

            var file2 = new Mock<IPackageFile>();
            file2.Setup(m => m.Path).Returns(@"content\web.config.uninstall.xdt");
            file2.Setup(m => m.EffectivePath).Returns("web.config.uninstall.xdt");
            file2.Setup(m => m.GetStream()).Returns(() =>
@"<configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform"">
    <system.web>
        <compilation xdt:Locator=""Match(debug)"" debug=""false"" xdt:Transform=""Remove"" />
    </system.web>
</configuration>".AsStream());

            package.Setup(m => m.GetFiles()).Returns(new[] { file.Object, file2.Object });
            mockRepository.AddPackage(package.Object);

            // Act
            projectManager.AddPackageReference("A");

            // Assert
            Assert.False(mockProjectSystem.FileExists("web.config.install.xdt"));
            Assert.False(mockProjectSystem.FileExists("web.config.uninstall.xdt"));
            Assert.True(mockProjectSystem.FileExists("web.config"));
            Assert.Equal(
@"<configuration>
    <system.web>
        <compilation debug=""false""/>
    </system.web>
</configuration>
", mockProjectSystem.OpenFile("web.config").ReadToEnd());
        }
        public void CreateRefreshFileUsesAbsolutePathIfRelativePathsCannotBeFormed()
        {
            // Arrange
            var projectSystem = new MockProjectSystem(VersionUtility.DefaultTargetFramework, @"z:\test\site\");
            var assemblyPath = @"x:\test\packages\Foo.1.0\lib\net40\Bar.net40.dll";

            // Act
            projectSystem.CreateRefreshFile(assemblyPath);

            // Assert
            Assert.Equal(@"x:\test\packages\Foo.1.0\lib\net40\Bar.net40.dll", projectSystem.ReadAllText(@"bin\Bar.net40.dll.refresh"));
        }
        public void CreateRefreshFileAddsRefreshFileUnderBinDirectory()
        {
            // Arrange
            var projectSystem = new MockProjectSystem(VersionUtility.DefaultTargetFramework, @"x:\test\site\");
            var assemblyPath = @"x:\test\packages\Foo.1.0\lib\net40\Foo.dll";

            // Act
            projectSystem.CreateRefreshFile(assemblyPath);

            // Assert
            Assert.Equal(@"..\packages\Foo.1.0\lib\net40\Foo.dll", projectSystem.ReadAllText(@"bin\Foo.dll.refresh"));
        }
        public void AddPackageAddsFileToFileSystem()
        {
            // Arrange
            var mockFileSystem = new MockProjectSystem();
            var repository = new LocalPackageRepository(new DefaultPackagePathResolver(mockFileSystem),
                                                        mockFileSystem);
            IPackage package = PackageUtility.CreatePackage("A", "1.0");

            // Act
            repository.AddPackage(package);

            // Assert
            Assert.IsTrue(mockFileSystem.FileExists(@"A.1.0\A.1.0.nupkg"));
        }
        public void AddFilesAddFilesToProjectSystem()
        {
            // Arrange
            var fileSystem = new MockProjectSystem();
            var files = PackageUtility.CreateFiles(new[] { "A", "B", "C" });

            // Act
            fileSystem.AddFiles(files, String.Empty);

            // Assert
            Assert.True(fileSystem.FileExists("A"));
            Assert.True(fileSystem.FileExists("B"));
            Assert.True(fileSystem.FileExists("C"));
        }
        public void InstallPackageUsesProjectTargetFramework()
        {
            // Arrange
            var localRepository = new Mock<MockPackageRepository>() { CallBase = true }.As<ISharedPackageRepository>().Object;
            var sourceRepository = new MockPackageRepository();
            var projectSystem = new MockProjectSystem(new FrameworkName(".NETFramework, Version=4.5"));
            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<IRecentPackageRepository>().Object, 
                new Mock<VsPackageInstallerEvents>().Object);

            var dependencySets = new PackageDependencySet[] {
                new PackageDependencySet(
                    new FrameworkName(".NETFramework, Version=4.5"),
                    new [] { new PackageDependency("B") }),

                new PackageDependencySet(
                    new FrameworkName(".NETFramework, Version=4.0"),
                    new [] { new PackageDependency("C") })
            };

            var package = PackageUtility.CreatePackageWithDependencySets(
                "foo", 
                "1.0", 
                new[] { "hello" },
                dependencySets: dependencySets);

            var packageB = PackageUtility.CreatePackage("B", "2.0", new[] { "good morning" });
            var packageC = PackageUtility.CreatePackage("C", "2.0", new[] { "good morning" });
            sourceRepository.AddPackage(package);
            sourceRepository.AddPackage(packageB);
            sourceRepository.AddPackage(packageC);

            // Act
            packageManager.InstallPackage(projectManager, "foo", new SemanticVersion("1.0"), ignoreDependencies: false, allowPrereleaseVersions: false, logger: NullLogger.Instance);

            // Assert
            Assert.True(packageManager.LocalRepository.Exists(package));
            Assert.True(packageManager.LocalRepository.Exists(packageB));
            Assert.False(packageManager.LocalRepository.Exists(packageC));
            Assert.True(projectManager.LocalRepository.Exists(package));
            Assert.True(projectManager.LocalRepository.Exists(packageB));
        }
        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")));
        }
Beispiel #23
0
        public void UninstallingPackageWithNoDependents()
        {
            // Arrange
            var localRepository = new MockPackageRepository();
            var sourceRepository = new MockPackageRepository();
            var projectSystem = new MockProjectSystem();
            var packageManager = new PackageManager(sourceRepository, new DefaultPackagePathResolver(projectSystem), projectSystem, localRepository);
            var package = PackageUtility.CreatePackage("foo", "1.2.33");
            localRepository.AddPackage(package);

            // Act
            packageManager.UninstallPackage("foo");

            // Assert
            Assert.False(packageManager.LocalRepository.Exists(package));
        }
Beispiel #24
0
        public void AddingPackageReferenceAddsPreprocessedFileToTargetPathWithRemovedExtension()
        {
            // Arrange            
            var sourceRepository = new MockPackageRepository();
            var projectSystem = new MockProjectSystem();
            var projectManager = new ProjectManager(sourceRepository, new DefaultPackagePathResolver(projectSystem), projectSystem, new MockPackageRepository());
            IPackage packageA = PackageUtility.CreatePackage("A", "1.0", new[] { @"foo\bar\file.pp" });
            sourceRepository.AddPackage(packageA);

            // Act
            projectManager.AddPackageReference("A");

            // Assert
            Assert.False(projectSystem.FileExists(@"foo\bar\file.pp"));
            Assert.True(projectSystem.FileExists(@"foo\bar\file"));
        }
Beispiel #25
0
        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 GetPackageFilesOnlyDetectsFilesWithPackageExtension()
        {
            // Arrange
            var mockFileSystem = new MockProjectSystem();
            mockFileSystem.AddFile("foo.nupkg");
            mockFileSystem.AddFile("bar.zip");

            var repository = new LocalPackageRepository(new DefaultPackagePathResolver(mockFileSystem),
                                                        mockFileSystem);

            // Act
            var files = repository.GetPackageFiles().ToList();

            // Assert
            Assert.Equal(1, files.Count);
            Assert.Equal("foo.nupkg", files[0]);
        }
Beispiel #27
0
        public void InstallPackgeWithNullProjectManagerOnlyInstallsIntoPackageManager()
        {
            // 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 packageManager = new VsPackageManager(TestUtils.GetSolutionManager(), sourceRepository, projectSystem, localRepository, new Mock<IRecentPackageRepository>().Object);

            var package = PackageUtility.CreatePackage("foo", "1.0", new[] { "hello" });
            sourceRepository.AddPackage(package);

            // Act
            packageManager.InstallPackage((IProjectManager)null, "foo", new Version("1.0"), ignoreDependencies: false, logger: NullLogger.Instance);

            // Assert
            Assert.IsTrue(packageManager.LocalRepository.Exists(package));
        }
Beispiel #28
0
        public void AddPackageReferenceThrowsWhenNoTargetFrameworkIsCompatibleWithPortableProject()
        {
            // Arrange
            var portableCollection = new NetPortableProfileCollection();
            portableCollection.Add(new NetPortableProfile("Profile104", new [] { VersionUtility.ParseFrameworkName("net45"), VersionUtility.ParseFrameworkName("sl5")}));

            NetPortableProfileTable.Profiles = portableCollection;

            var sourceRepository = new MockPackageRepository();
            var projectSystem = new MockProjectSystem(new FrameworkName(".NETPortable, Version=1.0, Profile=Profile104"));
            var projectManager = new ProjectManager(sourceRepository, new DefaultPackagePathResolver(projectSystem), projectSystem, new MockPackageRepository());
            IPackage packageA = PackageUtility.CreatePackage("A", "1.0", new[] { "silverlight4\\a.txt"});
            sourceRepository.AddPackage(packageA);

            // Act
            ExceptionAssert.Throws<InvalidOperationException>(
                () => projectManager.AddPackageReference("A"),
                "Could not install package 'A 1.0'. You are trying to install this package into a project that targets 'portable-net45+sl50', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.");
        }
Beispiel #29
0
        public void AddRefreshFilesForAssembliesReferencesIncludeFilesUnderLibRoot()
        {
            // Arrange
            var projectSystem = new MockProjectSystem();
            var projectSystemFactory = new Mock<IProjectSystemFactory>();
            projectSystemFactory.Setup(p => p.CreateProjectSystem(It.IsAny<Project>(), It.IsAny<IFileSystemProvider>()))
                                .Returns(projectSystem);

            var websiteHandler = new VsWebsiteHandler(null, projectSystemFactory.Object);

            var packagesFileSystem = new MockFileSystem("x:\\some\\path");
            packagesFileSystem.CreateDirectory("A.1.0");
            packagesFileSystem.CreateDirectory("A.1.0\\lib");
            packagesFileSystem.AddFile("A.1.0\\lib\\one.dll");
            packagesFileSystem.AddFile("A.1.0\\lib\\two.winmd");

            packagesFileSystem.CreateDirectory("B.3.0.0-beta");
            packagesFileSystem.CreateDirectory("B.3.0.0-beta\\lib");
            packagesFileSystem.AddFile("B.3.0.0-beta\\lib\\three.dll");
            packagesFileSystem.AddFile("B.3.0.0-beta\\lib\\four.exe");

            var properties = new MockDteProperties();
            properties.AddProperty("FullPath", "x:\\project");
            properties.AddProperty("TargetFrameworkMoniker", ".NETFramework,Version=v4.0");

            var website = new Mock<Project>();
            website.Setup(p => p.Kind).Returns(VsConstants.WebSiteProjectTypeGuid);
            website.Setup(p => p.Properties).Returns(properties);

            // Act
            websiteHandler.AddRefreshFilesForReferences(
                website.Object, packagesFileSystem, new[] { new PackageName("A", new SemanticVersion("1.0")), 
                                                            new PackageName("B", new SemanticVersion("3.0.0-beta"))});

            // Assert
            Assert.True(projectSystem.DirectoryExists("bin"));
            var refreshFiles = projectSystem.GetFiles("bin", "*.refresh").OrderBy(s => s).ToList();
            Assert.Equal(4, refreshFiles.Count);
            Assert.Equal("bin\\four.exe.refresh", refreshFiles[0]);
            Assert.Equal("bin\\one.dll.refresh", refreshFiles[1]);
            Assert.Equal("bin\\three.dll.refresh", refreshFiles[2]);
            Assert.Equal("bin\\two.winmd.refresh", refreshFiles[3]);
        }
Beispiel #30
0
        public void InstallPackageConvertsVersionToSemanticVersion()
        {
            // Arrange
            var localRepository = new Mock<MockPackageRepository>() { CallBase = true }.As<ISharedPackageRepository>().Object;
            var sourceRepository = new MockPackageRepository();
            var projectRepository = new MockProjectPackageRepository(localRepository);
            var fileSystem = new MockFileSystem();
            var projectSystem = new MockProjectSystem();
            var pathResolver = new DefaultPackagePathResolver(new MockProjectSystem());
            var project = TestUtils.GetProject("Foo");
            var projectManager = new ProjectManager(localRepository, pathResolver, projectSystem, projectRepository);
            var scriptExecutor = new Mock<IScriptExecutor>();
            var packageManager = new Mock<VsPackageManager>(
                TestUtils.GetSolutionManager(), 
                sourceRepository, 
                new Mock<IFileSystemProvider>().Object, 
                fileSystem, 
                localRepository, 
                new Mock<IDeleteOnRestartManager>().Object, 
                new Mock<VsPackageInstallerEvents>().Object,
                /* multiFrameworkTargeting */ null) 
                { 
                    CallBase = true 
                };
            var packageManagerFactory = new Mock<IVsPackageManagerFactory>();
            var packageRepositoryFactory = new Mock<IPackageRepositoryFactory>(MockBehavior.Strict);
            packageManagerFactory.Setup(m => m.CreatePackageManager(It.IsAny<IPackageRepository>(), false)).Returns(packageManager.Object);
            packageManager.Setup(m => m.GetProjectManager(project)).Returns(projectManager);
            packageRepositoryFactory.Setup(r => r.CreateRepository(@"x:\test")).Returns(new MockPackageRepository()).Verifiable();

            var package = NuGet.Test.PackageUtility.CreatePackage("foo", "1.0", new[] { "hello" }, tools: new[] { "init.ps1", "install.ps1" });
            sourceRepository.AddPackage(package);
            var installer = new VsPackageInstaller(packageManagerFactory.Object, scriptExecutor.Object, packageRepositoryFactory.Object, new Mock<IOutputConsoleProvider>().Object, new Mock<IVsCommonOperations>().Object, new Mock<ISolutionManager>().Object, null, null);

            // Act
            installer.InstallPackage(@"x:\test", project, "foo", new Version("1.0"), ignoreDependencies: false);

            // Assert
            scriptExecutor.Verify(e => e.Execute(It.IsAny<string>(), PowerShellScripts.Init, It.IsAny<IPackage>(), null, null, It.IsAny<ILogger>()), Times.Once());
            scriptExecutor.Verify(e => e.Execute(It.IsAny<string>(), PowerShellScripts.Install, It.IsAny<IPackage>(), It.IsAny<Project>(), It.IsAny<FrameworkName>(), It.IsAny<ILogger>()), Times.Once());
            packageRepositoryFactory.Verify();
        }