eris/tests/tools_test.py

195 lines
6.1 KiB
Python
Raw Normal View History

2021-05-18 20:39:25 +10:00
#!/usr/bin/env python3.9
2016-02-05 01:37:51 +00:00
import contextlib
import os
import shutil
2016-02-05 01:37:51 +00:00
import unittest
import unittest.mock
2016-10-26 14:09:05 +02:00
os.environ["TERM"] = "xterm-256color"
2016-10-26 13:43:02 +02:00
import golden
2018-09-17 23:59:38 +10:00
import eris.tools as tools
import fill3
2016-02-05 01:37:51 +00:00
2016-09-27 13:30:08 +02:00
os.environ["TZ"] = "GMT"
2018-09-17 23:59:38 +10:00
ERIS_ROOT = os.path.dirname(__file__)
2016-02-05 01:37:51 +00:00
class ExecutablesTestCase(unittest.TestCase):
def test_executables_exist_in_path(self):
# Tools not in ubuntu:
exceptions = {tools.wasm_validate, tools.wasm_objdump}
for tool in tools.tools_all() - exceptions:
if hasattr(tool, "executables"):
for executable in tool.executables:
with self.subTest(executable=executable, tool=tool):
self.assertTrue(shutil.which(executable))
2016-02-05 01:37:51 +00:00
def widget_to_string(widget):
appearance = widget.appearance_min()
return str(fill3.join("\n", appearance))
@contextlib.contextmanager
def chdir(path):
old_cwd = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(old_cwd)
def result_path(tool, input_filename):
filename = tool.__name__ + "-" + input_filename.replace(".", "_")
2018-09-17 23:59:38 +10:00
return os.path.join(ERIS_ROOT, "golden-files", "results", filename)
2016-02-05 01:37:51 +00:00
def run_tool(tool, input_filename):
2018-09-17 23:59:38 +10:00
with chdir(os.path.join(ERIS_ROOT, "golden-files")):
2016-02-05 01:37:51 +00:00
return tool(os.path.join(".", "input", input_filename))
class ToolsTestCase(unittest.TestCase):
2016-02-10 01:25:45 +00:00
def _test_tool(self, tool, sub_tests):
for input_filename, expected_status in sub_tests:
2016-02-08 21:55:22 +00:00
with self.subTest(input_filename=input_filename):
status, result = run_tool(tool, input_filename)
golden_path = result_path(tool, input_filename)
golden.assertGolden(str(result), golden_path)
2016-02-08 21:55:22 +00:00
self.assertEqual(status, expected_status)
2016-02-05 01:37:51 +00:00
def test_metadata(self):
mock_stat_result = unittest.mock.Mock(
st_mode=0o755, st_mtime=1454282045, st_ctime=1454282045,
st_atime=1454282047, st_size=12, st_uid=1111, st_gid=1111,
st_nlink=2)
mock_pw_entry = unittest.mock.Mock(pw_name="foo")
with unittest.mock.patch.object(os, "stat",
return_value=mock_stat_result):
with unittest.mock.patch.object(tools.pwd, "getpwuid",
return_value=mock_pw_entry):
2016-02-10 01:25:45 +00:00
self._test_tool(tools.metadata,
[("hi3.py", tools.Status.ok)])
2016-02-05 01:37:51 +00:00
def test_contents(self):
self._test_tool(tools.contents, [("hi3.py", tools.Status.ok)])
2016-02-05 01:37:51 +00:00
HI_OK = [("hi3.py", tools.Status.ok)]
2016-02-10 01:33:51 +00:00
2016-02-05 01:37:51 +00:00
def test_python_syntax(self):
2016-02-10 01:33:51 +00:00
self._test_tool(tools.python_syntax, self.HI_OK)
2016-02-05 01:37:51 +00:00
HI_OK = [("hi3.py", tools.Status.ok)]
2016-02-10 01:33:51 +00:00
def test_pydoc(self):
# FIX: This is failing inside AppImages.
if "APPDIR" not in os.environ:
self._test_tool(tools.pydoc, self.HI_OK)
2016-02-05 01:37:51 +00:00
2016-10-15 18:45:32 +02:00
def test_mypy(self):
self._test_tool(tools.mypy, self.HI_OK)
2016-10-15 18:45:32 +02:00
def test_pycodestyle(self):
self._test_tool(tools.pycodestyle, self.HI_OK)
2016-02-05 01:37:51 +00:00
def test_pyflakes(self):
2016-02-10 01:33:51 +00:00
self._test_tool(tools.pyflakes, self.HI_OK)
2016-02-05 01:37:51 +00:00
def test_pylint(self):
2016-02-10 01:33:51 +00:00
self._test_tool(tools.pylint, self.HI_OK)
2016-02-05 01:37:51 +00:00
def test_python_gut(self):
self._test_tool(tools.python_gut, self.HI_OK)
2016-02-05 01:37:51 +00:00
def test_python_modulefinder(self):
self._test_tool(tools.python_modulefinder, self.HI_OK)
2016-02-05 01:37:51 +00:00
2019-09-02 14:11:35 +10:00
def test_python_mccabe(self):
2016-02-10 01:33:51 +00:00
self._test_tool(tools.python_mccabe, self.HI_OK)
2016-02-05 01:37:51 +00:00
def test_perl_syntax(self):
2016-02-18 22:34:46 +00:00
self._test_tool(tools.perl_syntax,
[("perl.pl", tools.Status.ok)])
2016-02-08 21:55:22 +00:00
def test_c_syntax_gcc(self):
self._test_tool(tools.c_syntax_gcc, [("hello.c", tools.Status.ok)])
2016-11-21 11:59:56 +01:00
2016-02-10 01:25:45 +00:00
def test_objdump_headers(self):
self._test_tool(tools.objdump_headers,
[("rotatingtree.o", tools.Status.ok)])
2016-02-10 01:25:45 +00:00
def test_objdump_disassemble(self):
self._test_tool(tools.objdump_disassemble,
[("rotatingtree.o", tools.Status.problem)])
2016-02-10 01:25:45 +00:00
def test_readelf(self):
self._test_tool(tools.readelf, [("rotatingtree.o", tools.Status.ok)])
def test_zipinfo(self):
self._test_tool(tools.zipinfo, [("hi.zip", tools.Status.ok)])
2016-02-09 22:18:46 +00:00
2016-02-09 22:23:29 +00:00
def test_tar_gz(self):
self._test_tool(tools.tar_gz, [("hi.tar.gz", tools.Status.ok),
("hi.tgz", tools.Status.ok)])
2016-02-09 22:23:29 +00:00
2016-02-09 22:26:38 +00:00
def test_tar_bz2(self):
self._test_tool(tools.tar_bz2, [("hi.tar.bz2", tools.Status.ok)])
2016-02-09 22:26:38 +00:00
2016-02-09 22:56:36 +00:00
def test_nm(self):
self._test_tool(tools.nm, [("libieee.a", tools.Status.ok),
("libpcprofile.so", tools.Status.ok)])
2016-02-09 22:56:36 +00:00
2016-02-10 00:47:16 +00:00
def test_pdf2txt(self):
self._test_tool(tools.pdf2txt, [("standard.pdf", tools.Status.ok)])
2016-02-10 00:47:16 +00:00
2016-02-09 21:38:33 +00:00
def test_html_syntax(self):
2016-02-10 01:25:45 +00:00
self._test_tool(tools.html_syntax, [("hi.html", tools.Status.problem)])
2016-02-09 21:38:33 +00:00
def test_html2text(self):
self._test_tool(tools.html2text, [("hi.html", tools.Status.ok)])
2016-02-09 21:38:33 +00:00
def test_cpp_syntax_gcc(self):
self._test_tool(tools.cpp_syntax_gcc, [("hello.cpp", tools.Status.ok)])
2016-11-21 11:59:56 +01:00
def test_php8_syntax(self):
self._test_tool(tools.php8_syntax, [("root.php", tools.Status.ok)])
2016-02-09 21:48:09 +00:00
2017-03-28 22:53:03 +02:00
def test_pil(self):
for extension in ["png", "jpg", "gif", "bmp", "ppm", "tiff", "tga"]:
2017-03-28 22:53:03 +02:00
self._test_tool(tools.pil, [("circle." + extension,
tools.Status.ok)])
2017-03-28 22:53:03 +02:00
2016-02-05 01:37:51 +00:00
class LruCacheWithEvictionTestCase(unittest.TestCase):
def _assert_cache(self, func, hits, misses, current_size):
cache_info = func.cache_info()
self.assertEqual(cache_info.hits, hits)
self.assertEqual(cache_info.misses, misses)
self.assertEqual(cache_info.currsize, current_size)
def test_lru_cache_with_eviction(self):
@tools.lru_cache_with_eviction()
def a(foo):
return foo
self._assert_cache(a, 0, 0, 0)
self.assertEqual(a(1), 1)
self._assert_cache(a, 0, 1, 1)
a(1)
self._assert_cache(a, 1, 1, 1)
a.evict(1)
self._assert_cache(a, 1, 1, 1)
a(1)
self._assert_cache(a, 1, 2, 2)
2016-02-05 01:37:51 +00:00
if __name__ == "__main__":
golden.main()