Exemplo n.º 1
0
            private string GetSearchPaths(IEnumerable <TestCase> tests, PythonProjectSettings settings)
            {
                var paths = settings.SearchPath.ToList();

                HashSet <string> knownModulePaths = new HashSet <string>();

                foreach (var test in tests)
                {
                    string testFilePath = PathUtils.GetAbsoluteFilePath(settings.ProjectHome, test.CodeFilePath);
                    var    modulePath   = ModulePath.FromFullPath(testFilePath);
                    if (knownModulePaths.Add(modulePath.LibraryPath))
                    {
                        paths.Insert(0, modulePath.LibraryPath);
                    }
                }

                paths.Insert(0, settings.WorkingDirectory);
                if (_debugMode == PythonDebugMode.PythonOnly)
                {
                    paths.Insert(0, PtvsdSearchPath);
                }

                string searchPaths = string.Join(
                    ";",
                    paths.Where(Directory.Exists).Distinct(StringComparer.OrdinalIgnoreCase)
                    );

                return(searchPaths);
            }
Exemplo n.º 2
0
        internal static string DoSubstitutions(LaunchConfiguration original, IPythonProject project, string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return(str);
            }

            return(SubstitutionPattern.Replace(
                       str,
                       m => {
                switch (m.Groups[1].Value.ToLowerInvariant())
                {
                case "startupfile":
                    return original.ScriptName;

                case "startupmodule":
                    try {
                        return ModulePath.FromFullPath(original.ScriptName, project.ProjectHome).ModuleName;
                    } catch (ArgumentException) {
                    }
                    break;
                }
                return m.Value;
            }
                       ));
        }
Exemplo n.º 3
0
            private IEnumerable <KeyValuePair <string, TestCase> > GetTestCases()
            {
                var moduleCache = new Dictionary <string, ModulePath>();

                foreach (var test in _tests)
                {
                    string testFile, testClass, testMethod;
                    TestReader.ParseFullyQualifiedTestName(
                        test.FullyQualifiedName,
                        out testFile,
                        out testClass,
                        out testMethod
                        );

                    ModulePath module;
                    if (!moduleCache.TryGetValue(testFile, out module))
                    {
                        string testFilePath = PathUtils.GetAbsoluteFilePath(_settings.ProjectHome, testFile);
                        moduleCache[testFile] = module = ModulePath.FromFullPath(testFilePath);
                    }

                    yield return(new KeyValuePair <string, TestCase>("{0}.{1}.{2}".FormatInvariant(
                                                                         module.ModuleName,
                                                                         testClass,
                                                                         testMethod
                                                                         ), test));
                }
            }
Exemplo n.º 4
0
        public void ModulePathFromFullPath()
        {
            var basePath = @"C:\Not\A\Real\Path\";

            // Replace the usual File.Exists(p + '__init__.py') check so we can
            // test without real files.
            var packagePaths = new HashSet <string>(PathEqualityComparer.Instance)
            {
                basePath + @"A\",
                basePath + @"A\B\"
            };

            Func <string, bool> isPackage = p => {
                Console.WriteLine("isPackage({0})", p);
                return(packagePaths.Contains(p));
            };

            // __init__ files appear in the full name but not the module name.
            var mp = ModulePath.FromFullPath(Path.Combine(basePath, "A", "B", "__init__.py"), isPackage: isPackage);

            Assert.AreEqual("A.B", mp.ModuleName);
            Assert.AreEqual("A.B.__init__", mp.FullName);
            Assert.AreEqual("__init__", mp.Name);

            mp = ModulePath.FromFullPath(Path.Combine(basePath, "A", "B", "Module.py"), isPackage: isPackage);
            Assert.AreEqual("A.B.Module", mp.ModuleName);

            // Ensure we don't go back past the top-level directory if specified
            mp = ModulePath.FromFullPath(
                Path.Combine(basePath, "A", "B", "Module.py"),
                Path.Combine(basePath, "A"),
                isPackage
                );
            Assert.AreEqual("B.Module", mp.ModuleName);
        }
