diff options
Diffstat (limited to 'qa/base.py')
-rw-r--r-- | qa/base.py | 43 |
1 files changed, 22 insertions, 21 deletions
@@ -1,20 +1,16 @@ -# pylint: disable=bad-option-value,unidiomatic-typecheck,undefined-variable,no-else-return, -# pylint: disable=too-many-function-args,unexpected-keyword-arg - import os import platform import shutil import sys import tempfile -from datetime import datetime -from uuid import uuid4 +from datetime import datetime, timezone from unittest import TestCase +from uuid import uuid4 import arrow - -from qa.shell import git, gitlint, RunningCommand -from qa.utils import DEFAULT_ENCODING +from qa.shell import RunningCommand, git, gitlint +from qa.utils import FILE_ENCODING, PLATFORM_IS_WINDOWS, TERMINAL_ENCODING class BaseTestCase(TestCase): @@ -40,18 +36,19 @@ class BaseTestCase(TestCase): for tmpfile in self.tmpfiles: os.remove(tmpfile) for repo in self.tmp_git_repos: - shutil.rmtree(repo) + # On windows we need to ignore errors because git might still be holding on to some files + shutil.rmtree(repo, ignore_errors=PLATFORM_IS_WINDOWS) - def assertEqualStdout(self, output, expected): # pylint: disable=invalid-name + def assertEqualStdout(self, output, expected): self.assertIsInstance(output, RunningCommand) - output = output.stdout.decode(DEFAULT_ENCODING) + output = output.stdout.decode(TERMINAL_ENCODING) output = output.replace("\r", "") self.assertMultiLineEqual(output, expected) @staticmethod def generate_temp_path(): - timestamp = datetime.now().strftime("%Y%m%d-%H%M%S-%f") - return os.path.realpath(f"/tmp/gitlint-test-{timestamp}") + timestamp = datetime.now(timezone.utc).strftime("%Y%m%d-%H%M%S-%f") + return os.path.realpath(f"/tmp/gitlint-test-{timestamp}") # noqa def create_tmp_git_repo(self): """Creates a temporary git repository and returns its directory path""" @@ -72,6 +69,9 @@ class BaseTestCase(TestCase): # http://stackoverflow.com/questions/5581857/git-and-the-umlaut-problem-on-mac-os-x git("config", "core.precomposeunicode", "true", _cwd=tmp_git_repo) + # Git now does commit message cleanup by default (e.g. removing trailing whitespace), disable that for testing + git("config", "commit.cleanup", "verbatim", _cwd=tmp_git_repo) + return tmp_git_repo @staticmethod @@ -84,13 +84,12 @@ class BaseTestCase(TestCase): if isinstance(content, bytes): open_kwargs = {"mode": "wb"} else: - open_kwargs = {"mode": "w", "encoding": DEFAULT_ENCODING} + open_kwargs = {"mode": "w", "encoding": FILE_ENCODING} - with open(full_path, **open_kwargs) as f: # pylint: disable=unspecified-encoding + with open(full_path, **open_kwargs) as f: f.write(content) else: - # pylint: disable=consider-using-with - open(full_path, "a", encoding=DEFAULT_ENCODING).close() + open(full_path, "a", encoding=FILE_ENCODING).close() # noqa: SIM115 (Use context handler for opening files) return test_filename @@ -150,9 +149,9 @@ class BaseTestCase(TestCase): if isinstance(content, bytes): open_kwargs = {"mode": "wb"} else: - open_kwargs = {"mode": "w", "encoding": DEFAULT_ENCODING} + open_kwargs = {"mode": "w", "encoding": FILE_ENCODING} - with open(tmpfile, **open_kwargs) as f: # pylint: disable=unspecified-encoding + with open(tmpfile, **open_kwargs) as f: f.write(content) return tmpfilepath @@ -181,7 +180,8 @@ class BaseTestCase(TestCase): specified by variable_dict.""" expected_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "expected") expected_path = os.path.join(expected_dir, filename) - with open(expected_path, encoding=DEFAULT_ENCODING) as file: + # Expected files are UTF-8 encoded (not dependent on the system's default encoding) + with open(expected_path, encoding=FILE_ENCODING) as file: expected = file.read() if variable_dict: @@ -199,7 +199,8 @@ class BaseTestCase(TestCase): "git_version": expected_git_version, "gitlint_version": expected_gitlint_version, "GITLINT_USE_SH_LIB": BaseTestCase.GITLINT_USE_SH_LIB, - "DEFAULT_ENCODING": DEFAULT_ENCODING, + "TERMINAL_ENCODING": TERMINAL_ENCODING, + "FILE_ENCODING": FILE_ENCODING, } def get_debug_vars_last_commit(self, git_repo=None): |