public void ShouldAddFileInCorrectSubfolder()
        {
            var mockLogger = new MockLogger();
            var sourceProject = new MockProject(@"c:\mockPath1\project1.csproj");
            var targetProject = new MockProject();
            var sourceFolder = new MockProjectItem("MyFolder") { Kind = Constants.vsProjectItemKindPhysicalFolder };
            sourceProject.ProjectItems.AddProjectItem(sourceFolder);
            sourceFolder.ProjectItems.AddProjectItem(new MockProjectItem("MyFile.txt"));

            var targetFolder = new MockProjectItem("MyFolder") { Kind = Constants.vsProjectItemKindPhysicalFolder };
            targetProject.ProjectItems.AddProjectItem(targetFolder);

            var syncher = new ProjectItemsSynchronizer(sourceProject, targetProject, mockLogger, null, new MockProjectItemsFilter());
            string fileToAdd = @"c:\mockPath1\MyFolder\MyFile.txt";

            syncher.FileAddedToSource(fileToAdd);

            Assert.IsTrue(targetFolder.ProjectItems.AddFromFileCalled);
            Assert.AreEqual(1, targetFolder.ProjectItems.Count);
            StringAssert.EndsWith(targetFolder.ProjectItems.Item(0).Name, "MyFile.txt");
            Assert.AreEqual(1, mockLogger.MessageLog.Count);
            StringAssert.Contains(mockLogger.MessageLog[0], "added");
            StringAssert.Contains(mockLogger.MessageLog[0], @"MyFolder\MyFile.txt");
        }
        public void ShouldUseRelativePathWhenEvaluatingFilter()
        {
            var sourceProject = new MockProject(@"c:\mockPath1\project1.csproj");
            var targetProject = new MockProject();
            var mockLogger = new MockLogger();
            var mockFilter = new MockProjectItemsFilter();
            var sourceFolder = new MockProjectItem("MyFolder") { Kind = Constants.vsProjectItemKindPhysicalFolder };
            sourceProject.ProjectItems.AddProjectItem(sourceFolder);
            sourceFolder.ProjectItems.AddProjectItem(new MockProjectItem("ABC.txt"));

            var syncher = new ProjectItemsSynchronizer(sourceProject, targetProject, mockLogger, null, mockFilter);
            string fileToAdd = @"c:\mockPath1\MyFolder\ABC.txt";

            syncher.FileAddedToSource(fileToAdd);

            Assert.IsTrue(mockFilter.IsSynchronizableCalled);
            Assert.AreEqual(@"MyFolder\ABC.txt", mockFilter.IsSynchronizableArgument);
        }
        public void ShouldRemoveLinkedFolderWhenDeletingFolderFromSource()
        {
            var mockLogger = new MockLogger();
            var sourceProject = new MockProject(@"c:\mockPath1\project1.csproj");
            var targetProject = new MockProject();
            var folder = new MockProjectItem("MyFolder") { Kind = Constants.vsProjectItemKindPhysicalFolder };
            targetProject.ProjectItems.AddProjectItem(folder);

            string sourceFolder = Path.Combine(@"c:\mockPath1", @"MyFolder");
            var syncher = new ProjectItemsSynchronizer(sourceProject, targetProject, mockLogger, null, new MockProjectItemsFilter());
            Assert.AreEqual(1, targetProject.ProjectItems.Count);

            syncher.DirectoryRemovedFromSource(sourceFolder);

            Assert.IsTrue(folder.DeleteCalled);
            Assert.AreEqual(1, mockLogger.MessageLog.Count);
            StringAssert.Contains(mockLogger.MessageLog[0], "removed");
            StringAssert.Contains(mockLogger.MessageLog[0], @"MyFolder");
            StringAssert.Contains(mockLogger.MessageLog[0], targetProject.Name);
        }
        public void ShouldThrowIfCOMExceptionErrorCodeIsNotTheExpected()
        {
            var mockSolution = new MockIVsSolution();
            var mockTargetVsHierarchy = new MockVsHierarchy();
            mockSolution.Hierarchies.Add(mockTargetVsHierarchy);
            var sourceProject = new MockProject(@"c:\mockPath1\project1.csproj");
            var targetProject = new MockProject(@"c:\mockPath2\project1.csproj");
            targetProject.ProjectItems.ThrowOnAddFolder = true;
            targetProject.ProjectItems.ErrorCode = VSConstants.S_FALSE;

            var mockHierarchyHelper = new MockHierarchyHelper();
            var mockLogger = new MockLogger();
            var syncher = new ProjectItemsSynchronizer(sourceProject, targetProject, mockLogger, mockSolution, mockHierarchyHelper, new MockProjectItemsFilter());
            string directoryToAdd = @"c:\mockPath1\MyFolder\";

            syncher.DirectoryAddedToSource(directoryToAdd);
        }
        public void ShouldNotRemoveFileIfTargetFileIsNotALink()
        {
            var mockLogger = new MockLogger();
            var sourceProject = new MockProject(@"c:\mockPath1\project1.csproj");
            var targetProject = new MockProject();
            var targetFile = new MockProjectItem("MyClass.cs") { Kind = Constants.vsProjectItemKindPhysicalFile };
            targetProject.ProjectItems.AddProjectItem(targetFile);

            string sourceFile = Path.Combine(@"c:\mockPath1", @"MyClass.cs");
            var syncher = new ProjectItemsSynchronizer(sourceProject, targetProject, mockLogger, null, new MockProjectItemsFilter());
            Assert.AreEqual(1, targetProject.ProjectItems.Count);

            syncher.FileRemovedFromSource(sourceFile);

            Assert.IsFalse(targetFile.DeleteCalled);
            Assert.AreEqual(1, mockLogger.MessageLog.Count);
            StringAssert.Contains(mockLogger.MessageLog[0], "not linked");
            StringAssert.Contains(mockLogger.MessageLog[0], @"MyClass.cs");
            StringAssert.Contains(mockLogger.MessageLog[0], targetProject.Name);
        }
        public void ShouldRemoveLinkedFileWhenDeletingFromSource()
        {
            string sourceFile = @"c:\mockPath1\MyClass.cs";
            var mockLogger = new MockLogger();
            var sourceProject = new MockProject(@"c:\mockPath1\project1.csproj");
            var targetProject = new MockProject();
            var targetFile = new MockProjectItem(sourceFile, true);
            targetProject.ProjectItems.AddProjectItem(targetFile);

            var syncher = new ProjectItemsSynchronizer(sourceProject, targetProject, mockLogger, null, new MockProjectItemsFilter());
            Assert.AreEqual(1, targetProject.ProjectItems.Count);

            syncher.FileRemovedFromSource(sourceFile);

            Assert.IsTrue(targetFile.DeleteCalled);
            Assert.AreEqual(1, mockLogger.MessageLog.Count);
            StringAssert.Contains(mockLogger.MessageLog[0], "removed");
            StringAssert.Contains(mockLogger.MessageLog[0], @"MyClass.cs");
            StringAssert.Contains(mockLogger.MessageLog[0], targetProject.Name);
        }
        public void ShouldIncludeSubDirectoryInTargetWhenItsPresentInFileSystem()
        {
            var mockSolution = new MockIVsSolution();
            var mockTargetVsHierarchy = new MockVsHierarchy();
            var sourceProject = new MockProject(@"c:\mockPath1\project1.csproj");
            var targetProject = new MockProject(@"c:\mockPath2\project1.csproj");
            mockTargetVsHierarchy.GetPropertyProjectValue = targetProject;
            mockSolution.Hierarchies.Add(mockTargetVsHierarchy);

            targetProject.ProjectItems.AddFolder("Folder1", Constants.vsProjectItemKindPhysicalFolder);
            MockProjectItem folder1 = targetProject.ProjectItems.Item(0) as MockProjectItem;
            folder1.ProjectItems.ThrowOnAddFolder = true;

            var mockHierarchyHelper = new MockHierarchyHelper();
            var mockLogger = new MockLogger();
            var syncher = new ProjectItemsSynchronizer(sourceProject, targetProject, mockLogger, mockSolution, mockHierarchyHelper, new MockProjectItemsFilter());
            string directoryToAdd = @"c:\mockPath1\Folder1\Folder2\";

            syncher.DirectoryAddedToSource(directoryToAdd);

            Assert.IsTrue(mockTargetVsHierarchy.AddItemCalled);
            Assert.AreEqual(VSConstants.VSITEMID_ROOT, mockTargetVsHierarchy.AddItemArgumentItemidLoc);
            Assert.AreEqual(string.Empty, mockTargetVsHierarchy.AddItemArgumentItemName);
            Assert.AreEqual<uint>(1, mockTargetVsHierarchy.AddItemArgumentFilesToOpen);
            Assert.AreEqual(@"c:\mockPath2\Folder1\Folder2", mockTargetVsHierarchy.AddItemArgumentArrayFilesToOpen[0]);
            Assert.AreEqual(1, mockLogger.MessageLog.Count);
            StringAssert.Contains(mockLogger.MessageLog[0], "already exists");
            StringAssert.Contains(mockLogger.MessageLog[0], "included");
            StringAssert.Contains(mockLogger.MessageLog[0], @"Folder2");
            StringAssert.Contains(mockLogger.MessageLog[0], targetProject.Name);
        }
        public void ShouldLogMessageIfFileDoesNotExistInTargetProject()
        {
            var mockLogger = new MockLogger();
            var sourceProject = new MockProject(@"c:\mockPath1\project1.csproj");
            var targetProject = new MockProject();

            string sourceFile = Path.Combine(@"c:\mockPath1", @"MyClass.cs");
            var syncher = new ProjectItemsSynchronizer(sourceProject, targetProject, mockLogger, null, new MockProjectItemsFilter());

            syncher.FileRemovedFromSource(sourceFile);

            Assert.AreEqual(1, mockLogger.MessageLog.Count);
            StringAssert.Contains(mockLogger.MessageLog[0], "not linked");
            StringAssert.Contains(mockLogger.MessageLog[0], @"MyClass.cs");
            StringAssert.Contains(mockLogger.MessageLog[0], targetProject.Name);
        }
        public void ShouldIncludeDirectoryInTargetWhenItsPresentInFileSystem()
        {
            var mockSolution = new MockIVsSolution();
            var mockTargetVsHierarchy = new MockVsHierarchy();
            var sourceProject = new MockProject(@"c:\mockPath1\project1.csproj");
            var targetProject = new MockProject(@"c:\mockPath2\project1.csproj");
            mockTargetVsHierarchy.GetPropertyProjectValue = targetProject;
            mockSolution.Hierarchies.Add(mockTargetVsHierarchy);
            var mockHierarchyHelper = new MockHierarchyHelper();
            var mockLogger = new MockLogger();
            var syncher = new ProjectItemsSynchronizer(sourceProject, targetProject, mockLogger, mockSolution, mockHierarchyHelper, new MockProjectItemsFilter());
            string directoryToAdd = @"c:\mockPath1\MyFolder\";
            targetProject.ProjectItems.ThrowOnAddFolder = true;

            syncher.DirectoryAddedToSource(directoryToAdd);

            Assert.IsTrue(mockTargetVsHierarchy.AddItemCalled);
            Assert.AreEqual(VSConstants.VSITEMID_ROOT, mockTargetVsHierarchy.AddItemArgumentItemidLoc);
            Assert.AreEqual(string.Empty, mockTargetVsHierarchy.AddItemArgumentItemName);
            Assert.AreEqual<uint>(1, mockTargetVsHierarchy.AddItemArgumentFilesToOpen);
            Assert.AreEqual(@"c:\mockPath2\MyFolder", mockTargetVsHierarchy.AddItemArgumentArrayFilesToOpen[0]);
        }
        public void ShouldCreateFolderStructureWhenAddingLinkedFile()
        {
            var mockLogger = new MockLogger();
            var sourceProject = new MockProject(@"c:\mockPath1\project1.csproj");
            var targetProject = new MockProject();
            var sourceFolder = new MockProjectItem("MyFolder") { Kind = Constants.vsProjectItemKindPhysicalFolder };
            sourceProject.ProjectItems.AddProjectItem(sourceFolder);
            sourceFolder.ProjectItems.AddProjectItem(new MockProjectItem("MyFile.txt"));

            var syncher = new ProjectItemsSynchronizer(sourceProject, targetProject, mockLogger, null, new MockProjectItemsFilter());
            string fileToAdd = @"c:\mockPath1\MyFolder\MyFile.txt";

            syncher.FileAddedToSource(fileToAdd);

            Assert.IsNotNull(targetProject.ProjectItems.Item(0));
            Assert.AreEqual(targetProject.ProjectItems.Item(0).Name, "MyFolder");
            Assert.IsNotNull(targetProject.ProjectItems.Item(0).ProjectItems);
            Assert.AreEqual(1, targetProject.ProjectItems.Item(0).ProjectItems.Count);
            StringAssert.EndsWith(targetProject.ProjectItems.Item(0).ProjectItems.Item(0).Name, "MyFile.txt");
            Assert.IsTrue(mockLogger.MessageLog.Count > 1);
            string loggedMessage = mockLogger.MessageLog.FirstOrDefault(x => x.IndexOf("folder", StringComparison.OrdinalIgnoreCase) >= 0);
            Assert.IsNotNull(loggedMessage);
            StringAssert.Contains(loggedMessage, "created");
            Assert.AreEqual(-1, loggedMessage.IndexOf("MyFile.txt"));
        }
        public void ShouldAddItemToTargetWhenAddingToSourceProject()
        {
            var sourceProject = new MockProject(@"c:\mockPath1\project1.csproj");
            var targetProject = new MockProject();
            var mockLogger = new MockLogger();
            sourceProject.ProjectItems.AddProjectItem(new MockProjectItem("ABC.txt"));

            var syncher = new ProjectItemsSynchronizer(sourceProject, targetProject, mockLogger, null, new MockProjectItemsFilter());
            string fileToAdd = Path.Combine(@"c:\mockPath1", @"ABC.txt");

            syncher.FileAddedToSource(fileToAdd);

            Assert.IsTrue(targetProject.ProjectItems.AddFromFileCalled);
            Assert.AreEqual(1, targetProject.ProjectItems.Count);
            StringAssert.EndsWith(targetProject.ProjectItems.Item(0).Name, "ABC.txt");
            Assert.AreEqual(1, mockLogger.MessageLog.Count);
            StringAssert.Contains(mockLogger.MessageLog[0], "added");
            StringAssert.Contains(mockLogger.MessageLog[0], "ABC.txt");
            StringAssert.Contains(mockLogger.MessageLog[0], targetProject.Name);
        }
        public void ShouldAddItemToTargetWhenAddingLinkedFileToSourceProject()
        {
            string fileToAdd = @"c:\alternativeExternalPath\file.txt";
            var sourceProject = new MockProject(@"c:\mockPath1\project1.csproj");
            var targetProject = new MockProject();
            var mockLogger = new MockLogger();
            var linkedSourceFile = new MockProjectItem("file.txt");
            linkedSourceFile.MockProperties.PropertiesList.Add(new MockProperty("FullPath", fileToAdd));
            sourceProject.ProjectItems.AddProjectItem(linkedSourceFile);

            var syncher = new ProjectItemsSynchronizer(sourceProject, targetProject, mockLogger, null, new MockProjectItemsFilter());

            syncher.FileAddedToSource(fileToAdd);

            Assert.IsTrue(targetProject.ProjectItems.AddFromFileCalled);
            Assert.AreEqual(1, targetProject.ProjectItems.Count);
            StringAssert.EndsWith(targetProject.ProjectItems.Item(0).Name, "file.txt");
        }
        public void ShouldAddFileThatIsLinkInCorrectSubfolder()
        {
            string fileToAdd = @"c:\alternativeExternalPath\file.txt";
            var mockLogger = new MockLogger();
            var sourceProject = new MockProject(@"c:\mockPath1\project1.csproj");
            var targetProject = new MockProject();
            var sourceFolder = new MockProjectItem("MyFolder") { Kind = Constants.vsProjectItemKindPhysicalFolder };
            sourceProject.ProjectItems.AddProjectItem(sourceFolder);
            var linkedSourceFile = new MockProjectItem("file.txt");
            linkedSourceFile.MockProperties.PropertiesList.Add(new MockProperty("FullPath", fileToAdd));
            sourceFolder.ProjectItems.AddProjectItem(linkedSourceFile);

            var targetFolder = new MockProjectItem("MyFolder") { Kind = Constants.vsProjectItemKindPhysicalFolder };
            targetProject.ProjectItems.AddProjectItem(targetFolder);

            var syncher = new ProjectItemsSynchronizer(sourceProject, targetProject, mockLogger, null, new MockProjectItemsFilter());

            syncher.FileAddedToSource(fileToAdd);

            Assert.IsTrue(targetFolder.ProjectItems.AddFromFileCalled);
            Assert.AreEqual(1, targetFolder.ProjectItems.Count);
            StringAssert.EndsWith(targetFolder.ProjectItems.Item(0).Name, "file.txt");
        }