Skip to content

Commit

Permalink
fix: add newline at end of file
Browse files Browse the repository at this point in the history
  • Loading branch information
muripic committed Aug 29, 2021
1 parent 2ab1b38 commit 7712c61
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 29 deletions.
5 changes: 5 additions & 0 deletions src/yamlfix/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def fix_code(source_code: str) -> str:
_restore_truthy_strings,
_restore_double_exclamations,
_fix_top_level_lists,
_add_newline_at_end_of_file,
]
for fixer in fixers:
source_code = fixer(source_code)
Expand Down Expand Up @@ -275,3 +276,7 @@ def _restore_double_exclamations(source_code: str) -> str:
fixed_source_lines.append(line)

return "\n".join(fixed_source_lines)


def _add_newline_at_end_of_file(source_code: str) -> str:
return source_code + "\n"
9 changes: 6 additions & 3 deletions tests/e2e/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def test_corrects_one_file(runner: CliRunner, tmpdir: LocalPath) -> None:
fixed_source = dedent(
"""\
---
program: yamlfix"""
program: yamlfix
"""
)

result = runner.invoke(cli, [str(test_file)])
Expand All @@ -58,7 +59,8 @@ def test_corrects_three_files(runner: CliRunner, tmpdir: LocalPath) -> None:
fixed_source = dedent(
"""\
---
program: yamlfix"""
program: yamlfix
"""
)

result = runner.invoke(cli, [str(test_file) for test_file in test_files])
Expand All @@ -74,7 +76,8 @@ def test_corrects_code_from_stdin(runner: CliRunner) -> None:
fixed_source = dedent(
"""\
---
program: yamlfix"""
program: yamlfix
"""
)

result = runner.invoke(cli, ["-"], input=source)
Expand Down
101 changes: 75 additions & 26 deletions tests/unit/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def test_fix_code_adds_header() -> None:
fixed_source = dedent(
"""\
---
program: yamlfix"""
program: yamlfix
"""
)

result = fix_code(source)
Expand All @@ -51,7 +52,8 @@ def test_fix_code_doesnt_double_the_header() -> None:
source = dedent(
"""\
---
program: yamlfix"""
program: yamlfix
"""
)

result = fix_code(source)
Expand All @@ -66,14 +68,16 @@ def test_fix_code_corrects_indentation_on_lists() -> None:
---
hosts:
- item1
- item2"""
- item2
"""
)
fixed_source = dedent(
"""\
---
hosts:
- item1
- item2"""
- item2
"""
)

result = fix_code(source)
Expand All @@ -87,7 +91,8 @@ def test_fix_code_respects_parent_lists() -> None:
"""\
---
- item1
- item2"""
- item2
"""
)

result = fix_code(source)
Expand All @@ -101,7 +106,8 @@ def test_fix_code_preserves_comments() -> None:
"""\
---
# Keep comments!
program: yamlfix"""
program: yamlfix
"""
)

result = fix_code(source)
Expand All @@ -116,7 +122,8 @@ def test_fix_code_respects_parent_lists_with_comments() -> None:
---
# Comment
- item1
- item2"""
- item2
"""
)

result = fix_code(source)
Expand All @@ -130,7 +137,8 @@ def test_fix_code_preserves_indented_comments() -> None:
"""\
---
- program:
# Keep comments!"""
# Keep comments!
"""
)

result = fix_code(source)
Expand All @@ -143,12 +151,14 @@ def test_fix_code_removes_extra_apostrophes() -> None:
source = dedent(
"""\
---
title: 'Why we sleep'"""
title: 'Why we sleep'
"""
)
fixed_source = dedent(
"""\
---
title: Why we sleep"""
title: Why we sleep
"""
)

result = fix_code(source)
Expand All @@ -168,14 +178,16 @@ def test_fix_code_converts_non_valid_true_booleans(true_string: str) -> None:
---
True dictionary: {true_string}
True list:
- {true_string}"""
- {true_string}
"""
)
fixed_source = dedent(
"""\
---
True dictionary: true
True list:
- true"""
- true
"""
)

