From 94cfe206204bef812aee017c0b871e880c944cf1 Mon Sep 17 00:00:00 2001 From: Carmen Bianca BAKKER Date: Mon, 8 Jul 2024 17:23:35 +0200 Subject: [PATCH] Factor out all_files into its own module covered_files Signed-off-by: Carmen Bianca BAKKER --- src/reuse/covered_files.py | 120 ++++++++++++++++ src/reuse/project.py | 104 +------------- tests/conftest.py | 2 + tests/test_covered_files.py | 257 +++++++++++++++++++++++++++++++++ tests/test_project.py | 274 ++++++++---------------------------- 5 files changed, 440 insertions(+), 317 deletions(-) create mode 100644 src/reuse/covered_files.py create mode 100644 tests/test_covered_files.py diff --git a/src/reuse/covered_files.py b/src/reuse/covered_files.py new file mode 100644 index 000000000..2f14d963e --- /dev/null +++ b/src/reuse/covered_files.py @@ -0,0 +1,120 @@ +# SPDX-FileCopyrightText: 2017 Free Software Foundation Europe e.V. +# +# SPDX-License-Identifier: GPL-3.0-or-later + +"""The REUSE Specification has a concept called Covered Files; files which must +contain licensing information. Some files in a project are not Covered Files, +and thus needn't contain licensing information. This module contains all that +logic. +""" + +import contextlib +import logging +import os +from pathlib import Path +from typing import Generator, Optional + +from . import ( + _IGNORE_DIR_PATTERNS, + _IGNORE_FILE_PATTERNS, + _IGNORE_MESON_PARENT_DIR_PATTERNS, +) +from ._util import StrPath +from .vcs import VCSStrategy + +_LOGGER = logging.getLogger(__name__) + + +def is_path_ignored( + path: Path, + include_submodules: bool = False, + include_meson_subprojects: bool = False, + vcs_strategy: Optional[VCSStrategy] = None, +) -> bool: + """Is *path* ignored by some mechanism?""" + # pylint: disable=too-many-return-statements,too-many-branches + name = path.name + parent_parts = path.parent.parts + parent_dir = parent_parts[-1] if len(parent_parts) > 0 else "" + + if path.is_symlink(): + _LOGGER.debug("skipping symlink '%s'", path) + return True + + if path.is_file(): + for pattern in _IGNORE_FILE_PATTERNS: + if pattern.match(name): + return True + # Suppressing this error because I simply don't want to deal + # with that here. + with contextlib.suppress(OSError): + if path.stat().st_size == 0: + _LOGGER.debug("skipping 0-sized file '%s'", path) + return True + + elif path.is_dir(): + for pattern in _IGNORE_DIR_PATTERNS: + if pattern.match(name): + return True + if not include_meson_subprojects: + for pattern in _IGNORE_MESON_PARENT_DIR_PATTERNS: + if pattern.match(parent_dir): + _LOGGER.info( + "ignoring '%s' because it is a Meson subproject", path + ) + return True + if ( + not include_submodules + and vcs_strategy + and vcs_strategy.is_submodule(path) + ): + _LOGGER.info("ignoring '%s' because it is a submodule", path) + return True + + if vcs_strategy and vcs_strategy.is_ignored(path): + return True + + return False + + +def all_files( + directory: StrPath, + include_submodules: bool = False, + include_meson_subprojects: bool = False, + vcs_strategy: Optional[VCSStrategy] = None, +) -> Generator[Path, None, None]: + """Yield all Covered Files in *directory* and its subdirectories according + to the REUSE Specification. + """ + directory = Path(directory) + + for root_str, dirs, files in os.walk(directory): + root = Path(root_str) + _LOGGER.debug("currently walking in '%s'", root) + + # Don't walk ignored directories + for dir_ in list(dirs): + the_dir = root / dir_ + if is_path_ignored( + the_dir, + include_submodules=include_submodules, + include_meson_subprojects=include_meson_subprojects, + vcs_strategy=vcs_strategy, + ): + _LOGGER.debug("ignoring '%s'", the_dir) + dirs.remove(dir_) + + # Filter files. + for file_ in files: + the_file = root / file_ + if is_path_ignored( + the_file, + include_submodules=include_submodules, + include_meson_subprojects=include_meson_subprojects, + vcs_strategy=vcs_strategy, + ): + _LOGGER.debug("ignoring '%s'", the_file) + continue + + _LOGGER.debug("yielding '%s'", the_file) + yield the_file diff --git a/src/reuse/project.py b/src/reuse/project.py index 30ab45012..10e494529 100644 --- a/src/reuse/project.py +++ b/src/reuse/project.py @@ -29,13 +29,7 @@ from binaryornot.check import is_binary -from . import ( - _IGNORE_DIR_PATTERNS, - _IGNORE_FILE_PATTERNS, - _IGNORE_MESON_PARENT_DIR_PATTERNS, - IdentifierNotFound, - ReuseInfo, -) +from . import IdentifierNotFound, ReuseInfo from ._licenses import EXCEPTION_MAP, LICENSE_MAP from ._util import ( _LICENSEREF_PATTERN, @@ -44,6 +38,7 @@ relative_from_root, reuse_info_of_file, ) +from .covered_files import all_files from .global_licensing import ( GlobalLicensing, NestedReuseTOML, @@ -431,98 +426,3 @@ def _detect_vcs_strategy(cls, root: StrPath) -> VCSStrategy: ).format(root) ) return VCSStrategyNone(root) - - -def is_path_ignored( - path: Path, - include_submodules: bool = False, - include_meson_subprojects: bool = False, - vcs_strategy: Optional[VCSStrategy] = None, -) -> bool: - """Is *path* ignored by some mechanism?""" - # pylint: disable=too-many-return-statements,too-many-branches - name = path.name - parent_parts = path.parent.parts - parent_dir = parent_parts[-1] if len(parent_parts) > 0 else "" - - if path.is_symlink(): - _LOGGER.debug("skipping symlink '%s'", path) - return True - - if path.is_file(): - for pattern in _IGNORE_FILE_PATTERNS: - if pattern.match(name): - return True - # Suppressing this error because I simply don't want to deal - # with that here. - with contextlib.suppress(OSError): - if path.stat().st_size == 0: - _LOGGER.debug("skipping 0-sized file '%s'", path) - return True - - elif path.is_dir(): - for pattern in _IGNORE_DIR_PATTERNS: - if pattern.match(name): - return True - if not include_meson_subprojects: - for pattern in _IGNORE_MESON_PARENT_DIR_PATTERNS: - if pattern.match(parent_dir): - _LOGGER.info( - "ignoring '%s' because it is a Meson subproject", path - ) - return True - if ( - not include_submodules - and vcs_strategy - and vcs_strategy.is_submodule(path) - ): - _LOGGER.info("ignoring '%s' because it is a submodule", path) - return True - - if vcs_strategy and vcs_strategy.is_ignored(path): - return True - - return False - - -def all_files( - directory: StrPath, - include_submodules: bool = False, - include_meson_subprojects: bool = False, - vcs_strategy: Optional[VCSStrategy] = None, -) -> Generator[Path, None, None]: - """Yield all Covered Files in *directory* and its subdirectories according - to the REUSE Specification. - """ - directory = Path(directory) - - for root_str, dirs, files in os.walk(directory): - root = Path(root_str) - _LOGGER.debug("currently walking in '%s'", root) - - # Don't walk ignored directories - for dir_ in list(dirs): - the_dir = root / dir_ - if is_path_ignored( - the_dir, - include_submodules=include_submodules, - include_meson_subprojects=include_meson_subprojects, - vcs_strategy=vcs_strategy, - ): - _LOGGER.debug("ignoring '%s'", the_dir) - dirs.remove(dir_) - - # Filter files. - for file_ in files: - the_file = root / file_ - if is_path_ignored( - the_file, - include_submodules=include_submodules, - include_meson_subprojects=include_meson_subprojects, - vcs_strategy=vcs_strategy, - ): - _LOGGER.debug("ignoring '%s'", the_file) - continue - - _LOGGER.debug("yielding '%s'", the_file) - yield the_file diff --git a/tests/conftest.py b/tests/conftest.py index 94b3b0952..699aeda7e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -58,6 +58,8 @@ sys.implementation.name != "cpython", reason="only CPython supported" ) git = pytest.mark.skipif(not GIT_EXE, reason="requires git") +hg = pytest.mark.skipif(not HG_EXE, reason="requires mercurial") +pijul = pytest.mark.skipif(not PIJUL_EXE, reason="requires pijul") no_root = pytest.mark.xfail(is_root, reason="fails when user is root") posix = pytest.mark.skipif(not is_posix, reason="Windows not supported") diff --git a/tests/test_covered_files.py b/tests/test_covered_files.py new file mode 100644 index 000000000..5f50ca1b4 --- /dev/null +++ b/tests/test_covered_files.py @@ -0,0 +1,257 @@ +# SPDX-FileCopyrightText: 2017 Free Software Foundation Europe e.V. +# SPDX-FileCopyrightText: 2022 Florian Snow +# SPDX-FileCopyrightText: 2023 Carmen Bianca BAKKER +# SPDX-FileCopyrightText: © 2020 Liferay, Inc. +# +# SPDX-License-Identifier: GPL-3.0-or-later + +"""Tests for reuse.covered_files.""" + +import os +from pathlib import Path + +from conftest import git, hg, pijul, posix + +from reuse.covered_files import all_files +from reuse.vcs import VCSStrategyGit, VCSStrategyHg, VCSStrategyPijul + + +class TestAllFiles: + """Test the all_files function.""" + + def test_simple(self, empty_directory): + """Given a directory with some files, yield all files.""" + (empty_directory / "foo").write_text("foo") + (empty_directory / "bar").write_text("foo") + + assert {file_.name for file_ in all_files(empty_directory)} == { + "foo", + "bar", + } + + def test_ignore_dot_license(self, empty_directory): + """When file and file.license are present, only yield file.""" + (empty_directory / "foo").write_text("foo") + (empty_directory / "foo.license").write_text("foo") + + assert {file_.name for file_ in all_files(empty_directory)} == {"foo"} + + def test_ignore_cal_license(self, empty_directory): + """CAL licenses contain SPDX tags referencing themselves. They should be + skipped. + """ + (empty_directory / "CAL-1.0").write_text("foo") + (empty_directory / "CAL-1.0.txt").write_text("foo") + (empty_directory / "CAL-1.0-Combined-Work-Exception").write_text("foo") + (empty_directory / "CAL-1.0-Combined-Work-Exception.txt").write_text( + "foo" + ) + + assert not list(all_files(empty_directory)) + + def test_ignore_shl_license(self, empty_directory): + """SHL-2.1 contains an SPDX tag referencing itself. It should be + skipped. + """ + (empty_directory / "SHL-2.1").write_text("foo") + (empty_directory / "SHL-2.1.txt").write_text("foo") + + assert not list(all_files(empty_directory)) + + def test_ignore_git(self, empty_directory): + """When the git directory is present, ignore it.""" + (empty_directory / ".git").mkdir() + (empty_directory / ".git/config").write_text("foo") + + assert not list(all_files(empty_directory)) + + def test_ignore_hg(self, empty_directory): + """When the hg directory is present, ignore it.""" + (empty_directory / ".hg").mkdir() + (empty_directory / ".hg/config").write_text("foo") + + assert not list(all_files(empty_directory)) + + def test_ignore_license_copying(self, empty_directory): + """When there are files names LICENSE, LICENSE.ext, COPYING, or + COPYING.ext, ignore them. + """ + (empty_directory / "LICENSE").write_text("foo") + (empty_directory / "LICENSE.txt").write_text("foo") + (empty_directory / "COPYING").write_text("foo") + (empty_directory / "COPYING.txt").write_text("foo") + + assert not list(all_files(empty_directory)) + + def test_not_ignore_license_copying_no_ext(self, empty_directory): + """Do not ignore files that start with LICENSE or COPYING and are + followed by some non-extension text. + """ + (empty_directory / "LICENSE_README.md").write_text("foo") + (empty_directory / "COPYING2").write_text("foo") + + assert len(list(all_files(empty_directory))) == 2 + + @posix + def test_ignore_symlinks(self, empty_directory): + """All symlinks must be ignored.""" + (empty_directory / "blob").write_text("foo") + (empty_directory / "symlink").symlink_to("blob") + + assert Path("symlink").absolute() not in all_files(empty_directory) + + def test_ignore_zero_sized(self, empty_directory): + """Empty files should be skipped.""" + (empty_directory / "foo").touch() + + assert Path("foo").absolute() not in all_files(empty_directory) + + +@git +class TestAllFilesGit: + """Test the all_files function with git.""" + + def test_simple(self, git_repository): + """Given a Git repository where some files are ignored, do not yield + those files. + """ + assert Path("build/hello.py").absolute() not in all_files( + git_repository, vcs_strategy=VCSStrategyGit(git_repository) + ) + + def test_not_ignored_if_no_strategy(self, git_repository): + """If no strategy is provided, the file is not ignored.""" + assert Path("build/hello.py").absolute() in all_files(git_repository) + + def test_different_cwd(self, git_repository): + """Given a Git repository where some files are ignored, do not yield + those files. + + Be in a different CWD during the above. + """ + os.chdir(git_repository / "LICENSES") + assert Path("build/hello.py").absolute() not in all_files( + git_repository, vcs_strategy=VCSStrategyGit(git_repository) + ) + + def test_ignored_contains_space(self, git_repository): + """Files that contain spaces are also ignored.""" + (git_repository / "I contain spaces.pyc").write_text("foo") + assert Path("I contain spaces.pyc").absolute() not in all_files( + git_repository, vcs_strategy=VCSStrategyGit(git_repository) + ) + + @posix + def test_ignored_contains_newline(self, git_repository): + """Files that contain newlines are also ignored.""" + (git_repository / "hello\nworld.pyc").write_text("foo") + assert Path("hello\nworld.pyc").absolute() not in all_files( + git_repository, vcs_strategy=VCSStrategyGit(git_repository) + ) + + def test_ignore_submodules(self, submodule_repository): + """Normally ignore submodules.""" + (submodule_repository / "submodule/foo.py").write_text("foo") + assert Path("submodule/foo.py").absolute() not in all_files( + submodule_repository, + vcs_strategy=VCSStrategyGit(submodule_repository), + ) + + def test_include_submodules(self, submodule_repository): + """If include_submodules is True, include files from the submodule.""" + (submodule_repository / "submodule/foo.py").write_text("foo") + assert Path("submodule/foo.py").absolute() in all_files( + submodule_repository, + include_submodules=True, + vcs_strategy=VCSStrategyGit(submodule_repository), + ) + + def test_submodule_is_ignored(self, submodule_repository): + """If a submodule is ignored, all_files should not raise an Exception""" + (submodule_repository / "submodule/foo.py").write_text("foo") + gitignore = submodule_repository / ".gitignore" + contents = gitignore.read_text() + contents += "\nsubmodule/\n" + gitignore.write_text(contents) + assert Path("submodule/foo.py").absolute() not in all_files( + submodule_repository, + vcs_strategy=VCSStrategyGit(submodule_repository), + ) + + +@hg +class TestAllFilesHg: + """Test the all_files function with Mercurial.""" + + def test_simple(self, hg_repository): + """Given a mercurial repository where some files are ignored, do not + yield those files. + """ + assert Path("build/hello.py").absolute() not in all_files( + hg_repository, vcs_strategy=VCSStrategyHg(hg_repository) + ) + + def test_different_cwd(self, hg_repository): + """Given a mercurial repository where some files are ignored, do not + yield those files. + + Be in a different CWD during the above. + """ + os.chdir(hg_repository / "LICENSES") + assert Path("build/hello.py").absolute() not in all_files( + hg_repository, vcs_strategy=VCSStrategyHg(hg_repository) + ) + + def test_ignored_contains_space(self, hg_repository): + """File names that contain spaces are also ignored.""" + (hg_repository / "I contain spaces.pyc").touch() + assert Path("I contain spaces.pyc").absolute() not in all_files( + hg_repository, vcs_strategy=VCSStrategyHg(hg_repository) + ) + + @posix + def test_ignored_contains_newline(self, hg_repository): + """File names that contain newlines are also ignored.""" + (hg_repository / "hello\nworld.pyc").touch() + assert Path("hello\nworld.pyc").absolute() not in all_files( + hg_repository, vcs_strategy=VCSStrategyHg(hg_repository) + ) + + +@pijul +class TestAllFilesPijul: + """Test the all_files function with Pijul.""" + + def test_simple(self, pijul_repository): + """Given a pijul repository where some files are ignored, do not yield + those files. + """ + assert Path("build/hello.py").absolute() not in all_files( + pijul_repository, vcs_strategy=VCSStrategyPijul(pijul_repository) + ) + + def test_all_files_pijul_ignored_different_cwd(self, pijul_repository): + """Given a pijul repository where some files are ignored, do not yield + those files. + + Be in a different CWD during the above. + """ + os.chdir(pijul_repository / "LICENSES") + assert Path("build/hello.py").absolute() not in all_files( + pijul_repository, vcs_strategy=VCSStrategyPijul(pijul_repository) + ) + + def test_ignored_contains_space(self, pijul_repository): + """File names that contain spaces are also ignored.""" + (pijul_repository / "I contain spaces.pyc").touch() + assert Path("I contain spaces.pyc").absolute() not in all_files( + pijul_repository, vcs_strategy=VCSStrategyPijul(pijul_repository) + ) + + @posix + def test_ignored_contains_newline(self, pijul_repository): + """File names that contain newlines are also ignored.""" + (pijul_repository / "hello\nworld.pyc").touch() + assert Path("hello\nworld.pyc").absolute() not in all_files( + pijul_repository, vcs_strategy=VCSStrategyPijul(pijul_repository) + ) diff --git a/tests/test_project.py b/tests/test_project.py index 0041466ea..955389b24 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -12,13 +12,15 @@ import warnings from inspect import cleandoc from pathlib import Path +from unittest import mock import pytest -from conftest import RESOURCES_DIRECTORY, posix +from conftest import RESOURCES_DIRECTORY from license_expression import LicenseSymbol from reuse import ReuseInfo, SourceType from reuse._util import _LICENSING +from reuse.covered_files import all_files from reuse.global_licensing import ( GlobalLicensingParseError, NestedReuseTOML, @@ -53,225 +55,67 @@ def test_project_conflicting_global_licensing(empty_directory): Project.from_directory(empty_directory) -def test_all_files(empty_directory): - """Given a directory with some files, yield all files.""" - (empty_directory / "foo").write_text("foo") - (empty_directory / "bar").write_text("foo") +class TestProjectAllFiles: + """Test Project.all_files.""" - project = Project.from_directory(empty_directory) - assert {file_.name for file_ in project.all_files()} == {"foo", "bar"} - - -def test_all_files_ignore_dot_license(empty_directory): - """When file and file.license are present, only yield file.""" - (empty_directory / "foo").write_text("foo") - (empty_directory / "foo.license").write_text("foo") - - project = Project.from_directory(empty_directory) - assert {file_.name for file_ in project.all_files()} == {"foo"} - - -def test_all_files_ignore_cal_license(empty_directory): - """CAL licenses contain SPDX tags referencing themselves. They should be - skipped. - """ - (empty_directory / "CAL-1.0").write_text("foo") - (empty_directory / "CAL-1.0.txt").write_text("foo") - (empty_directory / "CAL-1.0-Combined-Work-Exception").write_text("foo") - (empty_directory / "CAL-1.0-Combined-Work-Exception.txt").write_text("foo") - - project = Project.from_directory(empty_directory) - assert not list(project.all_files()) - - -def test_all_files_ignore_shl_license(empty_directory): - """SHL-2.1 contains an SPDX tag referencing itself. It should be skipped.""" - (empty_directory / "SHL-2.1").write_text("foo") - (empty_directory / "SHL-2.1.txt").write_text("foo") - - project = Project.from_directory(empty_directory) - assert not list(project.all_files()) - - -def test_all_files_ignore_git(empty_directory): - """When the git directory is present, ignore it.""" - (empty_directory / ".git").mkdir() - (empty_directory / ".git/config").write_text("foo") - - project = Project.from_directory(empty_directory) - assert not list(project.all_files()) - - -def test_all_files_ignore_hg(empty_directory): - """When the hg directory is present, ignore it.""" - (empty_directory / ".hg").mkdir() - (empty_directory / ".hg/config").write_text("foo") - - project = Project.from_directory(empty_directory) - assert not list(project.all_files()) - - -def test_all_files_ignore_license_copying(empty_directory): - """When there are files names LICENSE, LICENSE.ext, COPYING, or COPYING.ext, - ignore them. - """ - (empty_directory / "LICENSE").write_text("foo") - (empty_directory / "LICENSE.txt").write_text("foo") - (empty_directory / "COPYING").write_text("foo") - (empty_directory / "COPYING.txt").write_text("foo") - - project = Project.from_directory(empty_directory) - assert not list(project.all_files()) - - -def test_all_files_not_ignore_license_copying_no_ext(empty_directory): - """Do not ignore files that start with LICENSE or COPYING and are followed - by some non-extension text. - """ - (empty_directory / "LICENSE_README.md").write_text("foo") - (empty_directory / "COPYING2").write_text("foo") - - project = Project.from_directory(empty_directory) - assert len(list(project.all_files())) == 2 - - -@posix -def test_all_files_symlinks(empty_directory): - """All symlinks must be ignored.""" - (empty_directory / "blob").write_text("foo") - (empty_directory / "blob.license").write_text( - cleandoc( - """ - SPDX-FileCopyrightText: Jane Doe - - SPDX-License-Identifier: GPL-3.0-or-later - """ + def test_with_mock(self, monkeypatch, empty_directory): + """Instead of testing the entire behaviour, just test that a mock is + called and its value is returned. + """ + mock_all_files = mock.create_autospec(all_files) + mock_all_files.return_value = "foo" + monkeypatch.setattr("reuse.project.all_files", mock_all_files) + + project = Project.from_directory(empty_directory) + result = project.all_files(empty_directory) + assert result == "foo" + + mock_all_files.assert_called_once_with( + empty_directory, + include_submodules=project.include_submodules, + include_meson_subprojects=project.include_meson_subprojects, + vcs_strategy=project.vcs_strategy, ) - ) - (empty_directory / "symlink").symlink_to("blob") - project = Project.from_directory(empty_directory) - assert Path("symlink").absolute() not in project.all_files() + def test_with_mock_implicit_dir(self, monkeypatch, empty_directory): + """If no argument is given to Project.all_files, project.root is + implied. + """ + mock_all_files = mock.create_autospec(all_files) + mock_all_files.return_value = "foo" + monkeypatch.setattr("reuse.project.all_files", mock_all_files) + + project = Project.from_directory(empty_directory) + result = project.all_files() + assert result == "foo" + + mock_all_files.assert_called_once_with( + empty_directory, + include_submodules=project.include_submodules, + include_meson_subprojects=project.include_meson_subprojects, + vcs_strategy=project.vcs_strategy, + ) -def test_all_files_ignore_zero_sized(empty_directory): - """Empty files should be skipped.""" - (empty_directory / "foo").touch() - - project = Project.from_directory(empty_directory) - assert Path("foo").absolute() not in project.all_files() - - -def test_all_files_git_ignored(git_repository): - """Given a Git repository where some files are ignored, do not yield those - files. - """ - project = Project.from_directory(git_repository) - assert Path("build/hello.py").absolute() not in project.all_files() - - -def test_all_files_git_ignored_different_cwd(git_repository): - """Given a Git repository where some files are ignored, do not yield those - files. - - Be in a different CWD during the above. - """ - os.chdir(git_repository / "LICENSES") - project = Project.from_directory(git_repository) - assert Path("build/hello.py").absolute() not in project.all_files() - - -def test_all_files_git_ignored_contains_space(git_repository): - """Files that contain spaces are also ignored.""" - (git_repository / "I contain spaces.pyc").write_text("foo") - project = Project.from_directory(git_repository) - assert Path("I contain spaces.pyc").absolute() not in project.all_files() - - -@posix -def test_all_files_git_ignored_contains_newline(git_repository): - """Files that contain newlines are also ignored.""" - (git_repository / "hello\nworld.pyc").write_text("foo") - project = Project.from_directory(git_repository) - assert Path("hello\nworld.pyc").absolute() not 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") - gitignore = submodule_repository / ".gitignore" - contents = gitignore.read_text() - contents += "\nsubmodule/\n" - gitignore.write_text(contents) - project = Project.from_directory(submodule_repository) - assert Path("submodule/foo.py").absolute() not in project.all_files() - - -def test_all_files_hg_ignored(hg_repository): - """Given a mercurial repository where some files are ignored, do not yield - those files. - """ - project = Project.from_directory(hg_repository) - assert Path("build/hello.py").absolute() not in project.all_files() - - -def test_all_files_hg_ignored_different_cwd(hg_repository): - """Given a mercurial repository where some files are ignored, do not yield - those files. - - Be in a different CWD during the above. - """ - os.chdir(hg_repository / "LICENSES") - project = Project.from_directory(hg_repository) - assert Path("build/hello.py").absolute() not in project.all_files() - - -def test_all_files_hg_ignored_contains_space(hg_repository): - """File names that contain spaces are also ignored.""" - (hg_repository / "I contain spaces.pyc").touch() - project = Project.from_directory(hg_repository) - assert Path("I contain spaces.pyc").absolute() not in project.all_files() - - -@posix -def test_all_files_hg_ignored_contains_newline(hg_repository): - """File names that contain newlines are also ignored.""" - (hg_repository / "hello\nworld.pyc").touch() - project = Project.from_directory(hg_repository) - assert Path("hello\nworld.pyc").absolute() not in project.all_files() - - -def test_all_files_pijul_ignored(pijul_repository): - """Given a pijul repository where some files are ignored, do not yield - those files. - """ - project = Project.from_directory(pijul_repository) - assert Path("build/hello.py").absolute() not in project.all_files() - - -def test_all_files_pijul_ignored_different_cwd(pijul_repository): - """Given a pijul repository where some files are ignored, do not yield - those files. - - Be in a different CWD during the above. - """ - os.chdir(pijul_repository / "LICENSES") - project = Project.from_directory(pijul_repository) - assert Path("build/hello.py").absolute() not in project.all_files() - - -def test_all_files_pijul_ignored_contains_space(pijul_repository): - """File names that contain spaces are also ignored.""" - (pijul_repository / "I contain spaces.pyc").touch() - project = Project.from_directory(pijul_repository) - assert Path("I contain spaces.pyc").absolute() not in project.all_files() - + def test_with_mock_includes(self, monkeypatch, empty_directory): + """include_submodules and include_meson_subprojects are true.""" + mock_all_files = mock.create_autospec(all_files) + mock_all_files.return_value = "foo" + monkeypatch.setattr("reuse.project.all_files", mock_all_files) -@posix -def test_all_files_pijul_ignored_contains_newline(pijul_repository): - """File names that contain newlines are also ignored.""" - (pijul_repository / "hello\nworld.pyc").touch() - project = Project.from_directory(pijul_repository) - assert Path("hello\nworld.pyc").absolute() not in project.all_files() + project = Project.from_directory( + empty_directory, + include_submodules=True, + include_meson_subprojects=True, + ) + result = project.all_files() + assert result == "foo" + + mock_all_files.assert_called_once_with( + empty_directory, + include_submodules=project.include_submodules, + include_meson_subprojects=project.include_meson_subprojects, + vcs_strategy=project.vcs_strategy, + ) def test_reuse_info_of_file_does_not_exist(fake_repository):