Exemplo n.º 5
0
        public void Export()
        {
            _writer.WriteStartElement("CoverageDSPriv");
            _writer.WriteRaw(_schema);

            foreach (var keyValue in _covInfo)
            {
                var file      = keyValue.Key;
                var collector = keyValue.Value;
                _writer.WriteStartElement("Module");

                WriteElement("ModuleName", collector.ModuleName);
                WriteElement("ImageSize", new FileInfo(file.Filename).Length);
                WriteElement("ImageLinkTime", "0");

                int linesCovered = 0, linesNotCovered = 0, blocksCovered = 0, blocksNotCovered = 0;
                AggregateStats(collector.GlobalScope, ref linesCovered, ref linesNotCovered, ref blocksCovered, ref blocksNotCovered);
                foreach (var klass in collector.Classes)
                {
                    AggregateStats(klass, ref linesCovered, ref linesNotCovered, ref blocksCovered, ref blocksNotCovered);
                }

                WriteCoverageData(linesCovered, linesNotCovered, blocksCovered, blocksNotCovered);

                _writer.WriteStartElement("NamespaceTable");

                WriteCoverageData(linesCovered, linesNotCovered, blocksCovered, blocksNotCovered);
                WriteElement("ModuleName", ModulePath.FromFullPath(file.Filename).ModuleName);
                WriteElement("NamespaceKeyName", file.Filename);
                WriteElement("NamespaceName", "");

                // Write top-level class
                WriteClass(file.Filename, collector.GlobalScope, "<global>");

                foreach (var klass in collector.Classes)
                {
                    WriteClass(file.Filename, klass, GetQualifiedName(klass.Statement));
                }

                _writer.WriteEndElement();  // NamespaceTable
                _writer.WriteEndElement();  // Module

                _curFile++;
            }

            int curFile = 0;

            foreach (var file in _covInfo.Keys)
            {
                _writer.WriteStartElement("SourceFileNames");
                WriteElement("SourceFileID", curFile + 1);
                WriteElement("SourceFileName", file.Filename);
                _writer.WriteEndElement();

                curFile++;
            }

            _writer.WriteEndElement();
            _writer.Flush();
        }
Exemplo n.º 6
0
        public static IPythonModule FromStream(
            IPythonInterpreter interpreter,
            Stream sourceFile,
            string fileName,
            PythonLanguageVersion langVersion,
            string moduleFullName
            )
        {
            PythonAst ast;
            var       sink   = KeepParseErrors ? new CollectingErrorSink() : ErrorSink.Null;
            var       parser = Parser.CreateParser(sourceFile, langVersion, new ParserOptions {
                StubFile  = fileName.EndsWithOrdinal(".pyi", ignoreCase: true),
                ErrorSink = sink
            });

            ast = parser.ParseFile();

            return(new AstPythonModule(
                       moduleFullName ?? ModulePath.FromFullPath(fileName, isPackage: IsPackageCheck).FullName,
                       interpreter,
                       ast,
                       fileName,
                       (sink as CollectingErrorSink)?.Errors.Select(e => "{0} ({1}): {2}".FormatUI(fileName ?? "(builtins)", e.Span, e.Message))
                       ));
        }
Exemplo n.º 7
0
        private static IEnumerable <ModulePath> GetModules(string args)
        {
            var m        = Regex.Match(args, "(\\*|[\\w\\.]+)\\s+(.+)");
            var modName  = m.Groups[1].Value;
            var fileName = m.Groups[2].Value;

            if (modName != "*")
            {
                fileName = Path.GetFullPath(fileName);
                yield return(new ModulePath(modName, fileName, null));

                yield break;
            }

            if (!fileName.Contains("*"))
            {
                yield return(ModulePath.FromFullPath(Path.GetFullPath(fileName)));

                yield break;
            }

            var opt = SearchOption.TopDirectoryOnly;

            var dir    = PathUtils.TrimEndSeparator(PathUtils.GetParent(fileName));
            var filter = GetFileOrDirectoryName(fileName);

            if (dir.EndsWith("**"))
            {
                opt = SearchOption.AllDirectories;
                dir = dir.Substring(0, dir.Length - 2);
            }

            if (!Path.IsPathRooted(dir))
            {
                dir = Path.Combine(Environment.CurrentDirectory, dir);
            }

            if (!Directory.Exists(dir))
            {
                Console.WriteLine("Invalid directory: {0}", dir);
                yield break;
            }

            Console.WriteLine("Adding modules from {0}:{1}", dir, filter);
            foreach (var file in Directory.EnumerateFiles(dir, filter, opt))
            {
                ModulePath mp;
                try {
                    mp = ModulePath.FromFullPath(file, PathUtils.GetParent(dir));
                } catch (ArgumentException) {
                    Console.WriteLine("Failed to get module name for {0}", file);
                    continue;
                }
                yield return(mp);
            }
        }
