示例#1
0
        public IEnumerable <FileDifferences> CompareDirectories(string oldDirectory, string newDirectory)
        {
            List <string>          fileNames   = new List <string>();
            List <FileDifferences> differences = new List <FileDifferences>();

            foreach (string newFile in directoryReader.GetFiles(newDirectory, "*.htm"))
            {
                string newFileName = Path.GetFileName(newFile);
                string oldFile     = Path.Combine(oldDirectory, newFileName);
                fileNames.Add(oldFile);
                if (newFileName == "index.htm")
                {
                    continue;
                }

                string oldFileContent = fileReader.Exists(oldFile) ? fileReader.ReadAllText(oldFile) : string.Empty;
                string newFileContent = fileReader.ReadAllText(newFile);
                IEnumerable <FileDifferences> fileDifferences = CompareFileContent(oldFileContent, newFileContent);
                if (fileDifferences != null)
                {
                    differences.AddRange(fileDifferences);
                }
            }

            AddDeletedFileComparisons(oldDirectory, newDirectory, fileNames, differences);
            return(differences);
        }
示例#2
0
        public void CompareCurrentWithPrevious()
        {
            var files = _directoryReader.GetFiles(_directoryReader.GetCurrentDirectory(), "log_*.txt");

            if (files.Length < 2)
            {
                throw new ArgumentOutOfRangeException($"{nameof(files.Length)}={files.Length}", "Not enough log files in the the directory to do a comparison. Expecting 2 or more files.");
            }

            var mostRecentLog = (from file in files
                                 orderby DateTime.ParseExact(_pathReader.GetFileNameWithoutExtension(file).Replace("log_", ""),
                                                             "yyyy_MM_d__HH_mm_ss", CultureInfo.InvariantCulture) descending
                                 select file).Take(1).First();


            const string errorKey = "[ERR]";

            foreach (var file in files)
            {
                if (file == mostRecentLog)
                {
                    continue;
                }

                foreach (var line in _fileReader.ReadAllLines(file))
                {
                    if (line.Contains(errorKey))
                    {
                        var indexOf     = line.IndexOf(errorKey, StringComparison.Ordinal);
                        var trimmedLine = line.Remove(0, indexOf + 1 + errorKey.Length).Trim();
                        _previousScan.Add(trimmedLine);
                    }
                }
            }

            foreach (var line in _fileReader.ReadAllLines(mostRecentLog))
            {
                if (line.Contains(errorKey))
                {
                    var indexOf     = line.IndexOf(errorKey, StringComparison.Ordinal);
                    var trimmedLine = line.Remove(0, indexOf + 1 + errorKey.Length).Trim();
                    _currentScan.Add(trimmedLine);
                }
            }

            _currentScan.ExceptWith(_previousScan);

            var totalProblems = 0;

            foreach (var line in _currentScan)
            {
                _logger.Error(line);
                totalProblems++;
            }

            if (totalProblems > 0)
            {
                _logger.Info($"Total problems found: {totalProblems}.");
            }
        }
示例#3
0
        public void Should_Find_Source_File_Names()
        {
            FileCrawler fileCrawler = CreateFileCrawler();
            string      directory   = "TestData";

            _subDirectoryReader.GetFiles("TestData", "*.cs").Returns(new string[] { @"TestData\TestData.cs" });
            _subDirectoryReader.GetDirectories("TestData").Returns(new string[] { @"TestData\TestDataSubDirectory" });
            _subDirectoryReader.GetFiles(@"TestData\TestDataSubDirectory", "*.cs").Returns(new string[] { @"TestData\TestDataSubDirectory\SubTestData.cs" });
            _subDirectoryReader.GetDirectories(@"TestData\TestDataSubDirectory").Returns(Array.Empty <string>());
            var expectedResult = new List <string>()
            {
                @"TestData\TestData.cs",
                @"TestData\TestDataSubDirectory\SubTestData.cs"
            };

            IEnumerable <string> result = fileCrawler.FindSourceFileNames(
                directory);

            result.Should().BeEquivalentTo(expectedResult);
        }
示例#4
0
 public IEnumerable <string> FindSourceFileNames(string directory)
 {
     foreach (string filePath in _directoryReader.GetFiles(directory, "*.cs"))
     {
         yield return(filePath);
     }
     foreach (string directoryPath in _directoryReader.GetDirectories(directory))
     {
         foreach (string filePath in FindSourceFileNames(directoryPath))
         {
             yield return(filePath);
         }
     }
 }
