Esempio n. 1
0
        public void General()
        {
            string code = @"def f(a, *b, **c): pass

def f1(x = 42): pass
def f2(x = []): pass

class C(object): 
    @property
    def f(self):
        return 42
    def g(self):
        return 100

class D(C): 
    x = C().g

abc = 42
fob = int

class X(object): pass
class Y(object): pass

union = X()
union = Y()

list_of_int = [1, 2, 3]
tuple_of_str = 'a', 'b', 'c'

m = max

class Aliased(object):
    def f(self):
        pass

def Aliased(fob):
    pass
";

            using (var newPs = SaveLoad(PythonLanguageVersion.V27, new AnalysisModule("test", "test.py", code))) {
                AssertUtil.Contains(newPs.Analyzer.GetModules().Select(x => x.Name), "test");

                string codeText = @"
import test
abc = test.abc
fob = test.fob
a = test.C()
cf = a.f
cg = a.g()
dx = test.D().x
scg = test.C.g
f1 = test.f1
union = test.union
list_of_int = test.list_of_int
tuple_of_str = test.tuple_of_str
f1 = test.f1
f2 = test.f2
m = test.m
Aliased = test.Aliased
";
                var    newMod   = newPs.NewModule("baz", codeText);
                int    pos      = codeText.LastIndexOf('\n');

                AssertUtil.ContainsExactly(newMod.Analysis.GetTypeIdsByIndex("abc", pos), BuiltinTypeId.Int);
                AssertUtil.ContainsExactly(newMod.Analysis.GetTypeIdsByIndex("cf", pos), BuiltinTypeId.Int);
                AssertUtil.ContainsExactly(newMod.Analysis.GetTypeIdsByIndex("cg", pos), BuiltinTypeId.Int);
                Assert.AreEqual("function f1", newMod.Analysis.GetValuesByIndex("f1", pos).First().Description);
                Assert.AreEqual("bound method x", newMod.Analysis.GetValuesByIndex("dx", pos).First().Description);
                Assert.AreEqual("function g", newMod.Analysis.GetValuesByIndex("scg", pos).First().Description);
                var unionMembers = new List <AnalysisValue>(newMod.Analysis.GetValuesByIndex("union", pos));
                Assert.AreEqual(unionMembers.Count, 2);
                AssertUtil.ContainsExactly(unionMembers.Select(x => x.PythonType.Name), "X", "Y");

                var list = newMod.Analysis.GetValuesByIndex("list_of_int", pos).First();
                AssertUtil.ContainsExactly(newMod.Analysis.GetShortDescriptionsByIndex("list_of_int", pos), "list of int");
                AssertUtil.ContainsExactly(newMod.Analysis.GetShortDescriptionsByIndex("tuple_of_str", pos), "tuple of str");

                AssertUtil.ContainsExactly(newMod.Analysis.GetShortDescriptionsByIndex("fob", pos), "type int");

                var result = newMod.Analysis.GetSignaturesByIndex("f1", pos).ToArray();
                Assert.AreEqual(1, result.Length);
                Assert.AreEqual(1, result[0].Parameters.Length);
                Assert.AreEqual("int", result[0].Parameters[0].Type);

                result = newMod.Analysis.GetSignaturesByIndex("m", pos).ToArray();
                Assert.AreEqual(6, result.Length);

                var members = newMod.Analysis.GetMembersByIndex("Aliased", pos, GetMemberOptions.None);
                AssertUtil.Contains(members.Select(x => x.Name), "f");
                AssertUtil.Contains(members.Select(x => x.Name), "__self__");
            }
        }
