copier/tests/helpers.py
Jairo Llopis 0441b86f0c
Refactor (#314)
* Refactor
* Fix #110.
* Rewrite test_config_exclude, test_config_exclude_overridden and test_config_include. These tests were badly designed, using a monkeypatch that would never happen in the real world, and actually producing false positives. I moved them to test_exclude.py and rewritten to test the what and not the how.
* Fix #214 by removing skip option. Relevant tests use the better skip_if_exists=["**"].
* Remove subdirectory flag from API/CLI. It was confusing and could lead to bad maintenance situations. Fixes #315.
* Remove extra_paths and fix #321
* Remember that you cannot use _copier_conf.src_path as a path now
* use dataclasses
* Create errors module, simplify some tests, fix many others
* Fix some tests, complete EnvOps removal
* Fix #214 and some tests related to it
* Reorder code
* Update docs and imports
* Modularize test_complex_questions
* Interlink worker and questionary a bit better
* Removal of Questionary class, which only had 1 meaningful method that is now merged into Worker to avoid circular dependencies.
* Fix #280 in a simple way: only user answers are type-casted inside API, and CLI transforms all `--data` using YAML always. Predictable.
* Use prereleases correctly.
* Reorder AnswersMap to have a more significative repr.
* Simpler cache for old `Question.get_choices()` method (renamed now).
* fix wrong test
* Fix test_subdirectory
* Fix test_tasks (and remove tests/demo_tasks)
* Fix path filter tests, and move it to test_exclude, where it belongs
* Make test_config pass
* Fix more wrongly designed tests
* Use cached_property backport if needed
* xfail test known to fail on mac
* force posix paths on windows
* Add typing_extensions for python < 3.8
* Sort dependencies in pyproject.toml
* Support python 3.6 str-to-datetime conversion
* Workaround https://bugs.python.org/issue43095
* xfail test_path_filter on windows
* Upgrade mkdocs and other dependencies to fix https://github.com/pawamoy/mkdocstrings/issues/222
* Add missing reference docs.
* Add workaround for https://github.com/pawamoy/mkdocstrings/pull/209
* Docs.
* Remove validators module
* Add workaround for https://github.com/pawamoy/mkdocstrings/issues/225
* Restore docs autorefs as explained in https://github.com/pawamoy/mkdocstrings/issues/226#issuecomment-775413562.
* Workaround https://github.com/pawamoy/pytkdocs/issues/86
2021-02-09 18:22:47 +00:00

84 lines
2.4 KiB
Python

import filecmp
import os
import sys
import textwrap
from enum import Enum
from hashlib import sha1
from pathlib import Path
from typing import Dict
from plumbum import local
from prompt_toolkit.input.ansi_escape_sequences import REVERSE_ANSI_SEQUENCES
from prompt_toolkit.keys import Keys
import copier
from copier.types import StrOrPath
PROJECT_TEMPLATE = Path(__file__).parent / "demo"
DATA = {
"py3": True,
"make_secret": lambda: sha1(os.urandom(48)).hexdigest(),
"myvar": "awesome",
"what": "world",
"project_name": "Copier",
"version": "2.0.0",
"description": "A library for rendering projects templates",
}
COPIER_CMD = local.get(
# Allow debugging in VSCode
# HACK https://github.com/microsoft/vscode-python/issues/14222
str(Path(sys.executable).parent / "copier.cmd"),
str(Path(sys.executable).parent / "copier"),
# Poetry installs the executable as copier.cmd in Windows
"copier.cmd",
"copier",
)
COPIER_PATH = str(COPIER_CMD.executable)
# Helper to parse back an iso-formatted datetime; needed for py3.6, where
# datetime.datetime.fromisoformat() doesn't exist
ISOFORMAT = "%Y-%m-%d %H:%M:%S.%f"
class Keyboard(str, Enum):
ControlH = REVERSE_ANSI_SEQUENCES[Keys.ControlH]
Enter = "\r"
Esc = REVERSE_ANSI_SEQUENCES[Keys.Escape]
Home = REVERSE_ANSI_SEQUENCES[Keys.Home]
End = REVERSE_ANSI_SEQUENCES[Keys.End]
Up = REVERSE_ANSI_SEQUENCES[Keys.Up]
Down = REVERSE_ANSI_SEQUENCES[Keys.Down]
Right = REVERSE_ANSI_SEQUENCES[Keys.Right]
Left = REVERSE_ANSI_SEQUENCES[Keys.Left]
# Equivalent keystrokes in terminals; see python-prompt-toolkit for
# further explanations
Alt = Esc
Backspace = ControlH
def render(tmp_path, **kwargs):
kwargs.setdefault("quiet", True)
copier.copy(str(PROJECT_TEMPLATE), tmp_path, data=DATA, **kwargs)
def assert_file(tmp_path, *path):
p1 = os.path.join(str(tmp_path), *path)
p2 = os.path.join(str(PROJECT_TEMPLATE), *path)
assert filecmp.cmp(p1, p2)
def build_file_tree(spec: Dict[StrOrPath, str], dedent: bool = True):
"""Builds a file tree based on the received spec."""
for path, contents in spec.items():
path = Path(path)
if dedent:
contents = textwrap.dedent(contents)
path.parent.mkdir(parents=True, exist_ok=True)
with path.open("w") as fd:
fd.write(contents)