Пример #1
0
 public void WaitForAnalysis(CancellationToken?cancel = null)
 {
     if (_analyzer.Queue.Count == 0)
     {
         return;
     }
     _analyzer.AnalyzeQueuedEntries(cancel ?? CancellationTokens.After5s);
 }
Пример #2
0
 public void WaitForAnalysis(CancellationToken?cancel = null)
 {
     if (_analyzer.Queue.Count == 0)
     {
         return;
     }
     _analyzer.AnalyzeQueuedEntries(cancel ?? CancellationTokens.After5s);
     AssertListener.ThrowUnhandled();
 }
Пример #3
0
        public IEnumerable <TestCase> GetTestCases()
        {
            foreach (var entry in _entries)
            {
                entry.Analyze(CancellationToken.None, true);
            }
            _analyzer.AnalyzeQueuedEntries(CancellationToken.None);


            foreach (var entry in _entries)
            {
                foreach (var classValue in GetTestCaseClasses(entry))
                {
                    // Check the name of all functions on the class using the
                    // analyzer. This will return functions defined on this
                    // class and base classes
                    foreach (var member in GetTestCaseMembers(entry, classValue))
                    {
                        // Find the definition to get the real location of the
                        // member. Otherwise decorators will confuse us.
                        var definition = entry.Analysis
                                         .GetVariablesByIndex(classValue.Name + "." + member.Key, 0)
                                         .FirstOrDefault(v => v.Type == VariableType.Definition);

                        var location = (definition != null) ?
                                       definition.Location :
                                       member.Value.SelectMany(m => m.Locations).FirstOrDefault(loc => loc != null);

                        yield return(CreateTestCase(
                                         classValue.DeclaringModule.FilePath,
                                         classValue.Name,
                                         member.Key,
                                         location
                                         ));
                    }
                }
            }
        }
Пример #4
0
        private static void RunCommand(
            string cmd,
            string args,
            PythonAnalyzer analyzer,
            Dictionary <string, IPythonProjectEntry> modules,
            State state
            )
        {
            switch (cmd.ToLower())
            {
            case "print":
                Console.WriteLine(args);
                break;

            case "module":
                foreach (var mod in GetModules(args))
                {
                    IPythonProjectEntry entry;
                    if (!modules.TryGetValue(mod.ModuleName, out entry))
                    {
                        Console.WriteLine("Creating module {0}", mod.ModuleName);
                        modules[mod.ModuleName] = entry = analyzer.AddModule(mod.ModuleName, mod.SourceFile);
                    }
                    else
                    {
                        Console.WriteLine("Reusing module {0}", mod.ModuleName);
                    }

                    using (var file = File.OpenText(mod.SourceFile)) {
                        var parser = Parser.CreateParser(
                            file,
                            analyzer.LanguageVersion,
                            new ParserOptions()
                        {
                            BindReferences = true
                        }
                            );
                        entry.UpdateTree(parser.ParseFile(), null);
                    }
                }
                break;

            case "enqueue":
                if (args == "*")
                {
                    foreach (var entry in modules.Values)
                    {
                        entry.Analyze(CancellationToken.None, true);
                    }
                }
                else
                {
                    IPythonProjectEntry entry;
                    foreach (var modName in args.Split(',').Select(s => s.Trim()).Where(s => !string.IsNullOrEmpty(s)))
                    {
                        if (!modules.TryGetValue(modName, out entry))
                        {
                            Console.WriteLine("Could not enqueue {0}", modName);
                        }
                        else
                        {
                            entry.Analyze(CancellationToken.None, true);
                        }
                    }
                }
                break;

            case "analyze":
                Console.Write("Waiting for complete analysis... ");
                analyzer.AnalyzeQueuedEntries(CancellationToken.None);
                Console.WriteLine("done!");
                break;

            case "pause":
                Console.Write("Press enter to continue...");
                Console.ReadKey(true);
                Console.WriteLine();
                break;

            case "debugbreak":
                if (!args.Equals("ifattached", StringComparison.CurrentCultureIgnoreCase) || Debugger.IsAttached)
                {
                    Console.WriteLine("Breaking into the debugger");
                    Debugger.Break();
                }
                break;

            case "gc":
                Console.WriteLine("Collecting garbage");
                GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
                GC.WaitForFullGCComplete();
                GC.WaitForPendingFinalizers();
                break;

            case "dump": {
                var fullPath = Path.GetFullPath(args);
                var length   = WriteDump(Process.GetCurrentProcess(), fullPath, MiniDumpType.FullDump);
                Console.WriteLine(
                    "Dump written to {0} at {1:F1}MB ({2} bytes)",
                    fullPath,
                    length / (1024.0 * 1024.0),
                    length
                    );
                if (state.LastDumpSize > 0 && state.LastDumpSize != length)
                {
                    var delta     = Math.Abs(length - state.LastDumpSize);
                    var direction = (length > state.LastDumpSize) ? "larger" : "smaller";
                    Console.WriteLine(
                        "Dump is {0:F1}MB ({1} bytes) {2} than previous",
                        delta / (1024.0 * 1024.0),
                        delta,
                        direction
                        );
                }
                state.LastDumpSize = length;
                break;
            }

            default:
                Console.WriteLine("Command not available: {0}", cmd);
                break;
            }
        }
        private IEnumerable <Task> StartCrossThreadAnalysisCalls(PythonAnalyzer state, CancellationToken cancel)
        {
            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([])
";

            state.SetSearchPaths(new [] { TestData.GetTestSpecificRootUri().ToAbsolutePath() });
            var entries = Enumerable.Range(0, 100)
                          .Select(i => {
                var entry  = (ProjectEntry)state.AddModule(string.Format("mod{0:000}", i), TestData.GetTestSpecificPath(string.Format("mod{0:000}.py", i)));
                var parser = Parser.CreateParser(new StringReader(testCode.FormatInvariant(i + 1, PythonTypes[i % PythonTypes.Count])), PythonLanguageVersion.V34);
                using (var p = entry.BeginParse()) {
                    p.Tree = parser.ParseFile();
                    p.Complete();
                }
                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;
                        var parser = Parser.CreateParser(new StringReader(testCode.FormatInvariant(i + 1, PythonTypes[i % PythonTypes.Count])), PythonLanguageVersion.V34);
                        using (var p = t.Item2.BeginParse()) {
                            p.Tree = parser.ParseFile();
                            p.Complete();
                        }
                    }
                    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())
                    {
                        foreach (var s in sigs)
                        {
                            s.Name.Should().Be("test_func");
                            s.Parameters.Select(p => p.Name).Should().BeEquivalentTo("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));
        }