Exemplo n.º 8
0
        private static string GetCoveragePath(IEnumerable<TestCase> tests) {
            string bestFile = null, bestClass = null, bestMethod = null;

            // Try and generate a friendly name for the coverage report.  We use
            // the filename, class, and method.  We include each one if we're
            // running from a single filename/class/method.  When we have multiple
            // we drop the identifying names.  If we have multiple files we
            // go to the top level directory...  If all else fails we do "pycov".
            foreach (var test in tests) {
                string testFile, testClass, testMethod;
                TestReader.ParseFullyQualifiedTestName(
                    test.FullyQualifiedName,
                    out testFile,
                    out testClass,
                    out testMethod
                );

                bestFile = UpdateBestFile(bestFile, test.CodeFilePath);
                if (bestFile != test.CodeFilePath) {
                    // Different files, don't include class/methods even
                    // if they happen to be the same.
                    bestClass = bestMethod = "";
                }

                bestClass = UpdateBest(bestClass, testClass);
                bestMethod = UpdateBest(bestMethod, testMethod);
            }

            string filename = "";

            if (!String.IsNullOrWhiteSpace(bestFile)) {
                if (ModulePath.IsPythonSourceFile(bestFile)) {
                    filename = ModulePath.FromFullPath(bestFile).ModuleName;
                } else {
                    filename = Path.GetFileName(bestFile);
                }
            } else {
                filename = "pycov";
            }

            if (!String.IsNullOrWhiteSpace(bestClass)) {
                filename += "_" + bestClass;
            }

            if (!String.IsNullOrWhiteSpace(bestMethod)) {
                filename += "_" + bestMethod;
            }

            filename += "_" + DateTime.Now.ToString("s").Replace(':', '_');

            return Path.Combine(Path.GetTempPath(), filename);
        }
 internal static async Task <ModulePath> FindModuleAsync(IPythonInterpreterFactory factory, string filePath, CancellationToken cancellationToken)
 {
     try {
         var apif = factory as AstPythonInterpreterFactory;
         if (apif != null)
         {
             return(await apif.ModuleResolution.FindModuleAsync(filePath, cancellationToken));
         }
         return(ModulePath.FromFullPath(filePath));
     } catch (ArgumentException) {
         return(default(ModulePath));
     }
 }
Exemplo n.º 10
0
        public static ModulePath FindModule(IPythonInterpreterFactory factory, string filePath)
        {
            try {
                var apif = factory as AstPythonInterpreterFactory;
                if (apif != null)
                {
                    return(apif.FindModule(filePath));
                }

                return(ModulePath.FromFullPath(filePath));
            } catch (ArgumentException) {
                return(default(ModulePath));
            }
        }
Exemplo n.º 11
0
            private string[] GetArguments()
            {
                var arguments = new List <string>();

                arguments.Add(TestLauncherPath);
                foreach (var test in _tests)
                {
                    arguments.Add("-t");
                    string testFile, testClass, testMethod;
                    TestDiscoverer.ParseFullyQualifiedTestName(
                        test.FullyQualifiedName,
                        out testFile,
                        out testClass,
                        out testMethod
                        );

                    string testFilePath = CommonUtils.GetAbsoluteFilePath(_settings.ProjectHome, testFile);
                    var    module       = ModulePath.FromFullPath(testFilePath);

                    arguments.Add(
                        string.Format("{0}.{1}.{2}",
                                      module.ModuleName,
                                      testClass,
                                      testMethod
                                      )
                        );
                }

                if (_codeCoverageFile != null)
                {
                    arguments.Add("--coverage");
                    arguments.Add(_codeCoverageFile);
                }

                if (_debugMode == PythonDebugMode.PythonOnly)
                {
                    arguments.AddRange(new[] {
                        "-s", _debugSecret,
                        "-p", _debugPort.ToString()
                    });
                }
                else if (_debugMode == PythonDebugMode.PythonAndNative)
                {
                    arguments.Add("-x");
                }

                arguments.Add("-r");
                arguments.Add(((IPEndPoint)_socket.LocalEndPoint).Port.ToString());
                return(arguments.ToArray());
            }
        public override bool Execute()
        {
            var modules = new List <ITaskItem>();

            foreach (var path in Paths)
            {
                try {
                    modules.Add(new TaskItem(ModulePath.FromFullPath(path.ItemSpec, PathLimit).ModuleName));
                } catch (ArgumentException ex) {
                    Log.LogErrorFromException(ex);
                }
            }

            ModuleNames = modules.ToArray();
            return(!Log.HasLoggedErrors);
        }
Exemplo n.º 13
0
        public ModulePath FindModule(string filePath)
        {
            var bestLibraryPath = string.Empty;

            foreach (var p in InterpreterPaths.Concat(UserPaths))
            {
                if (PathEqualityComparer.Instance.StartsWith(filePath, p))
                {
                    if (p.Length > bestLibraryPath.Length)
                    {
                        bestLibraryPath = p;
                    }
                }
            }
            return(ModulePath.FromFullPath(filePath, bestLibraryPath));
        }
        public ModulePath FindModule(string filePath)
        {
            var bestLibraryPath = string.Empty;

            foreach (var p in Configuration.SearchPaths)
            {
                if (PathEqualityComparer.Instance.StartsWith(filePath, p))
                {
                    if (p.Length > bestLibraryPath.Length)
                    {
                        bestLibraryPath = p;
                    }
                }
            }
            return(ModulePath.FromFullPath(filePath, bestLibraryPath));
        }
