Skip to content

Commit

Permalink
test: create the ignored styles array only when suggesting styles
Browse files Browse the repository at this point in the history
  • Loading branch information
andreoliwa committed Nov 19, 2023
1 parent 44dcc9e commit 41a6393
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
8 changes: 6 additions & 2 deletions tests/helpers.py
Expand Up @@ -76,7 +76,7 @@ def tomlstring(value: Any) -> str:
class ProjectMock:
"""A mocked Python project to help on tests."""

def __init__(self, tmp_path: Path, **kwargs) -> None:
def __init__(self, tmp_path: Path, setup_py: bool = True, pyproject_toml: bool = False) -> None:
"""Create the root dir and make it the current dir (needed by NitpickChecker)."""
self._actual_violations: set[Fuss] = set()
self._flake8_errors: list[Flake8Error] = []
Expand All @@ -89,8 +89,10 @@ def __init__(self, tmp_path: Path, **kwargs) -> None:
self._remote_url: str | None = None
self.files_to_lint: list[Path] = []

if kwargs.get("setup_py", True):
if setup_py:
self.save_file("setup.py", "x = 1")
if pyproject_toml:
self.pyproject_toml("")

def create_symlink(self, link_name: str, target_dir: Path, target_file: str | None = None) -> ProjectMock:
"""Create a symlink to a target file.
Expand Down Expand Up @@ -432,6 +434,8 @@ def cli_init(
command_args.append("--suggest")
if style_urls:
command_args.extend(style_urls)
if exit_code is None:
exit_code = 1 if fix else 0
result, actual, expected = self._simulate_cli("init", expected_output, *command_args, exit_code=exit_code)
compare(actual=actual, expected=expected, prefix=f"Result: {result}")
return self
Expand Down
42 changes: 39 additions & 3 deletions tests/test_cli.py
Expand Up @@ -102,7 +102,7 @@ def test_create_update_tool_nitpick_table_on_config_files(
) -> None:
"""If no config file is found, create a basic .nitpick.toml."""
assert style_dir_with_types
project = ProjectMock(tmp_path, pyproject_toml=False, setup_py=True)
project = ProjectMock(tmp_path)
if pyproject_toml is None:
config_file = DOT_NITPICK_TOML
else:
Expand All @@ -117,7 +117,6 @@ def test_create_update_tool_nitpick_table_on_config_files(
""",
fix=fix,
suggest=True,
exit_code=1 if fix else 0,
)
if fix:
project.assert_file_contents(
Expand All @@ -138,5 +137,42 @@ def test_create_update_tool_nitpick_table_on_config_files(
assert not (tmp_path / DOT_NITPICK_TOML).exists()


# TODO(AA): test: create the ignored styles array only when suggesting styles
@pytest.mark.parametrize(
("fix", "footer"),
[
(
True,
f"The [{CONFIG_TOOL_NITPICK_KEY}] table was updated in {DOT_NITPICK_TOML!r}: 1 style appended. {EmojiEnum.STAR_CAKE.value}",
),
(
False,
f"Use --fix to append 1 style to the [{CONFIG_TOOL_NITPICK_KEY}] table in the config file '{DOT_NITPICK_TOML}'.",
),
],
)
def test_create_the_ignored_styles_array_only_when_suggesting_styles(tmp_path: Path, fix: bool, footer: str) -> None:
"""Create the ignored styles array only when suggesting styles."""
project = ProjectMock(tmp_path)
project.cli_init(
f"""
New styles:
- my-style.toml
{footer}
""",
fix=fix,
style_urls=["my-style.toml"],
)
if fix:
project.assert_file_contents(
DOT_NITPICK_TOML,
f"""
[{CONFIG_TOOL_NITPICK_KEY}]
style = [
"my-style.toml",]
""",
)
else:
assert not (tmp_path / DOT_NITPICK_TOML).exists()


# TODO(AA): test: style from CLI should be the first, before the suggested

0 comments on commit 41a6393

Please sign in to comment.