Skip to content

Commit

Permalink
feat: include user specified files even if excluded (#383)
Browse files Browse the repository at this point in the history
* feat: include user specified files even if excluded

* fix: remove unnecessary changes to file finder

* fix: add back unnecessarily removed test

Signed-off-by: joseph-sentry <joseph.sawaya@sentry.io>
  • Loading branch information
joseph-sentry committed Mar 12, 2024
1 parent 2272547 commit 5a36952
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 98 deletions.
3 changes: 1 addition & 2 deletions codecov_cli/services/upload/file_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def get_user_specified_files(self, regex_patterns_to_exclude):
files_excluded_but_user_includes.append(str(file))
if files_excluded_but_user_includes:
logger.warning(
"Some files being explicitly added are found in the list of excluded files for upload.",
"Some files being explicitly added are found in the list of excluded files for upload. We are still going to search for the explicitly added files.",
extra=dict(
extra_log_attributes=dict(files=files_excluded_but_user_includes)
),
Expand All @@ -246,7 +246,6 @@ def get_user_specified_files(self, regex_patterns_to_exclude):
self.search_root,
self.folders_to_ignore,
filename_include_regex=regex_patterns_to_include,
filename_exclude_regex=regex_patterns_to_exclude,
multipart_include_regex=multipart_include_regex,
)
)
Expand Down
280 changes: 184 additions & 96 deletions tests/services/upload/test_coverage_file_finder.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import tempfile
import unittest
from pathlib import Path

import pytest

from codecov_cli.services.upload.file_finder import FileFinder
from codecov_cli.types import UploadCollectionResultFile

Expand Down Expand Up @@ -153,179 +154,266 @@ def test_find_coverage_files_test_results(self, tmp_path):
assert actual - expected == {UploadCollectionResultFile(extra)}