Exemplo n.º 15
0
        private DjangoAnalyzer AnalyzerTest(string path)
        {
            string djangoDbPath = TestData.GetPath("TestData\\DjangoDB");

            Assert.IsTrue(
                PythonTypeDatabase.IsDatabaseVersionCurrent(djangoDbPath),
                "TestData\\DjangoDB needs updating."
                );

            var testFact = PythonInterpreterFactoryWithDatabase.CreateFromDatabase(
                new Version(2, 7),
                TestData.GetPath("CompletionDB"),
                djangoDbPath
                );

            var            serviceProvider = PythonToolsTestUtilities.CreateMockServiceProvider();
            PythonAnalyzer analyzer        = PythonAnalyzer.CreateAsync(testFact).WaitAndUnwrapExceptions();
            DjangoAnalyzer djangoAnalyzer  = new DjangoAnalyzer();

            djangoAnalyzer.Register(analyzer);

            analyzer.SetSearchPaths(new[] { path });

            List <IPythonProjectEntry> entries = new List <IPythonProjectEntry>();

            foreach (string file in Directory.EnumerateFiles(path, "*.py", SearchOption.AllDirectories))
            {
                var entry  = analyzer.AddModule(ModulePath.FromFullPath(file).ModuleName, file);
                var parser = Parser.CreateParser(
                    new FileStream(file, FileMode.Open, FileAccess.Read),
                    PythonLanguageVersion.V27
                    );
                using (var p = entry.BeginParse()) {
                    p.Tree = parser.ParseFile();
                    p.Complete();
                }
                entries.Add(entry);
            }

            foreach (var entry in entries)
            {
                entry.Analyze(CancellationToken.None, false);
            }

            return(djangoAnalyzer);
        }
Exemplo n.º 16
0
        private DjangoAnalyzer AnalyzerTest(string path)
        {
            string djangoDbPath = TestData.GetPath("TestData\\DjangoDB");

            Assert.IsTrue(
                PythonTypeDatabase.IsDatabaseVersionCurrent(djangoDbPath),
                "TestData\\DjangoDB needs updating."
                );

            var testFact = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(
                new Version(2, 7),
                "Django Test Interpreter",
                TestData.GetPath("CompletionDB"),
                djangoDbPath
                );

            var            serviceProvider = PythonToolsTestUtilities.CreateMockServiceProvider();
            PythonAnalyzer analyzer        = new PythonAnalyzer(testFact);
            DjangoAnalyzer djangoAnalyzer  = new DjangoAnalyzer(serviceProvider);

            djangoAnalyzer.OnNewAnalyzer(analyzer);

            analyzer.AddAnalysisDirectory(path);

            List <IPythonProjectEntry> entries = new List <IPythonProjectEntry>();

            foreach (string file in Directory.EnumerateFiles(path, "*.py", SearchOption.AllDirectories))
            {
                var entry  = analyzer.AddModule(ModulePath.FromFullPath(file).ModuleName, file);
                var parser = Parser.CreateParser(
                    new FileStream(file, FileMode.Open, FileAccess.Read),
                    PythonLanguageVersion.V27
                    );
                entry.UpdateTree(parser.ParseFile(), null);
                entries.Add(entry);
            }

            foreach (var entry in entries)
            {
                entry.Analyze(CancellationToken.None, false);
            }

            return(djangoAnalyzer);
        }
Exemplo n.º 17
0
        // TODO: Pylance
        // Replacement for ModulePath.GetModulesInLib which disappeared, this is an approximation
        private static IEnumerable <ModulePath> GetModulesInLib(
            string libraryPath,
            string sitePackagesPath,
            bool requiresInitPyFiles
            )
        {
            var folderPaths = Directory
                              .EnumerateDirectories(libraryPath, "*", SearchOption.AllDirectories)
                              .Where(folderPath => !PathUtils.IsSameDirectory(folderPath, sitePackagesPath))
                              .Where(folderPath => !requiresInitPyFiles || File.Exists(Path.Combine(folderPath, "__init__.py")))
                              .Prepend(libraryPath);

            foreach (var filePath in folderPaths.SelectMany(
                         folderPath => Directory.EnumerateFiles(folderPath, "*.py").Where(ModulePath.IsPythonFile))
                     )
            {
                yield return(ModulePath.FromFullPath(filePath, libraryPath));
            }
        }