Esempio n. 2
0
        public void BasicRequireCompletions()
        {
            using (new OptionHolder("TextEditor", "Node.js", "BraceCompletion", false)) {
                using (var solution = BasicProject.Generate().ToVs()) {
                    var server = solution.OpenItem("Require", "server.js");
                    Keyboard.Type("require(");

                    using (var completionSession = server.WaitForSession <ICompletionSession>()) {
                        Assert.AreEqual(1, completionSession.Session.CompletionSets.Count);

                        // we pick up built-ins, folders w/ package.json, and peers
                        AssertUtil.ContainsAtLeast(
                            completionSession.Session.GetDisplayTexts(),
                            "http",
                            "Foo",
                            "./myapp.js",
                            "./SomeFolder/baz.js",
                            "quox.js"
                            );

                        // we don't show our own file
                        AssertUtil.DoesntContain(completionSession.Session.GetDisplayTexts(), "./server.js");

                        AssertUtil.ContainsAtLeast(
                            completionSession.Session.GetInsertionTexts(),
                            "'http'",
                            "'Foo'",
                            "'./myapp.js'",
                            "'./SomeFolder/baz.js'",
                            "'quox.js'"
                            );

                        Keyboard.Type("htt");
                        server.WaitForText("require(htt");

                        // we should be filtered down
                        AssertUtil.ContainsExactly(
                            completionSession.Session.GetDisplayTexts(),
                            "http",
                            "https"
                            );

                        // this should trigger completion
                        Keyboard.Type(")");
                        server.WaitForText("require('http')");
                    }

                    Keyboard.Backspace(8);
                    server.WaitForText("require");

                    Keyboard.Type("(");
                    using (var completionSession = server.WaitForSession <ICompletionSession>()) {
                        Assert.AreEqual(1, completionSession.Session.CompletionSets.Count);

                        // this should dismiss the session and not complete anything
                        Keyboard.Type("'".ToString());
                        server.WaitForText("require('");

                        Assert.IsTrue(completionSession.Session.IsDismissed);
                    }

                    Keyboard.Backspace(2);
                    server.WaitForText("require");

                    Keyboard.Type("(");
                    using (var completionSession = server.WaitForSession <ICompletionSession>()) {
                        Assert.AreEqual(1, completionSession.Session.CompletionSets.Count);

                        // this should dismiss the session and not complete anything
                        Keyboard.Type("\"".ToString());
                        server.WaitForText("require(\"");

                        Assert.IsTrue(completionSession.Session.IsDismissed);
                    }
                }
            }
        }
Esempio n. 3
0
        public void FromImportCompletions()
        {
            using (var view = new PythonEditor()) {
                IEnumerable <string> completions = null;
                view.Text = "from ";
                AssertUtil.ContainsAtLeast(view.GetCompletions(-1), "nt", "sys");

                view.Text   = "from s";
                completions = view.GetCompletions(-1);
                AssertUtil.ContainsAtLeast(completions, "sys");
                AssertUtil.DoesntContain(completions, "nt");

                view.Text = "from sys ";
                AssertUtil.ContainsExactly(view.GetCompletions(-1), "import");

                view.Text = "from sys import";
                AssertUtil.ContainsExactly(view.GetCompletions(-1), "import");


                // This is the first time we have imported sys, so it will
                // run Python in the background to scrape it. We can wait.
                for (int retries = 100; retries > 0; --retries)
                {
                    view.Text = "from sys import ";
                    Thread.Sleep(100);
                    completions = view.GetCompletions(-1);
                    if (completions.Count() > 1)
                    {
                        break;
                    }
                }
                AssertUtil.ContainsAtLeast(completions,
                                           "*",          // Contains *
                                           "settrace",   // Contains functions
                                           "api_version" // Contains data members
                                           );

                // Error case - no completions
                view.Text = "from sys.";
                AssertUtil.ContainsExactly(view.GetCompletions(-1));

                view.Text = "from sys. import ";
                AssertUtil.ContainsExactly(view.GetCompletions(-1));

                view.Text = "from sys import settrace ";
                AssertUtil.ContainsExactly(view.GetCompletions(-1), "as");

                view.Text = "from sys import settrace as";
                AssertUtil.ContainsExactly(view.GetCompletions(-1), "as");

                view.Text   = "from sys import settrace,";
                completions = view.GetCompletions(-1);
                AssertUtil.ContainsAtLeast(completions, "api_version", "settrace");
                AssertUtil.DoesntContain(completions, "*");

                // No more completions after a *
                view.Text = "from sys import *, ";
                AssertUtil.ContainsExactly(view.GetCompletions(-1));

                view.Text = "from sys import settrace as ";
                AssertUtil.ContainsExactly(view.GetCompletions(-1));

                view.Text = "from sys import settrace as st ";
                AssertUtil.ContainsExactly(view.GetCompletions(-1));

                view.Text   = "from sys import settrace as st, ";
                completions = view.GetCompletions(-1);
                AssertUtil.ContainsAtLeast(completions, "api_version", "settrace");
                AssertUtil.DoesntContain(completions, "*");
            }
        }
