Exemplo n.º 1
0
 def generate_kythe(self, code, **kwargs):
     """Generate a kythe index from a code string."""
     with file_utils.Tempdir() as d:
         d.create_file("t.py", code)
         options = config.Options([d["t.py"]])
         options.tweak(pythonpath=[d.path], version=self.python_version)
         kythe_args = kythe.Args(corpus="corpus", root="root")
         ix = indexer.process_file(options, kythe_args=kythe_args)
         # Collect all the references from the kythe graph.
         kythe_index = [json.loads(x) for x in output.json_kythe_graph(ix)]
         return kythe_index
Exemplo n.º 2
0
 def test_kythe_args(self):
     code = textwrap.dedent("""
     def f(x):
       return 42
 """)
     with file_utils.Tempdir() as d:
         d.create_file("t.py", code)
         options = config.Options([d["t.py"]])
         options.tweak(pythonpath=[d.path], version=self.python_version)
         kythe_args = kythe.Args(corpus="corpus", root="root")
         ix = indexer.process_file(options, kythe_args=kythe_args)
         # Collect all the references from the kythe graph.
         kythe_index = [json.loads(x) for x in output.json_kythe_graph(ix)]
         k = kythe_index[0]["source"]
         self.assertEqual(k["corpus"], "corpus")
         self.assertEqual(k["root"], "root")
Exemplo n.º 3
0
    def test_resolved_imports(self):
        # We need all imports to be valid for pytype
        code = """\
        import f
        import x.y
        import a.b as c
        from a import b
        from p import q as r

        fx = f.X()
        cx = c.X()
        bx = b.X()
        rx = r.X()
        yx = x.y.X()
    """
        stub = "class X: pass"
        with file_utils.Tempdir() as d:
            d.create_file("t.py", code)
            d.create_file("f.pyi", stub)
            d.create_file("x/y.pyi", stub)
            d.create_file("a/b.pyi", stub)
            d.create_file("p/q.pyi", stub)
            options = config.Options([d["t.py"]])
            options.tweak(pythonpath=[d.path], version=self.python_version)
            ix = indexer.process_file(options)
            self.assertDef(ix, "module.f", "f", "Import")
            self.assertDef(ix, "module.x.y", "x.y", "Import")
            self.assertDef(ix, "module.c", "c", "Import")
            self.assertDef(ix, "module.b", "b", "ImportFrom")
            self.assertDef(ix, "module.r", "r", "ImportFrom")
            self.assertEqual(ix.modules["module.f"], "f")
            self.assertEqual(ix.modules["module.x.y"], "x.y")
            self.assertEqual(ix.modules["module.b"], "a.b")
            self.assertEqual(ix.modules["module.c"], "a.b")
            self.assertEqual(ix.modules["module.r"], "p.q")

            # Collect all the references from the kythe graph.
            kythe_index = [json.loads(x) for x in output.json_kythe_graph(ix)]
            refs = [
                x for x in kythe_index
                if x.get("edge_kind") == "/kythe/edge/ref"
            ]

            # Extract the span of text and the target symbol for each reference.
            src = ix.source.text
            out = []
            for r in refs:
                pos = r["source"]["signature"]
                start, end = pos[1:].split(":")
                start, end = int(start), int(end)
                text = src[start:end]
                out.append(
                    (text, r["target"]["signature"], r["target"]["path"]))

            expected = {
                # Imports as declarations in the source file
                ("f", "module.f", "t.py"),
                ("c", "module.c", "t.py"),
                ("b", "module.b", "t.py"),
                # Class X in remote files
                ("X", "module.X", "f.py"),
                ("X", "module.X", "a/b.py"),
                ("X", "module.X", "x/y.py"),
                ("X", "module.X", "p/q.py"),
                # Imports as references to remote files
                ("r", "module.r", "t.py"),
                ("b", ":module:", "a/b.py"),
                ("c", ":module:", "a/b.py"),
                ("f", ":module:", "f.py"),
                ("r", ":module:", "p/q.py"),
                ("x.y", ":module:", "x/y.py"),
                # x.y as references to remote files
                ("x", ":module:", "x/__init__.py"),
                ("y", ":module:", "x/y.py"),
            }

            # Resolve filepaths within the tempdir.
            expected = [(ref, target, d[path])
                        for (ref, target, path) in expected]
            self.assertEqual(set(out), set(expected))