Exemplo n.º 18
0
        private DjangoAnalyzer AnalyzerTest(string path)
        {
            var version  = new Version(2, 7);
            var testFact = new AstPythonInterpreterFactory(
                new InterpreterConfiguration($"AnalysisOnly|{version}", $"Analysis Only {version}", version: version, uiMode: InterpreterUIMode.Normal),
                new InterpreterFactoryCreationOptions {
                WatchFileSystem = false
            }
                );

            var            serviceProvider = PythonToolsTestUtilities.CreateMockServiceProvider();
            PythonAnalyzer analyzer        = PythonAnalyzer.CreateAsync(testFact).WaitAndUnwrapExceptions();
            DjangoAnalyzer djangoAnalyzer  = new DjangoAnalyzer();

            djangoAnalyzer.Register(analyzer);

            analyzer.SetSearchPaths(new[] { path });

            List <IPythonProjectEntry> entries = new List <IPythonProjectEntry>();

            foreach (string file in Directory.EnumerateFiles(path, "*.py", SearchOption.AllDirectories))
            {
                var entry  = analyzer.AddModule(ModulePath.FromFullPath(file).ModuleName, file);
                var parser = Parser.CreateParser(
                    new FileStream(file, FileMode.Open, FileAccess.Read),
                    PythonLanguageVersion.V27
                    );
                using (var p = entry.BeginParse()) {
                    p.Tree = parser.ParseFile();
                    p.Complete();
                }
                entries.Add(entry);
            }

            foreach (var entry in entries)
            {
                entry.Analyze(CancellationToken.None, false);
            }

            return(djangoAnalyzer);
        }
Exemplo n.º 19
0
        public static IPythonModule FromStream(
            IPythonInterpreter interpreter,
            Stream sourceFile,
            string fileName,
            PythonLanguageVersion langVersion,
            string moduleFullName
            )
        {
            PythonAst ast;

            using (var parser = Parser.CreateParser(sourceFile, langVersion)) {
                ast = parser.ParseFile();
            }

            return(new AstPythonModule(
                       moduleFullName ?? ModulePath.FromFullPath(fileName, isPackage: IsPackageCheck).FullName,
                       interpreter,
                       ast,
                       fileName
                       ));
        }
Exemplo n.º 20
0
        private ModulePath FindModule(string filePath)
        {
            var sp = GetSearchPaths();

            string bestLibraryPath = "";

            foreach (var p in sp)
            {
                if (PathUtils.IsSubpathOf(p.Path, filePath))
                {
                    if (p.Path.Length > bestLibraryPath.Length)
                    {
                        bestLibraryPath = p.Path;
                    }
                }
            }

            var mp = ModulePath.FromFullPath(filePath, bestLibraryPath);

            return(mp);
        }
Exemplo n.º 21
0
        private async Task <ModulePath> FindModuleAsync(string filePath)
        {
            var sp = await GetSearchPathsAsync();

            string bestLibraryPath = "";

            foreach (var p in sp)
            {
                if (PathEqualityComparer.Instance.StartsWith(filePath, p))
                {
                    if (p.Length > bestLibraryPath.Length)
                    {
                        bestLibraryPath = p;
                    }
                }
            }

            var mp = ModulePath.FromFullPath(filePath, bestLibraryPath);

            return(mp);
        }
Exemplo n.º 22
0
        private static IEnumerable <ModulePath> GetModules(string args)
        {
            var m        = Regex.Match(args, "(\\*|[\\w\\.]+)\\s+(.+)");
            var modName  = m.Groups[1].Value;
            var fileName = m.Groups[2].Value;

            if (modName != "*")
            {
                fileName = Path.GetFullPath(fileName);
                yield return(new ModulePath(modName, fileName, null));

                yield break;
            }

            if (!fileName.Contains("*"))
            {
                yield return(ModulePath.FromFullPath(Path.GetFullPath(fileName)));

                yield break;
            }

            var opt = SearchOption.TopDirectoryOnly;

            if (fileName.StartsWith("**\\"))
            {
                opt      = SearchOption.AllDirectories;
                fileName = fileName.Substring(3);
            }

            if (!Path.IsPathRooted(fileName))
            {
                fileName = Path.Combine(Environment.CurrentDirectory, fileName);
            }

            Console.WriteLine("Adding modules from {0}", fileName);
            foreach (var file in Directory.EnumerateFiles(Path.GetDirectoryName(fileName), Path.GetFileName(fileName), opt))
            {
                yield return(ModulePath.FromFullPath(file));
            }
        }
Exemplo n.º 23
0
        public static IPythonModule FromStream(
            IPythonInterpreter interpreter,
            Stream sourceFile,
            string fileName,
            PythonLanguageVersion langVersion,
            string moduleFullName
            )
        {
            PythonAst ast;
            var       parser = Parser.CreateParser(sourceFile, langVersion, new ParserOptions {
                StubFile = fileName.EndsWithOrdinal(".pyi", ignoreCase: true)
            });

            ast = parser.ParseFile();

            return(new AstPythonModule(
                       moduleFullName ?? ModulePath.FromFullPath(fileName, isPackage: IsPackageCheck).FullName,
                       interpreter,
                       ast,
                       fileName
                       ));
        }
