示例#1
0
        public void TestCompare_When_Directories_Are_Same()
        {
            var tempDir = Path.GetTempPath();
            var testDir = Path.Combine(tempDir, Guid.NewGuid().ToString("N"));

            Directory.CreateDirectory(testDir);

            try
            {
                // Arrange
                var dir1Path = Path.Combine(testDir, "1");
                Directory.CreateDirectory(dir1Path);

                File.WriteAllText(Path.Combine(dir1Path, "both_same.txt"), "1234567890");

                Directory.CreateDirectory(Path.Combine(dir1Path, "both"));
                File.WriteAllText(Path.Combine(dir1Path, "both", "both_same.txt"), "1234567890");

                var dir2Path = Path.Combine(testDir, "2");
                Directory.CreateDirectory(dir2Path);

                File.WriteAllText(Path.Combine(dir2Path, "both_same.txt"), "1234567890");

                Directory.CreateDirectory(Path.Combine(dir2Path, "both"));
                File.WriteAllText(Path.Combine(dir2Path, "both", "both_same.txt"), "1234567890");

                // Act
                var results = DirectoryComparer.Compare(dir1Path, dir2Path);

                // Assert
                var fileResults = results.OfType <FileCompareResult>().ToArray();
                Assert.AreEqual(1, fileResults.Length);
                var sameFileResult = fileResults.Single(r => r.Name == "both_same.txt");
                Assert.IsTrue(sameFileResult.AreSame);
                Assert.AreEqual(Path.Combine(dir1Path, "both_same.txt"), sameFileResult.FullPath1);
                Assert.AreEqual(Path.Combine(dir2Path, "both_same.txt"), sameFileResult.FullPath2);

                var dirResults = results.OfType <DirectoryCompareResult>().ToArray();

                Assert.AreEqual(1, dirResults.Length);
                var bothDirResult = dirResults.Single(r => r.Name == "both");
                Assert.IsTrue(bothDirResult.AreSame);
                Assert.AreEqual(Path.Combine(dir1Path, "both"), bothDirResult.FullPath1);
                Assert.AreEqual(Path.Combine(dir2Path, "both"), bothDirResult.FullPath2);
                Assert.AreEqual(1, bothDirResult.Items.Count);
            }
            finally
            {
                Directory.Delete(testDir, recursive: true);
            }
        }
        private IReadOnlyDictionary <string, DirectoryDifference> CreateComparisonsForInvocationUnit(IReadOnlyDictionary <string, string> commandAndPathMap)
        {
            Dictionary <string, DirectoryDifference> comparisonResults = new Dictionary <string, DirectoryDifference>();

            foreach (KeyValuePair <string, string> commandAndRelativePath in commandAndPathMap)
            {
                string command      = commandAndRelativePath.Key;
                string relativePath = commandAndRelativePath.Value;

                DirectoryComparer comparer = new DirectoryComparer(_masterDataBasePath, _secondaryDataBasePath, relativePath);
                comparisonResults[command] = comparer.Compare();
            }

            return(comparisonResults);
        }