Skip to content

silence logs/warnings in unittests #519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion gitlab/cli.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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