feat: initial commit

This commit is contained in:
2021-11-21 13:58:52 +00:00
commit fb7fec7ea6
53 changed files with 4920 additions and 0 deletions

0
tests/__init__.py Normal file
View File

View File

View File

@@ -0,0 +1 @@


View File

@@ -0,0 +1,5 @@
tembo:
scopes:
- name: some_scope
path: "some_scope"
extension: md

View File

@@ -0,0 +1,8 @@
tembo:
scopes:
- name: some_scope
example: tembo new some_scope
path: some_scope
filename: "{name}"
extension: md
template_filename: some_nonexistent_template.md.tpl

View File

@@ -0,0 +1,6 @@
tembo:
scopes:
- name: some_scope
path: "some_scope"
filename: "{name}"
extension: md

View File

@@ -0,0 +1,16 @@
tembo:
scopes:
- name: some_scope
example: tembo new some_scope
path: "some_scope"
filename: "{name}"
extension: md
- name: some_scope_no_example
path: "some_scope"
filename: "{name}"
extension: md
- name: another_some_scope
example: tembo new another_some_scope
path: "another_some_scope"
filename: "{name}"
extension: md

View File

@@ -0,0 +1 @@
already exists

303
tests/test_cli/test_cli.py Normal file
View File

@@ -0,0 +1,303 @@
import importlib
import os
import pathlib
import pytest
import tembo.cli
from tembo.cli.cli import new, list_all
def test_new_dry_run(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)
scope = "some_scope"
dry_run = "--dry-run"
# act
with pytest.raises(SystemExit) as system_exit:
new([scope, dry_run])
# assert
assert system_exit.value.code == 0
assert (
capsys.readouterr().out
== f"[TEMBO] {tmpdir}/some_scope/some_scope.md will be created 🐘\n"
)
# cleanup
del os.environ["TEMBO_CONFIG"]
del os.environ["TEMBO_BASE_PATH"]
def test_new_success(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)
scoped_page_file = pathlib.Path(tmpdir / "some_scope" / "some_scope").with_suffix(
".md"
)
# act
with pytest.raises(SystemExit) as system_exit:
new(["some_scope"])
# assert
assert scoped_page_file.exists()
assert system_exit.value.code == 0
assert capsys.readouterr().out == f"[TEMBO] Saved {scoped_page_file} to disk 🐘\n"
# cleanup
del os.environ["TEMBO_CONFIG"]
del os.environ["TEMBO_BASE_PATH"]
def test_new_success_already_exists(shared_datadir, capsys):
# arrange
os.environ["TEMBO_CONFIG"] = str(shared_datadir / "config" / "success")
os.environ["TEMBO_BASE_PATH"] = str(shared_datadir)
importlib.reload(tembo.cli)
scoped_page_file = pathlib.Path(
shared_datadir / "some_scope" / "some_scope"
).with_suffix(".md")
# act
with pytest.raises(SystemExit) as system_exit:
new(["some_scope"])
# assert
assert scoped_page_file.exists()
assert system_exit.value.code == 0
assert (
capsys.readouterr().out == f"[TEMBO] File {scoped_page_file} already exists 🐘\n"
)
# cleanup
del os.environ["TEMBO_CONFIG"]
del os.environ["TEMBO_BASE_PATH"]
def test_new_scope_not_found(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)
scoped_page_file = pathlib.Path(tmpdir / "some_scope" / "some_scope").with_suffix(
".md"
)
# act
with pytest.raises(SystemExit) as system_exit:
new(["some_nonexistent_scope"])
# assert
assert not scoped_page_file.exists()
assert system_exit.value.code == 1
assert (
capsys.readouterr().out
== "[TEMBO] Scope some_nonexistent_scope not found in config.yml 🐘\n"
)
# cleanup
del os.environ["TEMBO_CONFIG"]
del os.environ["TEMBO_BASE_PATH"]
def test_new_empty_config(shared_datadir, tmpdir, capsys):
# arrange
os.environ["TEMBO_CONFIG"] = str(shared_datadir / "config" / "empty")
os.environ["TEMBO_BASE_PATH"] = str(tmpdir)
importlib.reload(tembo.cli)
# act
with pytest.raises(SystemExit) as system_exit:
new(["some_nonexistent_scope"])
# assert
assert system_exit.value.code == 1
assert (
capsys.readouterr().out
== f"[TEMBO] Config.yml found in {shared_datadir}/config/empty is empty 🐘\n"
)
# cleanup
del os.environ["TEMBO_CONFIG"]
del os.environ["TEMBO_BASE_PATH"]
def test_new_missing_config(shared_datadir, tmpdir, capsys):
# arrange
os.environ["TEMBO_CONFIG"] = str(shared_datadir / "config" / "missing")
os.environ["TEMBO_BASE_PATH"] = str(tmpdir)
importlib.reload(tembo.cli)
# act
with pytest.raises(SystemExit) as system_exit:
new(["some_nonexistent_scope"])
# assert
assert system_exit.value.code == 1
assert (
capsys.readouterr().out
== f"[TEMBO] No config.yml found in {shared_datadir}/config/missing 🐘\n"
)
# cleanup
del os.environ["TEMBO_CONFIG"]
del os.environ["TEMBO_BASE_PATH"]
def test_new_missing_mandatory_key(shared_datadir, tmpdir, capsys):
# arrange
os.environ["TEMBO_CONFIG"] = str(shared_datadir / "config" / "missing_keys")
os.environ["TEMBO_BASE_PATH"] = str(tmpdir)
importlib.reload(tembo.cli)
# act
with pytest.raises(SystemExit) as system_exit:
new(["some_scope"])
# assert
assert system_exit.value.code == 1
assert (
capsys.readouterr().out == "[TEMBO] Key 'filename' not found in config.yml 🐘\n"
)
# cleanup
del os.environ["TEMBO_CONFIG"]
del os.environ["TEMBO_BASE_PATH"]
@pytest.mark.parametrize(
"path,message",
[
("success", "[TEMBO] Example for some_scope: tembo new some_scope 🐘\n"),
("optional_keys", "[TEMBO] No example in config.yml 🐘\n"),
],
)
def test_new_show_example(path, message, shared_datadir, capsys):
# arrange
os.environ["TEMBO_CONFIG"] = str(shared_datadir / "config" / path)
importlib.reload(tembo.cli)
# act
with pytest.raises(SystemExit) as system_exit:
new(["some_scope", "--example"])
# assert
assert system_exit.value.code == 0
assert capsys.readouterr().out == message
# cleanup
del os.environ["TEMBO_CONFIG"]
def test_new_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)
# act
with pytest.raises(SystemExit) as system_exit:
new(["some_scope"])
# assert
assert system_exit.value.code == 1
assert (
capsys.readouterr().out
== f"[TEMBO] Tembo base path of {tmpdir}/nonexistent/path does not exist. 🐘\n"
)
# cleanup
del os.environ["TEMBO_CONFIG"]
del os.environ["TEMBO_BASE_PATH"]
def test_new_template_file_does_not_exist(shared_datadir, tmpdir, capsys):
# arrange
os.environ["TEMBO_CONFIG"] = str(shared_datadir / "config" / "missing_template")
os.environ["TEMBO_BASE_PATH"] = str(tmpdir)
os.environ["TEMBO_TEMPLATE_PATH"] = str(tmpdir)
importlib.reload(tembo.cli)
# act
with pytest.raises(SystemExit) as system_exit:
new(["some_scope"])
# assert
assert (
capsys.readouterr().out
== f"[TEMBO] Template file {tmpdir}/some_nonexistent_template.md.tpl does not exist. 🐘\n"
)
assert system_exit.value.code == 1
# cleanup
del os.environ["TEMBO_CONFIG"]
del os.environ["TEMBO_TEMPLATE_PATH"]
def test_new_mismatched_tokens_with_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)
# act
with pytest.raises(SystemExit) as system_exit:
new(["some_scope", "input0", "input1"])
# assert
assert system_exit.value.code == 1
assert capsys.readouterr().out == "[TEMBO] Your tembo config.yml/template specifies 0 input tokens, you gave 2. Example: tembo new some_scope 🐘\n"
# cleanup
del os.environ["TEMBO_CONFIG"]
del os.environ["TEMBO_BASE_PATH"]
def test_new_mismatched_tokens_without_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)
# act
with pytest.raises(SystemExit) as system_exit:
new(["some_scope_no_example", "input0", "input1"])
# assert
assert system_exit.value.code == 1
assert capsys.readouterr().out == "[TEMBO] Your tembo config.yml/template specifies 0 input tokens, you gave 2 🐘\n"
# cleanup
del os.environ["TEMBO_CONFIG"]
del os.environ["TEMBO_BASE_PATH"]
def test_list_all_success(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)
scoped_page_file = pathlib.Path(tmpdir / "some_scope" / "some_scope").with_suffix(
".md"
)
# act
with pytest.raises(SystemExit) as system_exit:
list_all([])
# assert
assert system_exit.value.code == 0
assert (
capsys.readouterr().out
== "[TEMBO] 3 names found in config.yml: 'some_scope', 'some_scope_no_example', 'another_some_scope' 🐘\n"
)
# cleanup
del os.environ["TEMBO_CONFIG"]
del os.environ["TEMBO_BASE_PATH"]