Esempio n. 4
0
        private void ImportWizardVirtualEnvWorker(
            PythonVersion python,
            string venvModuleName,
            string expectedFile,
            bool brokenBaseInterpreter
            )
        {
            var mockService = new MockInterpreterOptionsService();

            mockService.AddProvider(new MockPythonInterpreterFactoryProvider("Test Provider",
                                                                             new MockPythonInterpreterFactory(
                                                                                 new InterpreterConfiguration(
                                                                                     python.Configuration.Id,
                                                                                     "Test Python",
                                                                                     python.Configuration.PrefixPath,
                                                                                     python.Configuration.InterpreterPath,
                                                                                     python.Configuration.WindowsInterpreterPath,
                                                                                     python.Configuration.LibraryPath,
                                                                                     python.Configuration.PathEnvironmentVariable,
                                                                                     python.Configuration.Architecture,
                                                                                     python.Configuration.Version,
                                                                                     python.Configuration.UIMode
                                                                                     )
                                                                                 )
                                                                             ));

            using (var wpf = new WpfProxy()) {
                var settings   = wpf.Create(() => new ImportSettings(null, mockService));
                var sourcePath = TestData.GetTempPath(randomSubPath: true);
                // Create a fake set of files to import
                File.WriteAllText(Path.Combine(sourcePath, "main.py"), "");
                Directory.CreateDirectory(Path.Combine(sourcePath, "A"));
                File.WriteAllText(Path.Combine(sourcePath, "A", "__init__.py"), "");
                // Create a real virtualenv environment to import
                using (var p = ProcessOutput.RunHiddenAndCapture(python.InterpreterPath, "-m", venvModuleName, Path.Combine(sourcePath, "env"))) {
                    Console.WriteLine(p.Arguments);
                    p.Wait();
                    Console.WriteLine(string.Join(Environment.NewLine, p.StandardOutputLines.Concat(p.StandardErrorLines)));
                    Assert.AreEqual(0, p.ExitCode);
                }

                if (brokenBaseInterpreter)
                {
                    var cfgPath = Path.Combine(sourcePath, "env", "Lib", "orig-prefix.txt");
                    if (File.Exists(cfgPath))
                    {
                        File.WriteAllText(cfgPath, string.Format("C:\\{0:N}", Guid.NewGuid()));
                    }
                    else if (File.Exists((cfgPath = Path.Combine(sourcePath, "env", "pyvenv.cfg"))))
                    {
                        File.WriteAllLines(cfgPath, File.ReadAllLines(cfgPath)
                                           .Select(line => {
                            if (line.StartsWith("home = "))
                            {
                                return(string.Format("home = C:\\{0:N}", Guid.NewGuid()));
                            }
                            return(line);
                        })
                                           );
                    }
                }

                Console.WriteLine("All files:");
                foreach (var f in Directory.EnumerateFiles(sourcePath, "*", SearchOption.AllDirectories))
                {
                    Console.WriteLine(PathUtils.GetRelativeFilePath(sourcePath, f));
                }

                Assert.IsTrue(
                    File.Exists(Path.Combine(sourcePath, "env", expectedFile)),
                    "Virtualenv was not created correctly"
                    );

                settings.SourcePath = sourcePath;

                string path = CreateRequestedProject(settings);

                Assert.AreEqual(settings.ProjectPath, path);
                var proj = XDocument.Load(path);

                // Does not include any .py files from the virtualenv
                AssertUtil.ContainsExactly(proj.Descendants(proj.GetName("Compile")).Select(x => x.Attribute("Include").Value),
                                           "main.py",
                                           "A\\__init__.py"
                                           );
                // Does not contain 'env'
                AssertUtil.ContainsExactly(proj.Descendants(proj.GetName("Folder")).Select(x => x.Attribute("Include").Value),
                                           "A"
                                           );

                var env = proj.Descendant("Interpreter");
                Assert.AreEqual("env\\", env.Attribute("Include").Value);
                Assert.AreEqual("lib\\", env.Descendant("LibraryPath").Value, true);
                if (brokenBaseInterpreter)
                {
                    Assert.AreEqual("env", env.Descendant("Description").Value);
                    Assert.AreEqual("", env.Descendant("InterpreterPath").Value);
                    Assert.AreEqual("", env.Descendant("WindowsInterpreterPath").Value);
                    Assert.AreEqual("", env.Descendant("BaseInterpreter").Value);
                    Assert.AreEqual("", env.Descendant("PathEnvironmentVariable").Value);
                }
                else
                {
                    Assert.AreEqual("env (Test Python)", env.Descendant("Description").Value);
                    Assert.AreEqual("scripts\\python.exe", env.Descendant("InterpreterPath").Value, true);
                    // The mock configuration uses python.exe for both paths.
                    Assert.AreEqual("scripts\\python.exe", env.Descendant("WindowsInterpreterPath").Value, true);
                    Assert.AreEqual(python.Id, env.Descendant("BaseInterpreter").Value, true);
                    Assert.AreEqual("PYTHONPATH", env.Descendant("PathEnvironmentVariable").Value, true);
                }
            }
        }
