Example #1
0
        public async Task RunMutationTest(MuTestOptions options)
        {
            try
            {
                if (!File.Exists(MuTestSettings.MSBuildPath))
                {
                    throw new MuTestInputException($"Unable to locate MSBuild Path at {MuTestSettings.MSBuildPath}. Please update MSBuildPath in MuTest.Console.exe.config if you are using different version");
                }

                _stopwatch = new Stopwatch();
                _stopwatch.Start();
                _options = options;

                _chalk.Default("\nPreparing Required Files...\n");

                DirectoryFactory.NumberOfMutantsExecutingInParallel = _options.ConcurrentTestRunners;

                _cppClass = new CppClass
                {
                    Configuration      = _options.Configuration,
                    SourceClass        = _options.SourceClass,
                    Platform           = _options.Platform,
                    TestClass          = _options.TestClass,
                    TestProject        = _options.TestProject,
                    Target             = _options.Target,
                    SourceHeader       = _options.SourceHeader,
                    TestSolution       = _options.TestSolution,
                    IncludeBuildEvents = _options.IncludeBuildEvents
                };

                Context = !_options.InIsolation
                    ? DirectoryFactory.PrepareTestFiles(_cppClass)
                    : DirectoryFactory.PrepareSolutionFiles(_cppClass);

                if (Context.TestContexts.Any())
                {
                    await ExecuteBuild();
                    await ExecuteTests();

                    if (!_options.DisableBuildOptimization)
                    {
                        Context.EnableBuildOptimization = true;
                    }

                    _chalk.Default("\nRunning Mutation Analysis...\n");


                    var defaultMutants = CppMutantOrchestrator.GetDefaultMutants(_options.SourceClass, _options.SpecificLines).ToList();
                    defaultMutants = _aridNodeMutantFilterer.FilterMutants(defaultMutants).ToList();
                    defaultMutants = _mutantsSelector.SelectMutants(_options.MutantsPerLine, defaultMutants).ToList();
                    _cppClass.Mutants.AddRange(defaultMutants);

                    if (_cppClass.CoveredLineNumbers.Any())
                    {
                        foreach (var mutant in _cppClass.Mutants)
                        {
                            if (_cppClass.CoveredLineNumbers.All(x => x != mutant.Mutation.LineNumber))
                            {
                                mutant.ResultStatus = MutantStatus.NotCovered;
                            }
                            else if (mutant.Mutation.EndLineNumber > mutant.Mutation.LineNumber)
                            {
                                if (!_cppClass.CoveredLineNumbers.Any(x => x > mutant.Mutation.LineNumber &&
                                                                      x <= mutant.Mutation.EndLineNumber))
                                {
                                    mutant.ResultStatus = MutantStatus.Skipped;
                                }
                            }
                        }
                    }

                    _chalk.Default($"\nNumber of Mutants: {_cppClass.Mutants.Count}\n");

                    var sourceHash = _cppClass
                                     .SourceClass
                                     .GetCodeFileContent()
                                     .ComputeHash();

                    var testHash = _cppClass
                                   .TestClass
                                   .GetCodeFileContent()
                                   .ComputeHash();

                    _cppClass.Sha256 = (sourceHash + testHash).ComputeHash();

                    var data = await _client.GetFileDataFromStorage(_cppClass.Sha256);

                    _cppClass.StoreInDb = true;

                    if (data != null)
                    {
                        var cppClass = JsonConvert.DeserializeObject <CppClass>(data);
                        cppClass.StoreInDb          = false;
                        cppClass.SourceClass        = _cppClass.SourceClass;
                        cppClass.SourceHeader       = _cppClass.SourceHeader;
                        cppClass.TestClass          = _cppClass.TestClass;
                        cppClass.TestProject        = _cppClass.TestProject;
                        cppClass.Configuration      = _cppClass.Configuration;
                        cppClass.Target             = _cppClass.Target;
                        cppClass.Platform           = _cppClass.Platform;
                        cppClass.TestSolution       = _cppClass.TestSolution;
                        cppClass.IncludeBuildEvents = _cppClass.IncludeBuildEvents;

                        _cppClass = cppClass;
                    }

                    MutantsExecutor = new CppMutantExecutor(_cppClass, Context, MuTestSettings)
                    {
                        EnableDiagnostics = _options.EnableDiagnostics,
                        KilledThreshold   = _options.KilledThreshold,
                        SurvivedThreshold = _options.SurvivedThreshold,
                        NumberOfMutantsExecutingInParallel = _options.ConcurrentTestRunners
                    };

                    _totalMutants   = _cppClass.NotRunMutants.Count;
                    _mutantProgress = 0;

                    if (_cppClass.Mutants.Any() && data == null)
                    {
                        MutantsExecutor.MutantExecuted += MutantAnalyzerOnMutantExecuted;
                        await MutantsExecutor.ExecuteMutants();
                    }

                    await GenerateReports();
                }
            }
            finally
            {
                if (Context != null)
                {
                    DirectoryFactory.DeleteTestFiles(Context);
                }
            }
        }
