public TestContext BuildContext(PathInfo file, TestOptions options) { if (file == null) { throw new ArgumentNullException("testFilePathInfo"); } ChutzpahTracer.TraceInformation("Building test context for '{0}'", file.FullPath); PathType testFileKind = file.Type; string testFilePath = file.FullPath; if (!IsValidTestPathType(testFileKind)) { throw new ArgumentException("Expecting a .js, .ts, .coffee or .html file or a url"); } if (testFilePath == null) { throw new FileNotFoundException("Unable to find file: " + file.Path); } var testFileDirectory = Path.GetDirectoryName(testFilePath); var chutzpahTestSettings = settingsService.FindSettingsFile(testFileDirectory); if (!IsTestPathIncluded(testFilePath, chutzpahTestSettings)) { ChutzpahTracer.TraceInformation("Excluded test file '{0}' given chutzpah.json settings", testFilePath); return(null); } string testFileText; if (testFileKind == PathType.Url) { testFileText = httpClient.GetContent(testFilePath); } else { testFileText = fileSystem.GetText(testFilePath); } IFrameworkDefinition definition; if (TryDetectFramework(testFileText, testFileKind, chutzpahTestSettings, out definition)) { // For HTML test files we don't need to create a test harness to just return this file if (testFileKind == PathType.Html || testFileKind == PathType.Url) { ChutzpahTracer.TraceInformation("Test kind is {0} so we are trusting the supplied test harness and not building our own", testFileKind); return(new TestContext { InputTestFile = testFilePath, TestHarnessPath = testFilePath, IsRemoteHarness = testFileKind == PathType.Url, TestRunner = definition.GetTestRunner(chutzpahTestSettings), }); } var referencedFiles = new List <ReferencedFile>(); var temporaryFiles = new List <string>(); string inputTestFileDir = Path.GetDirectoryName(testFilePath); var testHarnessDirectory = GetTestHarnessDirectory(chutzpahTestSettings, inputTestFileDir); var fileUnderTest = GetFileUnderTest(testFilePath, chutzpahTestSettings); referencedFiles.Add(fileUnderTest); definition.Process(fileUnderTest, testFileText, chutzpahTestSettings); referenceProcessor.GetReferencedFiles(referencedFiles, definition, testFileText, testFilePath, chutzpahTestSettings); // This is the legacy way Chutzpah compiled files that are TypeScript or CoffeeScript // Remaining but will eventually be deprecated and removed ProcessForFilesGeneration(referencedFiles, temporaryFiles, chutzpahTestSettings); IEnumerable <string> deps = definition.GetFileDependencies(chutzpahTestSettings); var coverageEngine = SetupCodeCoverageEngine(options, chutzpahTestSettings, definition, referencedFiles); AddTestFrameworkDependencies(deps, referencedFiles); return(new TestContext { FrameworkDefinition = definition, CoverageEngine = coverageEngine, InputTestFile = testFilePath, TestHarnessDirectory = testHarnessDirectory, ReferencedFiles = referencedFiles, TestRunner = definition.GetTestRunner(chutzpahTestSettings), TemporaryFiles = temporaryFiles, TestFileSettings = chutzpahTestSettings }); } else { ChutzpahTracer.TraceWarning("Failed to detect test framework for '{0}'", testFilePath); } return(null); }
public TestContext BuildContext(IEnumerable <PathInfo> files, TestOptions options) { if (files == null) { throw new ArgumentNullException("testFilePathInfo"); } if (!files.Any()) { ChutzpahTracer.TraceInformation("No files given to build test context for"); return(null); } var fileCount = files.Count(); var allFilePathString = string.Join(",", files.Select(x => x.FullPath)); ChutzpahTracer.TraceInformation("Building test context for '{0}'", allFilePathString); // Make sure all test paths have been resolved to real files var missingPaths = files.Where(x => x.FullPath == null).ToList(); if (missingPaths.Any()) { throw new FileNotFoundException("Unable to find files: " + string.Join(",", missingPaths.Select(x => x.Path))); } // Make sure all test paths have a valid file type if (!files.Select(x => x.Type).All(IsValidTestPathType)) { throw new ArgumentException("Expecting a .js, .ts, .coffee or .html file or a url"); } if (fileCount > 1 && files.Any(x => x.Type == PathType.Url || x.Type == PathType.Html)) { throw new InvalidOperationException("Cannot build a batch context for Url or Html test files"); } // We use the first file's directory to find the chutzpah.json file and to test framework // since we assume all files in the batch must have the same values for those PathType firstFileKind = files.First().Type; string firstFilePath = files.First().FullPath; var firstTestFileDirectory = Path.GetDirectoryName(firstFilePath); var chutzpahTestSettings = settingsService.FindSettingsFile(firstTestFileDirectory, options.ChutzpahSettingsFileEnvironments); // Exclude any files that are not included based on the settings file var testedPaths = files.Select(f => new { File = f, IsIncluded = IsTestPathIncluded(f.FullPath, chutzpahTestSettings) }).ToList(); if (testedPaths.Any(x => !x.IsIncluded)) { var pathString = string.Join(",", testedPaths.Where(x => !x.IsIncluded).Select(x => x.File.FullPath)); ChutzpahTracer.TraceInformation("Excluding test files {0} given chutzpah.json settings", pathString); files = testedPaths.Where(x => x.IsIncluded).Select(x => x.File).ToList(); if (!files.Any()) { return(null); } } string firstTestFileText; if (firstFileKind == PathType.Url) { firstTestFileText = httpClient.GetContent(firstFilePath); } else { firstTestFileText = fileSystem.GetText(firstFilePath); } IFrameworkDefinition definition; if (TryDetectFramework(firstTestFileText, firstFileKind, chutzpahTestSettings, out definition)) { // For HTML test files we don't need to create a test harness to just return this file if (firstFileKind == PathType.Html || firstFileKind == PathType.Url) { ChutzpahTracer.TraceInformation("Test kind is {0} so we are trusting the supplied test harness and not building our own", firstFileKind); return(new TestContext { ReferencedFiles = new List <ReferencedFile> { new ReferencedFile { IsFileUnderTest = true, Path = firstFilePath } }, InputTestFiles = new[] { firstFilePath }, FirstInputTestFile = firstFilePath, InputTestFilesString = firstFilePath, TestHarnessPath = firstFilePath, IsRemoteHarness = firstFileKind == PathType.Url, TestRunner = definition.GetTestRunner(chutzpahTestSettings), }); } var temporaryFiles = new List <string>(); string firstInputTestFileDir = Path.GetDirectoryName(firstFilePath); var testHarnessDirectory = GetTestHarnessDirectory(chutzpahTestSettings, firstInputTestFileDir); var referencedFiles = GetFilesUnderTest(files, chutzpahTestSettings).ToList(); referenceProcessor.GetReferencedFiles(referencedFiles, definition, chutzpahTestSettings); IEnumerable <string> deps = definition.GetFileDependencies(chutzpahTestSettings); var coverageEngine = SetupCodeCoverageEngine(options, chutzpahTestSettings, definition, referencedFiles); AddTestFrameworkDependencies(deps, referencedFiles); var testFiles = referencedFiles.Where(x => x.IsFileUnderTest).Select(x => x.Path).ToList(); return(new TestContext { FrameworkDefinition = definition, CoverageEngine = coverageEngine, InputTestFiles = testFiles, FirstInputTestFile = testFiles.FirstOrDefault(), InputTestFilesString = string.Join(",", testFiles), TestHarnessDirectory = testHarnessDirectory, ReferencedFiles = referencedFiles, TestRunner = definition.GetTestRunner(chutzpahTestSettings), TemporaryFiles = temporaryFiles, TestFileSettings = chutzpahTestSettings }); } else { ChutzpahTracer.TraceWarning("Failed to detect test framework for '{0}'", firstFilePath); } return(null); }