Skip to content

Commit

Permalink
CLI to read config from pyproject.toml using maison
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasha10 committed Nov 21, 2021
1 parent 02b969e commit 03e31dd
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@
[console_scripts]
autoimport=autoimport.entrypoints.cli:cli
""",
install_requires=["autoflake", "Click", "pyprojroot", "sh"],
install_requires=["autoflake", "Click", "pyprojroot", "sh", "maison"],
)
4 changes: 3 additions & 1 deletion src/autoimport/entrypoints/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Tuple

import click
from maison.config import ProjectConfig

from autoimport import services, version

Expand All @@ -15,8 +16,9 @@
@click.argument("files", type=click.File("r+"), nargs=-1)
def cli(files: Tuple[str]) -> None:
"""Corrects the source code of the specified files."""
config = ProjectConfig(project_name="autoimport").to_dict()
try:
fixed_code = services.fix_files(files)
fixed_code = services.fix_files(files, config)
except FileNotFoundError as error:
log.error(error)

Expand Down
4 changes: 2 additions & 2 deletions src/autoimport/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from autoimport.model import SourceCode


def fix_files(files: Tuple[TextIOWrapper]) -> Optional[str]:
def fix_files(files: Tuple[TextIOWrapper], config: Dict[str, Any]) -> Optional[str]:
"""Fix the python source code of a list of files.
If the input is taken from stdin, it will output the value to stdout.
Expand All @@ -24,7 +24,7 @@ def fix_files(files: Tuple[TextIOWrapper]) -> Optional[str]:
"""
for file_wrapper in files:
source = file_wrapper.read()
fixed_source = fix_code(source)
fixed_source = fix_code(source, config)

try:
# Click testing runner doesn't simulate correctly the reading from stdin
Expand Down
28 changes: 28 additions & 0 deletions tests/e2e/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,31 @@ def test_corrects_code_from_stdin(runner: CliRunner) -> None:

assert result.exit_code == 0
assert result.stdout == fixed_source


def test_pyproject_common_statements(runner: CliRunner, tmpdir: LocalPath) -> None:
"""Allow common_statements to be defined in pyproject.toml"""
pyproject_toml = tmpdir.join("pyproject.toml") # type: ignore
pyproject_toml.write(
dedent(
"""\
[tool.autoimport]
common_statements = { "FooBar" = "from baz_qux import FooBar" }
"""
)
)
test_file = tmpdir.join("source.py") # type: ignore
test_file.write("FooBar\n")
fixed_source = dedent(
"""\
from baz_qux import FooBar
FooBar
"""
)
with tmpdir.as_cwd():

result = runner.invoke(cli, [str(test_file)])

assert result.exit_code == 0
assert test_file.read() == fixed_source

0 comments on commit 03e31dd

Please sign in to comment.