Skip to content

Commit

Permalink
Try to support no args
Browse files Browse the repository at this point in the history
Use pytest.mark.parametrize to improve test_main.
  • Loading branch information
yanqd0 committed Jan 12, 2018
1 parent 3a09520 commit 09a948e
Showing 2 changed files with 19 additions and 12 deletions.
12 changes: 9 additions & 3 deletions csft/__main__.py
Original file line number Diff line number Diff line change
@@ -6,11 +6,12 @@
"""

import argparse as ap
import sys
from os.path import curdir
from pathlib import Path

from . import __name__ as _name
from . import __version__ as _version
from ._csft import csft2data, column
from . import __name__ as _name, __version__ as _version
from ._csft import column, csft2data


def _dir(path_str):
@@ -60,6 +61,11 @@ def pretty_byte(byte):

def main(argv=None):
"""Execute the application from CLI."""
if argv is None:
argv = sys.argv[1:]
if not argv:
argv = [curdir]

args = _parse_args(argv)
data = csft2data(args.path)

19 changes: 10 additions & 9 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# -*- coding:utf-8 -*-

from os.path import devnull
from os.path import curdir, devnull
from subprocess import check_call

from pytest import fixture, raises
from pytest import fixture, mark, raises

from csft import __main__ as main

@@ -18,20 +18,21 @@ def test_call(null):
check_call(['python', '-m', 'csft', 'csft'], stdout=null, stderr=null)


def test_main(mocker):
@mark.parametrize('argv', [None, [], ['csft'], ])
def test_main(argv, mocker):
obj = object()
mocker.patch('sys.argv', ['csft'])
csft2data = mocker.patch('csft.__main__.csft2data', return_value=obj)
pr = mocker.patch('builtins.print')
assert 0 == main.main(argv=['csft'])
csft2data.assert_called_once_with(main._dir('csft'))
assert 0 == main.main(argv=argv)
if argv:
csft2data.assert_called_once_with(main._dir(argv[0]))
else:
csft2data.assert_called_once_with(main._dir(curdir))
pr.assert_called_once_with(obj)


def test_wrong_path(capsys):
with raises(SystemExit):
main.main(argv=[])
assert capsys.readouterr()

with raises(SystemExit):
main.main(argv=['path/is/not/a/directory'])
assert capsys.readouterr()

0 comments on commit 09a948e

Please sign in to comment.