Skip to content

Commit

Permalink
Fix complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
yukihiko-shinoda committed May 25, 2020
1 parent 978b9ac commit d3852dc
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 51 deletions.
45 changes: 25 additions & 20 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pathlib import Path

from invoke import task # type: ignore
from invoke.runners import Failure # type: ignore
from invoke.runners import Failure, Result # type: ignore

ROOT_DIR = Path(__file__).parent
DOCS_DIR = ROOT_DIR.joinpath("docs")
Expand Down Expand Up @@ -34,30 +34,35 @@ def style(context, check=False):
"""
Format code
"""
python_dirs_string = " ".join(PYTHON_DIRS)
list_result = []
# Run isort
for result in [
isort(context, check),
pipenv_setup(context, check),
black(context, check),
]:
if result.failed:
raise Failure(result)


def isort(context, check=False) -> Result:
"""Runs isort."""
isort_options = "--recursive {}".format("--check-only --diff" if check else "")
list_result.append(
_run(
context, "isort {} {}".format(isort_options, python_dirs_string), warn=True
)
return _run(
context, "isort {} {}".format(isort_options, " ".join(PYTHON_DIRS)), warn=True
)
# Run pipenv-setup


def pipenv_setup(context, check=False) -> Result:
"""Runs pipenv-setup."""
isort_options = "{}".format("check --strict" if check else "sync --pipfile")
list_result.append(
_run(context, "pipenv-setup {}".format(isort_options), warn=True)
)
# Run black
return _run(context, "pipenv-setup {}".format(isort_options), warn=True)


def black(context, check=False) -> Result:
"""Runs black."""
black_options = "{}".format("--check --diff" if check else "")
list_result.append(
_run(
context, "black {} {}".format(black_options, python_dirs_string), warn=True
)
return _run(
context, "black {} {}".format(black_options, " ".join(PYTHON_DIRS)), warn=True
)
for result in list_result:
if result.failed:
raise Failure(result)


@task
Expand Down
39 changes: 24 additions & 15 deletions tests/test_bake_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
from contextlib import contextmanager
from pathlib import Path
from subprocess import PIPE
from typing import List

import pytest # type: ignore
from click.testing import CliRunner
from cookiecutter.utils import rmtree # type: ignore
from pytest_cookies.plugin import Result # type: ignore


@contextmanager
Expand Down Expand Up @@ -83,12 +85,15 @@ def test_bake_with_defaults(cookies):
assert result.project.isdir()
assert result.exit_code == 0
assert result.exception is None
check_toplevel_path_exist(
result, ["setup.py", "python_boilerplate", "tox.ini", "tests"]
)

found_toplevel_files = [f.basename for f in result.project.listdir()]
assert "setup.py" in found_toplevel_files
assert "python_boilerplate" in found_toplevel_files
assert "tox.ini" in found_toplevel_files
assert "tests" in found_toplevel_files

def check_toplevel_path_exist(result: Result, list_path: List[str]):
found_toplevel_files = list_files(result)
for path in list_path:
assert path in found_toplevel_files


def test_bake_and_run_tests(cookies):
Expand Down Expand Up @@ -147,18 +152,22 @@ def test_bake_without_author_file(cookies):
There should be no spaces in the toc tree.
"""
with bake_in_temp_dir(cookies, extra_context={"create_author_file": "n"}) as result:
found_toplevel_files = [f.basename for f in result.project.listdir()]
assert "AUTHORS.rst" not in found_toplevel_files
doc_files = [f.basename for f in result.project.join("docs").listdir()]
assert "authors.rst" not in doc_files
assert "AUTHORS.rst" not in list_files(result)
assert "authors.rst" not in list_files(result, ["docs"])
assert "contributing\n history" in read_text(result, "docs/index.rst")
assert "AUTHORS.rst" not in read_text(result, "MANIFEST.in")


def list_files(result: Result, directories: List[str] = None):
directories = [] if directories is None else directories
joined_path = result.project
for directory in directories:
joined_path = joined_path.join(directory)
return [f.basename for f in joined_path.listdir()]

docs_index_path = result.project.join("docs/index.rst")
with open(str(docs_index_path)) as index_file:
assert "contributing\n history" in index_file.read()

manifest_path = result.project.join("MANIFEST.in")
with open(str(manifest_path)) as manifest_file:
assert "AUTHORS.rst" not in manifest_file.read()
def read_text(result: Result, relative_path):
return Path(str(result.project.join(relative_path))).read_text()


@pytest.mark.parametrize(
Expand Down
35 changes: 19 additions & 16 deletions {{cookiecutter.project_slug}}/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import webbrowser

from invoke import task # type: ignore
from invoke.runners import Failure # type: ignore
from invoke.runners import Failure, Result # type: ignore


ROOT_DIR = Path(__file__).parent
Expand Down Expand Up @@ -44,24 +44,27 @@ def style(context, check=False):
"""
Format code
"""
python_dirs_string = " ".join(PYTHON_DIRS)
list_result = []
# Run isort
for result in [isort(context, check), pipenv_setup(context, check), black(context, check)]:
if result.failed:
raise Failure(result)


def isort(context, check=False) -> Result:
"""Runs isort."""
isort_options = "--recursive {}".format("--check-only --diff" if check else "")
list_result.append(
context.run("isort {} {}".format(isort_options, python_dirs_string), warn=True)
)
# Run pipenv-setup
return context.run("isort {} {}".format(isort_options, " ".join(PYTHON_DIRS)), warn=True)


def pipenv_setup(context, check=False) -> Result:
"""Runs pipenv-setup."""
isort_options = "{}".format("check --strict" if check else "sync --pipfile")
list_result.append(context.run("pipenv-setup {}".format(isort_options), warn=True))
# Run black
return context.run("pipenv-setup {}".format(isort_options), warn=True)


def black(context, check=False) -> Result:
"""Runs black."""
black_options = "{}".format("--check --diff" if check else "")
list_result.append(
context.run("black {} {}".format(black_options, python_dirs_string), warn=True)
)
for result in list_result:
if result.failed:
raise Failure(result)
return context.run("black {} {}".format(black_options, " ".join(PYTHON_DIRS)), warn=True)


@task
Expand Down

0 comments on commit d3852dc

Please sign in to comment.