Skip to content

Commit 95e0bb0

Browse files
fix: stricter check for "tests" and "docs" directories in sp-repo-review (#527)
* Stricter test for "docs" folder * Stricter checks for existence of "tests" directory * Updated directory name "test" => "tests" in test_py005_src test. * style: pre-commit fixes * Slightly more lenient check for "tests" directory. Also added some extra unit tests for the Py004 and Py005 checks. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent a0fcb39 commit 95e0bb0

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

src/sp_repo_review/checks/general.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ class PY004(General):
6565
@staticmethod
6666
def check(package: Traversable) -> bool:
6767
"Projects must have documentation in a folder called docs (disable if not applicable)"
68-
return len([p for p in package.iterdir() if "doc" in p.name]) > 0
68+
return (
69+
len([p for p in package.iterdir() if p.is_dir() and p.name == "docs"]) > 0
70+
)
6971

7072

7173
class PY005(General):
@@ -75,9 +77,18 @@ class PY005(General):
7577

7678
@staticmethod
7779
def check(package: Traversable) -> bool:
78-
"Projects must have a folder called `*test*` or `src/*/*test*`"
80+
"Projects must have a folder called `test*` or `src/*/test*`"
7981
# Out-of-source tests
80-
if len([p for p in package.iterdir() if "test" in p.name]) > 0:
82+
if (
83+
len(
84+
[
85+
p
86+
for p in package.iterdir()
87+
if p.is_dir() and p.name.startswith("test")
88+
]
89+
)
90+
> 0
91+
):
8192
return True
8293

8394
# In-source tests
@@ -86,7 +97,14 @@ def check(package: Traversable) -> bool:
8697
for pkg in src.iterdir():
8798
if (
8899
pkg.is_dir()
89-
and len([p for p in pkg.iterdir() if "test" in p.name]) > 0
100+
and len(
101+
[
102+
p
103+
for p in pkg.iterdir()
104+
if p.is_dir() and p.name.startswith("test")
105+
]
106+
)
107+
> 0
90108
):
91109
return True
92110
return False

tests/test_general.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ def test_py004(tmp_path: Path):
5454
assert compute_check("PY004", package=simple).result
5555

5656

57+
def test_py004_not_dir(tmp_path: Path):
58+
simple = tmp_path / "simple"
59+
simple.mkdir()
60+
simple.joinpath("docs")
61+
assert not compute_check("PY004", package=simple).result
62+
63+
5764
def test_py004_missing(tmp_path: Path):
5865
simple = tmp_path / "simple"
5966
simple.mkdir()
@@ -67,6 +74,34 @@ def test_py005(tmp_path: Path):
6774
assert compute_check("PY005", package=simple).result
6875

6976

77+
def test_py005_alt_singular(tmp_path: Path):
78+
simple = tmp_path / "simple"
79+
simple.mkdir()
80+
simple.joinpath("test").mkdir()
81+
assert compute_check("PY005", package=simple).result
82+
83+
84+
def test_py005_alt_integration(tmp_path: Path):
85+
simple = tmp_path / "simple"
86+
simple.mkdir()
87+
simple.joinpath("tests-integration").mkdir()
88+
assert compute_check("PY005", package=simple).result
89+
90+
91+
def test_py005_not_folder(tmp_path: Path):
92+
simple = tmp_path / "simple"
93+
simple.mkdir()
94+
simple.joinpath("tests")
95+
assert not compute_check("PY005", package=simple).result
96+
97+
98+
def test_py005_not_tests(tmp_path: Path):
99+
simple = tmp_path / "simple"
100+
simple.mkdir()
101+
simple.joinpath("fastest").mkdir()
102+
assert not compute_check("PY005", package=simple).result
103+
104+
70105
def test_py005_missing(tmp_path: Path):
71106
simple = tmp_path / "simple"
72107
simple.mkdir()
@@ -80,7 +115,7 @@ def test_py005_src(tmp_path: Path):
80115
src.mkdir()
81116
pkg = src.joinpath("pkg")
82117
pkg.mkdir()
83-
pkg.joinpath("test").mkdir()
118+
pkg.joinpath("tests").mkdir()
84119
assert compute_check("PY005", package=simple).result
85120

86121

0 commit comments

Comments
 (0)