Skip to content

Commit

Permalink
Make sure terminating newline is not lost in post-processing
Browse files Browse the repository at this point in the history
  • Loading branch information
abravalheri committed Mar 1, 2022
1 parent 9c62766 commit c9d37cd
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/ini2toml/drivers/full_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@


def convert(irepr: IntermediateRepr) -> str:
return dumps(collapse(irepr, root=True))
text = dumps(collapse(irepr, root=True))
return text.strip() + "\n" # ensure terminating newline (POSIX requirement)


@singledispatch
Expand Down
3 changes: 2 additions & 1 deletion src/ini2toml/drivers/lite_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@


def convert(irepr: IntermediateRepr) -> str:
return dumps(plain_builtins.convert(irepr))
text = dumps(plain_builtins.convert(irepr))
return text.strip() + "\n" # ensure terminating newline (POSIX requirement)
10 changes: 9 additions & 1 deletion src/ini2toml/plugins/profile_independent_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@


def activate(translator: Translator):
tasks = [normalise_newlines, remove_empty_table_headers]
tasks = [
normalise_newlines,
remove_empty_table_headers,
ensure_terminating_newlines,
]
for task in tasks:
translator.augment_profiles(post_process(task), active_by_default=True)

Expand Down Expand Up @@ -44,3 +48,7 @@ def remove_empty_table_headers(text: str) -> str:
prev_text = text
text = EMPTY_TABLES.sub(r"[\2]", text).strip()
return text


def ensure_terminating_newlines(text: str) -> str:
return text.strip() + "\n"
28 changes: 22 additions & 6 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ def test_examples_api(original, expected, validate):
translator = Translator()
available_profiles = list(translator.profiles.keys())
profile = cli.guess_profile(None, original, available_profiles)
out = translator.translate(Path(original).read_text(), profile).strip()
expected_text = Path(expected).read_text().strip()
orig = Path(original)

# Make sure file ends in a newline (requirement for posix text files)
out = translator.translate(orig.read_text(encoding="utf-8"), profile)
assert out.endswith("\n")

expected_text = Path(expected).read_text(encoding="utf-8")
assert out == expected_text

# Make sure they can be parsed
dict_equivalent = tomli.loads(out)
assert dict_equivalent == tomli.loads(expected_text)
Expand All @@ -64,8 +70,13 @@ def test_examples_api_lite(original, expected, validate):
# We cannot compare "flake8" sections (currently not handled)
# (ConfigParser automatically strips comments, contrary to ConfigUpdater)
orig = Path(original)
out = remove_flake8_from_toml(translator.translate(orig.read_text(), profile))
expected_text = remove_flake8_from_toml(Path(expected).read_text().strip())

# Make sure file ends in a newline (requirement for posix text files)
translated = translator.translate(orig.read_text(encoding="utf-8"), profile)
assert translated.endswith("\n")

out = remove_flake8_from_toml(translated)
expected_text = remove_flake8_from_toml(Path(expected).read_text(encoding="utf-8"))

# At least the Python-equivalents should be the same when parsing
dict_equivalent = tomli.loads(out)
Expand All @@ -87,8 +98,13 @@ def test_examples_api_lite(original, expected, validate):
def test_examples_cli(original, expected, capsys):
cli.run([original])
(out, err) = capsys.readouterr()
expected_text = Path(expected).read_text().strip()
assert out.strip() == expected_text

# Make sure file ends in a newline (requirement for posix text files)
assert out.endswith("\n")

expected_text = Path(expected).read_text(encoding="utf-8")
assert out == expected_text

# Make sure they can be parsed
assert tomli.loads(out) == tomli.loads(expected_text)

Expand Down

0 comments on commit c9d37cd

Please sign in to comment.