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

Fix #123. Skip shebang line if present. #130

Closed
wants to merge 3 commits into from
Closed
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: 10 additions & 1 deletion src/yamlfix/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ def fix_code(source_code: str) -> str:
# Leave Ansible vaults unmodified
if source_code.startswith("$ANSIBLE_VAULT;"):
return source_code

if source_code.startswith("#!"):
# Skip the shebang line if present, leaving it unmodified
eolpos = source_code.find("\n") + 1
shebang = source_code[:eolpos]
source_code = source_code[eolpos:]
else:
shebang = ""

fixers = [
_fix_truthy_strings,
_fix_comments,
Expand All @@ -72,7 +81,7 @@ def fix_code(source_code: str) -> str:
for fixer in fixers:
source_code = fixer(source_code)

return source_code
return shebang + source_code


def _ruamel_yaml_fixer(source_code: str) -> str:
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@
]


def test_fix_files_ignore_shebang() -> None:
"""Ignores shebang lines if present at the beginning of the source."""
source = dedent(
"""\
#! /this/line/should/be/ignored
---
program: yamlfix
"""
)

result = fix_code(source)

assert result == source


def test_fix_code_ignore_ansible_vaults() -> None:
"""Adds the --- at the beginning of the source."""
source = dedent(
Expand Down