-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest_subprocess_shell_false.py
More file actions
107 lines (92 loc) · 3 KB
/
test_subprocess_shell_false.py
File metadata and controls
107 lines (92 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import pytest
from codemodder.codemods.test import BaseCodemodTest
from core_codemods.subprocess_shell_false import SubprocessShellFalse
each_func = pytest.mark.parametrize(
"func", ["check_output", "check_call", "run", "call", "Popen"]
)
class TestSubprocessShellFalse(BaseCodemodTest):
codemod = SubprocessShellFalse
def test_name(self):
assert self.codemod.name == "subprocess-shell-false"
@each_func
def test_import(self, tmpdir, func):
input_code = f"""
import subprocess
subprocess.{func}(args, shell=True)
"""
expected = f"""
import subprocess
subprocess.{func}(args, shell=False)
"""
self.run_and_assert(tmpdir, input_code, expected)
@each_func
def test_from_import(self, tmpdir, func):
input_code = f"""
from subprocess import {func}
{func}(args, shell=True)
"""
expected = f"""
from subprocess import {func}
{func}(args, shell=False)
"""
self.run_and_assert(tmpdir, input_code, expected)
@each_func
def test_no_shell(self, tmpdir, func):
input_code = f"""
import subprocess
subprocess.{func}(args, timeout=1)
"""
self.run_and_assert(tmpdir, input_code, input_code)
@each_func
def test_shell_False(self, tmpdir, func):
input_code = f"""
import subprocess
subprocess.{func}(args, shell=False)
"""
self.run_and_assert(tmpdir, input_code, input_code)
def test_exclude_line(self, tmpdir):
input_code = (
expected
) = """
import subprocess
subprocess.run(args, shell=True)
"""
lines_to_exclude = [3]
self.run_and_assert(
tmpdir,
input_code,
expected,
lines_to_exclude=lines_to_exclude,
)
@each_func
def test_has_noqa(self, tmpdir, func):
input_code = (
expected
) = f"""
import subprocess
subprocess.{func}(args, shell=True) # noqa: S603
"""
self.run_and_assert(tmpdir, input_code, expected)
def test_different_noqa_message(self, tmpdir):
input_code = """
import subprocess
subprocess.run(args, shell=True) # noqa: S604
"""
expected = """
import subprocess
subprocess.run(args, shell=False) # noqa: S604
"""
self.run_and_assert(tmpdir, input_code, expected)
def test_no_change_if_first_arg_is_string(self, tmpdir):
input_code = """
import subprocess
subprocess.run("ls -l", shell=True)
subprocess.run("ls" "-l", shell=True)
ls_args = "-l -a"
subprocess.run(f"ls {ls_args}", shell=True)
subprocess.run('ls -l', shell=True)
subprocess.run('ls' '-l', shell=True)
ls_args = '-l -a'
subprocess.run(f'ls {ls_args}', shell=True)
"""
self.run_and_assert(tmpdir, input_code, input_code, num_changes=0)