public void FolderComparison_NullAndNotExistsFolders_True()
 {
     using (var temporaryFolder = new TemporaryFolder())
     {
         IFolder folder1 = new DataFolder(temporaryFolder.Folder + "\\" + "test");
         IFolder folder2 = null;
         IFolderComparisonReport report = folder1.Compare(folder2);
         Assert.That(report.Equal, Is.True);
     }
 }
        public void FolderComparison_ExistsAndNotExistsFolders_False()
        {
            using (var temporaryFolder = new TemporaryFolder())
            {
                IFolder folder2 = new DataFolder(temporaryFolder.Folder + "\\" + "test2");

                IFolderComparisonReport report = temporaryFolder.Compare(folder2);
                Assert.That(report.Equal, Is.False);
            }
        }
        public static IFolderComparisonReport Compare(this IFolder folder1, IFolder folder2, ReportOption reportOption = ReportOption.EqualOnly)
        {
            #region Null & Not Exists folders cases
            if (folder1 == null)
            {
                if (folder2 == null || !folder2.Exists())
                    return new FolderComparisonReport(equal: true);
                return new FolderComparisonReport(equal: false);
            }
            if (folder2 == null)
            {
                if (!folder1.Exists())
                    return new FolderComparisonReport(equal: true);
                return new FolderComparisonReport(equal: false);
            }

            if (!folder1.Exists() && !folder2.Exists())
                return new FolderComparisonReport(equal: true);

            if (!folder1.Exists() && folder2.Exists())
                return new FolderComparisonReport(equal: false);
            if (folder1.Exists() && !folder2.Exists())
                return new FolderComparisonReport(equal: false);
            #endregion

            #region Files comparison
            string[] entries1 = Directory.GetFileSystemEntries(folder1.Folder);
            string[] entries2 = Directory.GetFileSystemEntries(folder2.Folder);

            if (entries1.Length == 0 && entries2.Length == 0)
                return new FolderComparisonReport(equal: true);

            switch (reportOption)
            {
                case ReportOption.EqualOnly:
                    if (entries1.Length != entries2.Length)
                        return new FolderComparisonReport(equal: false);
                    break;
            }

            string[] files1 = Array.ConvertAll(Directory.GetFiles(folder1.Folder), fileName => Path.GetFileName(fileName).ToLower());
            string[] files2 = Array.ConvertAll(Directory.GetFiles(folder2.Folder), fileName => Path.GetFileName(fileName).ToLower());

            var uniqInFolder1 = new List<string>();
            var uniqInFolder2 = new List<string>();

            switch (reportOption)
            {
                case ReportOption.EqualOnly:
                    bool foundAllFilesFromList1InList2 = Found(files1, files2);
                    if (!foundAllFilesFromList1InList2)
                        return new FolderComparisonReport(equal: false);

                    bool foundAllFilesFromList2InList1 = Found(files2, files1);
                    if (!foundAllFilesFromList2InList1)
                        return new FolderComparisonReport(equal: false);
                    break;
                case ReportOption.CollectDifferentFiles:
                    FoundUniq(files1, files2, uniqInFolder1, uniqInFolder2);
                    break;
                default:
                    throw new NotImplementedException(String.Format("reportOption {0} not implemented", reportOption));
            }

            #endregion

            #region Directory comparison
            string[] directories1 = Array.ConvertAll(Directory.GetDirectories(folder1.Folder), folderName => Path.GetFileName(folderName).ToLower());
            string[] directories2 = Array.ConvertAll(Directory.GetDirectories(folder2.Folder), folderName => Path.GetFileName(folderName).ToLower());

            switch (reportOption)
            {
                case ReportOption.EqualOnly:
                    bool foundAllDirectoriesFromList1InList2 = Found(directories1, directories2);
                    if (!foundAllDirectoriesFromList1InList2)
                        return new FolderComparisonReport(equal: false);
                    bool foundAllDirectoriesFromList2InList1 = Found(directories2, directories1);
                    if (!foundAllDirectoriesFromList2InList1)
                        return new FolderComparisonReport(equal: false);
                    break;
                case ReportOption.CollectDifferentFiles:
                    FoundUniq(directories1, directories2, uniqInFolder1, uniqInFolder2);
                    break;
                default:
                    throw new NotImplementedException(String.Format("reportOption {0} not implemented", reportOption));
            }

            foreach (string subFolder in directories1)
            {
                IFolder subFolder1 = new DataFolder(Path.Combine(folder1.Folder, subFolder));
                IFolder subFolder2 = new DataFolder(Path.Combine(folder2.Folder, subFolder));
                IFolderComparisonReport comparisonReport = subFolder1.Compare(subFolder2, reportOption);
                switch (reportOption)
                {
                    case ReportOption.EqualOnly:
                        if (!comparisonReport.Equal)
                            return new FolderComparisonReport(equal: false);
                        break;
                    case ReportOption.CollectDifferentFiles:
                        uniqInFolder1.AddRange(comparisonReport.Folder1Files.ConvertAll(fileName => subFolder + @"\" + fileName));
                        uniqInFolder2.AddRange(comparisonReport.Folder2Files.ConvertAll(fileName => subFolder + @"\" + fileName));
                        break;
                    default:
                        throw new NotImplementedException(String.Format("reportOption {0} not implemented", reportOption));
                }
            }
            #endregion

            switch (reportOption)
            {
                case ReportOption.EqualOnly:
                    return new FolderComparisonReport(equal: true);
                case ReportOption.CollectDifferentFiles:
                    return new FolderComparisonReport(uniqInFolder1, uniqInFolder2);
                default:
                    throw new NotImplementedException(String.Format("reportOption {0} not implemented", reportOption));
            }
        }