result = fix_code(source)
Expand All @@ -195,14 +207,16 @@ def test_fix_code_converts_non_valid_false_booleans(false_string: str) -> None:
---
False dictionary: {false_string}
False list:
- {false_string}"""
- {false_string}
"""
)
fixed_source = dedent(
"""\
---
False dictionary: false
False list:
- false"""
- false
"""
)

result = fix_code(source)
Expand All @@ -221,7 +235,8 @@ def test_fix_code_respects_apostrophes_for_truthy_substitutions(
source = dedent(
f"""\
---
title: '{truthy_string}'"""
title: '{truthy_string}'
"""
)

result = fix_code(source)
Expand All @@ -237,13 +252,15 @@ def test_fix_code_adds_space_in_comment() -> None:
"""\
---
#This is a comment
project: yamlfix"""
project: yamlfix
"""
)
fixed_source = dedent(
"""\
---
# This is a comment
project: yamlfix"""
project: yamlfix
"""
)

result = fix_code(source)
Expand All @@ -259,13 +276,15 @@ def test_fix_code_not_add_extra_space_in_comment() -> None:
"""\
---
# This is a comment
project: yamlfix"""
project: yamlfix
"""
)
fixed_source = dedent(
"""\
---
# This is a comment
project: yamlfix"""
project: yamlfix
"""
)

result = fix_code(source)
Expand All @@ -280,12 +299,14 @@ def test_fix_code_add_space_inline_comment() -> None:
source = dedent(
"""\
---
project: yamlfix #This is a comment"""
project: yamlfix #This is a comment
"""
)
fixed_source = dedent(
"""\
---
project: yamlfix # This is a comment"""
project: yamlfix # This is a comment
"""
)

result = fix_code(source)
Expand All @@ -299,7 +320,8 @@ def test_fix_code_respects_url_anchors() -> None:
"""\
---
# https://lyz-code.github.io/yamlfix/#usage
foo: bar"""
foo: bar
"""
)

result = fix_code(source)
Expand All @@ -314,12 +336,14 @@ def test_fix_code_add_extra_space_inline_comment() -> None:
source = dedent(
"""\
---
project: yamlfix # This is a comment"""
project: yamlfix # This is a comment
"""
)
fixed_source = dedent(
"""\
---
project: yamlfix # This is a comment"""
project: yamlfix # This is a comment
"""
)

result = fix_code(source)
Expand All @@ -334,7 +358,8 @@ def test_fix_code_doesnt_change_double_exclamation_marks() -> None:
source = dedent(
"""\
---
format: !!python/name:mermaid2.fence_mermaid"""
format: !!python/name:mermaid2.fence_mermaid
"""
)

result = fix_code(source)
Expand All @@ -351,7 +376,8 @@ def test_fix_code_parses_files_with_multiple_documents() -> None:
---
project: yamlfix
---
project: yamlfix"""
project: yamlfix
"""
)

result = fix_code(source)
Expand All @@ -376,3 +402,26 @@ def test_fix_code_functions_emit_debug_logs(caplog: pytest.LogCaptureFixture) ->
assert caplog.messages == expected_logs
for record in caplog.records:
assert record.levelname == "DEBUG"


@pytest.mark.parametrize("whitespace", ["", "\n", "\n\n"])
def test_fixed_code_has_exactly_one_newline_at_end_of_file(whitespace) -> None:
"""Files should have exactly one newline at the end to comply with the POSIX
standard.
"""
source = dedent(
"""\
---
program: yamlfix"""
)
source += whitespace
fixed_code = dedent(
"""\
---
program: yamlfix
"""
)

result = fix_code(source)

assert result == fixed_code

0 comments on commit 7712c61

Please sign in to comment.