public void LayeredDatabase() { using (var db1 = MockCompletionDB.Create(PythonLanguageVersion.V27, "os")) using (var db2 = MockCompletionDB.Create(PythonLanguageVersion.V27, "posixpath")) { Assert.IsNotNull(db1.Database.GetModule("os")); Assert.IsNull(db1.Database.GetModule("posixpath")); var ptd1 = db1.Database; var ptd2 = ptd1.Clone(); ptd2.LoadDatabase(db2.DBPath); Assert.IsNull(ptd1.GetModule("posixpath")); Assert.IsNotNull(ptd2.GetModule("os")); Assert.IsNotNull(ptd2.GetModule("posixpath")); Assert.AreSame(ptd1.GetModule("os"), db1.Database.GetModule("os")); Assert.AreSame(ptd2.GetModule("os"), db1.Database.GetModule("os")); } var factory = PythonInterpreterFactoryWithDatabase.CreateFromDatabase(new Version(2, 7)); using (var db1 = MockCompletionDB.Create(PythonLanguageVersion.V27, "os", "posixpath")) using (var db2 = MockCompletionDB.Create(PythonLanguageVersion.V27, "posixpath")) { var ptd1 = new PythonTypeDatabase(factory, new[] { db1.DBPath, db2.DBPath }); Assert.IsNotNull(ptd1.GetModule("posixpath")); Assert.AreNotSame(ptd1.GetModule("posixpath"), db1.Database.GetModule("posixpath")); Assert.AreNotSame(ptd1.GetModule("posixpath"), db2.Database.GetModule("posixpath")); var ptd2 = new PythonTypeDatabase(factory, new[] { db2.DBPath, db1.DBPath }); Assert.IsNotNull(ptd2.GetModule("posixpath")); Assert.AreNotSame(ptd2.GetModule("posixpath"), db1.Database.GetModule("posixpath")); Assert.AreNotSame(ptd2.GetModule("posixpath"), db2.Database.GetModule("posixpath")); } using (var db1 = MockCompletionDB.Create(PythonLanguageVersion.V27, "os", "posixpath")) using (var db2 = MockCompletionDB.Create(PythonLanguageVersion.V27, "posixpath")) using (var db3 = MockCompletionDB.Create(PythonLanguageVersion.V27, "ntpath")) { var ptd = db1.Database; Assert.AreSame(ptd.GetModule("posixpath"), db1.Database.GetModule("posixpath")); Assert.AreNotSame(ptd.GetModule("posixpath"), db2.Database.GetModule("posixpath")); var ptd2 = ptd.Clone(); ptd2.LoadDatabase(db2.DBPath); Assert.AreNotSame(ptd2.GetModule("posixpath"), ptd.GetModule("posixpath")); var ptd3 = ptd2.Clone(); ptd3.LoadDatabase(db3.DBPath); Assert.IsNotNull(ptd3.GetModule("ntpath")); Assert.IsNull(ptd2.GetModule("ntpath")); } }
public MockCompletionDB(string path, PythonLanguageVersion version) { DBPath = path; LanguageVersion = version; Directory.CreateDirectory(DBPath); _factory = PythonInterpreterFactoryWithDatabase.CreateFromDatabase(version.ToVersion(), DBPath); }
private SaveLoadResult SaveLoad(PythonLanguageVersion version, params AnalysisModule[] modules) { IPythonProjectEntry[] entries = new IPythonProjectEntry[modules.Length]; var fact = PythonInterpreterFactoryWithDatabase.CreateFromDatabase(version.ToVersion()); var interp = fact.CreateInterpreter(); var dbFolder = TestData.GetTempPath(); var state = new PythonAnalysis(fact, interp); state.CreateProjectOnDisk = true; for (int i = 0; i < modules.Length; i++) { state.AddModule(modules[i].ModuleName, modules[i].Code, modules[i].Filename); } state.WaitForAnalysis(); new SaveAnalysis().Save(state.Analyzer, dbFolder); File.WriteAllText(Path.Combine(dbFolder, "database.ver"), PythonTypeDatabase.CurrentVersion.ToString()); File.Copy( Path.Combine(PythonTypeDatabase.BaselineDatabasePath, state.Analyzer._builtinName + ".idb"), Path.Combine(dbFolder, state.Analyzer._builtinName + ".idb"), true ); var loadFactory = PythonInterpreterFactoryWithDatabase.CreateFromDatabase(version.ToVersion(), dbFolder); return(new SaveLoadResult(CreateAnalyzer(loadFactory), state.CodeFolder)); }
public void TestPthFiles() { var outputPath = TestData.GetTempPath(); Console.WriteLine("Writing to: " + outputPath); // run the analyzer using (var output = ProcessOutput.RunHiddenAndCapture("Microsoft.PythonTools.Analyzer.exe", "/lib", TestData.GetPath("TestData", "PathStdLib"), "/version", "2.7", "/outdir", outputPath, "/indir", CompletionDB, "/unittest", "/log", "AnalysisLog.txt")) { output.Wait(); Console.WriteLine("* Stdout *"); foreach (var line in output.StandardOutputLines) { Console.WriteLine(line); } Console.WriteLine("* Stderr *"); foreach (var line in output.StandardErrorLines) { Console.WriteLine(line); } Assert.AreEqual(0, output.ExitCode); } File.Copy(Path.Combine(CompletionDB, "__builtin__.idb"), Path.Combine(outputPath, "__builtin__.idb")); var fact = PythonInterpreterFactoryWithDatabase.CreateFromDatabase(new Version(2, 7)); var paths = new List <string> { outputPath }; paths.AddRange(Directory.EnumerateDirectories(outputPath)); var typeDb = new PythonTypeDatabase(fact, paths); var module = typeDb.GetModule("SomeLib"); Assert.IsNotNull(module, "Could not import SomeLib"); var fobMod = typeDb.GetModule("SomeLib.fob"); Assert.IsNotNull(fobMod, "Could not import SomeLib.fob"); var cClass = ((IPythonModule)fobMod).GetMember(null, "C"); Assert.IsNotNull(cClass, "Could not get SomeLib.fob.C"); Assert.AreEqual(PythonMemberType.Class, cClass.MemberType); }
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); }
public void PydInPackage() { PythonPaths.Python27.AssertInstalled(); var outputPath = TestData.GetTempPath(); Console.WriteLine("Writing to: " + outputPath); // run the analyzer using (var output = ProcessOutput.RunHiddenAndCapture("Microsoft.PythonTools.Analyzer.exe", "/python", PythonPaths.Python27.InterpreterPath, "/lib", TestData.GetPath(@"TestData\PydStdLib"), "/version", "2.7", "/outdir", outputPath, "/indir", CompletionDB, "/unittest", "/log", "AnalysisLog.txt")) { output.Wait(); Console.WriteLine("* Stdout *"); foreach (var line in output.StandardOutputLines) { Console.WriteLine(line); } Console.WriteLine("* Stderr *"); foreach (var line in output.StandardErrorLines) { Console.WriteLine(line); } Assert.AreEqual(0, output.ExitCode); } var fact = PythonInterpreterFactoryWithDatabase.CreateFromDatabase(new Version(2, 7)); var paths = new List <string> { outputPath }; paths.AddRange(Directory.EnumerateDirectories(outputPath)); var typeDb = new PythonTypeDatabase(fact, paths); var module = typeDb.GetModule("Package.winsound"); Assert.IsNotNull(module, "Package.winsound was not analyzed"); var package = typeDb.GetModule("Package"); Assert.IsNotNull(package, "Could not import Package"); var member = package.GetMember(null, "winsound"); Assert.IsNotNull(member, "Could not get member Package.winsound"); Assert.AreSame(module, member); module = typeDb.GetModule("Package._testcapi"); Assert.IsNotNull(module, "Package._testcapi was not analyzed"); package = typeDb.GetModule("Package"); Assert.IsNotNull(package, "Could not import Package"); member = package.GetMember(null, "_testcapi"); Assert.IsNotNull(member, "Could not get member Package._testcapi"); Assert.IsNotInstanceOfType(member, typeof(CPythonMultipleMembers)); Assert.AreSame(module, member); module = typeDb.GetModule("Package.select"); Assert.IsNotNull(module, "Package.select was not analyzed"); package = typeDb.GetModule("Package"); Assert.IsNotNull(package, "Could not import Package"); member = package.GetMember(null, "select"); Assert.IsNotNull(member, "Could not get member Package.select"); Assert.IsInstanceOfType(member, typeof(CPythonMultipleMembers)); var mm = (CPythonMultipleMembers)member; AssertUtil.ContainsExactly(mm.Members.Select(m => m.MemberType), PythonMemberType.Module, PythonMemberType.Constant, PythonMemberType.Class ); Assert.IsNotNull(mm.Members.Contains(module)); try { // Only clean up if the test passed Directory.Delete(outputPath, true); } catch { } }