Skip to content

Commit

Permalink
BUG: fix formatter behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
neutrinoceros committed Nov 23, 2021
1 parent 2c3ee14 commit d9ff080
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 21 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Expand Up @@ -6,8 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [0.10.0] - 2021-11-23

ENH: expose name column length parameter to users in inifix-format
- ENH: expose name column length parameter to users in inifix-format
[PR #73](https://github.com/neutrinoceros/inifix/pull/73)
- BUG: fix formatter behabiour
[PR #83](https://github.com/neutrinoceros/inifix/pull/83)

## [0.9.1] - 2021-11-21

Expand Down
24 changes: 17 additions & 7 deletions inifix/format.py
Expand Up @@ -46,6 +46,9 @@ def iniformat(data: str, *, name_column_size: Optional[int] = None) -> str:
parameters.append(parameter)
values.append(value)

if not parameters:
return "\n".join(lines)

max_name_size = max(len(parameter) for parameter in parameters)

if name_column_size is None:
Expand Down Expand Up @@ -101,35 +104,42 @@ def main(argv: Optional[Sequence[str]] = None) -> int:

args = parser.parse_args(argv)

retv = 0

for file in args.files:
if not os.path.isfile(file):
print(f"Error: could not find {file}", file=sys.stderr)
return 1
retv += 1
continue
try:
load(file)
except ValueError as exc:
print(f"Error: {exc}", file=sys.stderr)
return 1
retv += 1

with open(file) as fh:
data = fh.read()

fmted_data = iniformat(data, name_column_size=args.name_column_size)

if fmted_data == data:
continue
print(f"{file} is already formatted", file=sys.stderr)
else:
print(f"Fixing {file}", file=sys.stderr)
retv += 1

if args.inplace:
print(f"Fixing {file}", file=sys.stderr)
try:
with open(file, "w") as fh:
fh.write(fmted_data)
except OSError:
print(f"Error: could not write to {file}", file=sys.stderr)
return 1
retv += 1
continue
else:
print(f"{file} >>>", file=sys.stderr)
print(fmted_data)
return 0

return retv


if __name__ == "__main__": # pragma: no cover
Expand Down
72 changes: 59 additions & 13 deletions tests/test_format.py
Expand Up @@ -13,27 +13,64 @@
@pytest.mark.parametrize("flag", ["-i", "--inplace"])
def test_format_keep_data(flag, inifile, capsys, tmp_path):
target = tmp_path / inifile.name
data_ref = load(inifile)

ref_data = load(inifile)
shutil.copyfile(inifile, target)

ret = main([str(target), flag])
assert ret == 0
assert isinstance(ret, int)

out, err = capsys.readouterr()

# nothing to output to stdout with --inplace
assert out == ""
if err == f"{target} is already formatted\n":
assert ret == 0
else:
assert err == f"Fixing {target}\n"
assert ret != 0

data_new = load(target)
assert data_new == data_ref
assert data_new == ref_data


@pytest.mark.parametrize("infile", ("format-in.ini", "format-out.ini"))
def test_exact_format_stdout(infile, capsys, tmp_path):
DATA_DIR = Path(__file__).parent / "data"

expected = (DATA_DIR / "format-out.ini").read_text() + "\n"

target = tmp_path / "out.ini"
shutil.copyfile(DATA_DIR / infile, target)
baseline = target.read_text() + "\n"

ret = main([str(target)])
out, err = capsys.readouterr()

assert out == expected
if err == f"{target} is already formatted\n":
assert ret == 0
assert out == baseline
else:
assert err == f"Fixing {target}\n"
assert ret != 0
assert out != ""


@pytest.mark.parametrize("flag", ["-i", "--inplace"])
def test_exact_format(flag, capsys, tmp_path):
def test_exact_format_inplace(flag, capsys, tmp_path):
DATA_DIR = Path(__file__).parent / "data"
target = tmp_path / "out.ini"
shutil.copyfile(DATA_DIR / "format-in.ini", target)

ret = main([str(target), flag])
assert ret == 0
out, err = capsys.readouterr()
assert out == ""

if err == f"{target} is already formatted\n":
assert ret == 0
else:
assert err == f"Fixing {target}\n"
assert ret != 0

expected = (DATA_DIR / "format-out.ini").read_text()
res = target.read_text()
Expand All @@ -46,10 +83,12 @@ def test_exact_format_with_column_size_flag(size, capsys, tmp_path):
DATA_DIR = Path(__file__).parent / "data"
target = tmp_path / "out.ini"
shutil.copyfile(DATA_DIR / "format-column-size-in.ini", target)

ret = main([str(target), "-i", "--name-column-size", size])
assert ret == 0
out, err = capsys.readouterr()
assert out == ""

assert err == f"Fixing {target}\n"
assert ret != 0

expected = (DATA_DIR / f"format-column-size-out-{size}.ini").read_text()
res = target.read_text()
Expand All @@ -71,7 +110,7 @@ def test_empty_file(capsys, tmp_path):
ret = main([str(target)])
assert ret != 0
out, err = capsys.readouterr()
assert out == ""
assert out == "\n"
assert f"Error: {target} appears to be emtpy.\n" in err


Expand All @@ -96,10 +135,17 @@ def test_write_to_console(inifile, capsys, tmp_path):
target = tmp_path / inifile.name
shutil.copy(inifile, target)

# format the file for easier comparison
ret = main([str(target), "--inplace"])
baseline = target.read_text() + "\n"

ret = main([str(target)])
assert ret == 0
# we can't predict if formatting is needed
assert isinstance(ret, int)
out, err = capsys.readouterr()
assert out == ""

if err == f"{target} is already formatted\n":
assert ret == 0
assert out == baseline
else:
assert err == f"Fixing {target}\n"
assert ret != 0
assert out != ""

0 comments on commit d9ff080

Please sign in to comment.