Exemplo n.º 24
0
        public static IPythonModule FromStream(
            IPythonInterpreter interpreter,
            Stream sourceFile,
            string fileName,
            PythonLanguageVersion langVersion,
            string moduleFullName
            )
        {
            PythonAst ast;

            using (var parser = Parser.CreateParser(sourceFile, langVersion, new ParserOptions {
                StubFile = fileName?.EndsWith(".pyi", StringComparison.OrdinalIgnoreCase) ?? false,
                Verbatim = true
            })) {
                ast = parser.ParseFile();
            }

            return(new AstPythonModule(
                       moduleFullName ?? ModulePath.FromFullPath(fileName, isPackage: IsPackageCheck).FullName,
                       interpreter,
                       ast,
                       fileName
                       ));
        }
Exemplo n.º 25
0
        internal PythonAnalyzer AnalyzeDir(string dir, PythonLanguageVersion version = PythonLanguageVersion.V27, IEnumerable <string> excludeDirectories = null, CancellationToken?cancel = null)
        {
            List <string> files = new List <string>();

            try {
                ISet <string> excluded = null;
                if (excludeDirectories != null)
                {
                    excluded = new HashSet <string>(excludeDirectories, StringComparer.InvariantCultureIgnoreCase);
                }
                CollectFiles(dir, files, excluded);
            } catch (DirectoryNotFoundException) {
                return(null);
            }

            List <FileStreamReader> sourceUnits = new List <FileStreamReader>();

            foreach (string file in files)
            {
                sourceUnits.Add(
                    new FileStreamReader(file)
                    );
            }

            Stopwatch sw = new Stopwatch();

            sw.Start();
            long start0 = sw.ElapsedMilliseconds;
            // Explicitly specify the builtins name because we are using a 2.7
            // interpreter for all versions.
            var fact         = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion());
            var projectState = new PythonAnalyzer(fact, fact.CreateInterpreter(), "__builtin__");

            projectState.Limits = AnalysisLimits.GetStandardLibraryLimits();

            var modules = new List <IPythonProjectEntry>();

            foreach (var sourceUnit in sourceUnits)
            {
                try {
                    modules.Add(projectState.AddModule(
                                    ModulePath.FromFullPath(sourceUnit.Path).ModuleName,
                                    sourceUnit.Path,
                                    null
                                    ));
                } catch (ArgumentException) {
                    // Invalid module name, so skip the module
                }
            }
            long start1 = sw.ElapsedMilliseconds;

            Trace.TraceInformation("AddSourceUnit: {0} ms", start1 - start0);

            var nodes = new List <Microsoft.PythonTools.Parsing.Ast.PythonAst>();

            for (int i = 0; i < modules.Count; i++)
            {
                PythonAst ast = null;
                try {
                    var sourceUnit = sourceUnits[i];

                    ast = Parser.CreateParser(sourceUnit, version).ParseFile();
                } catch (Exception) {
                }
                nodes.Add(ast);
            }
            long start2 = sw.ElapsedMilliseconds;

            Trace.TraceInformation("Parse: {0} ms", start2 - start1);

            for (int i = 0; i < modules.Count; i++)
            {
                var ast = nodes[i];

                if (ast != null)
                {
                    modules[i].UpdateTree(ast, null);
                }
            }

            long start3 = sw.ElapsedMilliseconds;

            for (int i = 0; i < modules.Count; i++)
            {
                Trace.TraceInformation("Analyzing {1}: {0} ms", sw.ElapsedMilliseconds - start3, sourceUnits[i].Path);
                var ast = nodes[i];
                if (ast != null)
                {
                    modules[i].Analyze(cancel ?? CancellationToken.None, true);
                }
            }
            if (modules.Count > 0)
            {
                Trace.TraceInformation("Analyzing queue");
                modules[0].AnalysisGroup.AnalyzeQueuedEntries(cancel ?? CancellationToken.None);
            }

            long start4 = sw.ElapsedMilliseconds;

            Trace.TraceInformation("Analyze: {0} ms", start4 - start3);
            return(projectState);
        }
