public static bool CompareDirectory(DirectoryInfo targetDir, DirectoryInfo actualDir) { if (!targetDir.Exists) { Console.WriteLine("No Output folders comparison"); return(true); //If the output_expected does not exist it means that the test doesn't have any expected output. } // Take a snapshot of the file system. var targetFiles = targetDir.GetFiles("*.*", System.IO.SearchOption.AllDirectories).ToList(); var actualFiles = actualDir.GetFiles("*.*", System.IO.SearchOption.AllDirectories).ToList(); targetFiles.Sort((f1, f2) => string.Compare(f1.Name, f2.Name, StringComparison.Ordinal)); actualFiles.Sort((f1, f2) => string.Compare(f1.Name, f2.Name, StringComparison.Ordinal)); FileCompare myFileCompare = new FileCompare(); /* * // This query determines whether the two folders contain * // identical file lists, based on the custom file comparer * // that is defined in the FileCompare class. * // The query executes immediately because it returns a bool. * bool areIdentical = targetFiles.SequenceEqual(actualFiles, myFileCompare); * * if (areIdentical == true) { * Console.WriteLine("the two folders are the same"); * } else { * Console.WriteLine("The two folders are not the same"); * } */ // Find the common files. It produces a sequence and doesn't // execute until the foreach statement. var commonTargetFiles = targetFiles.Intersect(actualFiles, myFileCompare).ToList(); var commonActualFiles = actualFiles.Intersect(targetFiles, myFileCompare).ToList(); if (commonTargetFiles.Count != commonActualFiles.Count) { throw new InvalidOperationException(); } bool dirIdentical = true; for (int i = 0; i < commonTargetFiles.Count; i++) { var targetFileContent = File.ReadAllLines(commonTargetFiles[i].FullName); var actualFileContent = File.ReadAllLines(commonActualFiles[i].FullName); if (!targetFileContent.SequenceEqual(actualFileContent)) { Console.WriteLine("File not equals: " + commonTargetFiles[i]); Console.WriteLine("___Actual file content___:\n"); foreach (var actual in actualFileContent) { Console.WriteLine(actual); } Console.WriteLine("\n________________\n"); Console.WriteLine("___Expected file content___:\n"); foreach (var expected in targetFileContent) { Console.WriteLine(expected); } Console.WriteLine("________________"); dirIdentical = false; } } // Find the set difference between the two folders. // Files only in targetFiles var queryTargetFilesOnly = (from file in targetFiles select file).Except(actualFiles, myFileCompare); if (queryTargetFilesOnly.Any()) { Console.WriteLine("Only present in expected folder"); dirIdentical = false; foreach (var v in queryTargetFilesOnly) { Console.WriteLine(v.FullName); } } var queryActualFilesOnly = (from file in actualFiles select file).Except(targetFiles, myFileCompare); if (queryActualFilesOnly.Any()) { Console.WriteLine("Only present in actual folder"); dirIdentical = false; foreach (var v in queryActualFilesOnly) { Console.WriteLine(v.FullName); } } return(dirIdentical); }
/// <summary> /// Compare results of LSR tests /// Or replace automatically the input content of the test if autoReplace is true /// </summary> /// <param name="targetDir"></param> /// <param name="actualDir"></param> /// <param name="testPlaylistDirectory"></param> /// <returns></returns> public static bool CompareDirectory(DirectoryInfo targetDir, DirectoryInfo actualDir, string testPlaylistDirectory = null) { bool autoReplace = false; if (targetDir == null && actualDir == null && testPlaylistDirectory.Contains("LSRTests")) { return(autoReplace); } if (!targetDir.Exists) { Console.WriteLine("No Output folders comparison"); return(true); //If the output_expected does not exist it means that the test doesn't have any expected output. } // Take a snapshot of the file system. var targetFiles = targetDir.GetFiles("*.*", System.IO.SearchOption.AllDirectories).ToList(); var actualFiles = actualDir.GetFiles("*.*", System.IO.SearchOption.AllDirectories).ToList(); targetFiles.Sort((f1, f2) => string.Compare(f1.Name, f2.Name, StringComparison.Ordinal)); actualFiles.Sort((f1, f2) => string.Compare(f1.Name, f2.Name, StringComparison.Ordinal)); FileCompare myFileCompare = new FileCompare(); /* * // This query determines whether the two folders contain * // identical file lists, based on the custom file comparer * // that is defined in the FileCompare class. * // The query executes immediately because it returns a bool. * bool areIdentical = targetFiles.SequenceEqual(actualFiles, myFileCompare); * * if (areIdentical == true) { * Console.WriteLine("the two folders are the same"); * } else { * Console.WriteLine("The two folders are not the same"); * } */ // Find the common files. It produces a sequence and doesn't // execute until the foreach statement. var commonTargetFiles = targetFiles.Intersect(actualFiles, myFileCompare).ToList(); var commonActualFiles = actualFiles.Intersect(targetFiles, myFileCompare).ToList(); if (commonTargetFiles.Count != commonActualFiles.Count) { throw new InvalidOperationException(); } bool dirIdentical = true; for (int i = 0; i < commonTargetFiles.Count; i++) { var targetFileContent = File.ReadAllLines(commonTargetFiles[i].FullName); var actualFileContent = File.ReadAllLines(commonActualFiles[i].FullName); if (!targetFileContent.SequenceEqual(actualFileContent)) { if (autoReplace && testPlaylistDirectory != null) { string path = string.Empty; //Convert path from generated test files to orignal string projectPath = commonTargetFiles[i].FullName.Split(new[] { "bin" }, StringSplitOptions.None)[0]; string testRelativePath = commonTargetFiles[i].FullName.Split(new[] { "Debug" }, StringSplitOptions.None)[1]; path = projectPath + testPlaylistDirectory + testRelativePath; if (testPlaylistDirectory.Contains("LanguageServer") && path != String.Empty) { ReplaceLSRTestFile(path, actualFileContent); } else if (testPlaylistDirectory.Contains("CLI") && path != String.Empty) { ReplaceCliTestFile(path, actualFileContent); } Console.WriteLine("File not equals: " + commonTargetFiles[i]); Console.WriteLine("Input file has been modified\n"); Console.WriteLine("Please rerun unit test\n"); dirIdentical = false; } else { Console.WriteLine("File not equals: " + commonTargetFiles[i]); Console.WriteLine("See \"CLITest.cs\" CompareDirectory method to autoreplace input file"); Console.WriteLine("___Actual file content___:\n"); foreach (var actual in actualFileContent) { Console.WriteLine(actual); } Console.WriteLine("\n________________\n"); Console.WriteLine("___Expected file content___:\n"); foreach (var expected in targetFileContent) { Console.WriteLine(expected); } Console.WriteLine("________________"); dirIdentical = false; } } } // Find the set difference between the two folders. // Files only in targetFiles var queryTargetFilesOnly = (from file in targetFiles select file).Except(actualFiles, myFileCompare); if (queryTargetFilesOnly.Any()) { Console.WriteLine("Only present in expected folder"); dirIdentical = false; foreach (var v in queryTargetFilesOnly) { Console.WriteLine(v.FullName); } } var queryActualFilesOnly = (from file in actualFiles select file).Except(targetFiles, myFileCompare); if (queryActualFilesOnly.Any()) { Console.WriteLine("Only present in actual folder"); dirIdentical = false; foreach (var v in queryActualFilesOnly) { Console.WriteLine(v.FullName); } } return(dirIdentical); }