diff options
author | Joris Roovers <joris.roovers@gmail.com> | 2021-09-27 13:30:18 +0200 |
---|---|---|
committer | Joris Roovers <joris.roovers@gmail.com> | 2021-09-27 14:52:08 +0200 |
commit | c5fcff0692eb84b7b43cace42e4b0eea799f52c6 (patch) | |
tree | da0269620c081314391c72ca925ce91a87f76b86 /qa | |
parent | e6afc6b9ce0bdd90c6837b36f416bec2370288dc (diff) |
Pylint 2.11.1 code fixes
Code fixes for new issues in pylint 2.11.1.
Diffstat (limited to 'qa')
-rw-r--r-- | qa/base.py | 10 | ||||
-rw-r--r-- | qa/shell.py | 4 | ||||
-rw-r--r-- | qa/test_hooks.py | 29 | ||||
-rw-r--r-- | qa/test_stdin.py | 8 |
4 files changed, 27 insertions, 24 deletions
@@ -87,6 +87,7 @@ class BaseTestCase(TestCase): def create_file(parent_dir): """ Creates a file inside a passed directory. Returns filename.""" test_filename = "test-fïle-" + str(uuid4()) + # pylint: disable=consider-using-with io.open(os.path.join(parent_dir, test_filename), 'a', encoding=DEFAULT_ENCODING).close() return test_filename @@ -159,11 +160,12 @@ 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) - expected = io.open(expected_path, encoding=DEFAULT_ENCODING).read() + with io.open(expected_path, encoding=DEFAULT_ENCODING) as file: + expected = file.read() - if variable_dict: - expected = expected.format(**variable_dict) - return expected + if variable_dict: + expected = expected.format(**variable_dict) + return expected @staticmethod def get_system_info_dict(): diff --git a/qa/shell.py b/qa/shell.py index 97dcd2c..630843f 100644 --- a/qa/shell.py +++ b/qa/shell.py @@ -67,8 +67,8 @@ else: popen_kwargs['env'] = kwargs['_env'] try: - p = subprocess.Popen(args, **popen_kwargs) - result = p.communicate() + with subprocess.Popen(args, **popen_kwargs) as p: + result = p.communicate() except FileNotFoundError as exc: raise CommandNotFound from exc diff --git a/qa/test_hooks.py b/qa/test_hooks.py index 32abcb0..ea58699 100644 --- a/qa/test_hooks.py +++ b/qa/test_hooks.py @@ -9,9 +9,9 @@ class HookTests(BaseTestCase): """ Integration tests for gitlint commitmsg hooks""" VIOLATIONS = ['gitlint: checking commit message...\n', - u'1: T3 Title has trailing punctuation (.): "WIP: This ïs a title."\n', - u'1: T5 Title contains the word \'WIP\' (case-insensitive): "WIP: This ïs a title."\n', - u'2: B4 Second line is not empty: "Contënt on the second line"\n', + '1: T3 Title has trailing punctuation (.): "WIP: This ïs a title."\n', + '1: T5 Title contains the word \'WIP\' (case-insensitive): "WIP: This ïs a title."\n', + '2: B4 Second line is not empty: "Contënt on the second line"\n', '3: B6 Body message is missing\n', '-----------------------------------------------\n', 'gitlint: \x1b[31mYour commit message contains violations.\x1b[0m\n'] @@ -28,15 +28,17 @@ class HookTests(BaseTestCase): # install git commit-msg hook and assert output output_installed = gitlint("install-hook", _cwd=self.tmp_git_repo) - expected_installed = "Successfully installed gitlint commit-msg hook in %s/.git/hooks/commit-msg\n" % \ - self.tmp_git_repo + expected_installed = ("Successfully installed gitlint commit-msg hook in " + f"{self.tmp_git_repo}/.git/hooks/commit-msg\n") + self.assertEqualStdout(output_installed, expected_installed) def tearDown(self): # uninstall git commit-msg hook and assert output output_uninstalled = gitlint("uninstall-hook", _cwd=self.tmp_git_repo) - expected_uninstalled = "Successfully uninstalled gitlint commit-msg hook from %s/.git/hooks/commit-msg\n" % \ - self.tmp_git_repo + expected_uninstalled = ("Successfully uninstalled gitlint commit-msg hook from " + f"{self.tmp_git_repo}/.git/hooks/commit-msg\n") + self.assertEqualStdout(output_uninstalled, expected_uninstalled) def _violations(self): @@ -60,9 +62,9 @@ class HookTests(BaseTestCase): short_hash = self.get_last_commit_short_hash() expected_output = ["gitlint: checking commit message...\n", "gitlint: \x1b[32mOK\x1b[0m (no violations in commit message)\n", - "[master %s] This ïs a title\n" % short_hash, + f"[master {short_hash}] This ïs a title\n", " 1 file changed, 0 insertions(+), 0 deletions(-)\n", - " create mode 100644 %s\n" % test_filename] + f" create mode 100644 {test_filename}\n"] self.assertListEqual(expected_output, self.githook_output) def test_commit_hook_continue(self): @@ -76,10 +78,9 @@ class HookTests(BaseTestCase): expected_output = self._violations() expected_output += ["Continue with commit anyways (this keeps the current commit message)? " + "[y(es)/n(no)/e(dit)] " + - "[master %s] WIP: This ïs a title. Contënt on the second line\n" - % short_hash, + f"[master {short_hash}] WIP: This ïs a title. Contënt on the second line\n", " 1 file changed, 0 insertions(+), 0 deletions(-)\n", - " create mode 100644 %s\n" % test_filename] + f" create mode 100644 {test_filename}\n"] assert len(self.githook_output) == len(expected_output) for output, expected in zip(self.githook_output, expected_output): @@ -124,9 +125,9 @@ class HookTests(BaseTestCase): expected_output += self._violations()[1:] expected_output += ['Continue with commit anyways (this keeps the current commit message)? ' + "[y(es)/n(no)/e(dit)] " + - "[master %s] WIP: This ïs a title. Contënt on the second line\n" % short_hash, + f"[master {short_hash}] WIP: This ïs a title. Contënt on the second line\n", " 1 file changed, 0 insertions(+), 0 deletions(-)\n", - " create mode 100644 %s\n" % test_filename] + f" create mode 100644 {test_filename}\n"] assert len(self.githook_output) == len(expected_output) for output, expected in zip(self.githook_output, expected_output): diff --git a/qa/test_stdin.py b/qa/test_stdin.py index 18d6e7e..c98580e 100644 --- a/qa/test_stdin.py +++ b/qa/test_stdin.py @@ -50,7 +50,7 @@ class StdInTests(BaseTestCase): # We need to use subprocess.Popen() here instead of sh because when passing a file_handle to sh, it will # deal with reading the file itself instead of passing it on to gitlint as a STDIN. Since we're trying to # test for the condition where stat.S_ISREG == True that won't work for us here. - p = subprocess.Popen("gitlint", stdin=file_handle, cwd=self.tmp_git_repo, - stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - output, _ = p.communicate() - self.assertEqual(output.decode(DEFAULT_ENCODING), self.get_expected("test_stdin/test_stdin_file_1")) + with subprocess.Popen("gitlint", stdin=file_handle, cwd=self.tmp_git_repo, + stdout=subprocess.PIPE, stderr=subprocess.STDOUT) as p: + output, _ = p.communicate() + self.assertEqual(output.decode(DEFAULT_ENCODING), self.get_expected("test_stdin/test_stdin_file_1")) |