Skip to content

Commit

Permalink
✨ Allowing multiple path arguments. Fixes usage with pre-commit (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
danygielow authored Nov 1, 2021
1 parent b533e31 commit f0d5f94
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
.venv/
.pytest_cache/
__pycache__/
.idea/
dist/
/.coverage

1 change: 1 addition & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
name: auto-optional
description: Adds the Optional type-hint to arguments where the default value is None
entry: auto-optional
require_serial: True
language: python
types: [python]
7 changes: 5 additions & 2 deletions src/auto_optional/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from pathlib import Path
from typing import List

import typer

Expand All @@ -8,8 +9,10 @@


@app.command()
def main(path: Path = typer.Argument(".")) -> None:
converted_files = convert_path(path)
def main(path: List[Path] = typer.Argument(None)) -> None:
if not path:
path = [Path(".")]
converted_files = sum(convert_path(p) for p in path)
if converted_files:
typer.echo(f"{converted_files} were fixed.")
else:
Expand Down
78 changes: 78 additions & 0 deletions test/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@
from pathlib import Path
from typing import List, Union

import py
import pytest
from pydantic import BaseModel
from typer.testing import CliRunner
from yaml import load

from auto_optional.file_handling import convert_file
from auto_optional.main import app

try:
from yaml import CLoader as YamlLoader
except ImportError:
from yaml import YamlLoader # type: ignore


runner = CliRunner()


class BaseTestConfig(BaseModel, ABC):
name: str

Expand Down Expand Up @@ -44,6 +50,39 @@ def get_singe_file_tests() -> List:
SINGLE_FILE_TESTS = get_singe_file_tests()


def prepare_file(
test_config: Union[BeforeAndAfterTest, UnchangedTest], tmpdir: Path
) -> Path:
if isinstance(test_config, BeforeAndAfterTest):
code = test_config.before
elif isinstance(test_config, UnchangedTest):
code = test_config.unchanged
else:
raise NotImplementedError(
f"tests of type {type(test_config)} are not yet supported"
)

code_path = tmpdir / f"{hash(code)}.py"
code_path.write_text(code)
return code_path


def assert_code_processed_correctly(
test_config: Union[BeforeAndAfterTest, UnchangedTest],
path: Path,
) -> None:
if isinstance(test_config, BeforeAndAfterTest):
code = test_config.after
elif isinstance(test_config, UnchangedTest):
code = test_config.unchanged
else:
raise NotImplementedError(
f"tests of type {type(test_config)} are not yet supported"
)

assert path.read_text() == code


@pytest.mark.parametrize(
"test_config",
SINGLE_FILE_TESTS,
Expand All @@ -60,3 +99,42 @@ def test_single_files_from_yaml(
raise NotImplementedError(
f"tests of type {type(test_config)} are not yet supported"
)


@pytest.mark.parametrize(
"test_config",
SINGLE_FILE_TESTS,
ids=[test.name for test in SINGLE_FILE_TESTS],
)
def test_cli_single_files_from_yaml(
test_config: Union[BeforeAndAfterTest, UnchangedTest], tmpdir: py.path.local
) -> None:
single_code_file_path = prepare_file(test_config, Path(tmpdir))
runner.invoke(app, [str(single_code_file_path)])
assert_code_processed_correctly(test_config, single_code_file_path)


@pytest.mark.parametrize(
"test_config",
SINGLE_FILE_TESTS,
ids=[test.name for test in SINGLE_FILE_TESTS],
)
def test_cli_no_file_should_use_current_dir(
test_config: Union[BeforeAndAfterTest, UnchangedTest], tmpdir: py.path.local
) -> None:
tmpdir.chdir()
single_code_file_path = prepare_file(test_config, Path(tmpdir))
runner.invoke(app)
assert_code_processed_correctly(test_config, single_code_file_path)


def test_cli_multi_file(tmpdir: py.path.local) -> None:
test_with_paths = [
(test_config, prepare_file(test_config, Path(tmpdir)))
for test_config in SINGLE_FILE_TESTS
]

runner.invoke(app, [str(path) for _, path in test_with_paths])

for test_config, path in test_with_paths:
assert_code_processed_correctly(test_config, path)

0 comments on commit f0d5f94

Please sign in to comment.