예제 #1
0
        private List <List <PathInfo> > BuildTestingGroups(IEnumerable <PathInfo> scriptPaths, TestOptions testOptions)
        {
            // Find all chutzpah.json files for the input files
            // Then group files by their respective settings file
            var testGroups        = new List <List <PathInfo> >();
            var fileSettingGroups = from path in scriptPaths
                                    let settingsFile = testSettingsService.FindSettingsFile(Path.GetDirectoryName(path.FullPath), testOptions.ChutzpahSettingsFileEnvironments)
                                                       group path by settingsFile;

            // Scan over the grouped test files and if this file is set up for batching we add those files
            // as a group to be tested. Otherwise, we will explode them out individually so they get run in their
            // own context
            foreach (var group in fileSettingGroups)
            {
                if (group.Key.EnableTestFileBatching.Value)
                {
                    testGroups.Add(group.ToList());
                }
                else
                {
                    foreach (var path in group)
                    {
                        testGroups.Add(new List <PathInfo> {
                            path
                        });
                    }
                }
            }
            return(testGroups);
        }
예제 #2
0
        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);
        }