internal bool IsTestFile(string pathToFile)
        {
            var testCaseFile = pathToFile;
            var project      = GetTestProjectFromFile(pathToFile);

            if (project == null)
            {
                //The file is not included in the project.
                //Don't look for tests in it.
                return(false);
            }

            //
            //Check to see if we are dealing with a TypeScript file
            //  If we are then switch the test container to the underlying js file
            //
            if (TypeScriptHelpers.IsTypeScriptFile(pathToFile))
            {
                var jsFile = TypeScriptHelpers.GetTypeScriptBackedJavaScriptFile(project, pathToFile);
                if (!File.Exists(jsFile))
                {
                    //Ignore the file for now.  On the next build event the typescript compiler will generate the file
                    //  at that point this function gets invoked again on the .ts file and we'll see the newly created .js file
                    return(false);
                }
            }
            else if (!StringComparer.OrdinalIgnoreCase.Equals(Path.GetExtension(pathToFile), NodejsConstants.JavaScriptExtension))
            {
                return(false);
            }

            ErrorHandler.Succeeded(((IVsHierarchy)project).ParseCanonicalName(pathToFile, out var itemId));

            return(IsTestFile(itemId, project));
        }
        private string ResolveStartupFile() {
            string startupFile = _project.GetStartupFile();
            if (string.IsNullOrEmpty(startupFile)) {
                throw new ApplicationException("Please select a startup file to launch by right-clicking the file in Solution Explorer and selecting 'Set as Node.js Startup File' or by modifying your configuration in project properties.");
            }

            if (TypeScriptHelpers.IsTypeScriptFile(startupFile)) {
                startupFile = TypeScriptHelpers.GetTypeScriptBackedJavaScriptFile(_project, startupFile);
            }
            return startupFile;
        }
        internal bool IsTestFile(string pathToFile)
        {
            var project = this.GetTestProjectFromFile(pathToFile);

            if (project == null)
            {
                //The file is not included in the project.
                //Don't look for tests in it.
                return(false);
            }

            //
            //Check to see if we are dealing with a TypeScript file
            //  If we are then switch the test container to the underlying js file
            //
            if (TypeScriptHelpers.IsTypeScriptFile(pathToFile))
            {
                var jsFile = TypeScriptHelpers.GetTypeScriptBackedJavaScriptFile(project, pathToFile);
                if (!File.Exists(jsFile))
                {
                    //Ignore the file for now.  On the next build event the typescript compiler will generate the file
                    //  at that point this function gets invoked again on the .ts file and we'll see the newly created .js file
                    return(false);
                }
            }
            else if (!StringComparer.OrdinalIgnoreCase.Equals(Path.GetExtension(pathToFile), NodejsConstants.JavaScriptExtension))
            {
                return(false);
            }

            if (!(project is IVsBuildPropertyStorage propStore))
            {
                Debug.Fail($"Why is {nameof(project)} not of type {nameof(IVsBuildPropertyStorage)}?");

                return(false);
            }

            var hr = propStore.GetPropertyValue(NodeProjectProperty.TestRoot, /*configuration*/ "", (uint)_PersistStorageType.PST_PROJECT_FILE, out var testRoot);

            // if test root is specified check if the file is contained, otherwise fall back to old logic
            if (ErrorHandler.Succeeded(hr) && !string.IsNullOrEmpty(testRoot))
            {
                project.TryGetProjectPath(out var root);
                var testRootPath = Path.Combine(root, testRoot);

                return(CommonUtils.IsSubpathOf(root, pathToFile));
            }

            ErrorHandler.Succeeded(((IVsHierarchy)project).ParseCanonicalName(pathToFile, out var itemId));

            return(IsTestFile(itemId, project));
        }
