public void PrepareTestHarnessForCoverage(TestHarness harness, IFrameworkDefinition definition, ChutzpahTestSettingsFile testSettingsFile) { string blanketScriptName = GetBlanketScriptName(definition, testSettingsFile); // Construct array of scripts to exclude from instrumentation/coverage collection. var filesToExcludeFromCoverage = new List <string>(); var filesToIncludeInCoverage = new List <string>(); var extensionMap = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); foreach (TestHarnessItem refScript in harness.ReferencedScripts.Where(rs => rs.HasFile)) { // Skip files which the user is asking us to exclude if (!IsFileEligibleForInstrumentation(refScript.ReferencedFile.Path)) { filesToExcludeFromCoverage.Add(refScript.Attributes["src"]); } else { refScript.Attributes["type"] = "text/blanket"; // prevent Phantom/browser parsing } // Build extension map for when we conver to regex the include/exclude patterns if (!string.IsNullOrEmpty(refScript.ReferencedFile.GeneratedFilePath)) { var sourceExtension = Path.GetExtension(refScript.ReferencedFile.Path); extensionMap[sourceExtension] = ".js"; } } // Construct array of scripts to exclude from instrumentation/coverage collection. filesToExcludeFromCoverage.AddRange( harness.TestFrameworkDependencies.Concat(harness.CodeCoverageDependencies) .Where(dep => dep.HasFile && IsScriptFile(dep.ReferencedFile)) .Select(dep => dep.Attributes["src"]) .Concat(excludePatterns.Select(f => ToRegex(f, extensionMap)))); filesToIncludeInCoverage.AddRange(includePatterns.Select(f => ToRegex(f, extensionMap))); // Name the coverage object so that the JS runner can pick it up. harness.ReferencedScripts.Add(new Script(string.Format("window.{0}='_$blanket';", Constants.ChutzpahCoverageObjectReference))); // Configure Blanket. TestHarnessItem blanketMain = harness.CodeCoverageDependencies.Single( d => d.Attributes.ContainsKey("src") && (d.Attributes["src"].EndsWith(blanketScriptName) || d.Attributes["src"].Contains(blanketScriptName + "?"))); string dataCoverNever = "[" + string.Join(",", filesToExcludeFromCoverage.Select(file => "'" + file + "'")) + "]"; string dataCoverOnly = filesToIncludeInCoverage.Any() ? "[" + string.Join(",", filesToIncludeInCoverage.Select(file => "'" + file + "'")) + "]" : "//.*/"; ChutzpahTracer.TraceInformation("Adding data-cover-never attribute to blanket: {0}", dataCoverNever); blanketMain.Attributes.Add("data-cover-flags", "ignoreError autoStart"); blanketMain.Attributes.Add("data-cover-only", dataCoverOnly); blanketMain.Attributes.Add("data-cover-never", dataCoverNever); blanketMain.Attributes.Add("data-cover-timeout", testSettingsFile.CodeCoverageTimeout.HasValue ? testSettingsFile.CodeCoverageTimeout.Value.ToString() : "5000"); }
public void PrepareTestHarnessForCoverage(TestHarness harness, IFrameworkDefinition definition, ChutzpahTestSettingsFile testSettingsFile) { string blanketScriptName = GetBlanketScriptName(definition, testSettingsFile); // Construct array of scripts to exclude from instrumentation/coverage collection. var filesToExcludeFromCoverage = harness.TestFrameworkDependencies.Concat(harness.CodeCoverageDependencies) .Where(dep => dep.HasFile && IsScriptFile(dep.ReferencedFile)) .Select(dep => dep.Attributes["src"]) .Concat(excludePatterns.Select(ToRegex)) .ToList(); var filesToIncludeInCoverage = includePatterns.Select(ToRegex).ToList(); foreach (TestHarnessItem refScript in harness.ReferencedScripts.Where(rs => rs.HasFile)) { // Exclude files which the user is asking us to ignores if (!IsFileEligibleForInstrumentation(refScript.ReferencedFile.Path)) { filesToExcludeFromCoverage.Add(refScript.Attributes["src"]); } else { refScript.Attributes["type"] = "text/blanket"; // prevent Phantom/browser parsing } } // Name the coverage object so that the JS runner can pick it up. harness.ReferencedScripts.Add(new Script(string.Format("window.{0}='_$blanket';", Constants.ChutzpahCoverageObjectReference))); // Configure Blanket. TestHarnessItem blanketMain = harness.CodeCoverageDependencies.Single( d => d.Attributes.ContainsKey("src") && d.Attributes["src"].EndsWith(blanketScriptName)); string dataCoverNever = "[" + string.Join(",", filesToExcludeFromCoverage.Select(file => "'" + file + "'")) + "]"; string dataCoverOnly = filesToIncludeInCoverage.Any() ? "[" + string.Join(",", filesToIncludeInCoverage.Select(file => "'" + file + "'")) + "]" : "//.*/"; ChutzpahTracer.TraceInformation("Adding data-cover-never attribute to blanket: {0}", dataCoverNever); blanketMain.Attributes.Add("data-cover-flags", "ignoreError autoStart"); blanketMain.Attributes.Add("data-cover-only", dataCoverOnly); blanketMain.Attributes.Add("data-cover-never", dataCoverNever); }