Skip to content

Commit

Permalink
Add cli fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Jan 16, 2020
1 parent cab09be commit e571370
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
1 change: 0 additions & 1 deletion ckan/cli/cli.py
Expand Up @@ -61,7 +61,6 @@ def format_commands(self, ctx, formatter):
@click.group(cls=CustomGroup)
@click.help_option(u'-h', u'--help')
@click_config_option
# @click.pass_context
def ckan(config, *args, **kwargs):
pass

Expand Down
1 change: 1 addition & 0 deletions ckan/tests/cli/__init__.py
@@ -0,0 +1 @@
# -*- coding: utf-8 -*-
35 changes: 35 additions & 0 deletions ckan/tests/cli/test_cli.py
@@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-

import pytest

from ckan.cli.cli import ckan


def test_ckan_without_args(cli):
"""Show help by default.
"""
result = cli.invoke(ckan)
assert result.output.startswith(u'Usage: ckan')
assert not result.exit_code


def test_ckan_incorrect_config(cli):
"""Config file must exist.
"""
result = cli.invoke(ckan, [u'-c', u'/a/b/c/d/e/f/g/h.ini'])
assert result.output.startswith(u'Config file not found')


def test_ckan_correct_config(cli, ckan_config):
"""Presense of config file disables default printing of help message.
"""
result = cli.invoke(ckan, [u'-c', ckan_config[u'__file__']])
assert u'Error: Missing command.' in result.output
assert result.exit_code


def test_ckan_correct_config_with_help(cli, ckan_config):
"""Config file not ignored when displaying usage.
"""
result = cli.invoke(ckan, [u'-c', ckan_config[u'__file__'], u'-h'])
assert not result.exit_code
15 changes: 15 additions & 0 deletions ckan/tests/pytest_ckan/fixtures.py
Expand Up @@ -29,6 +29,8 @@
"""

import pytest

from click.testing import CliRunner
import ckan.tests.helpers as test_helpers
import ckan.plugins
import ckan.lib.search as search
Expand Down Expand Up @@ -100,6 +102,19 @@ def test_dataset_search(self, app):
return make_app()


@pytest.fixture(scope=u"session")
def cli():
"""Provides object for invoking CLI commands from tests.
This is pure `click.testing.CliRunner`, so all examples from
`Click docs
<https://click.palletsprojects.com/en/master/testing/>`_ are valid
for it.
"""
return CliRunner()


@pytest.fixture(scope=u"session")
def reset_db():
"""Callable for resetting the database to the initial state.
Expand Down
4 changes: 2 additions & 2 deletions ckan/tests/pytest_ckan/test_fixtures.py
Expand Up @@ -36,7 +36,7 @@ def test_with_plugins_is_able_to_run_with_stats():
assert plugins.plugin_loaded(u"stats")


class TestMethodLevelConfig:
class TestMethodLevelConfig(object):
"""Verify that config overrides work for individual methods.
"""

Expand All @@ -50,7 +50,7 @@ def test_ckan_config_mark_second(self, ckan_config):


@pytest.mark.ckan_config(u"some.new.config", u"exists")
class TestMethodLevelConfig:
class TestMethodLevelConfig(object):
"""Verify that config overrides applied for each method when applied
per on class level.
"""
Expand Down

0 comments on commit e571370

Please sign in to comment.