Esempio n. 5
0
        private IEnumerable <Task> StartCrossThreadAnalysisCalls(
            CancellationToken cancel,
            PythonLanguageVersion version
            )
        {
            var fact  = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion());
            var state = PythonAnalyzer.CreateSynchronously(fact);

            const string testCode = @"from mod{0:000} import test_func as other_test_func, MyClass as other_mc

c = None
def test_func(a, b={1}()):
    '''My test function'''
    globals c
    a = b
    a = {1}(a)
    b = other_test_func(a)
    c = other_mc.fn(b)
    return b

class MyClass:
    fn = test_func

my_test_func = test_func
my_test_func = other_test_func
my_test_func('abc')

mc = MyClass()
mc.fn([])
";

            var entries = Enumerable.Range(0, 100)
                          .Select(i => {
                var entry = state.AddModule(string.Format("mod{0:000}", i), string.Format("mod{0:000}.py", i));
                entry.ParseFormat(PythonLanguageVersion.V34, testCode, i + 1, PythonTypes[i % PythonTypes.Count]);
                return(entry);
            })
                          .ToList();

            // One analysis before we start
            foreach (var e in entries)
            {
                e.Analyze(cancel, true);
            }
            state.AnalyzeQueuedEntries(cancel);

            // Repeatedly re-analyse the code
            yield return(Task.Run(() => {
                var rnd = new Random();
                while (!cancel.IsCancellationRequested)
                {
                    var shufEntries = entries
                                      .Select(e => Tuple.Create(rnd.Next(), e))
                                      .OrderBy(t => t.Item1)
                                      .Take(entries.Count / 2)
                                      .Select(t => t.Item2)
                                      .ToList();
                    foreach (var e in shufEntries)
                    {
                        e.Analyze(cancel, true);
                    }

                    state.AnalyzeQueuedEntries(cancel);
                    Console.WriteLine("Analysis complete");
                    Thread.Sleep(1000);
                }
            }, cancel));

            // Repeatedly re-parse the code
            yield return(Task.Run(() => {
                var rnd = new Random();
                while (!cancel.IsCancellationRequested)
                {
                    var shufEntries = entries
                                      .Select((e, i) => Tuple.Create(rnd.Next(), e, i))
                                      .OrderBy(t => t.Item1)
                                      .Take(entries.Count / 4)
                                      .ToList();
                    foreach (var t in shufEntries)
                    {
                        var i = t.Item3;
                        t.Item2.ParseFormat(PythonLanguageVersion.V34, testCode, i + 1, PythonTypes[i % PythonTypes.Count]);
                    }
                    Thread.Sleep(1000);
                }
            }, cancel));

            // Repeatedly request signatures
            yield return(Task.Run(() => {
                var entry = entries[1];
                while (!cancel.IsCancellationRequested)
                {
                    var sigs = entry.Analysis.GetSignaturesByIndex("my_test_func", 0).ToList();

                    if (sigs.Any())
                    {
                        AssertUtil.ContainsExactly(
                            sigs.Select(s => s.Name),
                            "test_func"
                            );

                        foreach (var s in sigs)
                        {
                            AssertUtil.ContainsExactly(s.Parameters.Select(p => p.Name), "a", "b");
                        }
                    }
                }
            }, cancel));

            // Repeated request variables and descriptions
            yield return(Task.Run(() => {
                var entry = entries[1];
                while (!cancel.IsCancellationRequested)
                {
                    var descriptions = entry.Analysis.GetDescriptionsByIndex("my_test_func", 0).ToList();
                    descriptions = entry.Analysis.GetDescriptionsByIndex("c", 0).ToList();
                }
            }, cancel));

            // Repeated request members and documentation
            yield return(Task.Run(() => {
                var entry = entries[1];
                while (!cancel.IsCancellationRequested)
                {
                    var descriptions = entry.Analysis.GetCompletionDocumentationByIndex("mc", "fn", 0).ToList();
                }
            }, cancel));
        }
