Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit f2a7d7e

Browse files
kuba--dennwc
authored andcommitted
Fixes for the latest version bblfshd and python driver + extra tests (encode and get_all/load)
Signed-off-by: kuba-- <[email protected]>
1 parent d59cc7b commit f2a7d7e

File tree

6 files changed

+100
-27
lines changed

6 files changed

+100
-27
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.idea
2+
.vscode
23

34
# Byte-compiled / optimized / DLL files
45
__pycache__/

.travis.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
language: python
22
sudo: true
33
dist: xenial
4-
env:
5-
- BBLFSHD_VERSION=v2.12.1 BBLFSH_PYTHON_VERSION=v2.9.0
64
services:
75
- docker
86
cache:
@@ -12,8 +10,9 @@ python:
1210
- "3.6"
1311
- "3.7"
1412
install:
15-
- docker run --privileged -d -p 9432:9432 --name bblfshd bblfsh/bblfshd:$BBLFSHD_VERSION
16-
- docker exec bblfshd bblfshctl driver install bblfsh/python-driver:$BBLFSH_PYTHON_VERSION
13+
- docker run --privileged -d -p 9432:9432 --name bblfshd bblfsh/bblfshd
14+
- docker exec bblfshd bblfshctl driver install bblfsh/python-driver
15+
- docker exec bblfshd bblfshctl driver install bblfsh/cpp-driver
1716
- wget https://github.com/bblfsh/python-client/releases/download/v2.2.1/protobuf-python_3.4.1-1_amd64.deb
1817
- sudo dpkg -i protobuf-python_3.4.1-1_amd64.deb
1918
- if [[ ! -z "$TRAVIS_TAG" ]]; then sed -i -e "s/^VERSION\s*=\s*\"[^\"]\+\"/VERSION = \"${TRAVIS_TAG#v}\"/g" setup.py; fi

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,17 @@ If you don't have a bblfsh server running you can execute it using the following
4141

4242
```sh
4343
docker run --privileged --rm -it -p 9432:9432 -v bblfsh_cache:/var/lib/bblfshd --name bblfshd bblfsh/bblfshd
44-
docker exec -it bblfshd bblfshctl driver install python bblfsh/python-driver:latest
4544
```
4645

