Skip to content

Commit

Permalink
Merge pull request #519 from jouve/silence
Browse files Browse the repository at this point in the history
silence logs/warnings in unittests
  • Loading branch information
Gauvain Pocentek committed Jun 7, 2018
2 parents 92ca5c4 + 3fa24ea commit bbefb99
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
3 changes: 2 additions & 1 deletion gitlab/cli.py
Expand Up @@ -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)
Expand Down
31 changes: 26 additions & 5 deletions gitlab/tests/test_cli.py
Expand Up @@ -22,12 +22,25 @@
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

Expand All @@ -48,9 +61,11 @@ class TestClass(object):
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):
Expand All @@ -73,8 +88,14 @@ def test_parse_value(self):
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()
Expand Down
2 changes: 1 addition & 1 deletion gitlab/tests/test_gitlab.py
Expand Up @@ -439,7 +439,7 @@ def resp_cont(url, request):
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):
Expand Down

0 comments on commit bbefb99

Please sign in to comment.