Esempio n. 6
0
        internal void LocalsTest(
            string filename,
            int breakpoint,
            int frameIndex             = 0,
            string[] expectedParams    = null,
            string[] expectedLocals    = null,
            string[] expectedValues    = null,
            string[] expectedHexValues = null
            )
        {
            TestDebuggerSteps(
                filename,
                new[] {
                new TestStep(action: TestAction.AddBreakpoint, targetBreakpoint: breakpoint),
                new TestStep(action: TestAction.ResumeProcess, expectedBreakpointHit: breakpoint),
                new TestStep(validation: (process, thread) => {
                    var frame = thread.Frames[frameIndex];
                    AssertUtil.ContainsExactly(
                        new HashSet <string>(expectedParams ?? new string[] { }),
                        frame.Parameters.Select(x => x.Expression)
                        );
                    AssertUtil.ContainsExactly(
                        new HashSet <string>(expectedLocals ?? new string[] { }),
                        frame.Locals.Select(x => x.Expression)
                        );

                    if (expectedValues != null || expectedHexValues != null)
                    {
                        foreach (var evaluationResult in frame.Parameters.Concat(frame.Locals))
                        {
                            int i     = 0;
                            var match = -1;
                            if (expectedParams != null)
                            {
                                foreach (var expectedParam in expectedParams)
                                {
                                    if (evaluationResult.Expression == expectedParam)
                                    {
                                        match = i;
                                        break;
                                    }
                                    ++i;
                                }
                            }
                            if (match == -1 && expectedLocals != null)
                            {
                                foreach (var expectedLocal in expectedLocals)
                                {
                                    if (evaluationResult.Expression == expectedLocal)
                                    {
                                        match = i;
                                        break;
                                    }
                                    ++i;
                                }
                            }
                            Assert.IsTrue(match > -1);
                            if (expectedValues != null)
                            {
                                Assert.AreEqual(expectedValues[match], evaluationResult.StringValue);
                            }
                            if (expectedHexValues != null)
                            {
                                Assert.AreEqual(expectedHexValues[match], evaluationResult.HexValue);
                            }
                        }
                    }
                }),
                new TestStep(action: TestAction.ResumeProcess, expectedExitCode: 0),
            }
                );
        }
Esempio n. 7
0
        public void PydInPackage()
        {
            PythonPaths.Python27.AssertInstalled();

            var outputPath = TestData.GetTempPath(randomSubPath: true);

            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", TestData.GetPath("CompletionDB"),
                                                                  "/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  = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(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 { }
        }
Esempio n. 8
0
        public async Task LiveShareCallback_References()
        {
            var cb = PythonLanguageServiceProviderCallback.CreateTestInstance();

            using (var analyzer = await CreateAnalyzerAsync())
            {
                var t     = analyzer.WaitForNextCompleteAnalysis();
                var entry = await analyzer.AnalyzeFileAsync(TestData.GetPath("TestData", "LiveShare", "module.py"));

                await t;

                cb.SetAnalyzer(entry.DocumentUri, analyzer);

                Location[] res = Array.Empty <Location>();
                // References sometimes need extra warmup. This needs to be fixed on the language
                // server side though, not here.
                for (int retries = 5; retries > 0; --retries)
                {
                    res = await cb.RequestAsync(
                        new LS.LspRequest <ReferenceParams, Location[]>(Methods.TextDocumentReferencesName),
                        new ReferenceParams
                    {
                        TextDocument = new TextDocumentIdentifier {
                            Uri = entry.DocumentUri
                        },
                        Position = new Position {
                            Line = 2, Character = 10
                        },
                        Context = new ReferenceContext {
                            IncludeDeclaration = true
                        }
                    },
                        null,
                        CancellationToken.None
                        );

                    if (res != null && res.Length > 0)
                    {
                        break;
                    }
                    Thread.Sleep(100);
                }

                AssertUtil.ContainsExactly(res.Select(r => r.Uri), entry.DocumentUri);
                AssertUtil.ContainsAtLeast(res.Select(r => r.Range.Start.Line), 2, 12);

                res = await cb.RequestAsync(
                    new LS.LspRequest <ReferenceParams, Location[]>(Methods.TextDocumentReferencesName),
                    new ReferenceParams
                {
                    TextDocument = new TextDocumentIdentifier {
                        Uri = entry.DocumentUri
                    },
                    Position = new Position {
                        Line = 3, Character = 10
                    },
                    Context = new ReferenceContext {
                        IncludeDeclaration = true
                    }
                },
                    null,
                    CancellationToken.None
                    );

                AssertUtil.ContainsExactly(res.Select(r => r.Uri), entry.DocumentUri);
                AssertUtil.ContainsAtLeast(res.Select(r => r.Range.Start.Line), 3, 13);
            }
        }
Esempio n. 9
0
 public void AssertHasAttrExact(IPythonProjectEntry module, string expr, int index, params string[] attrs)
 {
     AssertUtil.ContainsExactly(GetMemberNames(module, expr, index), attrs);
 }
