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...
This commit is contained in:
parent
ac2ca54570
commit
f1b83748ed
3 changed files with 26 additions and 52 deletions
|
@ -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:
|
||||
|
|
|
@ -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
|
|
@ -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")
|
||||
|
|
Loading…
Reference in a new issue