Exemple #1
0
        public async Task <ICoverageAnalysisResult> AnalyseCoverage(Config config)
        {
            var memberIdsToNames = new Dictionary <string, string>();

            var baseTempDirectory = TempDirectory.Create();

            long memberId = 0;

            long GenerateMemberId() => ++ memberId;

            try
            {
                var copiedTestAssemblyFilePaths =
                    CopyTestAssembliesToTempDirectories(
                        config.TestAssemblyFilePaths,
                        baseTempDirectory)
                    .ToList();

                using (var workspace = MSBuildWorkspaceFactory.Create())
                {
                    var solution = await workspace.OpenSolutionAsync(config.SolutionFilePath);

                    await InstrumentThenCompileMultipleProjects(
                        solution.Projects.Where(p => Filtering.ShouldMutateProject(p, config)),
                        config,
                        baseTempDirectory,
                        copiedTestAssemblyFilePaths,
                        GenerateMemberId,
                        memberIdsToNames);

                    var result = new CoverageAnalysisResult();

                    for (int testAssemblyIndex = 0; testAssemblyIndex < config.TestAssemblyFilePaths.Length; ++testAssemblyIndex)
                    {
                        var copiedTestAssemblyFilePath = copiedTestAssemblyFilePaths[testAssemblyIndex];

                        var numTests = testFinder.FindTests(new[] { copiedTestAssemblyFilePath }).Length;

                        var runResult = testRunner.RunAllTestsAndAnalyseCoverage(
                            testAssemblyFilePaths: new [] { copiedTestAssemblyFilePath },
                            memberIdsToNames: memberIdsToNames,
                            onAnalysingTestCase: (test, index) => eventListener.BeginCoverageAnalysisOfTestCase(test, index, numTests));

                        if (runResult.Status != TestRunStatus.AllTestsPassed)
                        {
                            return(CoverageAnalysisResult.Error(runResult.Error));
                        }

                        var originalTestAssemblyFilePath = config.TestAssemblyFilePaths[testAssemblyIndex];
                        result = result.WithCoveredMembers(runResult.MembersAndCoveringTests, originalTestAssemblyFilePath);
                    }

                    return(result);
                }
            }
            finally
            {
                Directory.Delete(baseTempDirectory, recursive: true);
            }
        }
Exemple #2
0
        internal CoverageAnalysisResult WithCoveredMembers(
            IDictionary <string, ImmutableHashSet <string> > membersAndTheirCoveringTests,
            string testAssemblyFilePath)
        {
            var result = new CoverageAnalysisResult {
                coverageByTestAssembly = coverageByTestAssembly
            };

            result.coverageByTestAssembly.Add(testAssemblyFilePath, membersAndTheirCoveringTests);
            return(result);
        }