diff --git a/ckan/cli/cli.py b/ckan/cli/cli.py index c55ac8db3b8..4efec34bb9c 100644 --- a/ckan/cli/cli.py +++ b/ckan/cli/cli.py @@ -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 diff --git a/ckan/tests/cli/__init__.py b/ckan/tests/cli/__init__.py new file mode 100644 index 00000000000..40a96afc6ff --- /dev/null +++ b/ckan/tests/cli/__init__.py @@ -0,0 +1 @@ +# -*- coding: utf-8 -*- diff --git a/ckan/tests/cli/test_cli.py b/ckan/tests/cli/test_cli.py new file mode 100644 index 00000000000..675267002df --- /dev/null +++ b/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 diff --git a/ckan/tests/pytest_ckan/fixtures.py b/ckan/tests/pytest_ckan/fixtures.py index 880bd0b9de3..943ffa8246e 100644 --- a/ckan/tests/pytest_ckan/fixtures.py +++ b/ckan/tests/pytest_ckan/fixtures.py @@ -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 @@ -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 + `_ are valid + for it. + + """ + return CliRunner() + + @pytest.fixture(scope=u"session") def reset_db(): """Callable for resetting the database to the initial state. diff --git a/ckan/tests/pytest_ckan/test_fixtures.py b/ckan/tests/pytest_ckan/test_fixtures.py index a273c2cf95b..07e1f31ba4c 100644 --- a/ckan/tests/pytest_ckan/test_fixtures.py +++ b/ckan/tests/pytest_ckan/test_fixtures.py @@ -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. """ @@ -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. """