Skip to content

Commit 86e907b

Browse files
Enable flake8-bandit rules and comply with them (#658)
1 parent 6d83f06 commit 86e907b

File tree

8 files changed

+45
-14
lines changed

8 files changed

+45
-14
lines changed

pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,13 @@ lint.select = [
108108
"PLR",
109109
"PT",
110110
"RUF",
111+
"S",
111112
"SIM",
112113
"TC",
113114
"UP",
114115
]
115-
lint.ignore = [ "PLR2004", "SIM108" ]
116-
lint.per-file-ignores."tests/**/*.py" = [ "D", "INP", "S101" ]
116+
lint.ignore = [ "PLR2004", "S101", "SIM108" ]
117+
lint.per-file-ignores."tests/**/*.py" = [ "D", "INP", "S603" ]
117118
lint.flake8-type-checking.runtime-evaluated-base-classes = [ "pydantic.BaseModel" ]
118119
lint.pydocstyle.convention = "google"
119120

src/usethis/_subprocess.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class SubprocessFailedError(Exception):
99

1010
def call_subprocess(args: list[str]) -> str:
1111
try:
12-
process = subprocess.run(
12+
process = subprocess.run( # noqa: S603
1313
args,
1414
check=True,
1515
capture_output=True,

src/usethis/_tool/impl/pytest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,9 @@ def get_rule_config(self) -> RuleConfig:
151151
def get_active_config_file_managers(self) -> set[KeyValueFileManager]:
152152
# This is a variant of the "first" method
153153
config_spec = self.get_config_spec()
154-
assert config_spec.resolution == "bespoke"
154+
if config_spec.resolution != "bespoke":
155+
# Something has gone badly wrong, perhaps in a subclass of PytestTool.
156+
raise NotImplementedError
155157
# As per https://docs.pytest.org/en/stable/reference/customize.html#finding-the-rootdir
156158
# Files will only be matched for configuration if:
157159
# - pytest.ini: will always match and take precedence, even if empty.

tests/conftest.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,25 @@ def _vary_network_conn(_online_status: NetworkConn) -> Generator[None, None, Non
114114
@pytest.fixture
115115
def usethis_dev_dir() -> Path:
116116
return Path(__file__).parent.parent
117+
118+
119+
@pytest.fixture
120+
def git_path() -> Path:
121+
"""Fixture to get the path to the git executable."""
122+
git_path = shutil.which("git")
123+
124+
if not git_path:
125+
pytest.skip("Git executable not found")
126+
127+
return Path(git_path)
128+
129+
130+
@pytest.fixture
131+
def uv_path() -> Path:
132+
"""Fixture to get the path to the uv executable."""
133+
uv_path = shutil.which("uv")
134+
135+
if not uv_path:
136+
pytest.skip("uv executable not found")
137+
138+
return Path(uv_path)

tests/usethis/_core/test_core_tool.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ def test_stdout_unfrozen(
548548
)
549549

550550
@pytest.mark.usefixtures("_vary_network_conn")
551-
def test_run_deptry_fail(self, uv_init_dir: Path):
551+
def test_run_deptry_fail(self, uv_init_dir: Path, uv_path: Path):
552552
# Arrange
553553
f = uv_init_dir / "bad.py"
554554
f.write_text("import broken_dependency")
@@ -560,7 +560,9 @@ def test_run_deptry_fail(self, uv_init_dir: Path):
560560
# Assert
561561
with pytest.raises(subprocess.CalledProcessError):
562562
subprocess.run(
563-
["uv", "run", "deptry", "."], cwd=uv_init_dir, check=True
563+
[uv_path.as_posix(), "run", "deptry", "."],
564+
cwd=uv_init_dir,
565+
check=True,
564566
)
565567

566568
@pytest.mark.usefixtures("_vary_network_conn")
@@ -1467,7 +1469,7 @@ def test_config_file_already_exists(self, uv_init_repo_dir: Path):
14671469
)
14681470

14691471
@pytest.mark.usefixtures("_vary_network_conn")
1470-
def test_bad_commit(self, uv_env_dir: Path):
1472+
def test_bad_commit(self, uv_env_dir: Path, git_path: Path):
14711473
# This needs a venv so that we can actually run pre-commit via git
14721474

14731475
# Arrange
@@ -1476,9 +1478,11 @@ def test_bad_commit(self, uv_env_dir: Path):
14761478
# Act
14771479
with change_cwd(uv_env_dir), files_manager():
14781480
use_pre_commit()
1479-
subprocess.run(["git", "add", "."], cwd=uv_env_dir, check=True)
1481+
subprocess.run(
1482+
[git_path.as_posix(), "add", "."], cwd=uv_env_dir, check=True
1483+
)
14801484
result = subprocess.run(
1481-
["git", "commit", "-m", "Good commit"], cwd=uv_env_dir
1485+
[git_path.as_posix(), "commit", "-m", "Good commit"], cwd=uv_env_dir
14821486
)
14831487
assert not result.stderr
14841488
assert result.returncode == 0, (
@@ -1487,9 +1491,11 @@ def test_bad_commit(self, uv_env_dir: Path):
14871491

14881492
# Assert
14891493
(uv_env_dir / ".pre-commit-config.yaml").write_text("[")
1490-
subprocess.run(["git", "add", "."], cwd=uv_env_dir, check=True)
1494+
subprocess.run(
1495+
[git_path.as_posix(), "add", "."], cwd=uv_env_dir, check=True
1496+
)
14911497
result = subprocess.run(
1492-
["git", "commit", "-m", "Bad commit"],
1498+
[git_path.as_posix(), "commit", "-m", "Bad commit"],
14931499
cwd=uv_env_dir,
14941500
capture_output=True,
14951501
)

tests/usethis/_integrations/ci/bitbucket/test_bitbucket_schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_matches_schema_store(self):
2525
local_schema_json = (Path(__file__).parent / "schema.json").read_text()
2626
try:
2727
online_schema_json = requests.get(
28-
"https://api.bitbucket.org/schemas/pipelines-configuration"
28+
"https://api.bitbucket.org/schemas/pipelines-configuration", timeout=5
2929
).text
3030
except requests.exceptions.ConnectionError as err:
3131
if os.getenv("CI"):

tests/usethis/_integrations/pre_commit/test_pre_commit_schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def test_matches_schema_store(self):
2727
local_schema_json = (Path(__file__).parent / "schema.json").read_text()
2828
try:
2929
online_schema_json = requests.get(
30-
"https://json.schemastore.org/pre-commit-config.json"
30+
"https://json.schemastore.org/pre-commit-config.json", timeout=5
3131
).text
3232
except requests.exceptions.ConnectionError as err:
3333
if os.getenv("CI"):

tests/usethis/_tool/impl/test_pyproject_toml.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def test_link_isnt_dead(self):
6767

6868
if not usethis_config.offline:
6969
# Act
70-
result = requests.head(url)
70+
result = requests.head(url, timeout=5)
7171

7272
# Assert
7373
assert result.status_code == 200

0 commit comments

Comments
 (0)