Skip to content

Commit

Permalink
chore: rewrite test code without os.system()
Browse files Browse the repository at this point in the history
- mklink is a built-in command of cmd.exe
  • Loading branch information
miurahr committed Dec 15, 2024
1 parent d98b916 commit 3a658ab
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions tests/test_win32compat.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ctypes
import os
import pathlib
import subprocess

Check warning on line 4 in tests/test_win32compat.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/test_win32compat.py#L4

Consider possible security implications associated with the subprocess module.
import sys

import pytest
Expand Down Expand Up @@ -63,7 +64,9 @@ def test_hardlink_readlink(tmp_path):
hard = tmp_path / "target" / "link"
hard.parent.mkdir(parents=True, exist_ok=True)
if sys.platform.startswith("win"):
os.system("mklink /H %s %s" % (str(hard), str(target.resolve())))
cmd_path = r"c:\Windows\System32\cmd.exe"
command = [cmd_path, "/c", "mklink", "/H", str(hard), str(target.resolve())]
subprocess.run(command)

Check warning on line 69 in tests/test_win32compat.py

View check run for this annotation

reviewdog / pylint

[pylint] tests/test_win32compat.py#L69 <1510>

'subprocess.run' used without explicitly defining the value for 'check'. (subprocess-run-check)
Raw output
tests/test_win32compat.py:69:8: W1510: 'subprocess.run' used without explicitly defining the value for 'check'. (subprocess-run-check)

Check failure on line 69 in tests/test_win32compat.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/test_win32compat.py#L69

Detected subprocess function 'run' without a static string.

Check failure on line 69 in tests/test_win32compat.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/test_win32compat.py#L69

Python possesses many mechanisms to invoke an external executable.

Check warning on line 69 in tests/test_win32compat.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/test_win32compat.py#L69

subprocess call - check for execution of untrusted input.
else:
os.link(str(target.resolve()), str(hard))
assert hard.open("r").read() == "Original"
Expand All @@ -82,7 +85,9 @@ def test_junction_readlink(tmp_path):
f.write("Original")
junction = tmp_path / "target" / "link"
junction.parent.mkdir(parents=True, exist_ok=True)
os.system("mklink /J %s %s" % (str(junction), str(target.resolve())))
cmd_path = r"c:\Windows\System32\cmd.exe"
command = [cmd_path, "/c", "mklink", "/J", str(junction), str(target.resolve())]
subprocess.run(command)

Check warning on line 90 in tests/test_win32compat.py

View check run for this annotation

reviewdog / pylint

[pylint] tests/test_win32compat.py#L90 <1510>

'subprocess.run' used without explicitly defining the value for 'check'. (subprocess-run-check)
Raw output
tests/test_win32compat.py:90:4: W1510: 'subprocess.run' used without explicitly defining the value for 'check'. (subprocess-run-check)
assert not os.path.islink(str(junction))
assert py7zr.win32compat.is_reparse_point(str(junction))
assert py7zr.win32compat.readlink(str(junction)) == PATH_PREFIX + str(target.resolve())
Expand Down

0 comments on commit 3a658ab

Please sign in to comment.