Esempio n. 10
0
        public async Task LoadAndUnloadModule()
        {
            var services = PythonToolsTestUtilities.CreateMockServiceProvider().GetEditorServices();

            using (var are = new AutoResetEvent(false))
                using (var analyzer = await VsProjectAnalyzer.CreateForTestsAsync(services, InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(new Version(3, 6))))
                {
                    var m1Path = TestData.GetPath("TestData\\SimpleImport\\module1.py");
                    var m2Path = TestData.GetPath("TestData\\SimpleImport\\module2.py");

                    var toAnalyze = new HashSet <string>(StringComparer.OrdinalIgnoreCase)
                    {
                        m1Path, m2Path
                    };
                    analyzer.AnalysisComplete += (s, e) =>
                    {
                        lock (toAnalyze)
                        {
                            toAnalyze.Remove(e.Path);
                        }
                        are.Set();
                    };
                    var entry1 = await analyzer.AnalyzeFileAsync(m1Path);

                    var entry2 = await analyzer.AnalyzeFileAsync(m2Path);

                    WaitForEmptySet(are, toAnalyze, CancellationTokens.After60s);

                    var loc = new Microsoft.PythonTools.SourceLocation(1, 1);
                    AssertUtil.ContainsExactly(
                        analyzer.GetEntriesThatImportModuleAsync("module1", true).Result.Select(m => m.moduleName),
                        "module2"
                        );

                    AssertUtil.ContainsExactly(
                        analyzer.GetValueDescriptions(entry2, "x", loc),
                        "int"
                        );

                    toAnalyze.Add(m2Path);
                    await analyzer.UnloadFileAsync(entry1);

                    WaitForEmptySet(are, toAnalyze, CancellationTokens.After15s);

                    // Even though module1 has been unloaded, we still know that
                    // module2 imports it.
                    AssertUtil.ContainsExactly(
                        analyzer.GetEntriesThatImportModuleAsync("module1", true).Result.Select(m => m.moduleName),
                        "module2"
                        );

                    AssertUtil.ContainsExactly(
                        analyzer.GetValueDescriptions(entry2, "x", loc)
                        );

                    toAnalyze.Add(m1Path);
                    toAnalyze.Add(m2Path);
                    await analyzer.AnalyzeFileAsync(m1Path);

                    WaitForEmptySet(are, toAnalyze, CancellationTokens.After5s);

                    AssertUtil.ContainsExactly(
                        analyzer.GetEntriesThatImportModuleAsync("module1", true).Result.Select(m => m.moduleName),
                        "module2"
                        );

                    AssertUtil.ContainsExactly(
                        analyzer.GetValueDescriptions(entry2, "x", loc),
                        "int"
                        );
                }
        }
Esempio n. 11
0
        public void Basic()
        {
            var dict = new AnalysisDictionary <string, string>();

            Assert.IsFalse(dict.Remove("Foo"));
            dict.Add("1", "One");
            dict.Add("2", "Two");
            dict.Add("3", "Three");

            Assert.AreEqual("One", dict["1"]);
            Assert.AreEqual("Two", dict["2"]);
            Assert.AreEqual("Three", dict["3"]);
            Assert.IsTrue(dict.ContainsKey("1"));
            Assert.IsTrue(dict.ContainsKey("2"));
            Assert.IsTrue(dict.ContainsKey("3"));

            var keys = dict.ToArray().Select(x => x.Key).ToArray();

            AssertUtil.ContainsExactly(keys, "1", "2", "3");
            AssertUtil.ContainsExactly(dict.Keys, "1", "2", "3");
            AssertUtil.ContainsExactly(dict.KeysNoCopy, "1", "2", "3");
            AssertUtil.ContainsExactly(dict.Values, "One", "Two", "Three");
            AssertUtil.ContainsExactly(dict.EnumerateValues, "One", "Two", "Three");

            Assert.IsTrue(dict.Contains(new KeyValuePair <string, string>("1", "One")));
            Assert.IsFalse(dict.Contains(new KeyValuePair <string, string>("1", "AAA")));

            var items = new KeyValuePair <string, string> [3];

            dict.CopyTo(items, 0);
            AssertUtil.ContainsExactly(
                items,
                new KeyValuePair <string, string>("1", "One"),
                new KeyValuePair <string, string>("2", "Two"),
                new KeyValuePair <string, string>("3", "Three")
                );

            Assert.IsFalse(dict.IsReadOnly);
            Assert.AreEqual(EqualityComparer <string> .Default, dict.Comparer);

            Assert.AreEqual(dict.Count, 3);

            dict.Remove("1");

            Assert.AreEqual(dict.Count, 2);

            string value;

            Assert.AreEqual(false, dict.TryGetValue("1", out value));
            Assert.IsFalse(dict.ContainsKey("1"));
            Assert.AreEqual(null, value);

            dict["1"] = "One";

            Assert.AreEqual(dict.Count, 3);
            Assert.AreEqual("One", dict["1"]);
            Assert.AreEqual("Two", dict["2"]);
            Assert.AreEqual("Three", dict["3"]);
            Assert.IsTrue(dict.ContainsKey("1"));
            Assert.IsTrue(dict.ContainsKey("2"));
            Assert.IsTrue(dict.ContainsKey("3"));

            dict.Clear();
            Assert.IsFalse(dict.Remove("Foo"));
            Assert.AreEqual(0, dict.Count);
            Assert.IsFalse(dict.ContainsKey("1"));
            Assert.IsFalse(dict.ContainsKey("2"));
            Assert.IsFalse(dict.ContainsKey("3"));

            dict.Add(new KeyValuePair <string, string>("1", "One"));
            dict.Add(new KeyValuePair <string, string>("2", "Two"));
            dict.Add(new KeyValuePair <string, string>("3", "Three"));

            Assert.IsTrue(dict.Remove(new KeyValuePair <string, string>("1", "One")));
            Assert.IsFalse(dict.Remove(new KeyValuePair <string, string>("1", "One")));

            Assert.IsFalse(dict.Remove(new KeyValuePair <string, string>("2", "AAA")));
            Assert.IsTrue(dict.Remove(new KeyValuePair <string, string>("2", "Two")));
            Assert.IsTrue(dict.Remove(new KeyValuePair <string, string>("3", "Three")));

            Assert.AreEqual(0, dict.Count);

            AssertUtil.Throws <KeyNotFoundException>(
                () => { var x = dict["DNE"]; }
                );
        }
