Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 13 additions & 1 deletion gittensor/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ def _calculate_file_extension(self) -> str:
def is_test_file(self) -> bool:
filename_lower = self.filename.lower()
basename = filename_lower.split('/')[-1]
basename_original = self.filename.split('/')[-1]

# pytest fixture file — applies project-wide regardless of directory
if basename == 'conftest.py':
return True

test_dir_patterns = [
r'(^|/)tests?/',
Expand All @@ -100,7 +105,14 @@ def is_test_file(self) -> bool:
r'^tests\.[^.]+$',
]

return any(re.search(pattern, basename) for pattern in test_patterns)
if any(re.search(pattern, basename) for pattern in test_patterns):
return True

# PascalCase test conventions in compiled languages: `FooTest.kt`,
# `FooTests.swift`, `AccountServiceTests.cs`. The capital `T` boundary
# is what lets us distinguish these from incidental endings like
# `Latest.kt`, so we match against the original-case basename.
return bool(re.search(r'[A-Z][A-Za-z0-9]*Tests?\.(swift|kt|java|cs|scala)$', basename_original))

@classmethod
def from_github_response(cls, pr_number: int, repository_full_name: str, file_diff: DefaultDict) -> 'FileChange':
Expand Down
44 changes: 44 additions & 0 deletions tests/test_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,50 @@ def test_is_test_file_preserves_existing_test_conventions():
assert _file_change('src/foo/bar.py').is_test_file() is False


@pytest.mark.parametrize(
'filename',
[
'app/src/main/java/com/example/FooTest.java',
'app/src/main/java/com/example/FooTests.java',
'app/src/main/kotlin/com/example/FooTest.kt',
'app/src/main/kotlin/com/example/FooTests.kt',
'MyApp/Tests/FooTests.swift',
'MyApp/Tests/FooTest.swift',
'src/MyProject.Tests/AccountServiceTests.cs',
'src/MyProject.Tests/AccountServiceTest.cs',
'core/src/main/scala/com/example/FooTest.scala',
],
)
def test_is_test_file_detects_pascalcase_compiled_language_tests(filename):
assert _file_change(filename).is_test_file() is True


@pytest.mark.parametrize(
'filename',
[
'docs/latest.md',
'src/main/kotlin/com/example/Latest.kt',
'app/src/main/java/com/example/Manifest.java',
'src/styles/contest.css',
],
)
def test_is_test_file_rejects_pascalcase_lookalikes(filename):
assert _file_change(filename).is_test_file() is False


@pytest.mark.parametrize(
'filename',
[
'conftest.py',
'tests/conftest.py',
'project/conftest.py',
'project/sub/package/conftest.py',
],
)
def test_is_test_file_detects_conftest_at_any_depth(filename):
assert _file_change(filename).is_test_file() is True


def test_pull_request_handles_deleted_label_event():
pr_data = {
'number': 42,
Expand Down