feat(license): apply() for Bash with shebang-aware placement (ref #81)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
CyberMind-FR 2026-05-12 09:48:43 +02:00
parent 9b0fce3fac
commit 6474728dc4
2 changed files with 29 additions and 0 deletions

View File

@ -48,6 +48,7 @@ def render_header(style: str) -> str:
LANG_TABLE: dict[str, tuple[str, str]] = {
".py": ("hash", "python"),
".sh": ("hash", "shebang_hash"),
}
@ -63,8 +64,16 @@ def _place_python(header: str, text: str) -> str:
return "".join(lines[:insert_at]) + header + "\n" + "".join(lines[insert_at:])
def _place_shebang_hash(header: str, text: str) -> str:
"""Hash-comment language with a shebang line on line 1."""
lines = text.splitlines(keepends=True)
insert_at = 1 if lines and lines[0].startswith("#!") else 0
return "".join(lines[:insert_at]) + header + "\n" + "".join(lines[insert_at:])
_PLACERS = {
"python": _place_python,
"shebang_hash": _place_shebang_hash,
}

View File

@ -155,3 +155,23 @@ def test_apply_python_with_shebang_and_encoding():
assert lines[0] == "#!/usr/bin/env python3"
assert lines[1] == "# -*- coding: utf-8 -*-"
assert lines[2] == "# SPDX-License-Identifier: LicenseRef-CMSD-1.0"
def test_apply_bash_with_shebang():
src = '#!/usr/bin/env bash\nset -euo pipefail\necho "hi"\n'
out = license_headers.apply(src, ".sh")
assert out.startswith("#!/usr/bin/env bash\n")
assert out.splitlines()[1] == "# SPDX-License-Identifier: LicenseRef-CMSD-1.0"
def test_apply_bash_without_shebang():
src = 'echo "hi"\n'
out = license_headers.apply(src, ".sh")
assert out.startswith("# SPDX-License-Identifier: LicenseRef-CMSD-1.0\n")
def test_apply_bash_idempotent():
src = '#!/usr/bin/env bash\nset -e\n'
once = license_headers.apply(src, ".sh")
twice = license_headers.apply(once, ".sh")
assert once == twice