diff options
-rw-r--r-- | gitlab/cli.py | 3 | ||||
-rw-r--r-- | gitlab/tests/test_cli.py | 31 | ||||
-rw-r--r-- | gitlab/tests/test_gitlab.py | 2 |
3 files changed, 29 insertions, 7 deletions
diff --git a/gitlab/cli.py b/gitlab/cli.py index 4d41b83..4870192 100644 --- a/gitlab/cli.py +++ b/gitlab/cli.py @@ -120,7 +120,8 @@ def _parse_value(v): # If the user-provided value starts with @, we try to read the file # path provided after @ as the real value. Exit on any error. try: - return open(v[1:]).read() + with open(v[1:]) as fl: + return fl.read() except Exception as e: sys.stderr.write("%s\n" % e) sys.exit(1) diff --git a/gitlab/tests/test_cli.py b/gitlab/tests/test_cli.py index 3c148f8..3fe4a4e 100644 --- a/gitlab/tests/test_cli.py +++ b/gitlab/tests/test_cli.py @@ -22,12 +22,25 @@ from __future__ import absolute_import import argparse import os import tempfile +try: + from contextlib import redirect_stderr # noqa: H302 +except ImportError: + from contextlib import contextmanager # noqa: H302 + import sys + + @contextmanager + def redirect_stderr(new_target): + old_target, sys.stderr = sys.stderr, new_target + yield + sys.stderr = old_target try: import unittest except ImportError: import unittest2 as unittest +import six + from gitlab import cli import gitlab.v4.cli @@ -48,9 +61,11 @@ class TestCLI(unittest.TestCase): self.assertEqual("class", cli.cls_to_what(Class)) def test_die(self): - with self.assertRaises(SystemExit) as test: - cli.die("foobar") - + fl = six.StringIO() + with redirect_stderr(fl): + with self.assertRaises(SystemExit) as test: + cli.die("foobar") + self.assertEqual(fl.getvalue(), "foobar\n") self.assertEqual(test.exception.code, 1) def test_parse_value(self): @@ -73,8 +88,14 @@ class TestCLI(unittest.TestCase): self.assertEqual(ret, 'content') os.unlink(temp_path) - with self.assertRaises(SystemExit): - cli._parse_value('@/thisfileprobablydoesntexist') + fl = six.StringIO() + with redirect_stderr(fl): + with self.assertRaises(SystemExit) as exc: + cli._parse_value('@/thisfileprobablydoesntexist') + self.assertEqual(fl.getvalue(), + "[Errno 2] No such file or directory:" + " '/thisfileprobablydoesntexist'\n") + self.assertEqual(exc.exception.code, 1) def test_base_parser(self): parser = cli._get_base_parser() diff --git a/gitlab/tests/test_gitlab.py b/gitlab/tests/test_gitlab.py index 34b60b9..5174bd2 100644 --- a/gitlab/tests/test_gitlab.py +++ b/gitlab/tests/test_gitlab.py @@ -439,7 +439,7 @@ class TestGitlab(unittest.TestCase): with HTTMock(resp_cont): callback() self.assertEqual(self.gl.private_token, token) - self.assertDictContainsSubset(expected, self.gl.headers) + self.assertDictEqual(expected, self.gl.headers) self.assertEqual(self.gl.user.id, id_) def test_token_auth(self, callback=None): |