示例#5
0
        public async Task <IEnumerable <Article> > GetOrderedByDateAsync()
        {
            const string searchPattern = "*.html";
            var          files         = _directoryReader.GetFiles(_articleDirectoryPath, searchPattern);

            if (files == null || files.Length == 0)
            {
                return(null);
            }

            var articles = new SortedSet <Article>(new ByPublishDateDescending());

            foreach (var file in files)
            {
                var article = await _articleParser.ParseAsync(file).ConfigureAwait(false);

                articles.Add(article);
            }

            return(articles);
        }
        public string Hash(string fileOrFolderName)
        {
            var files = _directoryReader
                        .GetFiles(fileOrFolderName)
                        .ToList();

            for (int i = 0; i < files.Count; i++)
            {
                string file = files[i];

                // hash path
                string relativePath = file.Substring(fileOrFolderName.Length + 1);
                var    pathBytes    = Encoding.UTF8.GetBytes(relativePath.ToLower());
                HashAlgorithm.TransformBlock(pathBytes, 0, pathBytes.Length, pathBytes, 0);

                // hash contents
                try
                {
                    var contentBytes = File.ReadAllBytes(file);
                    if (i == files.Count - 1)
                    {
                        HashAlgorithm.TransformFinalBlock(contentBytes, 0, contentBytes.Length);
                    }
                    else
                    {
                        HashAlgorithm.TransformBlock(contentBytes, 0, contentBytes.Length, contentBytes, 0);
                    }
                }
                catch
                {
                    // can't open file: continue
                }
            }

            return(BitConverter
                   .ToString(HashAlgorithm.Hash)
                   .Replace("-", "")
                   .ToLower());
        }
        private IEnumerable <string> VisitDirectory(string path, VisitState visitState)
        {
            var visitArgs = new VisitArgs(path);

            OnDirectoryFound(visitArgs);
            if (visitArgs.StopSearch)
            {
                visitState.SearchFinished = true;
            }

            if (!IsFiltered(path))
            {
                yield break;
            }

            OnFilteredDirectoryFound(visitArgs);
            if (visitArgs.StopSearch)
            {
                visitState.SearchFinished = true;
            }

            yield return(path);

            foreach (var visitPath in _directoryReader.GetDirectories(path).SelectMany(p => VisitDirectory(p, visitState)))
            {
                yield return(visitPath);
            }

            foreach (var filePath in _directoryReader.GetFiles(path))
            {
                if (VisitFile(filePath, visitState) == filePath)
                {
                    yield return(filePath);
                }
            }
        }
        /// <summary>
        /// Compares the files defined in the WixDirectoryElement against those
        /// on the file system and returns any differences.
        /// </summary>
        public WixPackageFilesDiffResult[] Compare(WixDirectoryElementBase directoryElement)
        {
            List <string> wixPackageFiles = GetAllFiles(directoryElement);
            List <string> files           = new List <string>();

            // Find all files on the file system based on the directories
            // used in the Wix document.
            searchedDirectories = new List <string>();
            foreach (string fileName in wixPackageFiles)
            {
                string directory = Path.GetDirectoryName(fileName);
                if (!HasDirectoryBeenSearched(directory))
                {
                    if (directoryReader.DirectoryExists(directory))
                    {
                        foreach (string directoryFileName in directoryReader.GetFiles(directory))
                        {
                            if (!excludedFileNames.IsExcluded(directoryFileName))
                            {
                                files.Add(Path.Combine(directory, directoryFileName));
                            }
                        }
                    }
                }
            }

            // Look for new files.
            List <string> missingFiles = new List <string>();
            List <string> removedFiles = new List <string>();

            foreach (string fileName in wixPackageFiles)
            {
                int index = GetFileNameIndex(files, fileName);
                if (index >= 0)
                {
                    removedFiles.Add(files[index]);
                    files.RemoveAt(index);
                }
                else
                {
                    // Check that this file has not already been removed.
                    index = GetFileNameIndex(removedFiles, fileName);
                    if (index == -1)
                    {
                        missingFiles.Add(fileName);
                    }
                }
            }

            // Add new files.
            List <WixPackageFilesDiffResult> results = new List <WixPackageFilesDiffResult>();

            foreach (string fileName in files)
            {
                results.Add(new WixPackageFilesDiffResult(fileName, WixPackageFilesDiffResultType.NewFile));
            }

            // Add missing files.
            foreach (string fileName in missingFiles)
            {
                results.Add(new WixPackageFilesDiffResult(fileName, WixPackageFilesDiffResultType.MissingFile));
            }

            // Add new directories.
            results.AddRange(GetNewDirectories());

            return(results.ToArray());
        }
示例#9
0
        public void GetFiles()
        {
            var results = _reader.GetFiles(@"C:\gitrepo\DependencyTracker");

            Assert.NotEmpty(results);
        }