Esempio n. 1
0
        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);
        }
Esempio n. 2
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);
        }
Esempio n. 3
0
        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]);
        }
Esempio n. 4
0
        public void RemovePackageWithXdtTransformFile()
        {
            // Arrange
            var mockProjectSystem = new MockProjectSystem();
            var mockRepository    = new MockPackageRepository();

            mockProjectSystem.AddFile("web.config",
                                      @"<configuration>
    <system.web><compilation debug=""false"" /></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.uninstall.xdt");
            file.Setup(m => m.EffectivePath).Returns("web.config.uninstall.xdt");
            file.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 });
            mockRepository.AddPackage(package.Object);

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

            // Assert 1
            Assert.False(mockProjectSystem.FileExists("web.config.uninstall.xdt"));
            Assert.Equal(
                @"<configuration>
    <system.web><compilation debug=""false"" /></system.web>
</configuration>
", mockProjectSystem.OpenFile("web.config").ReadToEnd());

            // Act 2
            projectManager.RemovePackageReference("A");

            // Assert 2
            Assert.False(mockProjectSystem.FileExists("web.config.uninstall.xdt"));
            Assert.True(mockProjectSystem.FileExists("web.config"));
            Assert.Equal(
                @"<configuration>
    <system.web></system.web>
</configuration>
", mockProjectSystem.OpenFile("web.config").ReadToEnd());
        }
Esempio n. 5
0
        public void RemovePackageDoesNotRemovesRootIfNotEmpty()
        {
            // Arrange
            var mockFileSystem = new MockProjectSystem();

            mockFileSystem.AddFile(@"A.1.0\A.1.0.nupkg");
            mockFileSystem.AddFile(@"B.1.0\B.1.0.nupkg");
            var repository = new LocalPackageRepository(new DefaultPackagePathResolver(mockFileSystem),
                                                        mockFileSystem);
            IPackage package = PackageUtility.CreatePackage("A", "1.0");

            // Act
            repository.RemovePackage(package);

            // Assert
            Assert.Equal(2, mockFileSystem.Deleted.Count);
            Assert.True(mockFileSystem.Deleted.Contains("A.1.0"));
            Assert.True(mockFileSystem.Deleted.Contains(@"A.1.0\A.1.0.nupkg"));
        }
        public void AddFilesDoNotCallResolveFileConflictIfThereIsNoFileConflict()
        {
            var logger = new Mock <ILogger>();

            // Arrange
            var project = new MockProjectSystem();

            project.AddFile("a.txt", "this is a");
            project.AddFile("c.txt", "this is c");
            project.Logger = logger.Object;

            var files = PackageUtility.CreateFiles(new[] { "b.txt", "d.txt" }, "content");

            // Act
            project.AddFiles(files, new Dictionary <FileTransformExtensions, IPackageFileTransformer>());

            // Assert
            logger.Verify(l => l.ResolveFileConflict(It.IsAny <string>()), Times.Never());
        }
        public void AddFilesCallResolveFileConflictIfThereIsFileConflict()
        {
            var logger = new Mock <ILogger>();

            // Arrange
            var project = new MockProjectSystem(VersionUtility.ParseFrameworkName("net40"), "x:\\root");

            project.AddFile("a.txt", "this is a");
            project.AddFile("c.txt", "this is c");
            project.Logger = logger.Object;

            var files = PackageUtility.CreateFiles(new[] { "a.txt", "b.txt" }, "content");

            // Act
            project.AddFiles(files, new Dictionary <FileTransformExtensions, IPackageFileTransformer>());

            // Assert
            logger.Verify(l => l.ResolveFileConflict("File 'a.txt' already exists in project 'x:\\root'. Do you want to overwrite it?"), Times.Once());
        }
Esempio n. 8
0
        public void GetPackageFilesDetectsFilesInRootOrFirstLevelOfFolders()
        {
            // Arrange
            var mockFileSystem = new MockProjectSystem();

            mockFileSystem.AddFile("P1.nupkg");
            mockFileSystem.AddFile("bar.zip");
            mockFileSystem.AddFile(@"baz\P2.nupkg");
            mockFileSystem.AddFile(@"A\B\P3.nupkg");
            mockFileSystem.AddFile(@"A\P4.nupkg");
            var repository = new LocalPackageRepository(new DefaultPackagePathResolver(mockFileSystem),
                                                        mockFileSystem);

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

            // Assert
            Assert.Equal(3, files.Count);
            Assert.Equal(@"baz\P2.nupkg", files[0]);
            Assert.Equal(@"A\P4.nupkg", files[1]);
            Assert.Equal("P1.nupkg", files[2]);
        }
        public void AddFilesDoNotCallResolveFileConflictIfTheConflictFileDoesNotBelongToProject()
        {
            var logger = new Mock <ILogger>();

            // Arrange
            var project = new MockProjectSystem();

            project.AddFile("a.txt", "this is a");
            project.AddFile("b.txt", "this is b");
            project.ExcludeFileFromProject("b.txt");
            project.Logger = logger.Object;

            var files = PackageUtility.CreateFiles(new[] { "b.txt", "d.txt" }, "content");

            // Act
            project.AddFiles(files, new Dictionary <FileTransformExtensions, IPackageFileTransformer>());

            // Assert
            Assert.True(project.FileExists("d.txt"));
            Assert.False(project.FileExistsInProject("b.txt"));
            logger.Verify(l => l.ResolveFileConflict(It.IsAny <string>()), Times.Never());
        }
Esempio n. 10
0
        public void AddFilesAskingForResolutionForEveryConflict()
        {
            var resolutions = new FileConflictResolution[]
            {
                FileConflictResolution.Ignore,
                FileConflictResolution.Overwrite,
                FileConflictResolution.Ignore,
                FileConflictResolution.IgnoreAll,
                FileConflictResolution.OverwriteAll,
                FileConflictResolution.Overwrite,
            };

            var index  = 0;
            var logger = new Mock <ILogger>();

            logger.Setup(l => l.ResolveFileConflict(It.IsAny <string>()))
            .Returns(() => resolutions[index++]);

            // Arrange
            var project = new MockProjectSystem();

            project.AddFile("a.txt", "this is a");
            project.AddFile("b.txt", "this is b");
            project.AddFile("c.txt", "this is c");
            project.AddFile("d.txt", "this is d");
            project.AddFile("e.txt", "this is e");
            project.AddFile("f.txt", "this is f");
            project.Logger = logger.Object;

            var files = PackageUtility.CreateFiles(new [] { "a.txt", "b.txt", "c.txt", "d.txt", "e.txt", "f.txt" }, "content");

            // Act
            project.AddFiles(files, new Dictionary <FileTransformExtensions, IPackageFileTransformer>());

            // Assert
            Assert.True(project.FileExists("a.txt"));
            Assert.True(project.FileExists("b.txt"));
            Assert.True(project.FileExists("c.txt"));
            Assert.True(project.FileExists("d.txt"));
            Assert.True(project.FileExists("e.txt"));
            Assert.True(project.FileExists("f.txt"));

            logger.Verify(l => l.ResolveFileConflict(It.IsAny <string>()), Times.Exactly(6));

            Assert.Equal("this is a", project.ReadAllText("a.txt"));
            Assert.Equal("content\\b.txt", project.ReadAllText("b.txt"));
            Assert.Equal("this is c", project.ReadAllText("c.txt"));
            Assert.Equal("this is d", project.ReadAllText("d.txt"));
            Assert.Equal("content\\e.txt", project.ReadAllText("e.txt"));
            Assert.Equal("content\\f.txt", project.ReadAllText("f.txt"));
        }
Esempio n. 11
0
        public void RemovePackageRemovesPackageFileAndDirectoryAndRoot()
        {
            // Arrange
            var mockFileSystem = new MockProjectSystem();

            mockFileSystem.AddFile(PathFixUtility.FixPath(@"A.1.0\A.1.0.nupkg"));
            var repository = new LocalPackageRepository(new DefaultPackagePathResolver(mockFileSystem),
                                                        mockFileSystem);
            IPackage package = PackageUtility.CreatePackage("A", "1.0");

            // Act
            repository.RemovePackage(package);

            // Assert
            Assert.Equal(3, mockFileSystem.Deleted.Count);
            Assert.True(mockFileSystem.Deleted.Contains(""));
            Assert.True(mockFileSystem.Deleted.Contains("A.1.0"));
            Assert.True(mockFileSystem.Deleted.Contains(PathFixUtility.FixPath(@"A.1.0\A.1.0.nupkg")));
        }
Esempio n. 12
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());
        }