-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ole Mussmann
committed
Oct 19, 2023
1 parent
9dfe0a9
commit a4b1175
Showing
9 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "test/templates/mkdocs"] | ||
path = test/templates/mkdocs | ||
url = [email protected]:entangled/template-mkdocs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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? | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Minimal Template for Testing | ||
[![Entangled badge](https://img.shields.io/badge/entangled-Use%20the%20source!-%2300aeff)](https://entangled.github.io/) |
2 changes: 2 additions & 0 deletions
2
test/templates/minimal/template/{{_copier_conf.answers_file}}.jinja
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Changes here will be overwritten by Copier | ||
{{ _copier_answers|to_nice_yaml -}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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? | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Minimal Template for Testing | ||
[![Entangled badge](https://img.shields.io/badge/entangled-Use%20the%20source!-%2300aeff)](https://entangled.github.io/) |
2 changes: 2 additions & 0 deletions
2
test/templates/minimal_unsafe/template/{{_copier_conf.answers_file}}.jinja
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Changes here will be overwritten by Copier | ||
{{ _copier_answers|to_nice_yaml -}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
) |