Example #4
0
        private static (string, string)? GetTestFrameworkAndFilePath(Project project, ProjectItem projectItem, string projectRoot)
        {
            var testRoot          = project.GetProperty(NodeProjectProperty.TestRoot)?.EvaluatedValue;
            var testFrameworkName = project.GetProperty(NodeProjectProperty.TestFramework)?.EvaluatedValue;

            string fileAbsolutePath;

            if (!string.IsNullOrEmpty(testRoot) && !string.IsNullOrEmpty(testFrameworkName)) // If the test root and framework have been configured on the project.
            {
                var testRootPath = Path.GetFullPath(Path.Combine(project.DirectoryPath, testRoot));

                try
                {
                    fileAbsolutePath = CommonUtils.GetAbsoluteFilePath(projectRoot, projectItem.EvaluatedInclude);
                }
                catch (ArgumentException)
                {
                    // .Net core projects include invalid paths, ignore them and continue checking the items.
                    return(null);
                }

                if (!fileAbsolutePath.StartsWith(testRootPath, StringComparison.OrdinalIgnoreCase))
                {
                    return(null);
                }
            }
            else // If the file has been configured individually.
            {
                testFrameworkName = projectItem.GetMetadataValue("TestFramework");
                if (!TestFramework.IsValidTestFramework(testFrameworkName))
                {
                    return(null);
                }
                fileAbsolutePath = CommonUtils.GetAbsoluteFilePath(projectRoot, projectItem.EvaluatedInclude);
            }

            // Check if file is a typecript file. If so, get the javascript file. The javascript file needs to be in the same path and name.
            // It doesn't work with bundlers or minimizers. Also, project needs to be build in order to have the js file created.
            var typeScriptTest = TypeScriptHelpers.IsTypeScriptFile(fileAbsolutePath);

            if (typeScriptTest)
            {
                fileAbsolutePath = TypeScriptHelpers.GetTypeScriptBackedJavaScriptFile(project, fileAbsolutePath);
            }
            else if (!StringComparer.OrdinalIgnoreCase.Equals(Path.GetExtension(fileAbsolutePath), ".js"))
            {
                return(null);
            }

            return(testFrameworkName, fileAbsolutePath);
        }
Example #5
0
        private string ResolveStartupFile()
        {
            var startupFile = this._project.GetStartupFile();

            if (string.IsNullOrEmpty(startupFile))
            {
                throw new ApplicationException(Resources.DebugCouldNotResolveStartupFileErrorMessage);
            }

            if (TypeScriptHelpers.IsTypeScriptFile(startupFile))
            {
                startupFile = TypeScriptHelpers.GetTypeScriptBackedJavaScriptFile(this._project, startupFile);
            }
            return(startupFile);
        }
Example #6
0
        private string ResolveStartupFile()
        {
            string startupFile = _project.GetStartupFile();

            if (string.IsNullOrEmpty(startupFile))
            {
                throw new ApplicationException("No startup file is defined for the startup project.");
            }

            if (TypeScriptHelpers.IsTypeScriptFile(startupFile))
            {
                startupFile = TypeScriptHelpers.GetTypeScriptBackedJavaScriptFile(_project, startupFile);
            }
            return(startupFile);
        }
Example #7
0
        private IEnumerable <string> GetTestItems(string projectRoot, string outDir)
        {
            // TODO: Do our own directory traversal. It's better for performance.

            // If we find ts or tsx files, get the JS file and return.
            var files = Directory.EnumerateFiles(projectRoot, "*.ts?", SearchOption.AllDirectories)
                        .Where(x => !x.Contains("\\node_modules\\"));

            if (files.Any())
            {
                return(files
                       .Where(p => TypeScriptHelpers.IsTypeScriptFile(p))
                       .Select(p => TypeScriptHelpers.GetTypeScriptBackedJavaScriptFile(p, outDir, projectRoot)));
            }

            return(Directory.EnumerateFiles(projectRoot, "*.js", SearchOption.AllDirectories)
                   .Where(p => !p.Contains("\\node_modules\\")));
        }
        internal bool IsTestFile(string pathToFile)
        {
            var project = this.GetTestProjectFromFile(pathToFile);

            if (project == null)
            {
                //The file is not included in the project.
                //Don't look for tests in it.
                return(false);
            }

            //
            //Check to see if we are dealing with a TypeScript file
            //  If we are then switch the test container to the underlying js file
            //
            if (TypeScriptHelpers.IsTypeScriptFile(pathToFile))
            {
                var jsFile = TypeScriptHelpers.GetTypeScriptBackedJavaScriptFile(project, pathToFile);
                if (jsFile == null || !File.Exists(jsFile))
                {
                    //Ignore the file for now.  On the next build event the typescript compiler will generate the file
                    //  at that point this function gets invoked again on the .ts file and we'll see the newly created .js file
                    return(false);
                }
            }
            else if (!TypeScriptHelpers.IsJavaScriptFile(pathToFile))
            {
                return(false);
            }

            if (TryGetProjectUnitTestProperties(project, out var testRoot, out _) &&
                !string.IsNullOrEmpty(testRoot) &&
                project.TryGetProjectDirectory(out var root))
            {
                var testRootPath = Path.Combine(root, testRoot);

                return(CommonUtils.IsSubpathOf(testRootPath, pathToFile));
            }

            ErrorHandler.Succeeded(((IVsHierarchy)project).ParseCanonicalName(pathToFile, out var itemId));

            return(IsTestFile(itemId, project));
        }