예제 #1
0
        public IEnumerable <Mutant> FilterMutants(IEnumerable <Mutant> mutants, ReadOnlyFileLeaf file, StrykerOptions options)
        {
            var includePattern = options.FilePatterns.Where(x => !x.IsExclude).ToList();
            var excludePattern = options.FilePatterns.Where(x => x.IsExclude).ToList();

            return(mutants.Where(IsMutantIncluded));

            bool IsMutantIncluded(Mutant mutant)
            {
                // Check if the the mutant is included.
                if (!includePattern.Any(MatchesPattern))
                {
                    return(false);
                }

                // Check if the mutant is excluded.
                if (excludePattern.Any(MatchesPattern))
                {
                    return(false);
                }

                return(true);

                bool MatchesPattern(FilePattern pattern)
                {
                    // We check both the full and the relative path to allow for relative paths.
                    return(pattern.IsMatch(file.FullPath, mutant.Mutation.OriginalNode.Span) ||
                           pattern.IsMatch(file.RelativePathToProjectFile, mutant.Mutation.OriginalNode.Span));
                }
            }
        }
        public void BroadcastReporter_ShouldInvokeAllReportersInList()
        {
            var reporterMock = new Mock <IReporter>(MockBehavior.Strict);

            reporterMock.Setup(x => x.OnAllMutantsTested(It.IsAny <IReadOnlyProjectComponent>()));
            reporterMock.Setup(x => x.OnMutantsCreated(It.IsAny <IReadOnlyProjectComponent>()));
            reporterMock.Setup(x => x.OnMutantTested(It.IsAny <Mutant>()));

            var exampleInputComponent = new ReadOnlyFileLeaf(new FileLeaf());
            var exampleMutant         = new Mutant();

            var reporters = new Collection <IReporter>()
            {
                reporterMock.Object,
                reporterMock.Object
            };
            var target = new BroadcastReporter(reporters);

            target.OnAllMutantsTested(exampleInputComponent);
            target.OnMutantsCreated(exampleInputComponent);
            target.OnMutantTested(exampleMutant);

            reporterMock.Verify(x => x.OnAllMutantsTested(exampleInputComponent), Times.Exactly(2));
            reporterMock.Verify(x => x.OnMutantsCreated(exampleInputComponent), Times.Exactly(2));
            reporterMock.Verify(x => x.OnMutantTested(exampleMutant), Times.Exactly(2));
        }
예제 #3
0
        public JsonReportFileComponent(ReadOnlyFileLeaf file, ILogger logger = null)
        {
            logger = logger ?? ApplicationLogging.LoggerFactory.CreateLogger <JsonReportFileComponent>();

            Source   = file.SourceCode;
            Language = "cs";
            Mutants  = new HashSet <JsonMutant>(new UniqueJsonMutantComparer());

            foreach (var mutant in file.Mutants)
            {
                var jsonMutant = new JsonMutant
                {
                    Id          = mutant.Id.ToString(),
                    MutatorName = mutant.Mutation.DisplayName,
                    Replacement = mutant.Mutation.ReplacementNode.ToFullString(),
                    Location    = new JsonMutantLocation(mutant.Mutation.OriginalNode.GetLocation().GetMappedLineSpan()),
                    Status      = mutant.ResultStatus.ToString(),
                    Description = mutant.Mutation.Description
                };

                if (!Mutants.Add(jsonMutant))
                {
                    logger.LogWarning(
                        $"Mutant {mutant.Id} was generated twice in file {file.RelativePath}. \n" +
                        $"This should not have happened. Please create an issue at https://github.com/stryker-mutator/stryker-net/issues");
                }
            }
        }
