Skip to content

Commit bbefb99

Browse files
author
Gauvain Pocentek
authored
Merge pull request python-gitlab#519 from jouve/silence
silence logs/warnings in unittests
2 parents 92ca5c4 + 3fa24ea commit bbefb99

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

gitlab/cli.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ def _parse_value(v):
120120
# If the user-provided value starts with @, we try to read the file
121121
# path provided after @ as the real value. Exit on any error.
122122
try:
123-
return open(v[1:]).read()
123+
with open(v[1:]) as fl:
124+
return fl.read()
124125
except Exception as e:
125126
sys.stderr.write("%s\n" % e)
126127
sys.exit(1)

gitlab/tests/test_cli.py

+26-5
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,25 @@
2222
import argparse
2323
import os
2424
import tempfile
25+
try:
26+
from contextlib import redirect_stderr # noqa: H302
27+
except ImportError:
28+
from contextlib import contextmanager # noqa: H302
29+
import sys
30+
31+
@contextmanager
32+
def redirect_stderr(new_target):
33+
old_target, sys.stderr = sys.stderr, new_target
34+
yield
35+
sys.stderr = old_target
2536

2637
try:
2738
import unittest
2839
except ImportError:
2940
import unittest2 as unittest
3041

42+
import six
43+
3144
from gitlab import cli
3245
import gitlab.v4.cli
3346

@@ -48,9 +61,11 @@ class TestClass(object):
4861
self.assertEqual("class", cli.cls_to_what(Class))
4962

5063
def test_die(self):
51-
with self.assertRaises(SystemExit) as test:
52-
cli.die("foobar")
53-
64+
fl = six.StringIO()
65+
with redirect_stderr(fl):
66+
with self.assertRaises(SystemExit) as test:
67+
cli.die("foobar")
68+
self.assertEqual(fl.getvalue(), "foobar\n")
5469
self.assertEqual(test.exception.code, 1)
5570

5671
def test_parse_value(self):
@@ -73,8 +88,14 @@ def test_parse_value(self):
7388
self.assertEqual(ret, 'content')
7489
os.unlink(temp_path)
7590

76-
with self.assertRaises(SystemExit):
77-
cli._parse_value('@/thisfileprobablydoesntexist')
91+
fl = six.StringIO()
92+
with redirect_stderr(fl):
93+
with self.assertRaises(SystemExit) as exc:
94+
cli._parse_value('@/thisfileprobablydoesntexist')
95+
self.assertEqual(fl.getvalue(),
96+
"[Errno 2] No such file or directory:"
97+
" '/thisfileprobablydoesntexist'\n")
98+
self.assertEqual(exc.exception.code, 1)
7899

79100
def test_base_parser(self):
80101
parser = cli._get_base_parser()

gitlab/tests/test_gitlab.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ def resp_cont(url, request):
439439
with HTTMock(resp_cont):
440440
callback()
441441
self.assertEqual(self.gl.private_token, token)
442-
self.assertDictContainsSubset(expected, self.gl.headers)
442+
self.assertDictEqual(expected, self.gl.headers)
443443
self.assertEqual(self.gl.user.id, id_)
444444

445445
def test_token_auth(self, callback=None):

0 commit comments

Comments
 (0)