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

Support annotating a file that contains only a shebang #965

Merged
merged 1 commit into from
Apr 27, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Loading