Ejemplo n.º 1
0
        protected void CompareScreenshots(ExecutionOptions options, IUserConfig userConfig)
        {
            _logger.LogDebug("Starting the compare mode");

            var galleryModel = new GalleryModel
            {
                SourceDirectory = GetDirectory(userConfig.SourceDirectory),
                TargetDirectory = GetDirectory(userConfig.TargetDirectory),
                ResultDirectory = _directoryUtils.GenerateDirectoryFromPattern(userConfig.ResultDirectory)
            };

            if (!Directory.Exists(galleryModel.ResultDirectory))
            {
                try
                {
                    Directory.CreateDirectory(galleryModel.ResultDirectory);
                }
                catch (Exception ex)
                {
                    _logger.LogError(EventIds.CannotCreateeResutFolder, ex, $"Cannot create the directory {galleryModel.ResultDirectory}");
                    throw;
                }
            }

            var parallelOptions = new ParallelOptions()
            {
                MaxDegreeOfParallelism = userConfig.NumberOfThreads <= 0 ? 10 : userConfig.NumberOfThreads
            };

            Parallel.ForEach(Directory.GetFiles(galleryModel.SourceDirectory), parallelOptions, (sourceFile) =>
            {
                var targetFile = sourceFile.Replace(galleryModel.SourceDirectory, galleryModel.TargetDirectory);
                var resultFile = sourceFile.Replace(galleryModel.SourceDirectory, galleryModel.ResultDirectory);

                //Check if the diff file already exist and delete it if necessary
                CheckIfDiffFileExistAndDeleteItIsNecessary(resultFile);

                if (File.Exists(targetFile))
                {
                    //Compare the images
                    var percentageOfDifference = _imageComparer.Compare(sourceFile, targetFile, userConfig.Fuzziness, resultFile, userConfig.HighlightColor);

                    var urlTuple = GetUrlTupleFromUserConfig(userConfig, sourceFile);

                    galleryModel.Lines.Add(new GalleryLine()
                    {
                        DifferenceRate   = percentageOfDifference,
                        DifferencesImage = resultFile,
                        SourceImage      = sourceFile,
                        TargetImage      = targetFile,
                        Name             = Path.GetFileName(sourceFile),
                        //Url = urlTuple?.Value
                    });
                }
                else
                {
                    _logger.LogError($"The file '{sourceFile}' exist in the source but is not present in the target: '{targetFile}'");
                }
            });

            //Create the galleries
            try
            {
                _razorRenderer.RenderAndSave(userConfig.GalleryTemplateFullPath, galleryModel, userConfig.GalleryResult);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Error when rendering the gallery of differences. Template: '{userConfig.GalleryTemplateFullPath}'");
                throw;
            }
        }