From a4b1175a76c74a3f0124689b83d740ef3d4c1358 Mon Sep 17 00:00:00 2001 From: Ole Mussmann Date: Thu, 19 Oct 2023 15:47:50 +0200 Subject: [PATCH] test `entangled new` command --- .gitmodules | 3 + test/templates/minimal/copier.yml | 9 ++ test/templates/minimal/template/README.md | 2 + .../{{_copier_conf.answers_file}}.jinja | 2 + test/templates/minimal_unsafe/copier.yml | 12 ++ .../minimal_unsafe/template/README.md | 2 + .../{{_copier_conf.answers_file}}.jinja | 2 + test/templates/mkdocs | 1 + test/test_new.py | 133 ++++++++++++++++++ 9 files changed, 166 insertions(+) create mode 100644 .gitmodules create mode 100644 test/templates/minimal/copier.yml create mode 100644 test/templates/minimal/template/README.md create mode 100644 test/templates/minimal/template/{{_copier_conf.answers_file}}.jinja create mode 100644 test/templates/minimal_unsafe/copier.yml create mode 100644 test/templates/minimal_unsafe/template/README.md create mode 100644 test/templates/minimal_unsafe/template/{{_copier_conf.answers_file}}.jinja create mode 160000 test/templates/mkdocs create mode 100644 test/test_new.py diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..430b2f2 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "test/templates/mkdocs"] + path = test/templates/mkdocs + url = git@github.com:entangled/template-mkdocs diff --git a/test/templates/minimal/copier.yml b/test/templates/minimal/copier.yml new file mode 100644 index 0000000..01c4f87 --- /dev/null +++ b/test/templates/minimal/copier.yml @@ -0,0 +1,9 @@ +_subdirectory: template + +_message_after_copy: | + Your minimal project has been generated. + +project_name: + type: str + help: What is your project name? + diff --git a/test/templates/minimal/template/README.md b/test/templates/minimal/template/README.md new file mode 100644 index 0000000..5a2d441 --- /dev/null +++ b/test/templates/minimal/template/README.md @@ -0,0 +1,2 @@ +# Minimal Template for Testing +[![Entangled badge](https://img.shields.io/badge/entangled-Use%20the%20source!-%2300aeff)](https://entangled.github.io/) diff --git a/test/templates/minimal/template/{{_copier_conf.answers_file}}.jinja b/test/templates/minimal/template/{{_copier_conf.answers_file}}.jinja new file mode 100644 index 0000000..a96840d --- /dev/null +++ b/test/templates/minimal/template/{{_copier_conf.answers_file}}.jinja @@ -0,0 +1,2 @@ +# Changes here will be overwritten by Copier +{{ _copier_answers|to_nice_yaml -}} diff --git a/test/templates/minimal_unsafe/copier.yml b/test/templates/minimal_unsafe/copier.yml new file mode 100644 index 0000000..36eed46 --- /dev/null +++ b/test/templates/minimal_unsafe/copier.yml @@ -0,0 +1,12 @@ +_subdirectory: template + +_tasks: + - "git init" + +_message_after_copy: | + Your minimal project has been generated. + +project_name: + type: str + help: What is your project name? + diff --git a/test/templates/minimal_unsafe/template/README.md b/test/templates/minimal_unsafe/template/README.md new file mode 100644 index 0000000..5a2d441 --- /dev/null +++ b/test/templates/minimal_unsafe/template/README.md @@ -0,0 +1,2 @@ +# Minimal Template for Testing +[![Entangled badge](https://img.shields.io/badge/entangled-Use%20the%20source!-%2300aeff)](https://entangled.github.io/) diff --git a/test/templates/minimal_unsafe/template/{{_copier_conf.answers_file}}.jinja b/test/templates/minimal_unsafe/template/{{_copier_conf.answers_file}}.jinja new file mode 100644 index 0000000..a96840d --- /dev/null +++ b/test/templates/minimal_unsafe/template/{{_copier_conf.answers_file}}.jinja @@ -0,0 +1,2 @@ +# Changes here will be overwritten by Copier +{{ _copier_answers|to_nice_yaml -}} diff --git a/test/templates/mkdocs b/test/templates/mkdocs new file mode 160000 index 0000000..6a5729e --- /dev/null +++ b/test/templates/mkdocs @@ -0,0 +1 @@ +Subproject commit 6a5729e908099c4932df9808a498928e45c1e3b3 diff --git a/test/test_new.py b/test/test_new.py new file mode 100644 index 0000000..f5f0950 --- /dev/null +++ b/test/test_new.py @@ -0,0 +1,133 @@ +import pytest +import unittest + +from os.path import join +from typing import IO + +from entangled.commands.new import new +from entangled.config.templates import Template, templates +from entangled.errors.user import HelpfulUserError + + +def test_print_available_templates() -> None: + """Test if official template settings can be read""" + new(template="", project_path="", list_templates=True) + + +def test_pretend_template_copy() -> None: + """Test dry run""" + new( + template="./test/templates/minimal", + project_path="_", + data="project_name=my_project", + defaults=True, + pretend=True, + ) + + +def test_multiple_data() -> None: + """Test `-d/--data` syntax""" + new( + template="./test/templates/minimal", + project_path="_", + data="project_name=my_project;key=value;", + defaults=True, + pretend=True, + ) + + +def test_minimal_template_copy(tmp_path) -> None: + """Test if initializing a new project with a minimal template works""" + new( + template="./test/templates/minimal", + project_path=str(tmp_path), + data="project_name=my_project", + defaults=True, + ) + + +def test_untrusted_template_pass(tmp_path) -> None: + """Test if initializing a new project with an untrusted template fails""" + new( + template="./test/templates/minimal_unsafe", + project_path=str(tmp_path), + data="project_name=my_project", + defaults=True, + trust=True, + ) + + +def test_untrusted_template_fail(tmp_path) -> None: + """Test if initializing a new project with an untrusted template fails""" + with pytest.raises(HelpfulUserError, match="Template uses potentially unsafe"): + new( + template="./test/templates/minimal_unsafe", + project_path=str(tmp_path), + data="project_name=my_project", + defaults=True, + ) + + +def test_overwrite_template(tmp_path) -> None: + """Force overwrite a project with different data""" + new( + template="./test/templates/minimal", + project_path=str(tmp_path), + data="project_name=my_project", + defaults=True, + ) + new( + template="./test/templates/minimal", + project_path=str(tmp_path), + data="project_name=my_other_project", + defaults=True, + overwrite=True, + ) + + +def test_not_overwrite_template(tmp_path) -> None: + """Test that entangled will _not_ silently overwrite data""" + new( + trust=True, + data="project_name=my_project", + template="./test/templates/mkdocs", + project_path=str(tmp_path), + ) + # `copier` will ask for confirmation before overwriting + with pytest.raises( + HelpfulUserError, match="reading from stdin while output is captured" + ): + new( + trust=True, + data="project_name=my_other_project", + template="./test/templates/mkdocs", + project_path=str(tmp_path), + ) + + +def test_default_answers_file(tmp_path) -> None: + """Test pre-supplying answers to template questions""" + default_answers_file: str = join(tmp_path, ".copier-answers.yml") + f: IO + with open(default_answers_file, "w") as f: + f.write("project_name: default_project") + new( + template="./test/templates/minimal", + project_path=str(tmp_path), + defaults=True, + ) + + +def test_custom_answers_file(tmp_path) -> None: + """Test pre-supplying answers to template questions""" + my_answers_file_name = ".my-answers.yml" + my_answers_file: str = join(tmp_path, my_answers_file_name) + f: IO + with open(my_answers_file, "w") as f: + f.write("project_name: my_project") + new( + template="./test/templates/minimal", + project_path=str(tmp_path), + answers_file=my_answers_file_name, + defaults=True, + )