Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle latest ktlint on windows #68

Merged
merged 2 commits into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions language_formatters_pre_commit_hooks/pretty_format_kotlin.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ def get_url(_version: str) -> str:
)


def _fix_paths(paths: typing.Iterable[str]) -> typing.Iterable[str]:
# Starting from KTLint 0.41.0 paths cannot contain backward slashes as path separator
# Odd enough the error messages reported by KTLint contain `\` :(
for path in paths:
yield path.replace("\\", "/")


@java_required
def pretty_format_kotlin(argv: typing.Optional[typing.List[str]] = None) -> int:
parser = argparse.ArgumentParser()
Expand Down Expand Up @@ -60,15 +67,15 @@ def pretty_format_kotlin(argv: typing.Optional[typing.List[str]] = None) -> int:
# To workaround this limitation we do run ktlint in check mode only,
# which provides the expected exit status and we run it again in format
# mode if autofix flag is enabled
check_status, check_output = run_command("java", "-jar", ktlint_jar, "--verbose", "--relative", "--", *args.filenames)
check_status, check_output = run_command("java", "-jar", ktlint_jar, "--verbose", "--relative", "--", *_fix_paths(args.filenames))

not_pretty_formatted_files: typing.Set[str] = set()
if check_status != 0:
not_pretty_formatted_files.update(line.split(":", 1)[0] for line in check_output.splitlines())

if args.autofix:
print("Running ktlint format on {}".format(not_pretty_formatted_files))
run_command("java", "-jar", ktlint_jar, "--verbose", "--relative", "--format", "--", *not_pretty_formatted_files)
run_command("java", "-jar", ktlint_jar, "--verbose", "--relative", "--format", "--", *_fix_paths(not_pretty_formatted_files))

status = 0
if not_pretty_formatted_files:
Expand Down
2 changes: 1 addition & 1 deletion language_formatters_pre_commit_hooks/pretty_format_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def pretty_format_yaml(argv: typing.Optional[typing.List[str]] = None) -> int:
yaml.indent = args.indent
yaml.preserve_quotes = args.preserve_quotes
# Prevent ruamel.yaml to wrap yaml lines
yaml.width = maxsize
yaml.width = maxsize # type: ignore # mypy recognise yaml.width as None

separator = "---\n"

Expand Down
12 changes: 1 addition & 11 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,7 @@ strict_optional = True
warn_redundant_casts = True
warn_unused_configs = True

[mypy-tests.*]
check_untyped_defs = True
disallow_incomplete_defs = True
disallow_subclassing_any = True
disallow_untyped_calls = False
disallow_untyped_decorators = True
no_implicit_optional = True
warn_return_any = True
warn_unused_ignores = True

[mypy-language_formatters_pre_commit_hooks.*]
[mypy-*]
check_untyped_defs = True
disallow_incomplete_defs = True
disallow_subclassing_any = True
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.
coverage
mock
mypy
pre-commit
pytest
twine
14 changes: 6 additions & 8 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ def undecorate_function(func: F) -> typing.Generator[F, None, None]:
func = passed_function


def __read_file(path: str) -> str:
with open(path) as f:
return "".join(f.readlines())


def run_autofix_test(
tmpdir: py.path.local,
method: typing.Callable[[typing.List[str]], int],
Expand All @@ -47,12 +42,15 @@ def run_autofix_test(
tmpdir.mkdir("src")
not_pretty_formatted_tmp_path = tmpdir.join("src").join(basename(not_pretty_formatted_path))

# It is a relative paths as KTLint==0.41.0 dropped support for absolute paths
not_pretty_formatted_tmp_strpath = str(tmpdir.bestrelpath(not_pretty_formatted_tmp_path))

copyfile(not_pretty_formatted_path, not_pretty_formatted_tmp_path)
with change_dir_context(tmpdir.strpath):
assert method(["--autofix", tmpdir.bestrelpath(not_pretty_formatted_tmp_path)]) == 1
assert method(["--autofix", not_pretty_formatted_tmp_strpath]) == 1

# file was formatted (shouldn't trigger linter again)
with change_dir_context(tmpdir.strpath):
assert method(["--autofix", tmpdir.bestrelpath(not_pretty_formatted_tmp_path)]) == 0
assert method(["--autofix", not_pretty_formatted_tmp_strpath]) == 0

assert __read_file(not_pretty_formatted_tmp_path) == __read_file(formatted_path)
assert not_pretty_formatted_tmp_path.read_text("utf-8") == py.path.local(formatted_path).read_text("utf-8")
3 changes: 3 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ commands =

[testenv:pre-commit]
basepython = python3.9
setenv =
SKIP=mypy
commands =
pre-commit run --all-files
mypy language_formatters_pre_commit_hooks tests

[testenv:venv]
basepython = python3.9
Expand Down