mirror of
https://github.com/rpm-software-management/tito.git
synced 2025-02-23 12:12:47 +00:00
Silence spurious test output.
This commit is contained in:
parent
53144dc63e
commit
9b1d1c6988
3 changed files with 62 additions and 13 deletions
|
@ -25,6 +25,7 @@ from os.path import join
|
|||
from tito.common import run_command
|
||||
from tito.compat import * # NOQA
|
||||
from functional.fixture import TitoGitTestFixture, tito
|
||||
from unit import Capture
|
||||
|
||||
EXT_SRC_PKG = "extsrc"
|
||||
|
||||
|
@ -82,9 +83,10 @@ class FetchBuilderTests(TitoGitTestFixture):
|
|||
"noarch/extsrc-0.0.2-1.*noarch.rpm"))))
|
||||
|
||||
def test_tag_rejected(self):
|
||||
self.assertRaises(SystemExit, tito,
|
||||
'build --tag=extsrc-0.0.1-1 --rpm --output=%s --arg=source=%s ' %
|
||||
(self.output_dir, self.source_filename))
|
||||
with Capture(silent=True):
|
||||
self.assertRaises(SystemExit, tito,
|
||||
'build --tag=extsrc-0.0.1-1 --rpm --output=%s --arg=source=%s ' %
|
||||
(self.output_dir, self.source_filename))
|
||||
|
||||
def _setup_fetchbuilder_releaser(self, yum_repo_dir):
|
||||
self.write_file(join(self.repo_dir, '.tito/releasers.conf'),
|
||||
|
|
|
@ -11,6 +11,8 @@
|
|||
# granted to use or replicate Red Hat trademarks that are incorporated
|
||||
# in this software or its documentation.
|
||||
|
||||
import sys
|
||||
|
||||
from contextlib import contextmanager
|
||||
from mock import patch, MagicMock
|
||||
from tito.compat import PY2, StringIO
|
||||
|
@ -18,6 +20,47 @@ from tito.compat import PY2, StringIO
|
|||
file_spec = None
|
||||
|
||||
|
||||
class Capture(object):
|
||||
class Tee(object):
|
||||
def __init__(self, stream, silent):
|
||||
self.buf = StringIO()
|
||||
self.stream = stream
|
||||
self.silent = silent
|
||||
|
||||
def write(self, data):
|
||||
self.buf.write(data)
|
||||
if not self.silent:
|
||||
self.stream.write(data)
|
||||
|
||||
def getvalue(self):
|
||||
return self.buf.getvalue()
|
||||
|
||||
def isatty(self):
|
||||
return False
|
||||
|
||||
def __init__(self, silent=False):
|
||||
self.silent = silent
|
||||
|
||||
def __enter__(self):
|
||||
self.buffs = (self.Tee(sys.stdout, self.silent), self.Tee(sys.stderr, self.silent))
|
||||
self.stdout = sys.stdout
|
||||
self.stderr = sys.stderr
|
||||
sys.stdout, sys.stderr = self.buffs
|
||||
return self
|
||||
|
||||
@property
|
||||
def out(self):
|
||||
return self.buffs[0].getvalue()
|
||||
|
||||
@property
|
||||
def err(self):
|
||||
return self.buffs[1].getvalue()
|
||||
|
||||
def __exit__(self, exc_type, exc_value, traceback):
|
||||
sys.stdout = self.stdout
|
||||
sys.stderr = self.stderr
|
||||
|
||||
|
||||
@contextmanager
|
||||
def open_mock(content, **kwargs):
|
||||
"""Mock's mock_open only supports read() and write() which is not very useful.
|
||||
|
|
|
@ -24,7 +24,7 @@ import unittest
|
|||
|
||||
from mock import Mock, patch, call
|
||||
from textwrap import dedent
|
||||
from unit import open_mock
|
||||
from unit import open_mock, Capture
|
||||
|
||||
|
||||
class CommonTests(unittest.TestCase):
|
||||
|
@ -148,8 +148,9 @@ class CommonTests(unittest.TestCase):
|
|||
@patch("tito.common.find_file_with_extension")
|
||||
def test_find_spec_like_file_no_match(self, mock_find):
|
||||
mock_find.side_effect = [None, None]
|
||||
self.assertRaises(SystemExit, find_spec_like_file)
|
||||
self.assertEquals(2, len(mock_find.mock_calls))
|
||||
with Capture(silent=True):
|
||||
self.assertRaises(SystemExit, find_spec_like_file)
|
||||
self.assertEquals(2, len(mock_find.mock_calls))
|
||||
|
||||
@patch("os.listdir")
|
||||
def test_find_file_with_extension(self, mock_listdir):
|
||||
|
@ -168,7 +169,8 @@ class CommonTests(unittest.TestCase):
|
|||
@patch("os.listdir")
|
||||
def test_find_file_with_extension_duplicates(self, mock_listdir):
|
||||
mock_listdir.return_value = ["hello.txt", "goodbye.txt"]
|
||||
self.assertRaises(SystemExit, find_file_with_extension, "/tmp", ".txt")
|
||||
with Capture(silent=True):
|
||||
self.assertRaises(SystemExit, find_file_with_extension, "/tmp", ".txt")
|
||||
|
||||
def test_search_for(self):
|
||||
content = dedent("""
|
||||
|
@ -195,7 +197,8 @@ class CommonTests(unittest.TestCase):
|
|||
Goodbye World
|
||||
""")
|
||||
with open_mock(content):
|
||||
self.assertRaises(SystemExit, search_for, "foo", r"(NoMatch)")
|
||||
with Capture(silent=True):
|
||||
self.assertRaises(SystemExit, search_for, "foo", r"(NoMatch)")
|
||||
|
||||
|
||||
class CheetahRenderTest(unittest.TestCase):
|
||||
|
@ -228,12 +231,13 @@ class CheetahRenderTest(unittest.TestCase):
|
|||
mock_unlink.return_value = True
|
||||
mock_glob.return_value = []
|
||||
|
||||
self.assertRaises(SystemExit, render_cheetah, "foo.spec.tmpl", "/tmp", {})
|
||||
expected = "cheetah fill --flat --pickle=temp_pickle --odir=/tmp --oext=cheetah foo.spec.tmpl"
|
||||
self.assertEquals(call(expected), mock_run_command.mock_calls[0])
|
||||
with Capture(silent=True):
|
||||
self.assertRaises(SystemExit, render_cheetah, "foo.spec.tmpl", "/tmp", {})
|
||||
expected = "cheetah fill --flat --pickle=temp_pickle --odir=/tmp --oext=cheetah foo.spec.tmpl"
|
||||
self.assertEquals(call(expected), mock_run_command.mock_calls[0])
|
||||
|
||||
self.assertEquals(call("/tmp/*.cheetah"), mock_glob.mock_calls[0])
|
||||
self.assertEquals(call("temp_pickle"), mock_unlink.mock_calls[0])
|
||||
self.assertEquals(call("/tmp/*.cheetah"), mock_glob.mock_calls[0])
|
||||
self.assertEquals(call("temp_pickle"), mock_unlink.mock_calls[0])
|
||||
|
||||
|
||||
class VersionMathTest(unittest.TestCase):
|
||||
|
|
Loading…
Add table
Reference in a new issue