Exemplo n.º 26
0
        private async Task CompiledBuiltinScrapeAsync(InterpreterConfiguration configuration)
        {
            configuration.AssertInstalled();

            var moduleUri       = TestData.GetDefaultModuleUri();
            var moduleDirectory = Path.GetDirectoryName(moduleUri.LocalPath);

            var services = await CreateServicesAsync(moduleDirectory, configuration);

            var interpreter = services.GetService <IPythonInterpreter>();
            var fs          = services.GetService <IFileSystem>();

            // TODO: this is Windows only
            var dllsDir = Path.Combine(Path.GetDirectoryName(interpreter.Configuration.LibraryPath), "DLLs");

            if (!Directory.Exists(dllsDir))
            {
                Assert.Inconclusive("Configuration does not have DLLs");
            }

            var report           = new List <string>();
            var permittedImports = interpreter.LanguageVersion.Is2x() ?
                                   new[] { interpreter.ModuleResolution.BuiltinModuleName, "exceptions" } :
            new[] { interpreter.ModuleResolution.BuiltinModuleName };

            var modules = new List <IPythonModule>();

            foreach (var pyd in PathUtils.EnumerateFiles(fs, dllsDir, "*", recurse: false).Select(f => f.FullName).Where(ModulePath.IsPythonFile))
            {
                var mp = ModulePath.FromFullPath(pyd);
                if (mp.IsDebug)
                {
                    continue;
                }

                Console.WriteLine(@"Importing {0} from {1}", mp.ModuleName, mp.SourceFile);
                modules.Add(interpreter.ModuleResolution.GetOrLoadModule(mp.ModuleName));
            }

            foreach (var mod in modules)
            {
                Assert.IsInstanceOfType(mod, typeof(CompiledPythonModule));

                await((StubCache)interpreter.ModuleResolution.StubCache).CacheWritingTask;
                var modPath = interpreter.ModuleResolution.StubCache.GetCacheFilePath(mod.FilePath);
                Assert.IsTrue(File.Exists(modPath), "No cache file created");

                var doc = (IDocument)mod;
                var ast = await doc.GetAstAsync();

                var errors = doc.GetParseErrors().ToArray();
                foreach (var err in errors)
                {
                    Console.WriteLine(err);
                }
                Assert.AreEqual(0, errors.Length, "Parse errors occurred");


                var imports = ((SuiteStatement)ast.Body).Statements
                              .OfType <ImportStatement>()
                              .SelectMany(s => s.Names)
                              .Select(n => n.MakeString())
                              .Except(permittedImports)
                              .ToArray();

                // We expect no imports (after excluding builtins)
                report.AddRange(imports.Select(n => $"{mod.Name} imported {n}"));
            }

            report.Should().BeEmpty();
        }