Esempio n. 12
0
 public void AssertIsInstance(IPythonProjectEntry module, string expr, int index, params string[] className)
 {
     AnalysisValue[] vars = GetValues(module, expr, index);
     AssertUtil.ContainsAtLeast(vars.Select(v => v.MemberType), PythonMemberType.Instance);
     AssertUtil.ContainsExactly(vars.Select(v => v.ShortDescription), className);
 }
Esempio n. 13
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);
                }
            }
        }
Esempio n. 14
0
 public void AstImports() {
     var mod = Parse("Imports.py", PythonLanguageVersion.V35);
     AssertUtil.ContainsExactly(mod.GetMemberNames(null),
         "version_info", "a_made_up_module"
     );
 }
Esempio n. 15
0
        public void AddUpdateRemoveConfigurableFactory()
        {
            using (var wpf = new WpfProxy())
                using (var list = new EnvironmentListProxy(wpf)) {
                    var container    = CreateCompositionContainer();
                    var service      = container.GetExportedValue <IInterpreterOptionsService>();
                    var interpreters = container.GetExportedValue <IInterpreterRegistryService>();
                    list.Service      = service;
                    list.Interpreters = interpreters;

                    var before = wpf.Invoke(() => new HashSet <string>(
                                                list.Environments.Select(ev => (string)ev.InterpreterPath),
                                                StringComparer.OrdinalIgnoreCase
                                                ));

                    var id   = Guid.NewGuid().ToString();
                    var fact = list.Service.AddConfigurableInterpreter(
                        id,
                        new InterpreterConfiguration(
                            "",
                            "Blah",
                            "",
                            TestData.GetPath("HelloWorld\\HelloWorld.pyproj")
                            )
                        );

                    try {
                        var afterAdd = wpf.Invoke(() => new HashSet <string>(
                                                      list.Environments.Select(ev => (string)ev.InterpreterPath),
                                                      StringComparer.OrdinalIgnoreCase
                                                      ));

                        Assert.AreNotEqual(before.Count, afterAdd.Count, "Did not add a new environment");
                        AssertUtil.ContainsExactly(
                            afterAdd.Except(before),
                            TestData.GetPath("HelloWorld\\HelloWorld.pyproj")
                            );

                        list.Service.AddConfigurableInterpreter(
                            id,
                            new InterpreterConfiguration(
                                "",
                                "test",
                                "",
                                TestData.GetPath("HelloWorld2\\HelloWorld.pyproj")
                                )
                            );

                        var afterUpdate = wpf.Invoke(() => new HashSet <string>(
                                                         list.Environments.Select(ev => (string)ev.InterpreterPath),
                                                         StringComparer.OrdinalIgnoreCase
                                                         ));

                        Assert.AreEqual(afterAdd.Count, afterUpdate.Count, "Should not add/remove an environment");
                        AssertUtil.ContainsExactly(
                            afterUpdate.Except(before),
                            TestData.GetPath("HelloWorld2\\HelloWorld.pyproj")
                            );
                    } finally {
                        list.Service.RemoveConfigurableInterpreter(fact);
                    }

                    var afterRemove = wpf.Invoke(() => new HashSet <string>(
                                                     list.Environments.Select(ev => (string)ev.InterpreterPath),
                                                     StringComparer.OrdinalIgnoreCase
                                                     ));
                    AssertUtil.ContainsExactly(afterRemove, before);
                }
        }
