Skip to content

Commit

Permalink
add 'pygrep' lint group
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleades committed Feb 6, 2024
1 parent 5aa0273 commit 421562f
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 114 deletions.
2 changes: 1 addition & 1 deletion copier/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def printf(
if not style:
return action + _msg

out = style + [action] + Style.RESET + [INDENT, _msg] # type: ignore
out = style + [action] + Style.RESET + [INDENT, _msg] # type: ignore[operator]
print(*out, sep="", file=file_)
return None # HACK: Satisfy MyPy

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ style = "pep440"
vcs = "git"

[tool.ruff.lint]
extend-select = ["B", "D", "E", "F", "I", "UP"]
extend-select = ["B", "D", "E", "F", "I", "PGH", "UP"]
extend-ignore = ['B028', "B904", "D105", "D107", "E501"]

[tool.ruff.lint.per-file-ignores]
Expand Down
4 changes: 2 additions & 2 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def test_invalid_config_data(
) -> None:
template = Template(conf_path)
with pytest.raises(InvalidConfigFileError):
template.config_data # noqa: B018
template.config_data # noqa: B018
if check_err:
_, err = capsys.readouterr()
assert check_err(err)
Expand Down Expand Up @@ -198,7 +198,7 @@ def test_config_data_empty() -> None:
def test_multiple_config_file_error() -> None:
template = Template("tests/demo_multi_config")
with pytest.raises(MultipleConfigFilesError):
template.config_data # noqa: B018
template.config_data # noqa: B018


# ConfigData
Expand Down
190 changes: 80 additions & 110 deletions tests/test_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,21 @@ def test_project_not_found(tmp_path: Path) -> None:

def test_copy_with_non_version_tags(tmp_path_factory: pytest.TempPathFactory) -> None:
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
build_file_tree(
{
(src / ".copier-answers.yml.jinja"): (
"""\
build_file_tree({
(src / ".copier-answers.yml.jinja"): (
"""\
# Changes here will be overwritten by Copier
{{ _copier_answers|to_nice_yaml }}
"""
),
(src / "copier.yaml"): (
"""\
),
(src / "copier.yaml"): (
"""\
_tasks:
- cat v1.txt
"""
),
(src / "v1.txt"): "file only in v1",
}
)
),
(src / "v1.txt"): "file only in v1",
})
with local.cwd(src):
git("init")
git("add", ".")
Expand Down Expand Up @@ -91,14 +89,12 @@ def test_tag_autodetect_v_prefix_optional(
) -> None:
"""Tags with and without `v` prefix should work the same."""
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
build_file_tree(
{
(src / "version.txt"): "1",
(
src / "{{_copier_conf.answers_file}}.jinja"
): "{{ _copier_answers|to_nice_yaml -}}",
}
)
build_file_tree({
(src / "version.txt"): "1",
(
src / "{{_copier_conf.answers_file}}.jinja"
): "{{ _copier_answers|to_nice_yaml -}}",
})
# One stable tag
git_save(src, tag=f"{prefix}1.2.3")
# One pre or post release tag
Expand All @@ -123,23 +119,21 @@ def test_copy_with_non_version_tags_and_vcs_ref(
tmp_path_factory: pytest.TempPathFactory,
) -> None:
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
build_file_tree(
{
(src / ".copier-answers.yml.jinja"): (
"""\
build_file_tree({
(src / ".copier-answers.yml.jinja"): (
"""\
# Changes here will be overwritten by Copier
{{ _copier_answers|to_nice_yaml }}
"""
),
(src / "copier.yaml"): (
"""\
),
(src / "copier.yaml"): (
"""\
_tasks:
- cat v1.txt
"""
),
(src / "v1.txt"): "file only in v1",
}
)
),
(src / "v1.txt"): "file only in v1",
})
with local.cwd(src):
git("init")
git("add", ".")
Expand Down Expand Up @@ -264,15 +258,13 @@ def test_exclude_extends(tmp_path_factory: pytest.TempPathFactory) -> None:
def test_exclude_replaces(tmp_path_factory: pytest.TempPathFactory) -> None:
"""Exclude in copier.yml replaces default values."""
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
build_file_tree(
{
src / "test.txt": "Test text",
src / "test.json": '"test json"',
src / "test.yaml": '"test yaml"',
src / "copier.yaml.jinja": "purpose: template inception",
src / "copier.yml": "_exclude: ['*.json']",
}
)
build_file_tree({
src / "test.txt": "Test text",
src / "test.json": '"test json"',
src / "test.yaml": '"test yaml"',
src / "copier.yaml.jinja": "purpose: template inception",
src / "copier.yml": "_exclude: ['*.json']",
})
copier.run_copy(str(src), dst, exclude=["*.txt"])
assert (dst / "test.yaml").is_file()
assert not (dst / "test.txt").exists()
Expand Down Expand Up @@ -406,12 +398,10 @@ def test_preserved_permissions(

def test_commit_hash(tmp_path_factory: pytest.TempPathFactory) -> None:
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
build_file_tree(
{
src / "commit.jinja": "{{_copier_conf.vcs_ref_hash}}",
src / "tag.jinja": "{{_copier_answers._commit}}",
}
)
build_file_tree({
src / "commit.jinja": "{{_copier_conf.vcs_ref_hash}}",
src / "tag.jinja": "{{_copier_answers._commit}}",
})
with local.cwd(src):
git("init")
git("add", "-A")
Expand All @@ -425,13 +415,11 @@ def test_commit_hash(tmp_path_factory: pytest.TempPathFactory) -> None:

def test_value_with_forward_slash(tmp_path_factory: pytest.TempPathFactory) -> None:
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
build_file_tree(
{
(src / "{{ filename.replace('.', _copier_conf.sep) }}.txt"): (
"This is template."
),
}
)
build_file_tree({
(src / "{{ filename.replace('.', _copier_conf.sep) }}.txt"): (
"This is template."
),
})
copier.run_copy(str(src), dst, data={"filename": "a.b.c"})
assert (dst / "a" / "b" / "c.txt").read_text() == "This is template."

Expand Down Expand Up @@ -465,8 +453,8 @@ def test_value_with_forward_slash(tmp_path_factory: pytest.TempPathFactory) -> N
({"type": "str"}, True, does_not_raise()),
({"type": "str"}, False, does_not_raise()),
({"type": "str"}, Decimal(1.1), does_not_raise()),
({"type": "str"}, Enum("A", ["a", "b"], type=str).a, does_not_raise()), # type: ignore
({"type": "str"}, Enum("A", ["a", "b"]).a, pytest.raises(ValueError)), # type: ignore
({"type": "str"}, Enum("A", ["a", "b"], type=str).a, does_not_raise()), # type: ignore[attr-defined]
({"type": "str"}, Enum("A", ["a", "b"]).a, pytest.raises(ValueError)), # type: ignore[attr-defined]
({"type": "str"}, object(), pytest.raises(ValueError)),
({"type": "str"}, {}, pytest.raises(ValueError)),
({"type": "str"}, [], pytest.raises(ValueError)),
Expand Down Expand Up @@ -707,17 +695,13 @@ def test_validate_init_data(
expected: ContextManager[None],
) -> None:
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
build_file_tree(
{
(src / "copier.yml"): yaml.dump(
{
"_envops": BRACKET_ENVOPS,
"_templates_suffix": ".jinja",
"q": spec,
}
),
}
)
build_file_tree({
(src / "copier.yml"): yaml.dump({
"_envops": BRACKET_ENVOPS,
"_templates_suffix": ".jinja",
"q": spec,
}),
})
with expected:
copier.run_copy(str(src), dst, data={"q": value})

Expand All @@ -727,10 +711,9 @@ def test_validate_init_data_with_skipped_question(
tmp_path_factory: pytest.TempPathFactory, defaults: bool
) -> None:
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
build_file_tree(
{
(src / "copier.yml"): (
f"""\
build_file_tree({
(src / "copier.yml"): (
f"""\
_envops: {BRACKET_ENVOPS_JSON}
kind:
Expand All @@ -750,16 +733,15 @@ def test_validate_init_data_with_skipped_question(
type: str
help: any string foo?
"""
),
(src / "result.jinja"): (
"""\
),
(src / "result.jinja"): (
"""\
[[ kind ]]
[[ testbar ]]
[[ testfoo ]]
"""
),
}
)
),
})
copier.run_copy(
str(src), dst, defaults=defaults, data={"kind": "foo", "testfoo": "helloworld"}
)
Expand All @@ -774,17 +756,13 @@ def test_required_question_without_data(
tmp_path_factory: pytest.TempPathFactory, type_name: str
) -> None:
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
build_file_tree(
{
(src / "copier.yml"): yaml.safe_dump(
{
"question": {
"type": type_name,
}
}
)
}
)
build_file_tree({
(src / "copier.yml"): yaml.safe_dump({
"question": {
"type": type_name,
}
})
})
with pytest.raises(ValueError, match='Question "question" is required'):
copier.run_copy(str(src), dst, defaults=True)

Expand All @@ -803,18 +781,14 @@ def test_required_choice_question_without_data(
tmp_path_factory: pytest.TempPathFactory, type_name: str, choices: List[Any]
) -> None:
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
build_file_tree(
{
(src / "copier.yml"): yaml.safe_dump(
{
"question": {
"type": type_name,
"choices": choices,
}
}
)
}
)
build_file_tree({
(src / "copier.yml"): yaml.safe_dump({
"question": {
"type": type_name,
"choices": choices,
}
})
})
with pytest.raises(ValueError, match='Question "question" is required'):
copier.run_copy(str(src), dst, defaults=True)

Expand Down Expand Up @@ -886,17 +860,13 @@ def test_validate_default_value(
expected: ContextManager[None],
) -> None:
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
build_file_tree(
{
(src / "copier.yml"): yaml.dump(
{
"q": {
"type": type_name,
"default": default,
}
}
)
}
)
build_file_tree({
(src / "copier.yml"): yaml.dump({
"q": {
"type": type_name,
"default": default,
}
})
})
with expected:
copier.run_copy(str(src), dst, defaults=True)

0 comments on commit 421562f

Please sign in to comment.