class TestCoverageFileFinderUserInput(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory() # Create a temporary directory
self.project_root = Path(self.temp_dir.name)
self.folders_to_ignore = []
self.explicitly_listed_files = [
self.project_root / "test_file.abc",
self.project_root / "subdirectory" / "another_file.abc",
]
self.disable_search = False
self.coverage_file_finder = FileFinder(
self.project_root,
self.folders_to_ignore,
self.explicitly_listed_files,
self.disable_search,
)

def tearDown(self):
self.temp_dir.cleanup() # Clean up the temporary directory

def test_find_coverage_files_with_existing_files(self):
# Create some sample coverage files
@pytest.fixture()
def coverage_file_finder_fixture():
temp_dir = tempfile.TemporaryDirectory() # Create a temporary directory
project_root = Path(temp_dir.name)
folders_to_ignore = []
explicitly_listed_files = [
project_root / "test_file.abc",
project_root / "subdirectory" / "another_file.abc",
]
disable_search = False
coverage_file_finder = FileFinder(
project_root,
folders_to_ignore,
explicitly_listed_files,
disable_search,
)
yield project_root, coverage_file_finder
temp_dir.cleanup()


class TestCoverageFileFinderUserInput:
def test_find_coverage_files_with_existing_files(
self, coverage_file_finder_fixture
):
# Create some sample coverage coverage_file_finder_fixture
(
project_root,
coverage_file_finder,
) = coverage_file_finder_fixture
coverage_files = [
self.project_root / "coverage.xml",
self.project_root / "subdirectory" / "test_coverage.xml",
self.project_root / "other_file.txt",
self.project_root / ".tox" / "another_file.abc",
project_root / "coverage.xml",
project_root / "subdirectory" / "test_coverage.xml",
project_root / "other_file.txt",
project_root / ".tox" / "another_file.abc",
]
(self.project_root / "subdirectory").mkdir()
(self.project_root / ".tox").mkdir()
(project_root / "subdirectory").mkdir()
(project_root / ".tox").mkdir()
for file in coverage_files:
file.touch()

result = sorted(
[file.get_filename() for file in self.coverage_file_finder.find_files()]
[file.get_filename() for file in coverage_file_finder.find_files()]
)
expected = [
UploadCollectionResultFile(Path(f"{self.project_root}/coverage.xml")),
UploadCollectionResultFile(Path(f"{project_root}/coverage.xml")),
UploadCollectionResultFile(
Path(f"{self.project_root}/subdirectory/test_coverage.xml")
Path(f"{project_root}/subdirectory/test_coverage.xml")
),
]
expected_paths = sorted([file.get_filename() for file in expected])
self.assertEqual(result, expected_paths)

def test_find_coverage_files_with_no_files(self):
result = self.coverage_file_finder.find_files()
self.assertEqual(result, [])

def test_find_coverage_files_with_disabled_search(self):
# Create some sample coverage files
print("project root", self.project_root)
assert result == expected_paths

def test_find_coverage_files_with_no_files(self, coverage_file_finder_fixture):
(
_,
coverage_file_finder,
) = coverage_file_finder_fixture
result = coverage_file_finder.find_files()
assert result == []

def test_find_coverage_files_with_disabled_search(
self, coverage_file_finder_fixture
):
(
project_root,
coverage_file_finder,
) = coverage_file_finder_fixture
# Create some sample coverage coverage_file_finder_fixture
print("project root", project_root)
coverage_files = [
self.project_root / "test_file.abc",
self.project_root / "subdirectory" / "another_file.abc",
self.project_root / "subdirectory" / "test_coverage.xml",
self.project_root / "other_file.txt",
self.project_root / ".tox" / "another_file.abc",
project_root / "test_file.abc",
project_root / "subdirectory" / "another_file.abc",
project_root / "subdirectory" / "test_coverage.xml",
project_root / "other_file.txt",
project_root / ".tox" / "another_file.abc",
]
(self.project_root / "subdirectory").mkdir()
(self.project_root / ".tox").mkdir()
(project_root / "subdirectory").mkdir()
(project_root / ".tox").mkdir()
for file in coverage_files:
file.touch()

# Disable search
self.coverage_file_finder.disable_search = True
coverage_file_finder.disable_search = True

result = sorted(
[file.get_filename() for file in self.coverage_file_finder.find_files()]
[file.get_filename() for file in coverage_file_finder.find_files()]
)

expected = [
UploadCollectionResultFile(Path(f"{self.project_root}/test_file.abc")),
UploadCollectionResultFile(Path(f"{project_root}/test_file.abc")),
UploadCollectionResultFile(
Path(f"{self.project_root}/subdirectory/another_file.abc")
Path(f"{project_root}/subdirectory/another_file.abc")
),
]
expected_paths = sorted([file.get_filename() for file in expected])

self.assertEqual(result, expected_paths)
assert result == expected_paths

def test_find_coverage_files_with_user_specified_files(self):
# Create some sample coverage files
def test_find_coverage_files_with_user_specified_files(
self, coverage_file_finder_fixture
):
(
project_root,
coverage_file_finder,
) = coverage_file_finder_fixture

# Create some sample coverage coverage_file_finder_fixture
coverage_files = [
self.project_root / "coverage.xml",
self.project_root / "subdirectory" / "test_coverage.xml",
self.project_root / "test_file.abc",
self.project_root / "subdirectory" / "another_file.abc",
self.project_root / ".tox" / "another_file.abc",
project_root / "coverage.xml",
project_root / "subdirectory" / "test_coverage.xml",
project_root / "test_file.abc",
project_root / "subdirectory" / "another_file.abc",
project_root / ".tox" / "another_file.abc",
]
(self.project_root / "subdirectory").mkdir()
(self.project_root / ".tox").mkdir()
(project_root / "subdirectory").mkdir()
(project_root / ".tox").mkdir()
for file in coverage_files:
file.touch()

result = sorted(
[file.get_filename() for file in self.coverage_file_finder.find_files()]
[file.get_filename() for file in coverage_file_finder.find_files()]
)

expected = [
UploadCollectionResultFile(Path(f"{self.project_root}/coverage.xml")),
UploadCollectionResultFile(Path(f"{project_root}/coverage.xml")),
UploadCollectionResultFile(
Path(f"{self.project_root}/subdirectory/test_coverage.xml")
Path(f"{project_root}/subdirectory/test_coverage.xml")
),
UploadCollectionResultFile(Path(f"{self.project_root}/test_file.abc")),
UploadCollectionResultFile(Path(f"{project_root}/test_file.abc")),
UploadCollectionResultFile(
Path(f"{self.project_root}/subdirectory/another_file.abc")
Path(f"{project_root}/subdirectory/another_file.abc")
),
]
expected_paths = sorted([file.get_filename() for file in expected])
self.assertEqual(result, expected_paths)
assert result == expected_paths

def test_find_coverage_files_with_user_specified_files_not_found(self):
# Create some sample coverage files
def test_find_coverage_files_with_user_specified_files_not_found(
self, coverage_file_finder_fixture
):
(
project_root,
coverage_file_finder,
) = coverage_file_finder_fixture

# Create some sample coverage coverage_file_finder_fixture
coverage_files = [
self.project_root / "coverage.xml",
self.project_root / "subdirectory" / "test_coverage.xml",
self.project_root / ".tox" / "another_file.abc",
project_root / "coverage.xml",
project_root / "subdirectory" / "test_coverage.xml",
project_root / ".tox" / "another_file.abc",
]
(self.project_root / "subdirectory").mkdir()
(self.project_root / ".tox").mkdir()
(project_root / "subdirectory").mkdir()
(project_root / ".tox").mkdir()
for file in coverage_files:
file.touch()

# Add a non-existent file to explicitly_listed_files
self.coverage_file_finder.explicitly_listed_files.append(
self.project_root / "non_existent.xml"
coverage_file_finder.explicitly_listed_files.append(
project_root / "non_existent.xml"
)

result = sorted(
[file.get_filename() for file in self.coverage_file_finder.find_files()]
[file.get_filename() for file in coverage_file_finder.find_files()]
)

expected = [
UploadCollectionResultFile(Path(f"{self.project_root}/coverage.xml")),
UploadCollectionResultFile(Path(f"{project_root}/coverage.xml")),
UploadCollectionResultFile(
Path(f"{self.project_root}/subdirectory/test_coverage.xml")
Path(f"{project_root}/subdirectory/test_coverage.xml")
),
]
expected_paths = sorted([file.get_filename() for file in expected])
self.assertEqual(result, expected_paths)
assert result == expected_paths

def test_find_coverage_files_with_user_specified_files_in_default_ignored_folder(
self,
self, coverage_file_finder_fixture
):

(
project_root,
coverage_file_finder,
) = coverage_file_finder_fixture

# Create some sample coverage files
coverage_files = [
self.project_root / "coverage.xml",
self.project_root / "subdirectory" / "test_coverage.xml",
self.project_root / "test_file.abc",
self.project_root / "subdirectory" / "another_file.abc",
self.project_root / ".tox" / "another_file.abc",
project_root / "coverage.xml",
project_root / "subdirectory" / "test_coverage.xml",
project_root / "test_file.abc",
project_root / "subdirectory" / "another_file.abc",
project_root / ".tox" / "another_file.abc",
]
(self.project_root / "subdirectory").mkdir()
(self.project_root / ".tox").mkdir()
(project_root / "subdirectory").mkdir()
(project_root / ".tox").mkdir()
for file in coverage_files:
file.touch()

self.coverage_file_finder.explicitly_listed_files = [
self.project_root / ".tox" / "another_file.abc",
coverage_file_finder.explicitly_listed_files = [
project_root / ".tox" / "another_file.abc",
]
result = sorted(
[file.get_filename() for file in coverage_file_finder.find_files()]
)

expected = [
UploadCollectionResultFile(Path(f"{project_root}/coverage.xml")),
UploadCollectionResultFile(
Path(f"{project_root}/subdirectory/test_coverage.xml")
),
UploadCollectionResultFile(Path(f"{project_root}/.tox/another_file.abc")),
]
expected_paths = sorted([file.get_filename() for file in expected])

assert result == expected_paths

def test_find_coverage_files_with_user_specified_files_in_excluded(
self, capsys, coverage_file_finder_fixture
):
(
project_root,
coverage_file_finder,
) = coverage_file_finder_fixture

# Create some sample coverage coverage_file_finder_fixture
coverage_files = [
project_root / "coverage.xml",
project_root / "subdirectory" / "test_coverage.xml",
project_root / "test_file.abc",
project_root / "subdirectory" / "another_file.abc",
project_root / "subdirectory" / "another_file.bash",
project_root / ".tox" / "another_file.abc",
]
(project_root / "subdirectory").mkdir()
(project_root / ".tox").mkdir()
for file in coverage_files:
file.touch()

coverage_file_finder.explicitly_listed_files.append(
project_root / "subdirectory" / "another_file.bash"
)
result = sorted(
[file.get_filename() for file in self.coverage_file_finder.find_files()]
[file.get_filename() for file in coverage_file_finder.find_files()]
)

expected = [
UploadCollectionResultFile(Path(f"{self.project_root}/coverage.xml")),
UploadCollectionResultFile(Path(f"{project_root}/coverage.xml")),
UploadCollectionResultFile(
Path(f"{self.project_root}/subdirectory/test_coverage.xml")
Path(f"{project_root}/subdirectory/test_coverage.xml")
),
UploadCollectionResultFile(Path(f"{project_root}/test_file.abc")),
UploadCollectionResultFile(
Path(f"{self.project_root}/.tox/another_file.abc")
Path(f"{project_root}/subdirectory/another_file.abc")
),
UploadCollectionResultFile(
Path(f"{project_root}/subdirectory/another_file.bash")
),
]
expected_paths = sorted([file.get_filename() for file in expected])
self.assertEqual(result, expected_paths)

assert result == expected_paths

assert (
"Some files being explicitly added are found in the list of excluded files for upload. We are still going to search for the explicitly added files."
in capsys.readouterr().err
)

0 comments on commit 5a36952

Please sign in to comment.