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):
self._filename = filename
self._transform = transform
def is_package(self, mod_name):
return len(self._filename) > len(self._path)
def compile(self, name):
filename = self._filename
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):
self._calls.append(data)
def test_visit_sample(self):
import sys
print(sys.argv)
self._finder = Finder('sample', self.transform)
sys.meta_path.append(self._finder)
......
......@@ -7,11 +7,9 @@ import unittest
import sys
import context
from my2to3.hook import Finder
from my2to3.visit import DepthVisitor
from my2to3.visit import InstrumentVisitor
import my2to3.runtime
from ..my2to3.hook import Finder
from ..my2to3.visit import DepthVisitor
from ..my2to3.visit import InstrumentVisitor
class BinOpVisitor(InstrumentVisitor):
......@@ -160,20 +158,23 @@ class AddLogFunc(ast.NodeTransformer):
node = add_log_string_node(node, self._output_log)
return node
import os
LOG_PATH = os.path.dirname(__file__) + "/../logs/logs_dyn_injection"
class TestVisitBinOp(unittest.TestCase):
def setUp(self):
self._calls = []
self._finder = None
if not os.path.exists("../logs/logs_dyn_injection"):
os.makedirs("../logs/logs_dyn_injection")
if not os.path.exists(LOG_PATH):
os.makedirs(LOG_PATH)
else:
for test in os.listdir("../logs/logs_dyn_injection"):
for test in os.listdir(LOG_PATH):
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)
i = 0
for node in ast.walk(tree): # todo find a function with a specified order
......@@ -181,7 +182,7 @@ class TestVisitBinOp(unittest.TestCase):
for child in ast.iter_child_nodes(node):
child.parent = (i, node)
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
def info(self, data):
......@@ -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):
del my2to3.runtime.logger
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