Skip to content

Commit

Permalink
Merge pull request #41 from hartwork/denoise-tests
Browse files Browse the repository at this point in the history
tests: Stop writing to stdout/stderr
  • Loading branch information
hartwork committed Aug 15, 2021
2 parents 00c8b0e + 23eee20 commit 61b0b92
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 8 deletions.
4 changes: 3 additions & 1 deletion binary_gentoo/internal/cli/tests/test__enrich.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Licensed under GNU Affero GPL version 3 or later

import os.path
from io import StringIO
from unittest import TestCase
from unittest.mock import Mock, patch

Expand Down Expand Up @@ -49,7 +50,8 @@ def test_without_explicit_path(self, attribute, getter, magic_relative_filename)
config_mock = Mock(**{attribute: None})
expected_path = os.path.abspath(magic_relative_filename)

with patch('subprocess.check_output', self._fake_portageq):
with patch('subprocess.check_output',
self._fake_portageq), patch('sys.stdout', StringIO()):
getter(config_mock)

actual_path = getattr(config_mock, attribute)
Expand Down
7 changes: 5 additions & 2 deletions binary_gentoo/internal/cli/tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Licensed under GNU Affero GPL version 3 or later

from dataclasses import dataclass
from io import StringIO
from tempfile import TemporaryDirectory
from textwrap import dedent
from typing import List
Expand Down Expand Up @@ -68,7 +69,8 @@ def _fake_subprocess_check_output(cls, argv):
def test_portagq_interaction(self):
config = parse_command_line(['gentoo-build', 'cat/pkg'])

with patch('subprocess.check_output', self._fake_subprocess_check_output):
with patch('subprocess.check_output', self._fake_subprocess_check_output), \
patch('sys.stdout', StringIO()):
enrich_config(config)

self.assertEqual(config.gentoo_profile, self.magic_profile)
Expand Down Expand Up @@ -112,7 +114,8 @@ def _run_gentoo_build_with_subprocess_mocked(argv_extra: List[str] = None) -> Ru
'=cat/pkg-123',
]

with patch('sys.argv', argv), patch('subprocess.check_call') as check_call_mock:
with patch('sys.argv', argv), patch('subprocess.check_call') as check_call_mock, \
patch('sys.stdout', StringIO()):
main()

return RunRecord(call_args_list=check_call_mock.call_args_list, )
Expand Down
4 changes: 3 additions & 1 deletion binary_gentoo/internal/cli/tests/test_clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Licensed under GNU Affero GPL version 3 or later

from dataclasses import dataclass
from io import StringIO
from itertools import product
from tempfile import TemporaryDirectory
from typing import List
Expand Down Expand Up @@ -42,7 +43,8 @@ def _run_gentoo_clean_with_subprocess_mocked(pretend=False, interactive=True) ->
if not interactive:
argv.append('--non-interactive')

with patch('sys.argv', argv), patch('subprocess.check_call') as check_call_mock:
with patch('sys.argv', argv), patch('subprocess.check_call') as check_call_mock, \
patch('sys.stdout', StringIO()):
main()

return RunRecord(
Expand Down
5 changes: 3 additions & 2 deletions binary_gentoo/internal/cli/tests/test_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def test_success(self, _label, pretend):
)

time_mock = Mock(return_value=float(now_epoch_seconds))
with patch('time.time', time_mock):
with patch('time.time', time_mock), patch('sys.stdout', StringIO()):
run_delete(config_mock)

with open(os.path.join(tempdir, 'Packages')) as f:
Expand Down Expand Up @@ -245,6 +245,7 @@ def test_help(self, *argv): # plain smoke test
def test_list_failure_empty_directory(self): # just something that touches beyond argparse
with TemporaryDirectory() as tempdir:
argv = ['gentoo-packages', '--pkgdir', tempdir, 'list']
with patch('sys.argv', argv), self.assertRaises(SystemExit) as catcher:
with patch('sys.argv', argv), patch('sys.stderr', StringIO()), \
self.assertRaises(SystemExit) as catcher:
main()
self.assertEqual(catcher.exception.args, (1, )) # i.e. error
3 changes: 2 additions & 1 deletion binary_gentoo/internal/cli/tests/test_tree_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ def test_given__not_empty(self):

def test_not_given__auto_detection(self):
config = parse_command_line(['gentoo-tree-diff', 'dir1', 'dir2'])
with patch('subprocess.check_output', self._fake_subprocess_check_output):
with patch('subprocess.check_output', self._fake_subprocess_check_output), \
patch('sys.stdout', StringIO()):
enrich_config(config)
self.assertEqual(config.keywords, {'one', 'two', '~*'})

Expand Down
4 changes: 3 additions & 1 deletion binary_gentoo/internal/cli/tests/test_tree_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Licensed under GNU Affero GPL version 3 or later

from dataclasses import dataclass
from io import StringIO
from subprocess import call
from tempfile import TemporaryDirectory
from typing import List
Expand All @@ -28,7 +29,8 @@ def _run_gentoo_tree_sync_with_subprocess_mocked(backup: bool) -> RunRecord:
argv += ['--backup-to', temp_portdir_old]
argv.append(temp_portdir_new)

with patch('sys.argv', argv), patch('subprocess.check_call') as check_call_mock:
with patch('sys.argv', argv), patch('subprocess.check_call') as check_call_mock, \
patch('sys.stdout', StringIO()):
main()

return RunRecord(call_args_list=check_call_mock.call_args_list, )
Expand Down

0 comments on commit 61b0b92

Please sign in to comment.