Unify cmd tests to use capfd

This commit is contained in:
Tobias Gruetzmacher 2023-06-09 22:34:13 +02:00
parent 02718209ce
commit 355ef44b7e
No known key found for this signature in database

View file

@ -1,7 +1,7 @@
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
# Copyright (C) 2004-2008 Tristan Seligmann and Jonathan Jacobs # SPDX-FileCopyrightText: © 2004 Tristan Seligmann and Jonathan Jacobs
# Copyright (C) 2012-2014 Bastian Kleineidam # SPDX-FileCopyrightText: © 2012 Bastian Kleineidam
# Copyright (C) 2015-2020 Tobias Gruetzmacher # SPDX-FileCopyrightText: © 2015 Tobias Gruetzmacher
import json import json
import os import os
import re import re
@ -27,7 +27,7 @@ def cmd_err(*options):
@pytest.mark.usefixtures('_nosleep', '_noappdirs') @pytest.mark.usefixtures('_nosleep', '_noappdirs')
class TestDosage(object): class TestDosage:
"""Test the dosage commandline client.""" """Test the dosage commandline client."""
# This shouldn't hit the network at all, so add responses without mocks to # This shouldn't hit the network at all, so add responses without mocks to
@ -40,7 +40,7 @@ class TestDosage(object):
]) ])
def test_list_comics(self, option, capfd): def test_list_comics(self, option, capfd):
cmd_ok(option) cmd_ok(option)
out, err = capfd.readouterr() out = capfd.readouterr().out
assert 'ADummyTestScraper' in out assert 'ADummyTestScraper' in out
@responses.activate @responses.activate
@ -48,41 +48,41 @@ class TestDosage(object):
cmd_ok("--version") cmd_ok("--version")
@responses.activate @responses.activate
def test_update_available(self, capsys): def test_update_available(self, capfd):
responses.add(responses.GET, re.compile(r'https://api\.github\.com/'), responses.add(responses.GET, re.compile(r'https://api\.github\.com/'),
json={'tag_name': '9999.0', 'assets': [ json={'tag_name': '9999.0', 'assets': [
{'browser_download_url': 'TEST.whl'}, {'browser_download_url': 'TEST.whl'},
{'browser_download_url': 'TEST.exe'}, {'browser_download_url': 'TEST.exe'},
]}) ]})
cmd_ok('--version', '-v') cmd_ok('--version', '-v')
captured = capsys.readouterr() out = capfd.readouterr().out
best = 'TEST.exe' if os.name == 'nt' else 'TEST.whl' best = 'TEST.exe' if os.name == 'nt' else 'TEST.whl'
assert best in captured.out assert best in out
assert 'A new version' in captured.out assert 'A new version' in out
@responses.activate @responses.activate
def test_no_update_available(self, capsys): def test_no_update_available(self, capfd):
responses.add(responses.GET, re.compile(r'https://api\.github\.com/'), responses.add(responses.GET, re.compile(r'https://api\.github\.com/'),
json={'tag_name': '1.0'}) json={'tag_name': '1.0'})
cmd_ok('--version', '-v') cmd_ok('--version', '-v')
captured = capsys.readouterr() out = capfd.readouterr().out
assert 'Detected local or development' in captured.out assert 'Detected local or development' in out
@responses.activate @responses.activate
def test_current(self, capsys): def test_current(self, capfd):
responses.add(responses.GET, re.compile(r'https://api\.github\.com/'), responses.add(responses.GET, re.compile(r'https://api\.github\.com/'),
json={'tag_name': dosagelib.__version__}) json={'tag_name': dosagelib.__version__})
cmd_ok('--version', '-v') cmd_ok('--version', '-v')
captured = capsys.readouterr() out = capfd.readouterr().out
assert captured.out.endswith('issues\n') assert out.endswith('issues\n')
@responses.activate @responses.activate
def test_update_broken(self, capsys): def test_update_broken(self, capfd):
responses.add(responses.GET, re.compile(r'https://api\.github\.com/'), responses.add(responses.GET, re.compile(r'https://api\.github\.com/'),
json={}) json={})
cmd_ok('--version', '-v') cmd_ok('--version', '-v')
captured = capsys.readouterr() out = capfd.readouterr().out
assert 'invalid update file' in captured.out assert 'invalid update file' in out
def test_display_help(self): def test_display_help(self):
for option in ("-h", "--help"): for option in ("-h", "--help"):
@ -91,7 +91,7 @@ class TestDosage(object):
def test_module_help(self, capfd): def test_module_help(self, capfd):
cmd_ok("-m", "-t", "xkcd") cmd_ok("-m", "-t", "xkcd")
out, err = capfd.readouterr() out = capfd.readouterr().out
assert re.match(r'([0-9][0-9]:){2}.. xkcd>', out) assert re.match(r'([0-9][0-9]:){2}.. xkcd>', out)
def test_broken_basepath_removal(self): def test_broken_basepath_removal(self):