46+
To parse Python files you will also need a python driver installed on bblfsh server:
47+
48+
```sh
49+
docker exec -it bblfshd bblfshctl driver install python bblfsh/python-driver
50+
```
51+
52+
List of all available drivers you can find at the [official documentation](https://doc.bblf.sh/languages.html).
53+
54+
4755
Please, read the [getting started](https://doc.bblf.sh/using-babelfish/getting-started.html) guide to learn more about how to use and deploy a bblfshd.
4856

4957
### Parsing a file
@@ -79,7 +87,7 @@ for node in it:
7987
doSomething(node.get())
8088
```
8189

82-
XPath queries can return different types (`dict`, `int`, `float`, `bool` or `str`),
90+
XPath queries can return different types (`dict`, `int`, `float`, `bool` or `str`),
8391
calling `get()` with an item will return the right type, but if you must ensure
8492
that you are getting the expected type (to avoid errors in the queries) there
8593
are alternative typed versions:
@@ -92,7 +100,7 @@ z = next(ctx.filter("count(//*)")).get_int() # or get_float()
92100

93101
### Iteration
94102

95-
You can also iterate using iteration orders different than the
103+
You can also iterate using iteration orders different than the
96104
default pre-order using the `iterate` method on `parse` result or node objects:
97105

98106
```python
@@ -104,14 +112,14 @@ for node in it:
104112
# Over filter results (which by default are already iterators with PRE_ORDER):
105113
ctx = client.parse("file.py")
106114
it = ctx.filter("//python:Call").iterate(bblfsh.TreeOrder.LEVEL_ORDER)
107-
for node in it:
115+
for node in it:
108116
print(node)
109117

110118
# Over individual node objects to change the iteration order of
111119
# a specific subtree:
112120
ast = ctx.root
113121
it = ast.iterate(bblfsh.TreeOrder.POSITION_ORDER)
114-
for node in it:
122+
for node in it:
115123
print(node)
116124
```
117125

bblfsh/fixtures/test.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
unsigned long long fib(int n);
2+
3+
int main() {
4+
fib(12);
5+
return 0;
6+
}
7+
8+
unsigned long long fib(int n) {
9+
return (n <= 1) ? 1ULL : fib(n-2) + fib(n-1);
10+
}

bblfsh/test.py

Lines changed: 70 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
from bblfsh.client import NonUTF8ContentException
1111
from bblfsh.node import NodeTypedGetException
1212
from bblfsh.result_context import (Node, NodeIterator, ResultContext)
13-
from bblfsh.pyuast import uast
14-
13+
from bblfsh.pyuast import uast, decode
1514

1615
class BblfshTests(unittest.TestCase):
1716
BBLFSH_SERVER_EXISTED = None
18-
fixtures_file = "fixtures/test.py"
17+
fixtures_pyfile = "fixtures/test.py"
18+
fixtures_cfile = "fixtures/test.c"
1919

2020
@classmethod
2121
def setUpClass(cls: t.Any) -> None:
@@ -32,7 +32,7 @@ def setUp(self) -> None:
3232
self.client = BblfshClient("localhost:9432")
3333

3434
def _parse_fixture(self) -> ResultContext:
35-
ctx = self.client.parse(self.fixtures_file)
35+
ctx = self.client.parse(self.fixtures_pyfile)
3636
self._validate_ctx(ctx)
3737
return ctx
3838

@@ -44,7 +44,7 @@ def testVersion(self) -> None:
4444
self.assertTrue(version.build)
4545

4646
def testNativeParse(self) -> None:
47-
ctx = self.client.parse(self.fixtures_file, mode=Modes.NATIVE)
47+
ctx = self.client.parse(self.fixtures_pyfile, mode=Modes.NATIVE)
4848
self._validate_ctx(ctx)
4949
self.assertIsNotNone(ctx)
5050

@@ -67,12 +67,24 @@ def testUASTDefaultLanguage(self) -> None:
6767
self.assertEqual(ctx.language, "python")
6868

6969
def testUASTWithLanguage(self) -> None:
70-
ctx = self.client.parse(self.fixtures_file, language="Python")
70+
ctx = self.client.parse(self.fixtures_pyfile, language="Python")
7171
self._validate_ctx(ctx)
7272
self.assertEqual(ctx.language, "python")
7373

74+
def testUASTWithLanguageAlias(self) -> None:
75+
ctx = self.client.parse(self.fixtures_cfile)
76+
self._validate_ctx(ctx)
77+
self.assertEqual(ctx.language, "c")
78+
79+
it = ctx.filter("//uast:FunctionGroup/Nodes/uast:Alias/Name/uast:Identifier/Name")
80+
self.assertIsInstance(it, NodeIterator)
81+
82+
self.assertEqual(next(it).get(), "main")
83+
self.assertEqual(next(it).get(), "fib")
84+
85+
7486
def testUASTFileContents(self) -> None:
75-
with open(self.fixtures_file, "r") as fin:
87+
with open(self.fixtures_pyfile, "r") as fin:
7688
contents = fin.read()
7789

7890
ctx = self.client.parse("file.py", contents=contents)
@@ -83,14 +95,15 @@ def assert_strnode(n: Node, expected: str) -> None:
8395
self.assertIsInstance(n.get_str(), str)
8496
self.assertEqual(n.get_str(), expected)
8597

86-
it = ctx.filter("//uast:RuntimeImport/Path/uast:Alias/Name/uast:Identifier/Name")
98+
it = ctx.filter("//uast:RuntimeImport/Path/uast:Identifier/Name")
8799
self.assertIsInstance(it, NodeIterator)
88100

89101
assert_strnode(next(it), "os")
90102
assert_strnode(next(it), "resource")
91103
assert_strnode(next(it), "unittest")
92104
assert_strnode(next(it), "docker")
93105
assert_strnode(next(it), "bblfsh")
106+
assert_strnode(next(it), "bblfsh")
94107
self.assertRaises(StopIteration, next, it)
95108

96109
def testBrokenFilter(self) -> None:
@@ -285,15 +298,17 @@ def testFilterInsideIter(self) -> None:
285298

286299
def testItersMixingIterations(self) -> None:
287300
ctx = self._parse_fixture()
301+
288302
it = ctx.iterate(TreeOrder.PRE_ORDER)
289-
next(it); next(it); next(it)
303+
next(it); next(it); next(it); next(it)
304+
290305

291-
n = next(it)
292306
it2 = it.iterate(TreeOrder.PRE_ORDER)
293307
next(it2)
308+
294309
a = next(it).get()
295310
b = next(it2).get()
296-
self.assertListEqual(a, b)
311+
self.assertEqual(a, b)
297312

298313
def testManyFilters(self) -> None:
299314
ctx = self._parse_fixture()
@@ -310,7 +325,7 @@ def testManyFilters(self) -> None:
310325
def testManyParses(self) -> None:
311326
before = resource.getrusage(resource.RUSAGE_SELF)
312327
for _ in range(100):
313-
self.client.parse(self.fixtures_file)
328+
self.client.parse(self.fixtures_pyfile)
314329

315330
after = resource.getrusage(resource.RUSAGE_SELF)
316331

@@ -320,7 +335,7 @@ def testManyParses(self) -> None:
320335
def testManyParsesAndFilters(self) -> None:
321336
before = resource.getrusage(resource.RUSAGE_SELF)
322337
for _ in range(100):
323-
ctx = self.client.parse(self.fixtures_file)
338+
ctx = self.client.parse(self.fixtures_pyfile)
324339
ctx.filter("//*[@role='Identifier']")
325340

326341
after = resource.getrusage(resource.RUSAGE_SELF)
@@ -336,6 +351,48 @@ def testSupportedLanguages(self) -> None:
336351
self.assertTrue(hasattr(l, key))
337352
self.assertIsNotNone(getattr(l, key))
338353

354+
def testEncode(self) -> None:
355+
ctx = self._parse_fixture()
356+
self.assertEqual(ctx.ctx.encode(None, 0), ctx._response.uast)
357+
358+
def testEncodeWithEmptyContext(self) -> None:
359+
ctx = ResultContext()
360+
obj = {"k1": "v1", "k2": "v2"}
361+
fmt = 1 # YAML
362+
363+
data = ctx.ctx.encode(obj, fmt)
364+
self.assertDictEqual(obj, decode(data, format=fmt).load())
365+
366+
def testGetAll(self) -> None:
367+
ctx = self._parse_fixture()
368+
369+
expected = ["os", "resource", "unittest", "docker", "bblfsh"]
370+
actual = []
371+
for k in ctx.get_all()["body"]:
372+
if "@type" in k and k["@type"] == "uast:RuntimeImport" and "Path" in k:
373+
path = k["Path"]
374+
if "Name" in path:
375+
actual.append(k["Path"]["Name"])
376+
377+
self.assertListEqual(expected, actual)
378+
379+
def testLoad(self) -> None:
380+
ctx = self._parse_fixture()
381+
382+
it = ctx.iterate(TreeOrder.PRE_ORDER)
383+
next(it); next(it); next(it); next(it)
384+
385+
it2 = it.iterate(TreeOrder.PRE_ORDER)
386+
n = next(it2)
387+
node_ext = n.node_ext
388+
389+
obj = node_ext.load()
390+
typ = obj["@type"]
391+
self.assertEqual("uast:RuntimeImport", typ)
392+
393+
path = obj["Path"]
394+
self.assertEqual("uast:Identifier", path["@type"])
395+
self.assertEqual("os", path["Name"])
339396

340397
if __name__ == "__main__":
341398
unittest.main()

bblfsh/test_compat.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
import os
21
import resource
32
import unittest
43

5-
64
import docker
75

6+
from bblfsh.client import NonUTF8ContentException
7+
from bblfsh.compat import CompatBblfshClient as BblfshClient
88
from bblfsh.compat import (
99
filter as xpath_filter, role_id, iterator, role_name, Node, TreeOrder, filter_bool,
1010
filter_number, CompatNodeIterator
1111
)
12-
from bblfsh.compat import CompatBblfshClient as BblfshClient
1312
from bblfsh.launcher import ensure_bblfsh_is_running
14-
from bblfsh.client import NonUTF8ContentException
1513

1614

1715
class BblfshTests(unittest.TestCase):
@@ -252,7 +250,7 @@ def testManyFilters(self):
252250
root.properties['k2'] = 'v1'
253251

254252
before = resource.getrusage(resource.RUSAGE_SELF)
255-
for i in range(1000):
253+
for _ in range(1000):
256254
xpath_filter(root, "//*[@roleIdentifier]")
257255

258256
after = resource.getrusage(resource.RUSAGE_SELF)

0 commit comments

Comments
 (0)