From 526dd733b51aa4304be5aff63e400334d5786c6b Mon Sep 17 00:00:00 2001 From: Daniel Tomlinson Date: Mon, 1 Nov 2021 00:41:01 +0000 Subject: [PATCH] adding latest --- TODO.todo | 8 + dev/notes/test.md | 40 +--- tembo/cli.py | 12 +- tembo/exceptions.py | 4 + tembo/journal/pages.py | 23 +- tests/test_journal/test_pages.py | 219 +++++++++++++++++- .../.templates/some_template.md.tpl | 1 - .../some_template_date_tokens.md.tpl | 1 + .../some_template_input_tokens.md.tpl | 1 + .../some_template_name_tokens.md.tpl | 1 + .../.templates/some_template_no_tokens.md.tpl | 3 + .../test_pages/does_exist/some_note.md | 1 + 12 files changed, 270 insertions(+), 44 deletions(-) delete mode 100644 tests/test_journal/test_pages/.templates/some_template.md.tpl create mode 100644 tests/test_journal/test_pages/.templates/some_template_date_tokens.md.tpl create mode 100644 tests/test_journal/test_pages/.templates/some_template_input_tokens.md.tpl create mode 100644 tests/test_journal/test_pages/.templates/some_template_name_tokens.md.tpl create mode 100644 tests/test_journal/test_pages/.templates/some_template_no_tokens.md.tpl create mode 100644 tests/test_journal/test_pages/does_exist/some_note.md diff --git a/TODO.todo b/TODO.todo index ef1f800..1fdcd36 100644 --- a/TODO.todo +++ b/TODO.todo @@ -39,12 +39,16 @@ Documentation: ☐ 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 + ☐ Redo the documentation on a CLI, reorganise and inocropoate all the new tembo layouts Functionality: ☐ Replace loggers with `click.echo` for command outputs. Keep logging messages for actual logging messages? + Define a format: [TEMBO:$datetime] $message 🐘 - document this in general python for CLI ☐ Refactor the tembo new command so the cli is split out into manageable methods ☐ Use the complicated CLI example so the tembo new has its own module to define functions in ☐ Replace all logger errors with exceptions, move logger messages to the cli. + ☐ How to pass a successful save notification back to the CLI? Return a bool? Or is there some other way? + ☐ Replace pendulum with datetime ✔ Make options a property on the class, add to abstract @done(21-10-30 19:31) ☐ Use the python runner Duty @@ -53,6 +57,10 @@ Functionality: ☐ Build docs ☐ Document using Duty +Logging: + ☐ Make all internal tembo logs be debug + ☐ User can enable them with the config + VSCode: PyInstaller: ☐ Document build error: diff --git a/dev/notes/test.md b/dev/notes/test.md index 4a1a293..1681544 100644 --- a/dev/notes/test.md +++ b/dev/notes/test.md @@ -1,22 +1,25 @@ # testing notes +## options + optional: + - user_input - example - template_filename - template_path required: + - base_path - page_path - filename - extension - name +## tests to write -- page with/without a template - user input is None -- the given base path does not exist - page using/not using input tokens - user input does not match number of input tokens - no user input @@ -24,34 +27,13 @@ required: - with/without example - page using/not using date tokens - page using/not using name tokens - +- dry run - path/page filenames can contain spaces and they are converted +## tests done - -@dataclass -class PageCreatorOptions: - """Options dataclass to create a Page. - - Attributes: - base_path (str): - page_path (str): - filename (str): - extension (str): - name (str): - user_input (Collection[str] | None, optional): - example (str | None, optional): - template_filename (str | None, optional): - template_path (str | None, optional): - """ - - base_path: str - page_path: str - filename: str - extension: str - name: str - user_input: Collection[str] | None = None - example: str | None = None - template_filename: str | None = None - template_path: str | None = None +- page with/without a template +- the given base path does not exist +- the given template file does not exist +- page already exists diff --git a/tembo/cli.py b/tembo/cli.py index c2e3f5e..361243d 100644 --- a/tembo/cli.py +++ b/tembo/cli.py @@ -122,8 +122,12 @@ def new(scope, inputs, dry_run, example): tembo.logger.critical(template_file_not_found_error.args[0]) raise SystemExit(1) from template_file_not_found_error - scoped_page.save_to_disk(dry_run=dry_run) - raise SystemExit(0) + try: + scoped_page.save_to_disk(dry_run=dry_run) + raise SystemExit(0) + except exceptions.ScopedPageAlreadyExists as scoped_page_already_exists: + cli_message(f"File {scoped_page_already_exists}") + raise SystemExit(0) from scoped_page_already_exists if not _name_found and len(tembo.CONFIG.scopes) > 0: # if the name is missing in the config.yml, raise error tembo.logger.warning("Command %s not found in config.yml - exiting", scope) @@ -136,6 +140,10 @@ def new(scope, inputs, dry_run, example): raise SystemExit(1) +def cli_message(message: str) -> None: + click.echo(f"[TEMBO] {message} 🐘") + + run.add_command(new) run.add_command(list_all) diff --git a/tembo/exceptions.py b/tembo/exceptions.py index d4b0252..6d9fb1b 100644 --- a/tembo/exceptions.py +++ b/tembo/exceptions.py @@ -11,3 +11,7 @@ class BasePathDoesNotExistError(Exception): class TemplateFileNotFoundError(Exception): """Raised if the template file does not exist.""" + + +class ScopedPageAlreadyExists(Exception): + """Raised if the scoped page file already exists.""" diff --git a/tembo/journal/pages.py b/tembo/journal/pages.py index b566c6b..bfd0ba1 100644 --- a/tembo/journal/pages.py +++ b/tembo/journal/pages.py @@ -74,7 +74,8 @@ class PageCreator: else self.options.extension ) except IndexError: - # REVIEW: can this be removed now this is not called anywhere else? + # REVIEW: try putting a . in the config yaml and see what error is raised + # this is no longer generic it just gets the full path to the file. # IndexError means the path is not a file, just a path return path_to_file # return path with a file @@ -88,7 +89,9 @@ class PageCreator: self.options.template_path ).expanduser() else: - converted_template_path = pathlib.Path() + converted_template_path = ( + pathlib.Path(self.options.base_path).expanduser() / ".templates" + ) file_loader = jinja2.FileSystemLoader(converted_template_path) env = jinja2.Environment(loader=file_loader, autoescape=True) @@ -277,19 +280,21 @@ class ScopedPage(Page): SystemExit: Exit code 0 if dry run is `True`, page is successfully saved or if page already exists. """ + # TODO: move this functionality to the CLI so the page is created and the message + # returned to the user from the CLI. if dry_run: tembo.logger.info("%s will be created", self.path) raise SystemExit(0) # create the parent directories scoped_note_file = pathlib.Path(self.path) scoped_note_file.parents[0].mkdir(parents=True, exist_ok=True) - if not scoped_note_file.exists(): - with scoped_note_file.open("w", encoding="utf-8") as scoped_page: - scoped_page.write(self.page_content) - tembo.logger.info("Saved %s to disk", self.path) - else: - tembo.logger.info("%s already exists - skipping.", self.path) - raise SystemExit(0) + if scoped_note_file.exists(): + raise exceptions.ScopedPageAlreadyExists(f"{self.path} already exists") + + with scoped_note_file.open("w", encoding="utf-8") as scoped_page: + scoped_page.write(self.page_content) + # TODO: pass this back somehow + tembo.logger.info("Saved %s to disk", self.path) if __name__ == "__main__": diff --git a/tests/test_journal/test_pages.py b/tests/test_journal/test_pages.py index 16b4d54..ef2005d 100644 --- a/tests/test_journal/test_pages.py +++ b/tests/test_journal/test_pages.py @@ -1,7 +1,13 @@ +from datetime import date +import pathlib + import pytest from tembo.journal.pages import PageCreatorOptions, ScopedPageCreator -from tembo.exceptions import BasePathDoesNotExistError +from tembo import exceptions + + +DATE_TODAY = date.today().strftime("%d-%m-%Y") def test_create_page_base_path_does_not_exist(tmpdir): @@ -20,11 +26,218 @@ def test_create_page_base_path_does_not_exist(tmpdir): ) # act - with pytest.raises(BasePathDoesNotExistError) as base_path_does_not_exist_error: - scoped_page_creator = ScopedPageCreator().create_page(options) + with pytest.raises( + exceptions.BasePathDoesNotExistError + ) as base_path_does_not_exist_error: + scoped_page = ScopedPageCreator().create_page(options) # assert assert ( str(base_path_does_not_exist_error.value) == f"Tembo base path of {base_path} does not exist." ) + + +@pytest.mark.parametrize("template_path", [(None), ("/nonexistent/path")]) +def test_create_page_template_file_does_not_exist(template_path, tmpdir): + # arrange + options = PageCreatorOptions( + base_path=str(tmpdir), + page_path="", + filename="", + extension="", + name="", + user_input=None, + example=None, + template_filename="template.md.tpl", + template_path=template_path, + ) + + # act + with pytest.raises( + exceptions.TemplateFileNotFoundError + ) as template_file_not_found_error: + scoped_page = ScopedPageCreator().create_page(options) + + # assert + if template_path is None: + assert str(template_file_not_found_error.value) == ( + f"Template file {options.base_path}/.templates/{options.template_filename} does not exist." + ) + else: + assert str(template_file_not_found_error.value) == ( + f"Template file {template_path}/{options.template_filename} does not exist." + ) + + +def test_create_page_already_exists(datadir): + # arrange + options = PageCreatorOptions( + base_path=str(datadir), + page_path="does_exist", + filename="some_note", + extension="md", + name="some_name", + user_input=None, + example=None, + template_filename=None, + template_path=None, + ) + scoped_page_file = ( + pathlib.Path(options.base_path) / options.page_path / options.filename + ).with_suffix(f".{options.extension}") + + # act + scoped_page = ScopedPageCreator().create_page(options) + with pytest.raises(exceptions.ScopedPageAlreadyExists) as page_already_exists: + scoped_page.save_to_disk() + + # assert + assert scoped_page_file.exists() + assert str(page_already_exists.value) == f"{scoped_page_file} already exists" + with scoped_page_file.open("r", encoding="utf-8") as scoped_page_contents: + assert scoped_page_contents.readlines() == ["this file already exists\n"] + + +def test_create_page_without_template(tmpdir, caplog): + # arrange + options = PageCreatorOptions( + base_path=str(tmpdir), + page_path="some_path", + filename="some_filename", + extension="some_extension", + name="some_name", + user_input=None, + example=None, + template_filename=None, + template_path=None, + ) + # TODO: copy this pattern creation into the other tests + scoped_page_file = ( + pathlib.Path(options.base_path) / options.page_path / options.filename + ).with_suffix(f".{options.extension}") + + # act + scoped_page = ScopedPageCreator().create_page(options) + scoped_page.save_to_disk() + + # assert + assert scoped_page_file.exists() + assert caplog.records[0].message == f"Saved {scoped_page_file} to disk" + with scoped_page_file.open("r", encoding="utf-8") as scoped_page_contents: + assert scoped_page_contents.readlines() == [] + + +def test_create_page_with_template(datadir, caplog): + # arrange + options = PageCreatorOptions( + base_path=str(datadir), + page_path="some_path", + filename="some_note", + extension="md", + name="some_name", + user_input=None, + example=None, + template_filename="some_template_no_tokens.md.tpl", + template_path=None, + ) + scoped_page_file = ( + pathlib.Path(options.base_path) / options.page_path / options.filename + ).with_suffix(f".{options.extension}") + + # act + scoped_page = ScopedPageCreator().create_page(options) + scoped_page.save_to_disk() + + # assert + assert scoped_page_file.exists() + assert caplog.records[0].message == f"Saved {scoped_page_file} to disk" + with scoped_page_file.open("r", encoding="utf-8") as scoped_page_contents: + assert scoped_page_contents.readlines() == [ + "scoped page file\n", + "\n", + "no tokens", + ] + + +@pytest.mark.parametrize( + "user_input,template_filename,page_contents", + [ + (None, "some_template_date_tokens.md.tpl", f"some date token: {DATE_TODAY}"), + ( + ("first_input", "second_input"), + "some_template_input_tokens.md.tpl", + "some input tokens second_input first_input", + ), + (None, "some_template_name_tokens.md.tpl", "some name token some_name"), + ], +) +def test_create_tokened_page_tokens_in_template( + datadir, caplog, user_input, template_filename, page_contents +): + # arrange + options = PageCreatorOptions( + base_path=str(datadir), + page_path="some_path", + filename="some_note", + extension="md", + name="some_name", + user_input=user_input, + example=None, + template_filename=template_filename, + template_path=None, + ) + scoped_page_file = ( + pathlib.Path(options.base_path) / options.page_path / options.filename + ).with_suffix(f".{options.extension}") + + # act + scoped_page = ScopedPageCreator().create_page(options) + scoped_page.save_to_disk() + + # assert + assert scoped_page_file.exists() + assert caplog.records[0].message == f"Saved {scoped_page_file} to disk" + + with scoped_page_file.open("r", encoding="utf-8") as scoped_page_contents: + assert scoped_page_contents.readline() == page_contents + + +@pytest.mark.parametrize( + "user_input,filename,tokened_filename", + [ + (None, "date_token_{d:%d-%m-%Y}", f"date_token_{DATE_TODAY}"), + (None, "name_token_{name}", "name_token_some_name"), + ( + ("first_input", "second input"), + "input_token_{input1}_{input0}", + "input_token_second_input_first_input", + ), + ], +) +def test_create_tokened_page_tokens_in_filename( + datadir, caplog, user_input, filename, tokened_filename +): + # arrange + options = PageCreatorOptions( + base_path=str(datadir), + page_path="some_path", + filename=filename, + extension="md", + name="some_name", + user_input=user_input, + example=None, + template_filename=None, + template_path=None, + ) + scoped_page_file = ( + pathlib.Path(options.base_path) / options.page_path / tokened_filename + ).with_suffix(f".{options.extension}") + + # act + scoped_page = ScopedPageCreator().create_page(options) + scoped_page.save_to_disk() + + # assert + assert scoped_page_file.exists() + assert caplog.records[0].message == f"Saved {scoped_page_file} to disk" diff --git a/tests/test_journal/test_pages/.templates/some_template.md.tpl b/tests/test_journal/test_pages/.templates/some_template.md.tpl deleted file mode 100644 index 1a53169..0000000 --- a/tests/test_journal/test_pages/.templates/some_template.md.tpl +++ /dev/null @@ -1 +0,0 @@ -template contents diff --git a/tests/test_journal/test_pages/.templates/some_template_date_tokens.md.tpl b/tests/test_journal/test_pages/.templates/some_template_date_tokens.md.tpl new file mode 100644 index 0000000..da47289 --- /dev/null +++ b/tests/test_journal/test_pages/.templates/some_template_date_tokens.md.tpl @@ -0,0 +1 @@ +some date token: {d:%d-%m-%Y} diff --git a/tests/test_journal/test_pages/.templates/some_template_input_tokens.md.tpl b/tests/test_journal/test_pages/.templates/some_template_input_tokens.md.tpl new file mode 100644 index 0000000..18bcc20 --- /dev/null +++ b/tests/test_journal/test_pages/.templates/some_template_input_tokens.md.tpl @@ -0,0 +1 @@ +some input tokens {input1} {input0} diff --git a/tests/test_journal/test_pages/.templates/some_template_name_tokens.md.tpl b/tests/test_journal/test_pages/.templates/some_template_name_tokens.md.tpl new file mode 100644 index 0000000..77ecade --- /dev/null +++ b/tests/test_journal/test_pages/.templates/some_template_name_tokens.md.tpl @@ -0,0 +1 @@ +some name token {name} diff --git a/tests/test_journal/test_pages/.templates/some_template_no_tokens.md.tpl b/tests/test_journal/test_pages/.templates/some_template_no_tokens.md.tpl new file mode 100644 index 0000000..6a2f5e1 --- /dev/null +++ b/tests/test_journal/test_pages/.templates/some_template_no_tokens.md.tpl @@ -0,0 +1,3 @@ +scoped page file + +no tokens diff --git a/tests/test_journal/test_pages/does_exist/some_note.md b/tests/test_journal/test_pages/does_exist/some_note.md new file mode 100644 index 0000000..cc0459b --- /dev/null +++ b/tests/test_journal/test_pages/does_exist/some_note.md @@ -0,0 +1 @@ +this file already exists