Commit f4794a4d authored by Emmeline Vouriot's avatar Emmeline Vouriot

make testing module more easily changeable

parent b1f82106
...@@ -34,6 +34,9 @@ class Loader(object): ...@@ -34,6 +34,9 @@ class Loader(object):
self._filename = filename self._filename = filename
self._transform = transform self._transform = transform
def is_package(self, mod_name):
return len(self._filename) > len(self._path)
def compile(self, name): def compile(self, name):
filename = self._filename filename = self._filename
source = self._file.read() source = self._file.read()
......
import sample
\ No newline at end of file
1 // 2
print 'hello'
\ No newline at end of file
...@@ -36,6 +36,8 @@ class TestVisitBinOp(unittest.TestCase): ...@@ -36,6 +36,8 @@ class TestVisitBinOp(unittest.TestCase):
self._calls.append(data) self._calls.append(data)
def test_visit_sample(self): def test_visit_sample(self):
import sys
print(sys.argv)
self._finder = Finder('sample', self.transform) self._finder = Finder('sample', self.transform)
sys.meta_path.append(self._finder) sys.meta_path.append(self._finder)
......
...@@ -7,11 +7,9 @@ import unittest ...@@ -7,11 +7,9 @@ import unittest
import sys import sys
import context import context
from my2to3.hook import Finder from ..my2to3.hook import Finder
from my2to3.visit import DepthVisitor from ..my2to3.visit import DepthVisitor
from my2to3.visit import InstrumentVisitor from ..my2to3.visit import InstrumentVisitor
import my2to3.runtime
class BinOpVisitor(InstrumentVisitor): class BinOpVisitor(InstrumentVisitor):
...@@ -160,20 +158,23 @@ class AddLogFunc(ast.NodeTransformer): ...@@ -160,20 +158,23 @@ class AddLogFunc(ast.NodeTransformer):
node = add_log_string_node(node, self._output_log) node = add_log_string_node(node, self._output_log)
return node return node
import os
LOG_PATH = os.path.dirname(__file__) + "/../logs/logs_dyn_injection"
class TestVisitBinOp(unittest.TestCase): class TestVisitBinOp(unittest.TestCase):
def setUp(self): def setUp(self):
self._calls = [] self._calls = []
self._finder = None self._finder = None
if not os.path.exists("../logs/logs_dyn_injection"): if not os.path.exists(LOG_PATH):
os.makedirs("../logs/logs_dyn_injection") os.makedirs(LOG_PATH)
else: else:
for test in os.listdir("../logs/logs_dyn_injection"): for test in os.listdir(LOG_PATH):
if test.endswith(".txt"): if test.endswith(".txt"):
os.remove("../logs/logs_dyn_injection/" + test) os.remove(LOG_PATH + "/" + test)
def transform(self, source, name, filename): @staticmethod
def transform(source, name, filename):
tree = ast.parse(source, filename) tree = ast.parse(source, filename)
i = 0 i = 0
for node in ast.walk(tree): # todo find a function with a specified order for node in ast.walk(tree): # todo find a function with a specified order
...@@ -181,7 +182,7 @@ class TestVisitBinOp(unittest.TestCase): ...@@ -181,7 +182,7 @@ class TestVisitBinOp(unittest.TestCase):
for child in ast.iter_child_nodes(node): for child in ast.iter_child_nodes(node):
child.parent = (i, node) child.parent = (i, node)
i += 1 i += 1
ast.fix_missing_locations(AddLogFunc(source, ("../logs/logs_dyn_injection/" + name + ".txt", filename)).visit(tree)) ast.fix_missing_locations(AddLogFunc(source, (LOG_PATH + "/" + name + ".txt", filename)).visit(tree))
return tree return tree
def info(self, data): def info(self, data):
...@@ -204,6 +205,21 @@ class TestVisitBinOp(unittest.TestCase): ...@@ -204,6 +205,21 @@ class TestVisitBinOp(unittest.TestCase):
] ]
) )
def test_visit_re6st(self):
self._finder = Finder('re6st', self.transform)
sys.meta_path.append(self._finder)
my2to3.runtime.logger = self
self.assertEqual(len(self._calls), 2)
self.assertEqual(
self._calls,
[
'{"loc": {"depth": 3, "col": 4, "line": 5, "name": "sample.sample"}, "type": "<type \'int\'>", "value": "2", "desc": null}',
'{"loc": {"depth": 3, "col": 8, "line": 5, "name": "sample.sample"}, "type": "<type \'int\'>", "value": "3", "desc": null}'
]
)
def tearDown(self): def tearDown(self):
del my2to3.runtime.logger del my2to3.runtime.logger
sys.meta_path.remove(self._finder) sys.meta_path.remove(self._finder)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment