Skip to content

Commit

Permalink
Add some tests.
Browse files Browse the repository at this point in the history
Also remove default subcommand feature because of
pallets/click#205
  • Loading branch information
untitaker committed Aug 21, 2014
1 parent 6da9c6f commit 7bea3b3
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 5 deletions.
13 changes: 11 additions & 2 deletions khal/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
import logging
import StringIO
import sys

try:
Expand All @@ -38,6 +37,11 @@
from khal.log import logger
from khal.settings import get_config

try:
from StringIO import StringIO
except ImportError:
from io import StringIO


def _get_cli():
@click.group(invoke_without_command=True)
Expand Down Expand Up @@ -99,7 +103,12 @@ def cli(ctx, config, verbose, include_calendar, exclude_calendar):
collection._default_calendar_name = conf['default']['default_calendar']

if not ctx.invoked_subcommand:
ctx.invoke(cli.commands[conf.default.default_command])
command = conf['default']['default_command']
if command:
ctx.invoke(cli.commands[command])
else:
click.echo(ctx.get_help())
ctx.exit(1)

days_option = click.option('--days', default=None, type=int)
events_option = click.option('--events', default=None, type=int)
Expand Down
8 changes: 5 additions & 3 deletions khal/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

from click import echo

import datetime
import logging
import sys
Expand Down Expand Up @@ -109,7 +111,7 @@ def get_agenda(collection, dateformat, longdateformat, dates=[],
event_column.extend([colored(d, event.color) for d in desc])

if event_column == []:
event_column = [bstring('No Events')]
event_column = [bstring('No events')]
return event_column


Expand All @@ -126,7 +128,7 @@ def __init__(self, collection, date=[], firstweekday=0, encoding='utf-8',
firstweekday=firstweekday)

rows = merge_columns(calendar_column, event_column)
print('\n'.join(rows).encode(encoding))
echo('\n'.join(rows).encode(encoding))


class Agenda(object):
Expand All @@ -136,7 +138,7 @@ def __init__(self, collection, date=None, firstweekday=0, encoding='utf-8',
term_width, _ = get_terminal_size()
event_column = get_agenda(collection, dates=date, width=term_width,
**kwargs)
print('\n'.join(event_column).encode(encoding))
echo('\n'.join(event_column).encode(encoding))


class NewFromString(object):
Expand Down
2 changes: 2 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# coding: utf-8
# vim: set ts=4 sw=4 expandtab sts=4:
79 changes: 79 additions & 0 deletions tests/cli_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# coding: utf-8
# vim: set ts=4 sw=4 expandtab sts=4:

from pkg_resources import parse_version
import pytest
import click
from click.testing import CliRunner

from khal.cli import main_khal


@pytest.fixture
def runner():
return CliRunner()

config_template = '''
[calendars]
[[one]]
path = {calpath}
color = dark blue
[locale]
local_timezone = Europe/Berlin
default_timezone = America/New_York
timeformat = %H:%M
dateformat = %d.%m.
longdateformat = %d.%m.%Y
datetimeformat = %d.%m. %H:%M
longdatetimeformat = %d.%m.%Y %H:%M
firstweekday = 0
[default]
default_command = {command}
default_calendar = one
debug = 1'''


def test_simple(tmpdir, runner):
config = tmpdir.join('config.ini')
calendar = tmpdir.mkdir('calendar')

config.write(config_template.format(calpath=str(calendar),
command='NOPE'))
conf_arg = ['-c', str(config)]
result = runner.invoke(main_khal, conf_arg + ['agenda'])
assert not result.exception
assert result.output == 'No events\n'

from .event_test import cal_dt
calendar.join('test.ics').write('\n'.join(cal_dt))
result = runner.invoke(main_khal, conf_arg + ['agenda', '09.04.2014'])
assert not result.exception
assert result.output == u'09.04.2014\n09:30-10:30: An Event\n'


def test_default_command_empty(tmpdir, runner):
config = tmpdir.join('config.ini')
calendar = tmpdir.mkdir('calendar')
config.write(config_template.format(calpath=str(calendar), command=''))

conf_arg = ['-c', str(config)]
result = runner.invoke(main_khal, conf_arg)
assert result.exception
assert result.exit_code == 1
assert result.output.startswith('Usage: ')


def test_default_command_nonempty(tmpdir, runner):
config = tmpdir.join('config.ini')
calendar = tmpdir.mkdir('calendar')
config.write(config_template.format(calpath=str(calendar),
command='agenda'))

conf_arg = ['-c', str(config)]
result = runner.invoke(main_khal, conf_arg)
assert not result.exception
assert result.output == 'No events\n'

0 comments on commit 7bea3b3

Please sign in to comment.