public void Process(IFrameworkDefinition frameworkDefinition, ReferencedFile referencedFile, string testFileText, ChutzpahTestSettingsFile settings) { if (!referencedFile.IsFileUnderTest) { return; } var regExp = settings.TestPatternRegex ?? GetTestPattern(referencedFile,testFileText, settings); var lines = fileSystem.GetLines(referencedFile.Path); int lineNum = 1; foreach (var line in lines) { var match = regExp.Match(line); while (match.Success) { var testNameGroup = match.Groups["TestName"]; var testName = testNameGroup.Value; if (!string.IsNullOrWhiteSpace(testName)) { referencedFile.FilePositions.Add(lineNum, testNameGroup.Index + 1, testName); } match = match.NextMatch(); } lineNum++; } }
private string GetTestHarnessTemplatePath(IFrameworkDefinition definition, ChutzpahTestSettingsFile chutzpahTestSettings) { string templatePath = null; if (!string.IsNullOrEmpty(chutzpahTestSettings.CustomTestHarnessPath)) { // If CustomTestHarnessPath is absolute path then Path.Combine just returns it var harnessPath = Path.Combine(chutzpahTestSettings.SettingsFileDirectory, chutzpahTestSettings.CustomTestHarnessPath); var fullPath = fileProbe.FindFilePath(harnessPath); if (fullPath != null) { ChutzpahTracer.TraceInformation("Using Custom Test Harness from {0}", fullPath); templatePath = fullPath; } else { ChutzpahTracer.TraceError("Cannot find Custom Test Harness at {0}", chutzpahTestSettings.CustomTestHarnessPath); } } if (templatePath == null) { templatePath = fileProbe.GetPathInfo(Path.Combine(Constants.TestFileFolder, definition.GetTestHarness(chutzpahTestSettings))).FullPath; ChutzpahTracer.TraceInformation("Using builtin Test Harness from {0}", templatePath); } return templatePath; }
/// <summary> /// Scans the test file extracting all referenced files from it. /// </summary> /// <param name="referencedFiles">The list of referenced files</param> /// <param name="definition">Test framework defintition</param> /// <param name="textToParse">The content of the file to parse and extract from</param> /// <param name="currentFilePath">Path to the file under test</param> /// <param name="chutzpahTestSettings"></param> /// <returns></returns> public void GetReferencedFiles(List <ReferencedFile> referencedFiles, IFrameworkDefinition definition, ChutzpahTestSettingsFile chutzpahTestSettings) { var filesUnderTests = referencedFiles.Where(x => x.IsFileUnderTest).ToList(); var referencePathSet = new HashSet <string>(referencedFiles.Select(x => x.Path), StringComparer.OrdinalIgnoreCase); // Process the references that the user specifies in the chutzpah settings file foreach (var reference in chutzpahTestSettings.References.Where(reference => reference != null)) { // The path we assume default to the chuzpah.json directory if the Path property is not set var referencePath = string.IsNullOrEmpty(reference.Path) ? reference.SettingsFileDirectory : reference.Path; ProcessFilePathAsReference( referencePathSet, definition, reference.SettingsFileDirectory, chutzpahTestSettings, referencePath, referencedFiles, new ReferencePathSettings(reference)); } // Process the references defined using /// <reference comments in test file contents foreach (var fileUnderTest in filesUnderTests) { var testFileText = fileSystem.GetText(fileUnderTest.Path); definition.Process(fileUnderTest, testFileText, chutzpahTestSettings); if (fileUnderTest.ExpandReferenceComments) { var result = GetReferencedFiles( referencePathSet, definition, testFileText, fileUnderTest.Path, chutzpahTestSettings); var flattenedReferenceTree = from root in result from flattened in FlattenReferenceGraph(root) select flattened; referencedFiles.AddRange(flattenedReferenceTree); } } }
/// <summary> /// Scans the test file extracting all referenced files from it. /// </summary> /// <param name="referencedFiles">The list of referenced files</param> /// <param name="definition">Test framework defintition</param> /// <param name="textToParse">The content of the file to parse and extract from</param> /// <param name="currentFilePath">Path to the file under test</param> /// <param name="chutzpahTestSettings"></param> /// <returns></returns> public void GetReferencedFiles(List<ReferencedFile> referencedFiles, IFrameworkDefinition definition, ChutzpahTestSettingsFile chutzpahTestSettings) { var filesUnderTests = referencedFiles.Where(x => x.IsFileUnderTest).ToList(); var referencePathSet = new HashSet<string>(referencedFiles.Select(x => x.Path), StringComparer.OrdinalIgnoreCase); // Process the references that the user specifies in the chutzpah settings file foreach (var reference in chutzpahTestSettings.References.Where(reference => reference != null)) { // The path we assume default to the chuzpah.json directory if the Path property is not set var referencePath = string.IsNullOrEmpty(reference.Path) ? reference.SettingsFileDirectory : reference.Path; ProcessFilePathAsReference( referencePathSet, definition, reference.SettingsFileDirectory, chutzpahTestSettings, referencePath, referencedFiles, new ReferencePathSettings(reference)); } // Process the references defined using /// <reference comments in test file contents foreach (var fileUnderTest in filesUnderTests) { var testFileText = fileSystem.GetText(fileUnderTest.Path); definition.Process(fileUnderTest, testFileText, chutzpahTestSettings); if (fileUnderTest.ExpandReferenceComments) { var result = GetReferencedFiles( referencePathSet, definition, testFileText, fileUnderTest.Path, chutzpahTestSettings); var flattenedReferenceTree = from root in result from flattened in FlattenReferenceGraph(root) select flattened; referencedFiles.AddRange(flattenedReferenceTree); } } }
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); }
private IList <ReferencedFile> ExpandNestedReferences( HashSet <string> discoveredPaths, IFrameworkDefinition definition, string currentFilePath, ChutzpahTestSettingsFile chutzpahTestSettings) { try { string textToParse = fileSystem.GetText(currentFilePath); return(GetReferencedFiles(discoveredPaths, definition, textToParse, currentFilePath, chutzpahTestSettings)); } catch (IOException e) { // Unable to get file text ChutzpahTracer.TraceError(e, "Unable to get file text from test reference with path {0}", currentFilePath); } return(new List <ReferencedFile>()); }
private ReferencedFile VisitReferencedFile( string absoluteFilePath, IFrameworkDefinition definition, HashSet <string> discoveredPaths, ICollection <ReferencedFile> referencedFiles, ChutzpahTestSettingsFile chutzpahTestSettings, ReferencePathSettings pathSettings) { // If the file doesn't exit exist or we have seen it already then return if (discoveredPaths.Contains(absoluteFilePath)) { return(null); } var referencedFile = new ReferencedFile { Path = absoluteFilePath, IsLocal = true, IsTestFrameworkFile = pathSettings.IsTestFrameworkFile, IncludeInTestHarness = pathSettings.IncludeInTestHarness || chutzpahTestSettings.TestHarnessReferenceMode == TestHarnessReferenceMode.Normal, TemplateOptions = pathSettings.TemplateOptions }; ChutzpahTracer.TraceInformation( "Added file '{0}' to referenced files. Local: {1}, IncludeInTestHarness: {2}", referencedFile.Path, referencedFile.IsLocal, referencedFile.IncludeInTestHarness); referencedFiles.Add(referencedFile); discoveredPaths.Add(referencedFile.Path); // Remmember this path to detect reference loops ChutzpahTracer.TraceInformation("Processing referenced file '{0}' for expanded references", absoluteFilePath); if (pathSettings.ExpandReferenceComments) { referencedFile.ReferencedFiles = ExpandNestedReferences(discoveredPaths, definition, absoluteFilePath, chutzpahTestSettings); } return(referencedFile); }
public void Process(IFrameworkDefinition frameworkDefinition, ReferencedFile referencedFile, string testFileText, ChutzpahTestSettingsFile settings) { if (!referencedFile.IsFileUnderTest) { return; } var regExp = settings.TestPatternRegex ?? GetTestPattern(referencedFile, testFileText, settings); int lineNum = 1; using (var reader = new StringReader(testFileText)) { string line; while ((line = reader.ReadLine()) != null) { var match = regExp.Match(line); while (match.Success) { var testNameGroup = match.Groups["TestName"]; var testName = testNameGroup.Value; if (!string.IsNullOrWhiteSpace(testName)) { referencedFile.FilePositions.Add(lineNum, testNameGroup.Index + 1, testName); } match = match.NextMatch(); } lineNum++; } } }
public void Process(IFrameworkDefinition frameworkDefinition, ReferencedFile referencedFile, string testFileText, ChutzpahTestSettingsFile settings) { if (!referencedFile.IsFileUnderTest) { return; } var regExp = settings.TestPatternRegex ?? GetTestPattern(referencedFile, testFileText, settings); int lineNum = 1; using (var reader = new StringReader(testFileText)) { string line; while ((line = reader.ReadLine()) != null) { var match = regExp.Match(line); while (match.Success) { var testNameGroup = match.Groups["TestName"]; var testName = testNameGroup.Value; if (!string.IsNullOrWhiteSpace(testName)) { referencedFile.FilePositions.Add(lineNum, testNameGroup.Index + 1, testName); } match = match.NextMatch(); } lineNum++; } } }
private IList<ReferencedFile> GetReferencedFiles( HashSet<string> discoveredPaths, IFrameworkDefinition definition, string textToParse, string currentFilePath, ChutzpahTestSettingsFile chutzpahTestSettings) { var referencedFiles = new List<ReferencedFile>(); Regex regex = RegexPatterns.JsReferencePathRegex; foreach (Match match in regex.Matches(textToParse)) { if (!ShouldIncludeReference(match)) continue; string referencePath = match.Groups["Path"].Value; ProcessFilePathAsReference( discoveredPaths, definition, currentFilePath, chutzpahTestSettings, referencePath, referencedFiles, new ReferencePathSettings()); } foreach (Match match in RegexPatterns.JsTemplatePathRegex.Matches(textToParse)) { string referencePath = match.Groups["Path"].Value; referencePath = AdjustPathIfRooted(chutzpahTestSettings, referencePath); string relativeReferencePath = Path.Combine(Path.GetDirectoryName(currentFilePath), referencePath); string absoluteFilePath = fileProbe.FindFilePath(relativeReferencePath); if (referencedFiles.All(r => r.Path != absoluteFilePath)) { ChutzpahTracer.TraceInformation("Added html template '{0}' to referenced files", absoluteFilePath); referencedFiles.Add(new ReferencedFile {Path = absoluteFilePath, IsLocal = false, IncludeInTestHarness = true}); } } return referencedFiles; }
private IList<ReferencedFile> ExpandNestedReferences( HashSet<string> discoveredPaths, IFrameworkDefinition definition, string currentFilePath, ChutzpahTestSettingsFile chutzpahTestSettings) { try { string textToParse = fileSystem.GetText(currentFilePath); return GetReferencedFiles(discoveredPaths, definition, textToParse, currentFilePath, chutzpahTestSettings); } catch (IOException e) { // Unable to get file text ChutzpahTracer.TraceError(e, "Unable to get file text from test reference with path {0}", currentFilePath); } return new List<ReferencedFile>(); }
private ReferencedFile VisitReferencedFile( string absoluteFilePath, IFrameworkDefinition definition, HashSet<string> discoveredPaths, ICollection<ReferencedFile> referencedFiles, ChutzpahTestSettingsFile chutzpahTestSettings, ReferencePathSettings pathSettings) { // If the file doesn't exit exist or we have seen it already then return if (discoveredPaths.Contains(absoluteFilePath)) return null; var referencedFile = new ReferencedFile { Path = absoluteFilePath, IsLocal = true, IsTestFrameworkFile = pathSettings.IsTestFrameworkFile, IncludeInTestHarness = pathSettings.IncludeInTestHarness || chutzpahTestSettings.TestHarnessReferenceMode == TestHarnessReferenceMode.Normal, TemplateOptions = pathSettings.TemplateOptions }; ChutzpahTracer.TraceInformation( "Added file '{0}' to referenced files. Local: {1}, IncludeInTestHarness: {2}", referencedFile.Path, referencedFile.IsLocal, referencedFile.IncludeInTestHarness); referencedFiles.Add(referencedFile); discoveredPaths.Add(referencedFile.Path); // Remmember this path to detect reference loops ChutzpahTracer.TraceInformation("Processing referenced file '{0}' for expanded references", absoluteFilePath); if (pathSettings.ExpandReferenceComments) { referencedFile.ReferencedFiles = ExpandNestedReferences(discoveredPaths, definition, absoluteFilePath, chutzpahTestSettings); } return referencedFile; }
private void ProcessFilePathAsReference( HashSet<string> discoveredPaths, IFrameworkDefinition definition, string relativeProcessingPath, ChutzpahTestSettingsFile chutzpahTestSettings, string referencePath, List<ReferencedFile> referencedFiles, ReferencePathSettings pathSettings) { ChutzpahTracer.TraceInformation("Investigating reference file path '{0}'", referencePath); // Check test settings and adjust the path if it is rooted (e.g. /some/path) referencePath = AdjustPathIfRooted(chutzpahTestSettings, referencePath); var referenceUri = new Uri(referencePath, UriKind.RelativeOrAbsolute); string referenceFileName = Path.GetFileName(referencePath); // Ignore test runner, since we use our own. if (definition.ReferenceIsDependency(referenceFileName, chutzpahTestSettings)) { ChutzpahTracer.TraceInformation( "Ignoring reference file '{0}' as a duplicate reference to {1}", referenceFileName, definition.FrameworkKey); return; } var isRelativeUri = !referenceUri.IsAbsoluteUri; // If this either a relative uri or a file uri if (isRelativeUri || referenceUri.IsFile) { var relativeProcessingPathFolder = fileSystem.FolderExists(relativeProcessingPath) ? relativeProcessingPath : Path.GetDirectoryName(relativeProcessingPath); string relativeReferencePath = Path.Combine(relativeProcessingPathFolder, referencePath); // Check if reference is a file string absoluteFilePath = fileProbe.FindFilePath(relativeReferencePath); if (absoluteFilePath != null) { VisitReferencedFile(absoluteFilePath, definition, discoveredPaths, referencedFiles, chutzpahTestSettings, pathSettings); return; } // Check if reference is a folder string absoluteFolderPath = fileProbe.FindFolderPath(relativeReferencePath); if (absoluteFolderPath != null) { var includePatterns = pathSettings.Includes.Select(x => FileProbe.NormalizeFilePath(x)).ToList(); var excludePatterns = pathSettings.Excludes.Select(x => FileProbe.NormalizeFilePath(x)).ToList(); // Find all files in this folder including sub-folders. This can be ALOT of files. // Only a subset of these files Chutzpah might understand so many of these will be ignored. var childFiles = fileSystem.GetFiles(absoluteFolderPath, "*.*", SearchOption.AllDirectories); var validFiles = from file in childFiles let normalizedFile = FileProbe.NormalizeFilePath(file) where !fileProbe.IsTemporaryChutzpahFile(file) && (!includePatterns.Any() || includePatterns.Any(pat => NativeImports.PathMatchSpec(normalizedFile, pat))) && (!excludePatterns.Any() || !excludePatterns.Any(pat => NativeImports.PathMatchSpec(normalizedFile, pat))) select file; validFiles.ForEach(file => VisitReferencedFile(file, definition, discoveredPaths, referencedFiles, chutzpahTestSettings, pathSettings)); return; } // At this point we know that this file/folder does not exist! ChutzpahTracer.TraceWarning("Referenced file '{0}' which was resolved to '{1}' does not exist", referencePath, relativeReferencePath); } else if (referenceUri.IsAbsoluteUri) { var referencedFile = new ReferencedFile { Path = referencePath, IsLocal = false, IncludeInTestHarness = true, IsTestFrameworkFile = pathSettings.IsTestFrameworkFile, TemplateOptions = pathSettings.TemplateOptions }; ChutzpahTracer.TraceInformation( "Added file '{0}' to referenced files. Local: {1}, IncludeInTestHarness: {2}", referencedFile.Path, referencedFile.IsLocal, referencedFile.IncludeInTestHarness); referencedFiles.Add(referencedFile); } }
private IList<ReferencedFile> GetReferencedFiles( HashSet<string> discoveredPaths, IFrameworkDefinition definition, string textToParse, string currentFilePath, ChutzpahTestSettingsFile chutzpahTestSettings) { var referencedFiles = new List<ReferencedFile>(); Regex regex = RegexPatterns.JsReferencePathRegex; foreach (Match match in regex.Matches(textToParse)) { if (!ShouldIncludeReference(match)) continue; string referencePath = match.Groups["Path"].Value; ProcessFilePathAsReference( discoveredPaths, definition, currentFilePath, chutzpahTestSettings, referencePath, referencedFiles, new ReferencePathSettings()); } foreach (Match match in RegexPatterns.JsTemplatePathRegex.Matches(textToParse)) { string referencePath = null, templateId = null, templateType = null; TemplateMode templateMode = TemplateMode.Raw; for (var i = 0; i < match.Groups["PropName"].Captures.Count; i++) { var propName = match.Groups["PropName"].Captures[i].Value.ToLowerInvariant(); var propValue = match.Groups["PropValue"].Captures[i].Value; switch (propName) { case "path": referencePath = propValue; break; case "id": templateId = propValue; break; case "type": templateType = propValue; break; case "mode": if(propValue.Equals("script", StringComparison.OrdinalIgnoreCase)) { templateMode = TemplateMode.Script; } break; default: break; } } referencePath = AdjustPathIfRooted(chutzpahTestSettings, referencePath); string relativeReferencePath = Path.Combine(Path.GetDirectoryName(currentFilePath), referencePath); string absoluteFilePath = fileProbe.FindFilePath(relativeReferencePath); if (referencedFiles.All(r => r.Path != absoluteFilePath)) { ChutzpahTracer.TraceInformation("Added html template '{0}' to referenced files", absoluteFilePath); referencedFiles.Add(new ReferencedFile { Path = absoluteFilePath, IsLocal = false, IncludeInTestHarness = true, TemplateOptions = new TemplateOptions { Mode = templateMode, Id = templateId, Type = templateType } }); } } return referencedFiles; }
public void Process(IFrameworkDefinition frameworkDefinition, ReferencedFile referencedFile, string testFileText, ChutzpahTestSettingsFile settings) { referencedFile.FrameworkReplacements = frameworkDefinition.GetFrameworkReplacements(settings, referencedFile.Path, testFileText); }
private string CreateTestHarness( IFrameworkDefinition definition, ChutzpahTestSettingsFile chutzpahTestSettings, TestOptions options, string inputTestFilePath, IEnumerable<ReferencedFile> referencedFiles, ICoverageEngine coverageEngine, IList<string> temporaryFiles) { string inputTestFileDir = Path.GetDirectoryName(inputTestFilePath); string testFilePathHash = hasher.Hash(inputTestFilePath); string testHarnessDirectory; switch (chutzpahTestSettings.TestHarnessLocationMode) { case TestHarnessLocationMode.TestFileAdjacent: testHarnessDirectory = inputTestFileDir; break; case TestHarnessLocationMode.SettingsFileAdjacent: testHarnessDirectory = chutzpahTestSettings.SettingsFileDirectory; break; case TestHarnessLocationMode.Custom: testHarnessDirectory = chutzpahTestSettings.TestHarnessDirectory; break; default: throw new ArgumentOutOfRangeException("chutzpahTestSettings"); } string testHtmlFilePath = Path.Combine(testHarnessDirectory, string.Format(Constants.ChutzpahTemporaryFileFormat, testFilePathHash, "test.html")); temporaryFiles.Add(testHtmlFilePath); string templatePath = fileProbe.GetPathInfo(Path.Combine(Constants.TestFileFolder, definition.TestHarness)).FullPath; string testHtmlTemplate = fileSystem.GetText(templatePath); string amdModulePath = ""; if (chutzpahTestSettings.TestHarnessReferenceMode == TestHarnessReferenceMode.AMD && inputTestFilePath.Contains(testHarnessDirectory)) { amdModulePath = inputTestFilePath .Replace(Path.GetExtension(inputTestFilePath), "") .Replace(testHarnessDirectory, "") .Replace("\\", "/") .Trim('/', '\\'); } var harness = new TestHarness(chutzpahTestSettings, options, referencedFiles, fileSystem, amdModulePath); if (coverageEngine != null) { coverageEngine.PrepareTestHarnessForCoverage(harness, definition); } string testFileContents = fileSystem.GetText(inputTestFilePath); var frameworkReplacements = definition.GetFrameworkReplacements(chutzpahTestSettings, inputTestFilePath, testFileContents) ?? new Dictionary<string, string>(); string testHtmlText = harness.CreateHtmlText(testHtmlTemplate, frameworkReplacements); fileSystem.Save(testHtmlFilePath, testHtmlText); return testHtmlFilePath; }
public void Process(IFrameworkDefinition frameworkDefinition, ReferencedFile referencedFile, string testFileText, ChutzpahTestSettingsFile settings) { referencedFile.FrameworkReplacements = frameworkDefinition.GetFrameworkReplacements(settings, referencedFile.Path, testFileText); }
private bool TryDetectFramework(string content, PathType pathType, ChutzpahTestSettingsFile chutzpahTestSettings, out IFrameworkDefinition definition) { var strategies = new Func<IFrameworkDefinition>[] { // Check chutzpah settings () => frameworkDefinitions.FirstOrDefault(x => x.FrameworkKey.Equals(chutzpahTestSettings.Framework, StringComparison.OrdinalIgnoreCase)), // Check if we see an explicit reference to a framework file (e.g. <reference path="qunit.js" />) () => frameworkDefinitions.FirstOrDefault(x => x.FileUsesFramework(content, false, pathType)), // Check using basic heuristic like looking for test( or module( for QUnit () => frameworkDefinitions.FirstOrDefault(x => x.FileUsesFramework(content, true, pathType)) }; definition = strategies.Select(x => x()).FirstOrDefault(x => x != null); return definition != null; }
private ICoverageEngine SetupCodeCoverageEngine( TestOptions options, ChutzpahTestSettingsFile chutzpahTestSettings, IFrameworkDefinition definition, List<ReferencedFile> referencedFiles) { ICoverageEngine coverageEngine = GetConfiguredCoverageEngine(options, chutzpahTestSettings); if (coverageEngine != null) { var deps = coverageEngine.GetFileDependencies(definition); foreach (string item in deps) { string sourcePath = fileProbe.GetPathInfo(Path.Combine(Constants.TestFileFolder, item)).FullPath; ChutzpahTracer.TraceInformation( "Added code coverage dependency '{0}' to referenced files", sourcePath); referencedFiles.Add( new ReferencedFile { IsLocal = true, IsCodeCoverageDependency = true, Path = sourcePath, IncludeInTestHarness = true }); } } return coverageEngine; }
private IList <ReferencedFile> GetReferencedFiles( HashSet <string> discoveredPaths, IFrameworkDefinition definition, string textToParse, string currentFilePath, ChutzpahTestSettingsFile chutzpahTestSettings) { var referencedFiles = new List <ReferencedFile>(); Regex regex = RegexPatterns.JsReferencePathRegex; foreach (Match match in regex.Matches(textToParse)) { if (!ShouldIncludeReference(match)) { continue; } string referencePath = match.Groups["Path"].Value; ProcessFilePathAsReference( discoveredPaths, definition, currentFilePath, chutzpahTestSettings, referencePath, referencedFiles, new ReferencePathSettings()); } foreach (Match match in RegexPatterns.JsTemplatePathRegex.Matches(textToParse)) { string referencePath = null, templateId = null, templateType = null; TemplateMode templateMode = TemplateMode.Raw; for (var i = 0; i < match.Groups["PropName"].Captures.Count; i++) { var propName = match.Groups["PropName"].Captures[i].Value.ToLowerInvariant(); var propValue = match.Groups["PropValue"].Captures[i].Value; switch (propName) { case "path": referencePath = propValue; break; case "id": templateId = propValue; break; case "type": templateType = propValue; break; case "mode": if (propValue.Equals("script", StringComparison.OrdinalIgnoreCase)) { templateMode = TemplateMode.Script; } break; default: break; } } referencePath = AdjustPathIfRooted(chutzpahTestSettings, referencePath); string relativeReferencePath = Path.Combine(Path.GetDirectoryName(currentFilePath), referencePath); string absoluteFilePath = fileProbe.FindFilePath(relativeReferencePath); if (referencedFiles.All(r => r.Path != absoluteFilePath)) { ChutzpahTracer.TraceInformation("Added html template '{0}' to referenced files", absoluteFilePath); referencedFiles.Add(new ReferencedFile { Path = absoluteFilePath, IsLocal = false, IncludeInTestHarness = true, TemplateOptions = new TemplateOptions { Mode = templateMode, Id = templateId, Type = templateType } }); } } return(referencedFiles); }
public IEnumerable <string> GetFileDependencies(IFrameworkDefinition definition, ChutzpahTestSettingsFile testSettingsFile) { var blanketScriptName = GetBlanketScriptName(definition, testSettingsFile); yield return("Coverage\\" + blanketScriptName); }
private void ProcessFilePathAsReference( HashSet <string> discoveredPaths, IFrameworkDefinition definition, string relativeProcessingPath, ChutzpahTestSettingsFile chutzpahTestSettings, string referencePath, List <ReferencedFile> referencedFiles, ReferencePathSettings pathSettings) { ChutzpahTracer.TraceInformation("Investigating reference file path '{0}'", referencePath); // Check test settings and adjust the path if it is rooted (e.g. /some/path) referencePath = AdjustPathIfRooted(chutzpahTestSettings, referencePath); var referenceUri = new Uri(referencePath, UriKind.RelativeOrAbsolute); string referenceFileName = Path.GetFileName(referencePath); // Ignore test runner, since we use our own. if (definition.ReferenceIsDependency(referenceFileName, chutzpahTestSettings)) { ChutzpahTracer.TraceInformation( "Ignoring reference file '{0}' as a duplicate reference to {1}", referenceFileName, definition.FrameworkKey); return; } var isRelativeUri = !referenceUri.IsAbsoluteUri; // If this either a relative uri or a file uri if (isRelativeUri || referenceUri.IsFile) { var relativeProcessingPathFolder = fileSystem.FolderExists(relativeProcessingPath) ? relativeProcessingPath : Path.GetDirectoryName(relativeProcessingPath); string relativeReferencePath = Path.Combine(relativeProcessingPathFolder, referencePath); // Check if reference is a file string absoluteFilePath = fileProbe.FindFilePath(relativeReferencePath); if (absoluteFilePath != null) { VisitReferencedFile(absoluteFilePath, definition, discoveredPaths, referencedFiles, chutzpahTestSettings, pathSettings); return; } // Check if reference is a folder string absoluteFolderPath = fileProbe.FindFolderPath(relativeReferencePath); if (absoluteFolderPath != null) { var includePatterns = pathSettings.Includes.Select(x => UrlBuilder.NormalizeFilePath(x)).ToList(); var excludePatterns = pathSettings.Excludes.Select(x => UrlBuilder.NormalizeFilePath(x)).ToList(); // Find all files in this folder including sub-folders. This can be ALOT of files. // Only a subset of these files Chutzpah might understand so many of these will be ignored. var childFiles = fileSystem.GetFiles(absoluteFolderPath, "*.*", SearchOption.AllDirectories); var validFiles = from file in childFiles let normalizedFile = UrlBuilder.NormalizeFilePath(file) where !fileProbe.IsTemporaryChutzpahFile(file) && (!includePatterns.Any() || includePatterns.Any(pat => NativeImports.PathMatchSpec(normalizedFile, pat))) && (!excludePatterns.Any() || !excludePatterns.Any(pat => NativeImports.PathMatchSpec(normalizedFile, pat))) select file; validFiles.ForEach(file => VisitReferencedFile(file, definition, discoveredPaths, referencedFiles, chutzpahTestSettings, pathSettings)); return; } // At this point we know that this file/folder does not exist! ChutzpahTracer.TraceWarning("Referenced file '{0}' which was resolved to '{1}' does not exist", referencePath, relativeReferencePath); } else if (referenceUri.IsAbsoluteUri) { var referencedFile = new ReferencedFile { Path = referencePath, IsLocal = false, IncludeInTestHarness = true, IsTestFrameworkFile = pathSettings.IsTestFrameworkFile, TemplateOptions = pathSettings.TemplateOptions }; ChutzpahTracer.TraceInformation( "Added file '{0}' to referenced files. Local: {1}, IncludeInTestHarness: {2}", referencedFile.Path, referencedFile.IsLocal, referencedFile.IncludeInTestHarness); referencedFiles.Add(referencedFile); } }
private bool TryDetectFramework(PathInfo path, ChutzpahTestSettingsFile chutzpahTestSettings, out IFrameworkDefinition definition) { // TODO: Deprecate the fallback approach Lazy<string> fileText = new Lazy<string>(() => { string firstTestFileText; if (path.Type == PathType.Url) { firstTestFileText = httpClient.GetContent(path.FullPath); } else { firstTestFileText = fileSystem.GetText(path.FullPath); } return firstTestFileText; }); var strategies = new Func<IFrameworkDefinition>[] { // Check chutzpah settings () => frameworkDefinitions.FirstOrDefault(x => x.FrameworkKey.Equals(chutzpahTestSettings.Framework, StringComparison.OrdinalIgnoreCase)), // Check if we see an explicit reference to a framework file (e.g. <reference path="qunit.js" />) () => frameworkDefinitions.FirstOrDefault(x => x.FileUsesFramework(fileText.Value, false, path.Type)), // Check using basic heuristic like looking for test( or module( for QUnit () => frameworkDefinitions.FirstOrDefault(x => x.FileUsesFramework(fileText.Value, true, path.Type)) }; definition = strategies.Select(x => x()).FirstOrDefault(x => x != null); return definition != null; }
private bool TryDetectFramework(string content, PathType pathType, ChutzpahTestSettingsFile chutzpahTestSettings, out IFrameworkDefinition definition) { var strategies = new Func <IFrameworkDefinition>[] { // Check chutzpah settings () => frameworkDefinitions.FirstOrDefault(x => x.FrameworkKey.Equals(chutzpahTestSettings.Framework, StringComparison.OrdinalIgnoreCase)), // Check if we see an explicit reference to a framework file (e.g. <reference path="qunit.js" />) () => frameworkDefinitions.FirstOrDefault(x => x.FileUsesFramework(content, false, pathType)), // Check using basic heuristic like looking for test( or module( for QUnit () => frameworkDefinitions.FirstOrDefault(x => x.FileUsesFramework(content, true, pathType)) }; definition = strategies.Select(x => x()).FirstOrDefault(x => x != null); return(definition != null); }
private string GetBlanketScriptName(IFrameworkDefinition def, ChutzpahTestSettingsFile settingsFile) { return(def.GetBlanketScriptName(settingsFile)); }
private bool TryDetectFramework(PathInfo path, ChutzpahTestSettingsFile chutzpahTestSettings, out IFrameworkDefinition definition) { // TODO: Deprecate the fallback approach Lazy <string> fileText = new Lazy <string>(() => { string firstTestFileText; if (path.Type == PathType.Url) { firstTestFileText = httpClient.GetContent(path.FullPath); } else { firstTestFileText = fileSystem.GetText(path.FullPath); } return(firstTestFileText); }); var strategies = new Func <IFrameworkDefinition>[] { // Check chutzpah settings () => frameworkDefinitions.FirstOrDefault(x => x.FrameworkKey.Equals(chutzpahTestSettings.Framework, StringComparison.OrdinalIgnoreCase)), // Check if we see an explicit reference to a framework file (e.g. <reference path="qunit.js" />) () => frameworkDefinitions.FirstOrDefault(x => x.FileUsesFramework(fileText.Value, false, path.Type)), // Check using basic heuristic like looking for test( or module( for QUnit () => frameworkDefinitions.FirstOrDefault(x => x.FileUsesFramework(fileText.Value, true, path.Type)) }; definition = strategies.Select(x => x()).FirstOrDefault(x => x != null); return(definition != null); }
/// <summary> /// Scans the test file extracting all referenced files from it. /// </summary> /// <param name="referencedFiles">The list of referenced files</param> /// <param name="definition">Test framework defintition</param> /// <param name="textToParse">The content of the file to parse and extract from</param> /// <param name="currentFilePath">Path to the file under test</param> /// <returns></returns> public void GetReferencedFiles( List<ReferencedFile> referencedFiles, IFrameworkDefinition definition, string textToParse, string currentFilePath, ChutzpahTestSettingsFile chutzpahTestSettings) { var referencePathSet = new HashSet<string>(referencedFiles.Select(x => x.Path)); // Process the references that the user specifies in the chutzpah settings file foreach (var reference in chutzpahTestSettings.References.Where(reference => reference != null)) { ProcessFilePathAsReference( referencePathSet, definition, chutzpahTestSettings.SettingsFileDirectory, chutzpahTestSettings, reference.Path, referencedFiles, new ReferencePathSettings(reference)); } // Process the references defined using /// <reference comments in test file contents IList<ReferencedFile> result = GetReferencedFiles( referencePathSet, definition, textToParse, currentFilePath, chutzpahTestSettings); IEnumerable<ReferencedFile> flattenedReferenceTree = from root in result from flattened in FlattenReferenceGraph(root) select flattened; referencedFiles.AddRange(flattenedReferenceTree); }