Skip to content

Commit

Permalink
test entangled new command
Browse files Browse the repository at this point in the history
  • Loading branch information
Ole Mussmann committed Oct 19, 2023
1 parent 9dfe0a9 commit a4b1175
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
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
9 changes: 9 additions & 0 deletions test/templates/minimal/copier.yml
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?

2 changes: 2 additions & 0 deletions test/templates/minimal/template/README.md
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/)
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 -}}
12 changes: 12 additions & 0 deletions test/templates/minimal_unsafe/copier.yml
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?

2 changes: 2 additions & 0 deletions test/templates/minimal_unsafe/template/README.md
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/)
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 -}}
1 change: 1 addition & 0 deletions test/templates/mkdocs
Submodule mkdocs added at 6a5729
133 changes: 133 additions & 0 deletions test/test_new.py
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,
)

0 comments on commit a4b1175

Please sign in to comment.