Exemplo n.º 27
0
        public void DiscoverTests(IEnumerable <string> sources, IDiscoveryContext discoveryContext, IMessageLogger logger, ITestCaseDiscoverySink discoverySink)
        {
            ValidateArg.NotNull(sources, "sources");
            ValidateArg.NotNull(discoverySink, "discoverySink");

            var buildEngine = new MSBuild.ProjectCollection();

            try {
                // Load all the test containers passed in (.pyproj msbuild files)
                foreach (string source in sources)
                {
                    buildEngine.LoadProject(source);
                }

                foreach (var proj in buildEngine.LoadedProjects)
                {
                    using (var provider = new MSBuildProjectInterpreterFactoryProvider(_interpreterService, proj)) {
                        try {
                            provider.DiscoverInterpreters();
                        } catch (InvalidDataException) {
                            // This exception can be safely ignored here.
                        }
                        var factory = provider.ActiveInterpreter;
                        if (factory == _interpreterService.NoInterpretersValue)
                        {
                            if (logger != null)
                            {
                                logger.SendMessage(TestMessageLevel.Warning, "No interpreters available for project " + proj.FullPath);
                            }
                            continue;
                        }

                        var projectHome = Path.GetFullPath(Path.Combine(proj.DirectoryPath, proj.GetPropertyValue(PythonConstants.ProjectHomeSetting) ?? "."));

                        // Do the analysis even if the database is not up to date. At
                        // worst, we'll get no results.
                        using (var analyzer = new TestAnalyzer(
                                   factory,
                                   proj.FullPath,
                                   projectHome,
                                   TestExecutor.ExecutorUri
                                   )) {
                            // Provide all files to the test analyzer
                            foreach (var item in proj.GetItems("Compile"))
                            {
                                string fileAbsolutePath = CommonUtils.GetAbsoluteFilePath(projectHome, item.EvaluatedInclude);
                                string fullName;

                                try {
                                    fullName = ModulePath.FromFullPath(fileAbsolutePath).ModuleName;
                                } catch (ArgumentException) {
                                    if (logger != null)
                                    {
                                        logger.SendMessage(TestMessageLevel.Warning, "File has an invalid module name: " + fileAbsolutePath);
                                    }
                                    continue;
                                }

                                try {
                                    using (var reader = new StreamReader(fileAbsolutePath)) {
                                        analyzer.AddModule(fullName, fileAbsolutePath, reader);
                                    }
                                } catch (FileNotFoundException) {
                                    // user deleted file, we send the test update, but the project
                                    // isn't saved.
#if DEBUG
                                } catch (Exception ex) {
                                    if (logger != null)
                                    {
                                        logger.SendMessage(TestMessageLevel.Warning, "Failed to discover tests in " + fileAbsolutePath);
                                        logger.SendMessage(TestMessageLevel.Informational, ex.ToString());
                                    }
                                }
#else
                                } catch (Exception) {
                                    if (logger != null)
                                    {
                                        logger.SendMessage(TestMessageLevel.Warning, "Failed to discover tests in " + fileAbsolutePath);
                                    }
                                }
#endif
                            }
Exemplo n.º 28
0
        private void AstNativeBuiltinScrape(PythonVersion version) {
            AstScrapedPythonModule.KeepAst = true;
            version.AssertInstalled();
            using (var analysis = CreateAnalysis(version)) {
                try {
                    var fact = (AstPythonInterpreterFactory)analysis.Analyzer.InterpreterFactory;
                    var interp = (AstPythonInterpreter)analysis.Analyzer.Interpreter;
                    var ctxt = interp.CreateModuleContext();

                    var dllsDir = PathUtils.GetAbsoluteDirectoryPath(fact.Configuration.PrefixPath, "DLLs");
                    if (!Directory.Exists(dllsDir)) {
                        Assert.Inconclusive("Configuration does not have DLLs");
                    }

                    var report = new List<string>();
                    var permittedImports = fact.GetLanguageVersion().Is2x() ?
                        new[] { interp.BuiltinModuleName, "exceptions" } :
                        new[] { interp.BuiltinModuleName };

                    foreach (var pyd in PathUtils.EnumerateFiles(dllsDir, "*", recurse: false).Where(ModulePath.IsPythonFile)) {
                        var mp = ModulePath.FromFullPath(pyd);
                        if (mp.IsDebug) {
                            continue;
                        }

                        Console.WriteLine("Importing {0} from {1}", mp.ModuleName, mp.SourceFile);
                        var mod = interp.ImportModule(mp.ModuleName);
                        Assert.IsInstanceOfType(mod, typeof(AstScrapedPythonModule));
                        mod.Imported(ctxt);

                        var modPath = fact.GetCacheFilePath(pyd);
                        Assert.IsTrue(File.Exists(modPath), "No cache file created");
                        _moduleCache = File.ReadAllText(modPath);

                        var errors = ((AstScrapedPythonModule)mod).ParseErrors ?? Enumerable.Empty<string>();
                        foreach (var err in errors) {
                            Console.WriteLine(err);
                        }
                        Assert.AreEqual(0, errors.Count(), "Parse errors occurred");

                        var ast = ((AstScrapedPythonModule)mod).Ast;

                        
                        var imports = ((Ast.SuiteStatement)ast.Body).Statements
                            .OfType<Ast.ImportStatement>()
                            .SelectMany(s => s.Names)
                            .Select(n => n.MakeString())
                            .Except(permittedImports)
                            .ToArray();

                        // We expect no imports (after excluding builtins)
                        report.AddRange(imports.Select(n => $"{mp.ModuleName} imported {n}"));

                        _moduleCache = null;
                    }

                    AssertUtil.ContainsExactly(report);
                } finally {
                    _analysisLog = analysis.GetLogContent(CultureInfo.InvariantCulture);
                }
            }
        }
Exemplo n.º 29
0
            private void ConectionReceivedEvent(object sender, EventReceivedEventArgs e)
            {
                switch (e.Name)
                {
                case TP.ResultEvent.Name:
                    var         result  = (TP.ResultEvent)e.Event;
                    TestOutcome outcome = TestOutcome.None;
                    switch (result.outcome)
                    {
                    case "passed": outcome = TestOutcome.Passed; break;

                    case "failed": outcome = TestOutcome.Failed; break;

                    case "skipped": outcome = TestOutcome.Skipped; break;
                    }

                    var testResult = new TestResult(_curTest);
                    RecordEnd(
                        _frameworkHandle,
                        _curTest,
                        testResult,
                        _stdOut.ToString(),
                        _stdErr.ToString(),
                        outcome,
                        result
                        );

                    _stdOut.Clear();
                    _stdErr.Clear();
                    break;

                case TP.StartEvent.Name:
                    var start = (TP.StartEvent)e.Event;
                    _curTest = null;
                    foreach (var test in _tests)
                    {
                        string testFile, testClass, testMethod;
                        TestDiscoverer.ParseFullyQualifiedTestName(
                            test.FullyQualifiedName,
                            out testFile,
                            out testClass,
                            out testMethod
                            );

                        string testFilePath = CommonUtils.GetAbsoluteFilePath(_settings.ProjectHome, test.CodeFilePath);
                        var    modulePath   = ModulePath.FromFullPath(testFilePath);

                        if (start.test == modulePath.ModuleName + "." + testClass + "." + testMethod)
                        {
                            _curTest = test;
                            break;
                        }
                    }

                    if (_curTest != null)
                    {
                        _frameworkHandle.RecordStart(_curTest);
                    }
                    else
                    {
                        Warning(
                            string.Format(
                                "Unexpected test result: {0} {1} {2}",
                                start.classname,
                                start.method
                                )
                            );
                    }
                    break;

                case TP.StdErrEvent.Name:
                    var err = (TP.StdErrEvent)e.Event;
                    _stdErr.Append(err.content);
                    break;

                case TP.StdOutEvent.Name:
                    var outp = (TP.StdOutEvent)e.Event;
                    _stdOut.Append(outp.content);
                    break;

                case TP.DoneEvent.Name:
                    _done.Set();
                    break;
                }
            }