Skip to content

Commit

Permalink
Merge pull request #8 from atugushev/multiple-source-files
Browse files Browse the repository at this point in the history
Support multiple input files
  • Loading branch information
pappasam committed Apr 18, 2020
2 parents 8c10c8f + 83ddab3 commit a74cd36
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 20 deletions.
109 changes: 109 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Test the CLI"""

import os
import shutil

import pytest
from click.testing import CliRunner
Expand Down Expand Up @@ -39,3 +40,111 @@ def test_cli_defaults(path_unsorted: str, path_sorted: str) -> None:
result_stdin = runner.invoke(cli, input=original)
assert result_stdin.exit_code == 0
assert result_stdin.output == expected


@pytest.mark.parametrize(
"paths, expected_output, expected_exit_code",
(
pytest.param(
["from-toml-lang.toml", "weird.toml"],
[
"File 'tests/examples/from-toml-lang.toml'"
" would be re-formatted",
"File 'tests/examples/weird.toml' would be re-formatted",
],
1,
id="multiple unsorted files failed",
),
pytest.param(
["from-toml-lang.toml", "defaults/weird.toml"],
[
"File 'tests/examples/from-toml-lang.toml'"
" would be re-formatted",
],
1,
id="single unsorted file failed",
),
pytest.param(
["defaults/from-toml-lang.toml", "defaults/weird.toml"],
[],
0,
id="none failed, no output",
),
),
)
def test_multiple_files_check(paths, expected_output, expected_exit_code):
"""Unsorted files should be checked."""
paths_unsorted = [os.path.join(PATH_EXAMPLES, path) for path in paths]
runner = CliRunner()

result = runner.invoke(cli, ["--check"] + paths_unsorted)
assert result.exit_code == expected_exit_code, result.output
assert result.output.splitlines() == expected_output


def test_multiple_files_in_place(tmpdir):
"""Unsorted files should be sorted in-place."""
paths_sorted = [
os.path.join(PATH_EXAMPLES, "defaults/from-toml-lang.toml"),
os.path.join(PATH_EXAMPLES, "defaults/weird.toml"),
]

filenames_unsorted = [
"from-toml-lang.toml",
"weird.toml",
]
temp_paths_unsorted = []
for filename in filenames_unsorted:
orig_path = os.path.join(PATH_EXAMPLES, filename)
temp_path = tmpdir / filename
shutil.copy(orig_path, temp_path)
temp_paths_unsorted.append(str(temp_path))

runner = CliRunner()

result = runner.invoke(cli, ["--in-place"] + temp_paths_unsorted)
assert result.exit_code == 0, result.output

for path_unsorted, path_sorted in zip(temp_paths_unsorted, paths_sorted):
with open(path_unsorted) as file_unsorted:
actual = file_unsorted.read()
with open(path_sorted) as file_sorted:
expected = file_sorted.read()
assert actual == expected


@pytest.mark.parametrize(
"options, expected_output",
(
pytest.param(
[],
"--check or --in-place are required "
"if two or more input files are given",
id="--check or --in-place must be specified",
),
pytest.param(
["--check", "--output", "output.toml"],
"Cannot specify output file "
"if two or more input files are given",
id="cannot specify output with --check",
),
pytest.param(
["--in-place", "--output", "output.toml"],
"Cannot specify output file "
"if two or more input files are given",
id="cannot specify output with --in-place",
),
),
)
def test_multiple_files_and_errors(options, expected_output):
"""Test errors if two or more files are given."""
paths = [
os.path.join(PATH_EXAMPLES, "from-toml-lang.toml"),
os.path.join(PATH_EXAMPLES, "weird.toml"),
]
runner = CliRunner()

result = runner.invoke(cli, options + paths)

assert result.exit_code == 1, result.output
assert expected_output in result.output.splitlines()
65 changes: 45 additions & 20 deletions toml_sort/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ def _write_file(path: str, content: str) -> None:
),
)
@click.argument(
"filename",
"filenames",
nargs=-1,
type=click.Path(
exists=True, file_okay=True, readable=True, allow_dash=True
),
default=_STD_STREAM,
)
@click.version_option()
def cli(output, _all, in_place, no_header, check, filename) -> None:
def cli(output, _all, in_place, no_header, check, filenames) -> None:
"""Sort toml file FILENAME, saving results to a file, or stdout (default)
FILENAME a filepath or standard input (-)
Expand All @@ -93,7 +93,25 @@ def cli(output, _all, in_place, no_header, check, filename) -> None:
Inplace Disk : toml-sort --in-place input.toml
"""
if filename == "-" and sys.stdin.isatty():
if not filenames:
filenames = (_STD_STREAM,)

if len(filenames) > 1:
if not (in_place or check):
click.echo(
"--check or --in-place are required "
"if two or more input files are given",
err=True,
)
sys.exit(1)
elif output != _STD_STREAM:
click.echo(
"Cannot specify output file "
"if two or more input files are given",
err=True,
)
sys.exit(1)
elif _STD_STREAM in filenames and sys.stdin.isatty():
error_message_if_terminal = """
toml-sort: missing FILENAME, and no stdin
Usage: toml-sort [OPTIONS] [FILENAME]
Expand All @@ -102,24 +120,31 @@ def cli(output, _all, in_place, no_header, check, filename) -> None:
""".strip()
click.echo(error_message_if_terminal, err=True)
sys.exit(1)
elif in_place and filename == _STD_STREAM:
elif in_place and _STD_STREAM in filenames:
click.echo("Cannot format stdin in-place", err=True)
sys.exit(1)
elif in_place and output != _STD_STREAM:
click.echo("Cannot specify output file with in-place", err=True)
sys.exit(1)
original_toml = _read_file(filename)
sorted_toml = TomlSort(
input_toml=original_toml,
only_sort_tables=not bool(_all),
no_header=bool(no_header),
).sorted()
if check:
if original_toml != sorted_toml:
click.echo("File would be re-formatted", err=True)
sys.exit(1)
return
if in_place:
_write_file(filename, sorted_toml)
return
_write_file(output, sorted_toml)

check_failed = False

for filename in filenames:
original_toml = _read_file(filename)
sorted_toml = TomlSort(
input_toml=original_toml,
only_sort_tables=not bool(_all),
no_header=bool(no_header),
).sorted()
if check and original_toml != sorted_toml:
click.echo(f"File '{filename}' would be re-formatted", err=True)
check_failed = True
continue
if in_place:
_write_file(filename, sorted_toml)
continue
if len(filenames) == 1:
_write_file(output, sorted_toml)

if check and check_failed:
sys.exit(1)

0 comments on commit a74cd36

Please sign in to comment.