Skip to content

Commit

Permalink
Add tests for better handling of symlinks
Browse files Browse the repository at this point in the history
These tests assume the interpretation of the REUSE specification that:
1. a symlink pointing to a covered file is considered to be the same
   file as the covered file and can therefore be ignored.
2. a symlink pointing to a file that is not a covered file is itself
   considered to be a covered file and should not be ignored, unless the
   symlink itself is ignored by other means.
  • Loading branch information
matrss committed Jun 19, 2023
1 parent 3a1f698 commit 91acd13
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-FileCopyrightText: 2017 Free Software Foundation Europe e.V. <https://fsfe.org>
# SPDX-FileCopyrightText: 2022 Florian Snow <[email protected]>
# SPDX-FileCopyrightText: 2022 Carmen Bianca Bakker <[email protected]>
# SPDX-FileCopyrightText: 2023 Matthias Riße
#
# SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -137,6 +138,15 @@ def fake_repository(tmpdir_factory) -> Path:
encoding="utf-8",
)

(directory / "symlink-to-covered").symlink_to(directory / "doc/index.rst")
(directory / "symlink-to-not-covered").symlink_to(directory)
(directory / "symlink-to-not-covered.license").write_text(
"# SPDX-FileCopyrightText: 2017 Jane Doe\n"
"#\n"
"# SPDX-License-Identifier: GPL-3.0-or-later",
encoding="utf-8",
)

os.chdir(directory)
return directory

Expand Down
66 changes: 64 additions & 2 deletions tests/test_project.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-FileCopyrightText: 2017 Free Software Foundation Europe e.V. <https://fsfe.org>
# SPDX-FileCopyrightText: © 2020 Liferay, Inc. <https://liferay.com>
# SPDX-FileCopyrightText: 2022 Florian Snow <[email protected]>
# SPDX-FileCopyrightText: 2023 Matthias Riße
#
# SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -98,8 +99,8 @@ def test_all_files_ignore_hg(empty_directory):


@posix
def test_all_files_symlinks(empty_directory):
"""All symlinks must be ignored."""
def test_all_files_ignore_symlinks_to_covered_file(empty_directory):
"""All symlinks to covered files must be ignored."""
(empty_directory / "blob").write_text("foo")
(empty_directory / "blob.license").write_text(
cleandoc(
Expand All @@ -115,6 +116,15 @@ def test_all_files_symlinks(empty_directory):
assert Path("symlink").absolute() not in project.all_files()


@posix
@pytest.mark.parametrize("target", ["/outside_file", "non-existent-file"])
def test_all_files_cover_symlinks_to_uncovered_files(empty_directory, target):
"""All symlinks to files not covered must be included."""
(empty_directory / "symlink").symlink_to(target)
project = Project(empty_directory)
assert Path("symlink").absolute() in project.all_files()


def test_all_files_ignore_zero_sized(empty_directory):
"""Empty files should be skipped."""
(empty_directory / "foo").touch()
Expand Down Expand Up @@ -157,6 +167,33 @@ def test_all_files_git_ignored_contains_newline(git_repository):
assert Path("hello\nworld.pyc").absolute() not in project.all_files()


@posix
def test_all_files_git_ignore_symlinks_to_covered_file(git_repository):
"""All symlinks to covered files must be ignored."""
(git_repository / "symlink").symlink_to("doc/index.rst")
project = Project(git_repository)
assert Path("symlink").absolute() not in project.all_files()


@posix
@pytest.mark.parametrize(
"target",
[
"/outside_file",
".git/file-in-dotgit",
"build/hello.py",
"non-existent-file",
],
)
def test_all_files_git_cover_symlinks_to_uncovered_files(
git_repository, target
):
"""All symlinks to files not covered must be included."""
(git_repository / "symlink").symlink_to(target)
project = Project(git_repository)
assert Path("symlink").absolute() in project.all_files()


def test_all_files_submodule_is_ignored(submodule_repository):
"""If a submodule is ignored, all_files should not raise an Exception."""
(submodule_repository / "submodule/foo.py").write_text("foo")
Expand Down Expand Up @@ -202,6 +239,31 @@ def test_all_files_hg_ignored_contains_newline(hg_repository):
assert Path("hello\nworld.pyc").absolute() not in project.all_files()


@posix
def test_all_files_hg_ignore_symlinks_to_covered_file(hg_repository):
"""All symlinks to covered files must be ignored."""
(hg_repository / "symlink").symlink_to("doc/index.rst")
project = Project(hg_repository)
assert Path("symlink").absolute() not in project.all_files()


@posix
@pytest.mark.parametrize(
"target",
[
"/outside_file",
".hg/file-in-dothg",
"build/hello.py",
"non-existent-file",
],
)
def test_all_files_hg_cover_symlinks_to_uncovered_files(hg_repository, target):
"""All symlinks to files not covered must be included."""
(hg_repository / "symlink").symlink_to(target)
project = Project(hg_repository)
assert Path("symlink").absolute() in project.all_files()


def test_reuse_info_of_file_does_not_exist(fake_repository):
"""Raise FileNotFoundError when asking for the REUSE info of a file that
does not exist.
Expand Down

0 comments on commit 91acd13

Please sign in to comment.