diff --git a/tests/bash_tests/testcases.py b/tests/bash_tests/testcases.py index d0048676..2e7f8523 100644 --- a/tests/bash_tests/testcases.py +++ b/tests/bash_tests/testcases.py @@ -7,11 +7,11 @@ class TestCases(unittest.TestCase): def setUp(self): - os.chdir(utils.TEST_DIR) + os.chdir(utils.Conf.tmp_dir) def tearDown(self): - utils.log._buffer = [''] + utils.log.buffer = [''] def test_addmoddel(self): diff --git a/tests/bash_tests/utils.py b/tests/bash_tests/utils.py index ab2dbfdd..0f5a319c 100644 --- a/tests/bash_tests/utils.py +++ b/tests/bash_tests/utils.py @@ -7,38 +7,40 @@ import shutil import subprocess -# The configuration parameters for bash test -# The function configure_suite() in system_tests.py will override these parameters -EXIV2_DIR = os.path.normpath(os.path.join(os.path.abspath(__file__), '../../../')) -BIN_DIR = os.path.join(EXIV2_DIR, 'build/bin') -DATA_DIR = os.path.join(EXIV2_DIR, 'test/data') -TEST_DIR = os.path.join(EXIV2_DIR, 'test/tmp') -ENCODING = 'utf-8' -PLATFORM = platform.system() or 'Unknown' -os.makedirs(TEST_DIR, exist_ok=True) +class Conf: + # The configuration parameters for bash test + # The function configure_suite() in system_tests.py will override these parameters + exiv2_dir = os.path.normpath(os.path.join(os.path.abspath(__file__), '../../../')) + bin_dir = os.path.join(exiv2_dir, 'build/bin') + data_dir = os.path.join(exiv2_dir, 'test/data') + tmp_dir = os.path.join(exiv2_dir, 'test/tmp') + encoding = 'utf-8' + system_name = platform.system() or 'Unknown' + + @classmethod + def init(cls): + """ Init the test environment """ + os.makedirs(cls.tmp_dir, exist_ok=True) + cls.bin_files = [i.split('.')[0] for i in os.listdir(cls.bin_dir)] class Log: - _buffer = [''] + buffer = [''] - def _add_msg(self, msg): - self._buffer.append(msg) + def to_str(self): + return '\n'.join(self.buffer) - @property - def buffer(self): - return '\n'.join(self._buffer) + def add_msg(self, msg): + self.buffer.append(msg) def info(self, msg, index=None): - self._add_msg('[INFO] {}'.format(msg)) + self.add_msg('[INFO] {}'.format(msg)) def warn(self, msg): - self._add_msg('[WARN] {}'.format(msg)) + self.add_msg('[WARN] {}'.format(msg)) def error(self, msg): - self._add_msg('[ERROR] {}'.format(msg)) - - -log = Log() + self.add_msg('[ERROR] {}'.format(msg)) def cp(src, dest): @@ -60,27 +62,27 @@ def rm(*files): continue -def cat(*files, encoding=ENCODING): +def cat(*files, encoding=None): text = '' for i in files: - with open(i, 'r', encoding=encoding) as f: + with open(i, 'r', encoding=encoding or Conf.encoding) as f: text += f.read() return text -def grep(pattern, *files, encoding=ENCODING): +def grep(pattern, *files, encoding=None): result = '' pattern = '.*{}.*'.format(pattern) for i in files: - text = cat(i, encoding=encoding) + text = cat(i, encoding=encoding or Conf.encoding) result += '\n'.join(re.findall(pattern, text)) return result -def save(text: (str, list), filename, encoding=ENCODING): +def save(text: (str, list), filename, encoding=None): if not isinstance(text, str): text = '\n'.join(text) - with open(filename, 'w', encoding=encoding) as f: + with open(filename, 'w', encoding=encoding or Conf.encoding) as f: f.write(text) @@ -92,7 +94,7 @@ def diff(file1, file2): report = [] report += ['{}: {} lines'.format(file1, len(list1))] report += ['{}: {} lines'.format(file2, len(list2))] - max_lines = max(len(list1), len(list2)) + max_lines = max(len(list1), len(list2)) for i in [list1, list2]: i += [''] * (max_lines - len(i)) # Make them have the same number of lines for i in range(max_lines): @@ -105,15 +107,15 @@ def diff(file1, file2): def copyTestFile(src, dest=''): - """ Copy one test file from DATA_DIR to TEST_DIR """ + """ Copy one test file from data_dir to tmp_dir """ if not dest: dest = src - shutil.copy(os.path.join(DATA_DIR, src), - os.path.join(TEST_DIR, dest)) + shutil.copy(os.path.join(Conf.data_dir, src), + os.path.join(Conf.tmp_dir, dest)) def copyTestFiles(*files): - """ Copy one or more test files from DATA_DIR to TEST_DIR """ + """ Copy one or more test files from data_dir to tmp_dir """ for i in files: copyTestFile(i) @@ -124,14 +126,14 @@ def md5sum(filename): return hashlib.md5(f.read()).hexdigest() -def runTest(cmd: str, vars_dict=dict(), expected_returncodes=[0], encoding=ENCODING) -> list: +def runTest(cmd: str, vars_dict=dict(), expected_returncodes=[0], encoding=None) -> list: """ Execute a file in the exiv2 bin directory and return its stdout. """ cmd = cmd.format(**vars_dict) args = shlex.split(cmd) - args[0] = os.path.join(BIN_DIR, args[0]) - p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=TEST_DIR) + args[0] = os.path.join(Conf.bin_dir, args[0]) + p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=Conf.tmp_dir) stdout, stderr = p.communicate() - output = (stdout + stderr).decode(encoding).rstrip('\n') + output = (stdout + stderr).decode(encoding or Conf.encoding).rstrip('\n') if p.returncode not in expected_returncodes: log.error('Failed to excute: {}'.format(cmd)) log.error('The expected return code is {}, but get {}'.format(str(expected_returncodes), p.returncode)) @@ -140,26 +142,30 @@ def runTest(cmd: str, vars_dict=dict(), expected_returncodes=[0], encoding=ENCOD return output.split('\n') if output else [] -def reportTest(testname, output: (str, list), encoding=ENCODING): +def reportTest(testname, output: (str, list), encoding=None): """ If the output of the test case is correct, this function returns None. Otherwise print its error. """ if not isinstance(output, str): output = '\n'.join(output) - reference_file = os.path.join(DATA_DIR, '{}.out'.format(testname)) - reference_output = cat(reference_file, encoding=encoding) + reference_file = os.path.join(Conf.data_dir, '{}.out'.format(testname)) + reference_output = cat(reference_file, encoding=encoding or Conf.encoding) if reference_output == output: return log.error('The output of the testcase mismatch the reference') - output_file = os.path.join(TEST_DIR, '{}.out'.format(testname)) - save(output, output_file, encoding=encoding) + output_file = os.path.join(Conf.tmp_dir, '{}.out'.format(testname)) + save(output, output_file, encoding=encoding or Conf.encoding) log.info('The output has been saved to file {}'.format(output_file)) - log.error('diff:\n' + diff(reference_file, output_file)) - raise RuntimeError(log.buffer) + log.info('diff:\n' + diff(reference_file, output_file)) + raise RuntimeError(log.to_str()) def ioTest(filename): - src = os.path.join(DATA_DIR, filename) - out1 = os.path.join(TEST_DIR, '{}.1'.format(filename)) - out2 = os.path.join(TEST_DIR, '{}.2'.format(filename)) + src = os.path.join(Conf.data_dir, filename) + out1 = os.path.join(Conf.tmp_dir, '{}.1'.format(filename)) + out2 = os.path.join(Conf.tmp_dir, '{}.2'.format(filename)) runTest('iotest {src} {out1} {out2}', vars()) assert md5sum(src) == md5sum(out1), 'The output file is different' assert md5sum(src) == md5sum(out2), 'The output file is different' + + +Conf.init() +log = Log() diff --git a/tests/system_tests.py b/tests/system_tests.py index a1f11bb3..64166e5d 100644 --- a/tests/system_tests.py +++ b/tests/system_tests.py @@ -202,9 +202,10 @@ def configure_suite(config_file): ) # Configure the parameters for bash test - utils.BIN_DIR = os.path.abspath(config['ENV']['exiv2_path']) - utils.DATA_DIR = os.path.abspath(config['paths']['data_path']) - utils.TEST_DIR = os.path.abspath(config['paths']['tmp_path']) + utils.Conf.bin_dir = os.path.abspath(config['ENV']['exiv2_path']) + utils.Conf.data_dir = os.path.abspath(config['paths']['data_path']) + utils.Conf.tmp_dir = os.path.abspath(config['paths']['tmp_path']) + utils.Conf.init() class FileDecoratorBase(object):