Commit 20db85ed authored by Johannes Bechberger's avatar Johannes Bechberger

Auto detect main class in `compile-firm` mode

parent aba4af12
...@@ -30,7 +30,8 @@ class TestMode: ...@@ -30,7 +30,8 @@ class TestMode:
exec = "exec" exec = "exec"
USE_TESTS_OF_OTHER = { USE_TESTS_OF_OTHER = {
ast: [syntax] ast: [syntax],
compile_firm: [exec]
} }
""" All 'success' tests of the n.th mode can used as 'success' tests for the n-1.th mode""" """ All 'success' tests of the n.th mode can used as 'success' tests for the n-1.th mode"""
...@@ -39,7 +40,7 @@ TEST_MODES = [TestMode.lexer, TestMode.syntax, TestMode.ast, TestMode.semantic, ...@@ -39,7 +40,7 @@ TEST_MODES = [TestMode.lexer, TestMode.syntax, TestMode.ast, TestMode.semantic,
def get_test_dirname(mode: str) -> str: def get_test_dirname(mode: str) -> str:
d = { d = {
TestMode.compile_firm: "exec" # TestMode.compile_firm: "exec"
} }
if mode not in d: if mode not in d:
return mode return mode
......
import hashlib
import logging import logging
import os import os
import shutil import shutil
...@@ -6,6 +7,7 @@ from mjtest.environment import TestMode, Environment ...@@ -6,6 +7,7 @@ from mjtest.environment import TestMode, Environment
from mjtest.test.syntax_tests import BasicSyntaxTest from mjtest.test.syntax_tests import BasicSyntaxTest
from mjtest.test.tests import TestCase, BasicDiffTestResult, BasicTestResult from mjtest.test.tests import TestCase, BasicDiffTestResult, BasicTestResult
from mjtest.util.shell import SigKill from mjtest.util.shell import SigKill
from mjtest.util.utils import get_main_class_name
_LOG = logging.getLogger("exec_tests") _LOG = logging.getLogger("exec_tests")
...@@ -25,9 +27,11 @@ class JavaExecTest(BasicSyntaxTest): ...@@ -25,9 +27,11 @@ class JavaExecTest(BasicSyntaxTest):
if not path.exists(prev_out_dir): if not path.exists(prev_out_dir):
os.mkdir(prev_out_dir) os.mkdir(prev_out_dir)
self._prev_out_file = path.join(prev_out_dir, path.basename(self._expected_output_file)) self._prev_out_file = path.join(prev_out_dir, path.basename(self._expected_output_file))
self._prev_out_hash_file = self._prev_out_file + "_hash"
self._has_expected_output_file = path.exists(self._expected_output_file) self._has_expected_output_file = path.exists(self._expected_output_file)
if not self._has_expected_output_file: if not self._has_expected_output_file:
if path.exists(self._prev_out_file) and self.compare_files_by_age(file, self._prev_out_file) == -1: if path.exists(self._prev_out_file) and path.exists(self._prev_out_hash_file) \
and self._check_hash_sum(self.preprocessed_file, self._prev_out_hash_file):
self._has_expected_output_file = True self._has_expected_output_file = True
self._expected_output_file = self._prev_out_file self._expected_output_file = self._prev_out_file
_LOG.info("Reuse old java output file \"{}\"".format(path.relpath(self._prev_out_file))) _LOG.info("Reuse old java output file \"{}\"".format(path.relpath(self._prev_out_file)))
...@@ -40,6 +44,7 @@ class JavaExecTest(BasicSyntaxTest): ...@@ -40,6 +44,7 @@ class JavaExecTest(BasicSyntaxTest):
cwd = os.getcwd() cwd = os.getcwd()
os.chdir(tmp_dir) os.chdir(tmp_dir)
exp_out = None exp_out = None
#print(base_filename, get_main_class_name(base_filename + ".java"))
if not self._has_expected_output_file: if not self._has_expected_output_file:
_, _, javac_rtcode = \ _, _, javac_rtcode = \
self.env.run_command("javac", base_filename + ".java") self.env.run_command("javac", base_filename + ".java")
...@@ -48,11 +53,14 @@ class JavaExecTest(BasicSyntaxTest): ...@@ -48,11 +53,14 @@ class JavaExecTest(BasicSyntaxTest):
os.chdir(cwd) os.chdir(cwd)
raise InterruptedError() raise InterruptedError()
exp_out, _, _ = \ exp_out, _, _ = \
self.env.run_command("java", base_filename) self.env.run_command("java", get_main_class_name(base_filename + ".java"))
exp_out = exp_out.decode().strip() exp_out = exp_out.decode().strip()
with open(self._prev_out_file, "w") as f: with open(self._prev_out_file, "w") as f:
f.write(exp_out) f.write(exp_out)
f.flush() f.flush()
with open(self._prev_out_hash_file, "w") as f:
f.write(self._hash_sum_for_file(base_filename + ".java"))
f.flush()
if self._has_expected_output_file and self.type == self.MODE and self.env.mode == self.MODE: if self._has_expected_output_file and self.type == self.MODE and self.env.mode == self.MODE:
with open(self._expected_output_file, "r") as f: with open(self._expected_output_file, "r") as f:
exp_out = f.read() exp_out = f.read()
...@@ -71,18 +79,14 @@ class JavaExecTest(BasicSyntaxTest): ...@@ -71,18 +79,14 @@ class JavaExecTest(BasicSyntaxTest):
os.chdir(cwd) os.chdir(cwd)
raise raise
def get_file_time(self, file: str) -> float: def _check_hash_sum(self, file: str, hash_sum_file: str) -> bool:
return max(os.path.getmtime(file), os.path.getatime(file)) old_hash = ""
with open(hash_sum_file, "r") as f:
old_hash = f.readline().strip()
return self._hash_sum_for_file(file) == old_hash
def compare_files_by_age(self, file: str, other_file: str) -> int: def _hash_sum_for_file(self, file: str) -> str:
""" with open(file, "r") as f:
:return: -1 == file is older, 0 == almost equal ages, 1 == other file is older return hashlib.sha256(f.read().encode()).hexdigest()
"""
diff = self.get_file_time(file) - self.get_file_time(other_file)
if diff < 60:
return -1
if diff > -60:
return 1
return 0
TestCase.TEST_CASE_CLASSES[TestMode.compile_firm].append(JavaExecTest) TestCase.TEST_CASE_CLASSES[TestMode.compile_firm].append(JavaExecTest)
\ No newline at end of file
import logging import logging
from os import path from os import path
import sys import sys
from typing import Tuple from typing import Tuple, Optional
import re
COLOR_OUTPUT_IF_POSSIBLE = False COLOR_OUTPUT_IF_POSSIBLE = False
...@@ -41,4 +42,18 @@ def cprint(text: str, *args, **kwargs): ...@@ -41,4 +42,18 @@ def cprint(text: str, *args, **kwargs):
if COLOR_OUTPUT_IF_POSSIBLE: if COLOR_OUTPUT_IF_POSSIBLE:
termcolor.cprint(text, *args, **kwargs) termcolor.cprint(text, *args, **kwargs)
else: else:
print(text) print(text)
\ No newline at end of file
def get_main_class_name(file: str) -> Optional[str]:
current_class = None
with open(file, "r") as f:
for line in f:
if line.startswith("class "):
match = re.search("[A-Za-z_0-9]+", line.replace("class ", ""))
if match:
has_public_class = True
current_class = match.group(0)
elif "String[]" in line and "main" in line and "void" in line and "static" in line and "public" in line:
return current_class
return None
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