Esempio n. 1
0
        public TestContext BuildContext(PathInfo file, TestOptions options)
        {
            if (file == null)
            {
                throw new ArgumentNullException("testFilePathInfo");
            }

            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 = ChutzpahTestSettingsFile.Read(testFileDirectory, fileProbe, serializer);

            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.TestRunner,
                    };
                }

                var referencedFiles = new List<ReferencedFile>();
                var temporaryFiles = new List<string>();

                var fileUnderTest = GetFileUnderTest(testFilePath, chutzpahTestSettings);
                referencedFiles.Add(fileUnderTest);
                definition.Process(fileUnderTest);

                referenceProcessor.GetReferencedFiles(referencedFiles, definition, testFileText, testFilePath, chutzpahTestSettings);

                ProcessForFilesGeneration(referencedFiles, temporaryFiles, chutzpahTestSettings);

                IEnumerable<string> deps = definition.FileDependencies;

                var coverageEngine = SetupCodeCoverageEngine(options, chutzpahTestSettings, definition, referencedFiles);

                AddTestFrameworkDependencies(deps, referencedFiles);

                string testHtmlFilePath = CreateTestHarness(
                    definition,
                    chutzpahTestSettings,
                    options,
                    testFilePath,
                    referencedFiles,
                    coverageEngine,
                    temporaryFiles);

                return new TestContext
                {
                    InputTestFile = testFilePath,
                    TestHarnessPath = testHtmlFilePath,
                    ReferencedJavaScriptFiles = referencedFiles,
                    TestRunner = definition.TestRunner,
                    TemporaryFiles = temporaryFiles,
                    TestFileSettings = chutzpahTestSettings
                };
            }
            else
            {
                ChutzpahTracer.TraceWarning("Failed to detect test framework for '{0}'", testFilePath);
            }

            return null;
        }
Esempio n. 2
0
 public bool TryBuildContext(PathInfo file, TestOptions options, out TestContext context)
 {
     context = BuildContext(file, options);
     return context != null;
 }
Esempio n. 3
0
        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;
        }
Esempio n. 4
0
 public TestContext BuildContext(PathInfo file, TestOptions options)
 {
     return BuildContext(new List<PathInfo> { file }, options);
 }
Esempio n. 5
0
        public bool IsTestFile(string file, ChutzpahSettingsFileEnvironments environments = null)
        {
            ChutzpahTracer.TraceInformation("Determining if '{0}' might be a test file", file);
            if (string.IsNullOrWhiteSpace(file))
            {
                return false;
            }

            var testFilePath = fileProbe.FindFilePath(file);

            if (testFilePath == null)
            {
                ChutzpahTracer.TraceInformation("Rejecting '{0}' since either it doesnt exist", file);
                return false;
            }

            var chutzpahTestSettings = settingsService.FindSettingsFile(testFilePath, environments);

            if (!IsTestPathIncluded(testFilePath, chutzpahTestSettings))
            {
                ChutzpahTracer.TraceInformation("Excluded test file '{0}' given chutzpah.json settings", testFilePath);
                return false;
            }

            // If the framework or tests filters are set in the settings file then no need to check for 
            // test framework
            if (!string.IsNullOrEmpty(chutzpahTestSettings.Framework) || chutzpahTestSettings.Tests.Any())
            {
                return true;
            }
            else
            {
                string testFileText = fileSystem.GetText(testFilePath);

                IFrameworkDefinition definition;
                var info = new PathInfo { Path = file, FullPath = testFilePath, Type = FileProbe.GetFilePathType(testFilePath) };
                var frameworkDetetected = TryDetectFramework(info, chutzpahTestSettings, out definition);

                if (frameworkDetetected)
                {
                    ChutzpahTracer.TraceInformation("Assuming '{0}' is a test file", file);
                }

                return frameworkDetetected;
            }

        }