Skip to content

Commit

Permalink
Merge pull request #965 from carmenbianca/single-shebang
Browse files Browse the repository at this point in the history
Support annotating a file that contains only a shebang
  • Loading branch information
carmenbianca committed Apr 27, 2024
2 parents ca360e3 + 1825e0c commit 4d10e67
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -67,6 +67,7 @@ CLI command and its behaviour. There are no guarantees of stability for the
(#949)
- The datetime value for `Created:` was wrongly formatted since 3.0.0. It now
returns a correctly formatted ISO 8601 date again. (#952)
- Support annotating a file that contains only a shebang. (#965)

### Security

Expand Down
2 changes: 1 addition & 1 deletion src/reuse/header.py
Expand Up @@ -216,8 +216,8 @@ def _extract_shebang(prefix: str, text: str) -> Tuple[str, str]:
shebang_lines.append(line)
text = text.replace(line, "", 1)
else:
shebang = "\n".join(shebang_lines)
break
shebang = "\n".join(shebang_lines)
return (shebang, text)


Expand Down
25 changes: 23 additions & 2 deletions tests/test_header.py
@@ -1,5 +1,6 @@
# SPDX-FileCopyrightText: 2019 Free Software Foundation Europe e.V. <https://fsfe.org>
# SPDX-FileCopyrightText: 2022 Florian Snow <florian@familysnow.net>
# SPDX-FileCopyrightText: 2024 Carmen Bianca BAKKER <carmenbianca@fsfe.org>
#
# SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -368,8 +369,10 @@ def test_find_and_replace_separate_shebang():
assert find_and_replace_header(text, info) == expected


def test_find_and_replace_only_shebang():
"""When the file only contains a shebang, keep it at the top of the file."""
def test_find_and_replace_shebang_but_no_copyright():
"""When the file contains a shebang but no copyright information, keep it at
the top of the file.
"""
info = ReuseInfo({"GPL-3.0-or-later"})
text = cleandoc(
"""
Expand All @@ -395,6 +398,24 @@ def test_find_and_replace_only_shebang():
assert find_and_replace_header(text, info) == expected


def test_find_and_replace_only_shebang():
"""When the file only contains a shebang, add copyright info below it."""
info = ReuseInfo({"GPL-3.0-or-later"})
text = "#!/usr/bin/env python3"
expected = (
cleandoc(
"""
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-3.0-or-later
"""
)
+ "\n"
)

assert find_and_replace_header(text, info) == expected


def test_find_and_replace_keep_old_comment():
"""When encountering a comment that does not contain copyright and
licensing information, preserve it below the REUSE header.
Expand Down

0 comments on commit 4d10e67

Please sign in to comment.