From f1b83748ed639091925c9be331d7fa1941148068 Mon Sep 17 00:00:00 2001 From: Tobias Gruetzmacher Date: Thu, 12 Oct 2017 23:56:39 +0200 Subject: [PATCH] When testing the command line, call main method. Previously, we were spawning the main binary in a subprocess, which is fragile and interacts poorly with some testing frameworks... --- dosagelib/cmd.py | 4 ++-- tests/__init__.py | 29 ---------------------------- tests/test_dosage.py | 45 +++++++++++++++++++++++--------------------- 3 files changed, 26 insertions(+), 52 deletions(-) delete mode 100644 tests/__init__.py diff --git a/dosagelib/cmd.py b/dosagelib/cmd.py index 14d77098b..b835f0f7b 100644 --- a/dosagelib/cmd.py +++ b/dosagelib/cmd.py @@ -322,10 +322,10 @@ def get_tagged_scraper_name(scraperobj, limit=None, reasons=None): return name + suffix -def main(): +def main(args=None): """Parse options and execute commands.""" try: - options = setup_options().parse_args() + options = setup_options().parse_args(args=args) options.basepath = os.path.expanduser(options.basepath) res = run(options) except KeyboardInterrupt: diff --git a/tests/__init__.py b/tests/__init__.py deleted file mode 100644 index 4b02005d9..000000000 --- a/tests/__init__.py +++ /dev/null @@ -1,29 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright (C) 2013-2014 Bastian Kleineidam -# Copyright (C) 2015-2017 Tobias Gruetzmacher - -from __future__ import absolute_import, division, print_function - -import os -import subprocess - -basedir = os.path.dirname(__file__) -dosage_cmd = os.path.join(os.path.dirname(basedir), "dosage") - - -def run(cmd, verbosity=0, **kwargs): - """Run command without error checking. - @return: command return code""" - if kwargs.get("shell"): - # for shell calls the command must be a string - cmd = " ".join(cmd) - return subprocess.call(cmd, **kwargs) - - -def run_checked(cmd, ret_ok=(0,), **kwargs): - """Run command and raise OSError on error.""" - retcode = run(cmd, **kwargs) - if retcode not in ret_ok: - msg = "Command `%s' returned non-zero exit status %d" % (cmd, retcode) - raise OSError(msg) - return retcode diff --git a/tests/test_dosage.py b/tests/test_dosage.py index ec910cc65..88e244ff7 100644 --- a/tests/test_dosage.py +++ b/tests/test_dosage.py @@ -1,58 +1,61 @@ # -*- coding: utf-8 -*- # Copyright (C) 2004-2008 Tristan Seligmann and Jonathan Jacobs # Copyright (C) 2012-2014 Bastian Kleineidam -# Copyright (C) 2015-2016 Tobias Gruetzmacher +# Copyright (C) 2015-2017 Tobias Gruetzmacher from __future__ import absolute_import, division, print_function import pytest import sys -from . import dosage_cmd, run_checked +import dosagelib.cmd +def cmd(*options): + """'Fake' run dosage with given options.""" + return dosagelib.cmd.main(('--allow-multiple',) + options) -def run_with_options(options, cmd=dosage_cmd): - """Run dosage with given options.""" - run_checked([sys.executable, cmd, '--allow-multiple'] + options) +def cmd_ok(*options): + assert cmd(*options) == 0 +def cmd_err(*options): + assert cmd(*options) == 1 class TestDosage(object): """Test the dosage commandline client.""" def test_list_comics(self): for option in ("-l", "--list", "--singlelist"): - run_with_options([option]) + cmd_ok(option) def test_display_version(self): - run_with_options(["--version"]) + cmd_ok("--version") def test_display_help(self): for option in ("-h", "--help"): - run_with_options([option]) + with pytest.raises(SystemExit): + cmd(option) def test_module_help(self): - run_with_options(["-m", "xkcd"]) + cmd_ok("-m", "xkcd") def test_no_comics_specified(self): - with pytest.raises(OSError): - run_with_options([]) + cmd_err() def test_unknown_option(self): - with pytest.raises(OSError): - run_with_options(['--imadoofus']) + with pytest.raises(SystemExit): + cmd('--imadoofus') def test_multiple_comics_match(self): - with pytest.raises(OSError): - run_with_options(['Garfield']) + cmd_err('Garfield') def test_fetch_html_and_rss_json(self, tmpdir): - run_with_options(["-n", "2", "-v", "-b", str(tmpdir), "-o", "html", - "-o", "rss", "-o", "json", "xkcd"]) + cmd_ok("-n", "2", "-v", "-b", str(tmpdir), "-o", "html", "-o", "rss", + "-o", "json", "xkcd") def test_fetch_html_and_rss_2(self, tmpdir): - run_with_options(["--numstrips", "2", "--baseurl", "bla", - "--basepath", str(tmpdir), "--output", "rss", - "--output", "html", "--adult", "BloomingFaeries"]) + cmd_ok("--numstrips", "2", "--baseurl", "bla", "--basepath", + str(tmpdir), "--output", "rss", "--output", "html", "--adult", + "BloomingFaeries") def test_fetch_indexed(self, tmpdir): - run_with_options(["-n", "2", "-v", "-b", str(tmpdir), "xkcd:303"]) + cmd_ok("-n", "2", "-v", "-b", str(tmpdir), "xkcd:303")