diff --git a/TODO.todo b/TODO.todo index 05ddf5f..2ad76f5 100644 --- a/TODO.todo +++ b/TODO.todo @@ -46,6 +46,7 @@ Documentation: Use `capsys` `assert capsys.readouterr().out` A new line may be inserted if using `click.echo()` + ☐ Document using datadir with a module rather than a shared one. Link to tembo as an example. ☐ Can prospector ignore tests dir? document this in the gist if so diff --git a/tembo/cli/cli.py b/tembo/cli/cli.py index b2e47a8..0014fea 100644 --- a/tembo/cli/cli.py +++ b/tembo/cli/cli.py @@ -173,25 +173,21 @@ def _new_create_scoped_page(config_scope: dict, inputs: Collection[str]) -> page try: return pages.ScopedPageCreator(page_creator_options).create_page() except exceptions.BasePathDoesNotExistError as base_path_does_not_exist_error: - tembo.cli.logger.critical(base_path_does_not_exist_error) + cli_message(base_path_does_not_exist_error.args[0]) raise SystemExit(1) from base_path_does_not_exist_error except exceptions.TemplateFileNotFoundError as template_file_not_found_error: - tembo.cli.logger.critical(template_file_not_found_error.args[0]) + cli_message(template_file_not_found_error.args[0]) raise SystemExit(1) from template_file_not_found_error except exceptions.MismatchedTokenError as mismatched_token_error: if config_scope["example"] is not None: - tembo.cli.logger.critical( - "Your tembo config.yml/template specifies %s input tokens, you gave %s. Example: %s", - mismatched_token_error.expected, - mismatched_token_error.given, - config_scope["example"], + cli_message( + f'Your tembo config.yml/template specifies {mismatched_token_error.expected} input tokens, you gave {mismatched_token_error.given}. Example: {config_scope["example"]}' ) raise SystemExit(1) from mismatched_token_error - tembo.cli.logger.critical( - "Your tembo config.yml/template specifies %s input tokens, you gave %s", - mismatched_token_error.expected, - mismatched_token_error.given, + cli_message( + f'Your tembo config.yml/template specifies {mismatched_token_error.expected} input tokens, you gave {mismatched_token_error.given}' ) + raise SystemExit(1) from mismatched_token_error diff --git a/tests/test_cli/test_cli.py b/tests/test_cli/test_cli.py index 039e12f..de08919 100644 --- a/tests/test_cli/test_cli.py +++ b/tests/test_cli/test_cli.py @@ -1,5 +1,6 @@ import importlib import os +import pathlib import pytest @@ -9,6 +10,7 @@ from tembo.cli.cli import ( _new_verify_name_exists, _new_get_config_scope, _new_show_example, + _new_create_scoped_page, ) @@ -147,3 +149,97 @@ def test_new_show_example(path, message, shared_datadir, capsys): # assert assert capsys.readouterr().out == message assert system_exit.value.code == 0 + + +def test_new_create_scoped_page_success(shared_datadir, tmpdir): + # arrange + os.environ["TEMBO_CONFIG"] = str(shared_datadir / "config" / "success") + os.environ["TEMBO_BASE_PATH"] = str(tmpdir) + importlib.reload(tembo.cli) + + config_scope = _new_get_config_scope("some_scope") + inputs = () + scoped_page_file = pathlib.Path(tmpdir / "some_scope" / "some_scope").with_suffix( + ".md" + ) + + # act + scoped_page = _new_create_scoped_page(config_scope, inputs) + + # assert + assert scoped_page.path == scoped_page_file + assert scoped_page.page_content == "" + + +def test_new_create_scoped_page_base_path_does_not_exist( + shared_datadir, tmpdir, capsys +): + # arrange + os.environ["TEMBO_CONFIG"] = str(shared_datadir / "config" / "success") + os.environ["TEMBO_BASE_PATH"] = str(tmpdir / "nonexistent" / "path") + importlib.reload(tembo.cli) + + config_scope = _new_get_config_scope("some_scope") + inputs = () + + # act + with pytest.raises(SystemExit) as system_exit: + _new_create_scoped_page(config_scope, inputs) + + # assert + assert system_exit.value.code == 1 + assert ( + capsys.readouterr().out + == f'[TEMBO] Tembo base path of {os.environ["TEMBO_BASE_PATH"]} does not exist. 🐘\n' + ) + + +def test_new_create_scoped_page_template_file_does_not_exist( + shared_datadir, tmpdir, capsys +): + # arrange + os.environ["TEMBO_CONFIG"] = str(shared_datadir / "config" / "success") + os.environ["TEMBO_BASE_PATH"] = str(tmpdir) + os.environ["TEMBO_TEMPLATE_PATH"] = str(tmpdir) + importlib.reload(tembo.cli) + + config_scope = _new_get_config_scope("some_scope") + config_scope["template_filename"] = "some_nonexistent_template.md.tpl" + inputs = () + + # act + with pytest.raises(SystemExit) as system_exit: + _new_create_scoped_page(config_scope, inputs) + + # assert + assert system_exit.value.code == 1 + assert ( + capsys.readouterr().out + == f'[TEMBO] Template file {os.environ["TEMBO_TEMPLATE_PATH"]}/{config_scope["template_filename"]} does not exist. 🐘\n' + ) + + +@pytest.mark.parametrize("example", [(True,), (False,)]) +def test_new_create_scoped_page_mismatched_token( + example, shared_datadir, tmpdir, capsys +): + # arrange + os.environ["TEMBO_CONFIG"] = str(shared_datadir / "config" / "success") + os.environ["TEMBO_BASE_PATH"] = str(tmpdir) + importlib.reload(tembo.cli) + + config_scope = _new_get_config_scope("some_scope") + inputs = ("some_input",) + if not example: + config_scope["example"] = None + + # act + with pytest.raises(SystemExit) as system_exit: + _new_create_scoped_page(config_scope, inputs) + + # assert + assert system_exit.value.code == 1 + assert ( + capsys.readouterr().out + == f'[TEMBO] Your tembo config.yml/template specifies 0 input tokens, you gave 1. Example: tembo new some_scope 🐘\n' + )