예제 #4
0
        public IEnumerable <Mutant> FilterMutants(IEnumerable <Mutant> mutants, ReadOnlyFileLeaf file, IStrykerOptions options)
        {
            // Mutants can be enabled for testing based on multiple reasons. We store all the filtered mutants in this list and return this list.
            IEnumerable <Mutant> filteredMutants;

            // A non-csharp file is flagged by the diff result as modified. We cannot determine which mutants will be affected by this, thus all mutants have to be tested.
            if (_diffResult.ChangedTestFiles is { } && _diffResult.ChangedTestFiles.Any(x => !x.EndsWith(".cs")))
        public void FilterMutants_WhenMutantMatchesSourceCode_StatusIsSetToJsonMutant()
        {
            // Arrange
            var branchProvider       = new Mock <IGitInfoProvider>();
            var baselineProvider     = new Mock <IBaselineProvider>();
            var baselineMutantHelper = new Mock <IBaselineMutantHelper>();

            var options = new StrykerOptions(compareToDashboard: true, projectVersion: "version");

            var file = new ReadOnlyFileLeaf(new CsharpFileLeaf
            {
                RelativePath = "foo.cs"
            });

            var mutants = new List <Mutant>
            {
                new Mutant
                {
                    ResultStatus = MutantStatus.NotRun
                }
            };

            var jsonMutants = new HashSet <JsonMutant>
            {
                new JsonMutant
                {
                    Status = "Killed"
                }
            };

            // Setup Mocks
            var jsonReportFileComponent = new MockJsonReportFileComponent("", "", jsonMutants);

            var jsonFileComponents = new Dictionary <string, JsonReportFileComponent>
            {
                ["foo.cs"] = jsonReportFileComponent
            };

            var baseline = new MockJsonReport(null, jsonFileComponents);

            baselineProvider.Setup(mock => mock.Load(It.IsAny <string>()))
            .Returns(Task.FromResult(baseline as JsonReport));

            baselineMutantHelper.Setup(mock => mock.GetMutantSourceCode(It.IsAny <string>(), It.IsAny <JsonMutant>())).Returns("var foo = \"bar\";");
            baselineMutantHelper.Setup(mock => mock.GetMutantMatchingSourceCode(
                                           It.IsAny <IEnumerable <Mutant> >(),
                                           It.Is <JsonMutant>(m => m == jsonMutants.First()),
                                           It.Is <string>(source => source == "var foo = \"bar\";"))).Returns(mutants).Verifiable();

            // Act
            var target = new DashboardMutantFilter(options, baselineProvider.Object, branchProvider.Object, baselineMutantHelper.Object);

            var results = target.FilterMutants(mutants, file, options);

            // Assert
            results.ShouldHaveSingleItem().ResultStatus.ShouldBe(MutantStatus.Killed);
            baselineMutantHelper.Verify();
        }
예제 #6
0
        public IEnumerable <Mutant> FilterMutants(IEnumerable <Mutant> mutants, ReadOnlyFileLeaf file, IStrykerOptions options)
        {
            if (!options.IgnoredMethods.Any())
            {
                return(mutants);
            }

            return(mutants.Where(m => !IsPartOfIgnoredMethodCall(m.Mutation.OriginalNode, options)));
        }
        private void UpdateMutantsWithBaselineStatus(IEnumerable <Mutant> mutants, ReadOnlyFileLeaf file)
        {
            if (!_baseline.Files.ContainsKey(FilePathUtils.NormalizePathSeparators(file.RelativePath)))
            {
                return;
            }

            JsonReportFileComponent baselineFile = _baseline.Files[FilePathUtils.NormalizePathSeparators(file.RelativePath)];

            if (baselineFile is { })
        public void BroadcastReporter_NoReportersInList()
        {
            var reporters = new Collection <IReporter>()
            {
            };

            var exampleInputComponent = new ReadOnlyFileLeaf(new FileLeaf());
            var exampleMutant         = new Mutant();

            var target = new BroadcastReporter(reporters);

            target.OnAllMutantsTested(exampleInputComponent);
            target.OnMutantsCreated(exampleInputComponent);
            target.OnMutantTested(exampleMutant);
        }
예제 #9
0
        public IEnumerable <Mutant> FilterMutants(IEnumerable <Mutant> mutants, ReadOnlyFileLeaf file, IStrykerOptions options)
        {
            if (options.CompareToDashboard)
            {
                if (_baseline == null)
                {
                    _logger.LogDebug("Returning all mutants on {0} because there is no baseline available", file.RelativePath);
                }
                else
                {
                    UpdateMutantsWithBaselineStatus(mutants, file);
                }
            }

            return(mutants);
        }
        public void FilterMutants_WhenMutantSourceCodeIsNull_MutantIsReturned()
        {
            // Arrange
            var branchProvider       = new Mock <IGitInfoProvider>();
            var baselineProvider     = new Mock <IBaselineProvider>();
            var baselineMutantHelper = new Mock <IBaselineMutantHelper>();

            var options = new StrykerOptions(compareToDashboard: true, projectVersion: "version");

            var file = new ReadOnlyFileLeaf(new CsharpFileLeaf
            {
                RelativePath = "foo.cs"
            });

            var mutants = new List <Mutant>
            {
                new Mutant()
            };

            var jsonMutants = new HashSet <JsonMutant>
            {
                new JsonMutant()
            };

            // Setup Mocks
            var jsonReportFileComponent = new MockJsonReportFileComponent("", "", jsonMutants);

            var jsonFileComponents = new Dictionary <string, JsonReportFileComponent>
            {
                ["foo.cs"] = jsonReportFileComponent
            };

            var baseline = new MockJsonReport(null, jsonFileComponents);

            baselineProvider.Setup(mock => mock.Load(It.IsAny <string>()))
            .Returns(Task.FromResult((JsonReport)baseline));

            baselineMutantHelper.Setup(mock => mock.GetMutantSourceCode(It.IsAny <string>(), It.IsAny <JsonMutant>())).Returns(string.Empty);

            // Act
            var target = new DashboardMutantFilter(options, baselineProvider.Object, branchProvider.Object, baselineMutantHelper.Object);

            var results = target.FilterMutants(mutants, file, options);

            // Assert
            results.ShouldHaveSingleItem();
        }
예제 #11
0
        public void BroadcastReporter_ShouldInvokeSameMethodWithSameObject_OnAllMutantsTested()
        {
            var reporterMock = new Mock <IReporter>(MockBehavior.Strict);

            reporterMock.Setup(x => x.OnAllMutantsTested(It.IsAny <IReadOnlyProjectComponent>()));

            var exampleInputComponent = new ReadOnlyFileLeaf(new FileLeaf());
            var exampleMutant         = new Mutant();

            var reporters = new Collection <IReporter>()
            {
                reporterMock.Object
            };
            var target = new BroadcastReporter(reporters);

            target.OnAllMutantsTested(exampleInputComponent);

            reporterMock.Verify(x => x.OnAllMutantsTested(exampleInputComponent), Times.Once);
        }
예제 #12
0
        public IEnumerable <Mutant> FilterMutants(IEnumerable <Mutant> mutants, ReadOnlyFileLeaf file, IStrykerOptions options)
        {
            // Mutants can be enabled for testing based on multiple reasons. We store all the filtered mutants in this list and return this list.
            IEnumerable <Mutant> filteredMutants;

            // If the dashboard feature is turned on we first filter based on previous results
            if (options.CompareToDashboard)
            {
                // If the dashboard feature is enabled but we cannot find a baseline. We are going to test the entire project. Thus none of the mutants can be filtered out and all are returned.
                if (_baseline == null)
                {
                    _logger.LogDebug("Testing all mutants on {0} because there is no baseline available", file.RelativePath);
                    return(mutants);
                }

                // Updates all the mutants in this file with their counterpart's result in the report of the previous run
                UpdateMutantsWithBaselineStatus(mutants, file);
            }

            // A non-csharp file is flagged by the diff result as modified. We cannot determine which mutants will be affected by this, thus all mutants have to be tested.
            if (_diffResult.ChangedTestFiles is { } && _diffResult.ChangedTestFiles.Any(x => !x.EndsWith(".cs")))
예제 #13
0
        public IEnumerable <Mutant> FilterMutants(IEnumerable <Mutant> mutants, ReadOnlyFileLeaf file, StrykerOptions options)
        {
            IEnumerable <Mutant> mutantsToTest = mutants.ToList();

            // Then all other filters except diff filter
            foreach (var mutantFilter in MutantFilters.Where(f => !(f is DiffMutantFilter)))
            {
                // These mutants should be tested according to current filter
                var extraMutantsToTest = mutantFilter.FilterMutants(mutantsToTest, file, options);

                // All mutants that weren't filtered out by a previous filter but were by the current filter are set to Ignored
                foreach (var skippedMutant in mutantsToTest.Except(extraMutantsToTest))
                {
                    skippedMutant.ResultStatus       = MutantStatus.Ignored;
                    skippedMutant.ResultStatusReason = $"Removed by {mutantFilter.DisplayName}";
                }

                mutantsToTest = extraMutantsToTest;
            }

            // Diff filter goes last if it is enabled
            if (MutantFilters.SingleOrDefault(f => f is DiffMutantFilter) is var diffFilter && diffFilter is { })
예제 #14
0
 private IDictionary <string, JsonReportFileComponent> GenerateFileReportComponents(ReadOnlyFileLeaf fileComponent)
 {
     return(new Dictionary <string, JsonReportFileComponent> {
         { fileComponent.RelativePath, new JsonReportFileComponent(fileComponent) }
     });
 }
 public IEnumerable <Mutant> FilterMutants(IEnumerable <Mutant> mutants, ReadOnlyFileLeaf file, IStrykerOptions options)
 {
     return(mutants.Where(m => !Exclude(m.Mutation.OriginalNode)));
 }
 /// <inheritdoc />
 public IEnumerable <Mutant> FilterMutants(IEnumerable <Mutant> mutants, ReadOnlyFileLeaf file, StrykerOptions options)
 {
     return(mutants.Where(mutant => !options.ExcludedMutations.Contains(mutant.Mutation.Type)));
 }