View File

@@ -0,0 +1,388 @@
from datetime import date
import pathlib
import pytest
from tembo import PageCreatorOptions, ScopedPageCreator
from tembo import exceptions
from tembo.utils import Success
DATE_TODAY = date.today().strftime("%d-%m-%Y")
def test_create_page_base_path_does_not_exist(tmpdir):
# arrange
base_path = str(tmpdir / "nonexistent" / "path")
options = PageCreatorOptions(
base_path=base_path,
page_path="",
filename="",
extension="",
name="",
user_input=None,
example=None,
template_filename=None,
template_path=None,
)
# act
with pytest.raises(
exceptions.BasePathDoesNotExistError
) as base_path_does_not_exist_error:
scoped_page = ScopedPageCreator(options).create_page()
# 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="some_path",
filename="some_filename",
extension="some_extension",
name="some_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(options).create_page()
# 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(options).create_page()
with pytest.raises(exceptions.ScopedPageAlreadyExists) as page_already_exists:
result = 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):
# 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,
)
scoped_page_file = (
pathlib.Path(options.base_path) / options.page_path / options.filename
).with_suffix(f".{options.extension}")
# act
scoped_page = ScopedPageCreator(options).create_page()
result = scoped_page.save_to_disk()
# assert
assert scoped_page_file.exists()
assert isinstance(result, Success)
assert result.message == str(scoped_page_file)
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(options).create_page()
result = scoped_page.save_to_disk()
# assert
assert scoped_page_file.exists()
assert isinstance(result, Success)
assert result.message == str(scoped_page_file)
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(options).create_page()
result = scoped_page.save_to_disk()
# assert
assert scoped_page_file.exists()
assert isinstance(result, Success)
assert result.message == str(scoped_page_file)
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(options).create_page()
result = scoped_page.save_to_disk()
# assert
assert scoped_page_file.exists()
assert isinstance(result, Success)
assert result.message == str(scoped_page_file)
def test_create_tokened_page_input_tokens_preserve_order(datadir, caplog):
# arrange
tokened_filename = "input_token_fourth_input_first_input"
options = PageCreatorOptions(
base_path=str(datadir),
page_path="some_path",
filename="input_token_{input3}_{input0}",
extension="md",
name="some_name",
user_input=("first_input", "second_input", "third_input", "fourth_input"),
example=None,
template_filename="some_template_input_tokens_preserve_order.md.tpl",
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(options).create_page()
result = scoped_page.save_to_disk()
# assert
assert scoped_page_file.exists()
assert isinstance(result, Success)
assert result.message == str(scoped_page_file)
with scoped_page_file.open(mode="r", encoding="utf-8") as scoped_page_contents:
assert scoped_page_contents.readline() == "third_input second_input"
@pytest.mark.parametrize(
"user_input,expected,given",
[
(None, 3, 0),
(("first_input", "second_input"), 3, 2),
(("first_input", "second_input", "third_input", "fourth_input"), 3, 4),
],
)
def test_create_page_mismatched_tokens(tmpdir, user_input, expected, given):
# arrange
options = PageCreatorOptions(
base_path=str(tmpdir),
page_path="some_path",
filename="input_token_{input0}_{input1}_{input2}",
extension="md",
name="some_name",
user_input=user_input,
example=None,
template_filename=None,
template_path=None,
)
# act
with pytest.raises(exceptions.MismatchedTokenError) as mismatched_token_error:
scoped_page = ScopedPageCreator(options).create_page()
# assert
assert mismatched_token_error.value.expected == expected
assert mismatched_token_error.value.given == given
def test_create_page_spaces_in_path(tmpdir, caplog):
# arrange
options = PageCreatorOptions(
base_path=str(tmpdir),
page_path="some path with a space",
filename="some filename with a space",
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.replace(" ", "_")
/ options.filename.replace(" ", "_")
).with_suffix(f".{options.extension}")
# act
scoped_page = ScopedPageCreator(options).create_page()
result = scoped_page.save_to_disk()
# assert
assert scoped_page_file.exists()
assert isinstance(result, Success)
assert result.message == str(scoped_page_file)
def test_create_page_dot_in_extension(tmpdir, caplog):
# arrange
options = PageCreatorOptions(
base_path=str(tmpdir),
page_path="some_path",
filename="some_filename",
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[1:]}")
# act
scoped_page = ScopedPageCreator(options).create_page()
result = scoped_page.save_to_disk()
# assert
assert scoped_page_file.exists()
assert isinstance(result, Success)
assert result.message == str(scoped_page_file)
def test_create_page_str_representation(tmpdir):
# arrange
options = PageCreatorOptions(
base_path=str(tmpdir),
page_path="some_path",
filename="some_filename",
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(options).create_page()
# assert
assert str(scoped_page) == f"ScopedPage(\"{scoped_page_file}\")"

View File

@@ -0,0 +1 @@
some date token: {d:%d-%m-%Y}

View File

@@ -0,0 +1 @@
some input tokens {input1} {input0}

View File

@@ -0,0 +1 @@
some name token {name}

View File

@@ -0,0 +1,3 @@
scoped page file
no tokens

View File

@@ -0,0 +1 @@
this file already exists