From 3cd76de3ce5173f6f18318d3e453fe0b8d80dd8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linnea=20Gr=C3=A4f?= Date: Wed, 7 Aug 2024 23:58:08 +0200 Subject: [PATCH] Fix global licensing being ignored with a .license file Fixes https://github.com/fsfe/reuse-tool/issues/1057 --- AUTHORS.rst | 1 + .../fixed/license-overriding-global-name.md | 2 + src/reuse/project.py | 3 +- tests/test_project.py | 75 +++++++++++++++++++ 4 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 changelog.d/fixed/license-overriding-global-name.md diff --git a/AUTHORS.rst b/AUTHORS.rst index 1a40a48b9..734472ec8 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -151,3 +151,4 @@ Contributors - Сергій - Mersho - Skyler Grey +- Linnea Gräf diff --git a/changelog.d/fixed/license-overriding-global-name.md b/changelog.d/fixed/license-overriding-global-name.md new file mode 100644 index 000000000..f010d4727 --- /dev/null +++ b/changelog.d/fixed/license-overriding-global-name.md @@ -0,0 +1,2 @@ +- `REUSE.toml` `[[annotations]]` now use the correct path if a `.license` file + is present (#1058) diff --git a/src/reuse/project.py b/src/reuse/project.py index 9fc8b48de..67c41c4c6 100644 --- a/src/reuse/project.py +++ b/src/reuse/project.py @@ -3,6 +3,7 @@ # SPDX-FileCopyrightText: 2023 Carmen Bianca BAKKER # SPDX-FileCopyrightText: 2023 Matthias Riße # SPDX-FileCopyrightText: 2023 DB Systel GmbH +# SPDX-FileCopyrightText: 2024 Linnea Gräf # # SPDX-License-Identifier: GPL-3.0-or-later @@ -251,7 +252,7 @@ def reuse_info_of(self, path: StrPath) -> List[ReuseInfo]: # Search the global licensing file for REUSE information. if self.global_licensing: - relpath = self.relative_from_root(path) + relpath = self.relative_from_root(original_path) global_results = defaultdict( list, self.global_licensing.reuse_info_of(relpath) ) diff --git a/tests/test_project.py b/tests/test_project.py index fceba03e0..9b36261bb 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -3,6 +3,7 @@ # SPDX-FileCopyrightText: 2023 Carmen Bianca BAKKER # SPDX-FileCopyrightText: 2024 Skyler Grey # SPDX-FileCopyrightText: © 2020 Liferay, Inc. +# SPDX-FileCopyrightText: 2024 Linnea Gräf # # SPDX-License-Identifier: GPL-3.0-or-later @@ -647,6 +648,80 @@ def test_reuse_info_of_copyright_xor_licensing(empty_directory): assert not bar_file_info.spdx_expressions +def test_reuse_info_of_copyright_xor_licensing_with_license_dot( + empty_directory, +): + """Test a corner case where partial REUSE information is defined inside of a + .license file (copyright xor licensing). Get the missing information from the + REUSE.toml. + """ + (empty_directory / "REUSE.toml").write_text( + cleandoc( + """ + version = 1 + + [[annotations]] + path = "*.py" + SPDX-FileCopyrightText = "2017 Jane Doe" + SPDX-License-Identifier = "CC0-1.0" + + [[annotations]] + path = "*.py.license" + SPDX-FileCopyrightText = "2017 Jane Doe" + SPDX-License-Identifier = "MIT" + """ + ) + ) + (empty_directory / "foo.py").write_text( + cleandoc( + """ + print("This is very strictly copyrighted code") + """ + ) + ) + (empty_directory / "foo.py.license").write_text( + cleandoc( + """ + SPDX-FileCopyrightText: 2017 John Doe + """ + ) + ) + (empty_directory / "bar.py").write_text( + cleandoc( + """ + print("This is other very strictly copyrighted code") + """ + ) + ) + (empty_directory / "bar.py.license").write_text( + cleandoc( + """ + SPDX-License-Identifier: 0BSD + """ + ) + ) + project = Project.from_directory(empty_directory) + + foo_infos = project.reuse_info_of("foo.py") + assert len(foo_infos) == 2 + foo_toml_info = [info for info in foo_infos if info.spdx_expressions][0] + assert foo_toml_info.source_type == SourceType.REUSE_TOML + assert not foo_toml_info.copyright_lines + assert "MIT" not in str(foo_toml_info.spdx_expressions) + foo_file_info = [info for info in foo_infos if info.copyright_lines][0] + assert foo_file_info.source_type == SourceType.DOT_LICENSE + assert not foo_file_info.spdx_expressions + + bar_infos = project.reuse_info_of("bar.py") + assert len(bar_infos) == 2 + bar_toml_info = [info for info in bar_infos if info.copyright_lines][0] + assert bar_toml_info.source_type == SourceType.REUSE_TOML + assert not bar_toml_info.spdx_expressions + bar_file_info = [info for info in bar_infos if info.spdx_expressions][0] + assert bar_file_info.source_type == SourceType.DOT_LICENSE + assert not bar_file_info.copyright_lines + + def test_reuse_info_of_no_duplicates(empty_directory): """A file contains the same lines twice. The ReuseInfo only contains those lines once.