Browse Source

Add test for build_and_run_command

tags/3.1.0Beta1
Safihre 5 years ago
parent
commit
d57ecd4eaa
  1. 8
      sabnzbd/filesystem.py
  2. 72
      tests/test_misc.py

8
sabnzbd/filesystem.py

@ -524,10 +524,10 @@ def set_permissions(path, recursive=True):
def userxbit(filename):
# Returns boolean if the x-bit for user is set on the given file
# This is a workaround: os.access(filename, os.X_OK) does not work on certain mounted file systems
# Does not work on Windows, but it is not called on Windows
""" Returns boolean if the x-bit for user is set on the given file.
This is a workaround: os.access(filename, os.X_OK) does not work
on certain mounted file systems. Does not work at all on Windows.
"""
# rwx rwx rwx
# 876 543 210 # we want bit 6 from the right, counting from 0
userxbit = 1 << 6 # bit 6

72
tests/test_misc.py

@ -20,9 +20,13 @@ tests.test_misc - Testing functions in misc.py
"""
import datetime
import subprocess
import tempfile
from unittest import mock
from sabnzbd import lang
from sabnzbd import misc
from sabnzbd import newsunpack
from sabnzbd.config import ConfigCat
from sabnzbd.constants import HIGH_PRIORITY, TOP_PRIORITY, DEFAULT_PRIORITY, NORMAL_PRIORITY
from tests.testhelper import *
@ -213,3 +217,71 @@ class TestMisc:
# Remove files
os.unlink("test.cert")
os.unlink("test.key")
class TestBuildAndRunCommand:
def test_none_check(self):
with pytest.raises(IOError):
misc.build_and_run_command([None])
@set_platform("win32")
@mock.patch("subprocess.Popen")
def test_win(self, mock_subproc_popen):
# Needed for priority check
import win32process
misc.build_and_run_command(["test.cmd", "input 1"])
assert mock_subproc_popen.call_args.args[0] == ["test.cmd", "input 1"]
assert mock_subproc_popen.call_args.kwargs["creationflags"] == win32process.NORMAL_PRIORITY_CLASS
misc.build_and_run_command(["test.py", "input 1"])
assert mock_subproc_popen.call_args.args[0] == ["python.exe", "test.py", "input 1"]
assert mock_subproc_popen.call_args.kwargs["creationflags"] == win32process.NORMAL_PRIORITY_CLASS
# See: https://github.com/sabnzbd/sabnzbd/issues/1043
misc.build_and_run_command(["UnRar.exe", "\\\\?\\C:\\path\\"])
assert mock_subproc_popen.call_args.args[0] == ["UnRar.exe", "\\\\?\\C:\\path\\"]
misc.build_and_run_command(["UnRar.exe", "\\\\?\\C:\\path\\"], flatten_command=True)
assert mock_subproc_popen.call_args.args[0] == '"UnRar.exe" "\\\\?\\C:\\path\\"'
@mock.patch("subprocess.Popen")
def test_std_override(self, mock_subproc_popen):
misc.build_and_run_command(["test.py"], stderr=subprocess.DEVNULL)
assert mock_subproc_popen.call_args.kwargs["stderr"] == subprocess.DEVNULL
@set_platform("linux")
@set_config({"nice": "--adjustment=-7", "ionice": "-t -n9 -c7"})
@mock.patch("sabnzbd.misc.userxbit")
@mock.patch("subprocess.Popen")
def test_linux_features(self, mock_subproc_popen, userxbit):
# Path should exist
script_path = os.path.join(SAB_BASE_DIR, "test_misc.py")
# Should break on no-execute permissions
userxbit.return_value = False
with pytest.raises(IOError):
misc.build_and_run_command([script_path, "input 1"])
userxbit.return_value = True
# Check if python-call is added if not supplied by shebang
temp_file_fd, temp_file_path = tempfile.mkstemp(suffix=".py")
os.close(temp_file_fd)
misc.build_and_run_command([temp_file_path, "input 1"])
assert mock_subproc_popen.call_args.args[0] == ["python", temp_file_path, "input 1"]
os.remove(temp_file_path)
# Have to fake these for it to work
newsunpack.IONICE_COMMAND = "ionice"
newsunpack.NICE_COMMAND = "nice"
userxbit.return_value = True
misc.build_and_run_command([script_path, "input 1"])
assert mock_subproc_popen.call_args.args[0] == [
"nice",
"--adjustment=-7",
"ionice",
"-t",
"-n9",
"-c7",
script_path,
"input 1",
]

Loading…
Cancel
Save