Esempio n. 16
0
 public void AssertAttrIsType(IPythonProjectEntry module, string variable, string memberName, int index, params PythonMemberType[] types)
 {
     AssertUtil.ContainsExactly(GetMember(module, variable, memberName, index).Select(m => m.MemberType), types);
 }
Esempio n. 17
0
        private static void AreXPathNavigatorsEqual(XPathNavigator nav1, XPathNavigator nav2, string message)
        {
            while (true)
            {
                if (nav1.Name != nav2.Name)
                {
                    Assert.Fail("Expected element <{0}>. Actual element <{1}>.{2}", nav1.Name, nav2.Name, message);
                }
                var anav1 = nav1.CreateNavigator();
                var anav2 = nav2.CreateNavigator();
                var attr1 = new List <string>();
                var attr2 = new List <string>();

                if (anav1.MoveToFirstAttribute())
                {
                    do
                    {
                        attr1.Add(string.Format("{0}=\"{1}\"", anav1.Name, anav1.Value));
                    } while (anav1.MoveToNextAttribute());
                }
                if (anav2.MoveToFirstAttribute())
                {
                    do
                    {
                        attr2.Add(string.Format("{0}=\"{1}\"", anav2.Name, anav2.Value));
                    } while (anav2.MoveToNextAttribute());
                }

                AssertUtil.ContainsExactly(attr2, attr1);

                var cnav1 = nav1.CreateNavigator();
                var cnav2 = nav2.CreateNavigator();
                if (cnav1.MoveToFirstChild())
                {
                    if (cnav2.MoveToFirstChild())
                    {
                        AreXPathNavigatorsEqual(cnav1, cnav2, message);
                    }
                    else
                    {
                        Assert.Fail("Expected element {0}.{1}", GetFullPath(cnav1), message);
                    }
                }
                else if (cnav2.MoveToFirstChild())
                {
                    Assert.Fail("Unexpected element {0}.{1}", GetFullPath(cnav2), message);
                }

                if (nav1.MoveToNext())
                {
                    if (nav2.MoveToNext())
                    {
                        continue;
                    }
                    else
                    {
                        Assert.Fail("Expected element {0}.{1}", GetFullPath(nav1), message);
                    }
                }
                else if (nav2.MoveToNext())
                {
                    Assert.Fail("Unexpected element {0}.{1}", GetFullPath(nav2), message);
                }
                break;
            }
        }
Esempio n. 18
0
            public async Task RunAsync()
            {
                PythonThread thread = await _tests.RunAndBreakAsync(FileName, LineNo, breakFilename : BreakFileName, arguments : Arguments, processLoaded : ProcessLoaded, debugOptions : DebugOptions);

                PythonProcess process = thread.Process;

                try
                {
                    var frames             = thread.Frames;
                    var localNamesExpected = Locals.Select(v => v.Expression).ToSet();
                    var paramNamesExpected = Params.Select(v => v.Expression).ToSet();

                    string fileNameExpected;
                    if (BreakFileName == null)
                    {
                        fileNameExpected = Path.GetFullPath(_tests.DebuggerTestPath + FileName);
                    }
                    else if (Path.IsPathRooted(BreakFileName))
                    {
                        fileNameExpected = BreakFileName;
                    }
                    else
                    {
                        fileNameExpected = Path.GetFullPath(_tests.DebuggerTestPath + BreakFileName);
                    }

                    Assert.AreEqual(frames[0].FileName, fileNameExpected, true);

                    if (!IgnoreExtra)
                    {
                        AssertUtil.ContainsExactly(frames[0].Locals.Select(x => x.Expression), localNamesExpected);
                        AssertUtil.ContainsExactly(frames[0].Parameters.Select(x => x.Expression), paramNamesExpected);
                    }

                    foreach (var expectedLocal in Locals)
                    {
                        var actualLocal = frames[0].Locals.First(v => v.Expression == expectedLocal.Expression);
                        expectedLocal.Validate(actualLocal);
                    }

                    foreach (var expectedParam in Params)
                    {
                        var actualParam = frames[0].Parameters.First(v => v.Expression == expectedParam.Expression);
                        expectedParam.Validate(actualParam);
                    }

                    await process.ResumeAsync(TimeoutToken());

                    if (WaitForExit)
                    {
                        _tests.WaitForExit(process);
                    }
                }
                finally
                {
                    if (!process.HasExited)
                    {
                        process.Terminate();
                    }
                }
            }