Example #2
0
        private async Task ExecuteBuild()
        {
            _chalk.Default("\nBuilding Solution...\n");
            var log = new StringBuilder();

            void OutputData(object sender, string args) => log.AppendLine(args);

            var testCodeBuild = new CppBuildExecutor(
                MuTestSettings,
                string.Format(Context.TestSolution.FullName, 0),
                _cppClass.Target)
            {
                Configuration          = _options.Configuration,
                EnableLogging          = _options.EnableDiagnostics,
                IntDir                 = string.Format(Context.IntDir, 0),
                IntermediateOutputPath = string.Format(Context.IntermediateOutputPath, 0),
                OutDir                 = string.Format(Context.OutDir, 0),
                OutputPath             = string.Format(Context.OutputPath, 0),
                Platform               = _options.Platform,
                QuietWithSymbols       = true
            };

            if (!_options.IncludeBuildEvents)
            {
                string.Format(Context.TestProject.FullName, 0).RemoveBuildEvents();
            }

            testCodeBuild.OutputDataReceived    += OutputData;
            testCodeBuild.BeforeMsBuildExecuted += (sender, args) =>
            {
                _chalk.Yellow($"\nRunning MSBuild with {args}\n");
            };
            await testCodeBuild.ExecuteBuild();

            testCodeBuild.OutputDataReceived -= OutputData;

            if (testCodeBuild.LastBuildStatus == BuildExecutionStatus.Failed && !_options.InIsolation)
            {
                _chalk.Yellow("\nBuild Failed...Preparing new solution files\n");
                DirectoryFactory.DeleteTestFiles(Context);
                Context = DirectoryFactory.PrepareSolutionFiles(_cppClass);

                testCodeBuild = new CppBuildExecutor(
                    MuTestSettings,
                    string.Format(Context.TestSolution.FullName, 0),
                    _cppClass.Target)
                {
                    Configuration          = _options.Configuration,
                    EnableLogging          = _options.EnableDiagnostics,
                    IntDir                 = string.Format(Context.IntDir, 0),
                    IntermediateOutputPath = string.Format(Context.IntermediateOutputPath, 0),
                    OutDir                 = string.Format(Context.OutDir, 0),
                    OutputPath             = string.Format(Context.OutputPath, 0),
                    Platform               = _options.Platform,
                    QuietWithSymbols       = true
                };

                testCodeBuild.BeforeMsBuildExecuted += (sender, args) =>
                {
                    _chalk.Yellow($"\nRunning MSBuild with {args}\n");
                };
                await testCodeBuild.ExecuteBuild();
            }

            if (testCodeBuild.LastBuildStatus == BuildExecutionStatus.Failed)
            {
                _chalk.Yellow("\nBuild Failed...Taking Source Code Backup\n");
                _options.ConcurrentTestRunners = 1;
                DirectoryFactory.DeleteTestFiles(Context);
                Context = DirectoryFactory.TakingSourceCodeBackup(_cppClass);

                testCodeBuild = new CppBuildExecutor(
                    MuTestSettings,
                    Context.TestSolution.FullName,
                    _cppClass.Target)
                {
                    Configuration          = _options.Configuration,
                    EnableLogging          = _options.EnableDiagnostics,
                    IntDir                 = Context.IntDir,
                    IntermediateOutputPath = Context.IntermediateOutputPath,
                    OutDir                 = Context.OutDir,
                    OutputPath             = Context.OutputPath,
                    Platform               = _options.Platform,
                    QuietWithSymbols       = true
                };

                testCodeBuild.BeforeMsBuildExecuted += (sender, args) =>
                {
                    _chalk.Yellow($"\nRunning MSBuild with {args}\n");
                };
                await testCodeBuild.ExecuteBuild();
            }

            if (testCodeBuild.LastBuildStatus == BuildExecutionStatus.Failed)
            {
                throw new MuTestFailingBuildException(log.ToString());
            }

            _chalk.Green("\nBuild Succeeded!");
        }