Skip to content

Commit efd8eac

Browse files
committed
Fix some tests, add enabled tests on Windows
1 parent 0d8c8c5 commit efd8eac

File tree

11 files changed

+157
-49
lines changed

11 files changed

+157
-49
lines changed

salt/modules/cmdmod.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,8 @@ def _run(
419419
):
420420
cmd = _prep_powershell_cmd(win_shell, cmd, encoded_cmd)
421421
elif any(win_shell_lower.endswith(word) for word in ["cmd.exe"]):
422-
cmd = salt.platform.win.prepend_cmd(win_shell, cmd)
422+
if python_shell:
423+
cmd = salt.platform.win.prepend_cmd(win_shell, cmd)
423424
else:
424425
raise CommandExecutionError(f"unsupported shell type: {win_shell}")
425426
else:
@@ -725,7 +726,12 @@ def _run(
725726
f"Specified cwd '{cwd}' either not absolute or does not exist"
726727
)
727728

728-
if python_shell is not True and not isinstance(cmd, list):
729+
if (
730+
python_shell is not True
731+
and shell is not None
732+
and not salt.utils.platform.is_windows()
733+
and not isinstance(cmd, list)
734+
):
729735
cmd = salt.utils.args.shlex_split(cmd)
730736

731737
if success_retcodes is None:

salt/platform/win.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,6 @@ def prepend_cmd(win_shell, cmd):
13511351
args = subprocess.list2cmdline(cmd)
13521352
else:
13531353
args = cmd
1354-
new_cmd = f'{win_shell} /c "{args}"'
1354+
new_cmd = f"{win_shell} /c {args}"
13551355

13561356
return new_cmd

tests/integration/modules/test_cmdmod.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
if sys.platform.startswith("win32"):
2121
SHELL = "cmd"
22-
PYTHON_SHELL = False
22+
PYTHON_SHELL = True
2323
elif sys.platform.startswith(("freebsd", "openbsd")):
2424
SHELL = "/bin/sh"
2525
PYTHON_SHELL = True
@@ -370,6 +370,17 @@ def test_which(self):
370370
self.assertIsInstance(cmd_run, str)
371371
self.assertEqual(cmd_which.rstrip(), cmd_run.rstrip())
372372

373+
@pytest.mark.skip_unless_on_windows
374+
def test_which_win(self):
375+
"""
376+
cmd.which
377+
"""
378+
cmd_which = self.run_function("cmd.which", ["cmd"])
379+
self.assertIsInstance(cmd_which, str)
380+
cmd_run = self.run_function("cmd.run", ["where cmd"])
381+
self.assertIsInstance(cmd_run, str)
382+
self.assertEqual(cmd_which.rstrip().lower(), cmd_run.rstrip().lower())
383+
373384
@pytest.mark.skip_on_windows
374385
@pytest.mark.skip_if_binaries_missing("which")
375386
def test_which_bin(self):
@@ -380,6 +391,15 @@ def test_which_bin(self):
380391
ret = self.run_function("cmd.which_bin", [cmds])
381392
self.assertTrue(os.path.split(ret)[1] in cmds)
382393

394+
@pytest.mark.skip_unless_on_windows
395+
def test_which_bin_win(self):
396+
"""
397+
cmd.which_bin
398+
"""
399+
cmds = ["python.exe", "find.exe", "where.exe", "findstr.exe"]
400+
ret = self.run_function("cmd.which_bin", [cmds])
401+
self.assertTrue(os.path.split(ret)[1].lower() in cmds)
402+
383403
@pytest.mark.slow_test
384404
def test_has_exec(self):
385405
"""

tests/integration/states/test_cmd.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ class CMDTest(ModuleCase, SaltReturnAssertsMixin):
2626
def setUpClass(cls):
2727
cls.__cmd = "dir" if salt.utils.platform.is_windows() else "ls"
2828

29+
@pytest.mark.windows_whitelisted
2930
def test_run_simple(self):
3031
"""
3132
cmd.run
3233
"""
3334
ret = self.run_state("cmd.run", name=self.__cmd, cwd=tempfile.gettempdir())
3435
self.assertSaltTrueReturn(ret)
3536

37+
@pytest.mark.windows_whitelisted
3638
def test_run_output_loglevel(self):
3739
"""
3840
cmd.run with output_loglevel=quiet
@@ -45,6 +47,7 @@ def test_run_output_loglevel(self):
4547
)
4648
self.assertSaltTrueReturn(ret)
4749

50+
@pytest.mark.windows_whitelisted
4851
def test_run_simple_test_true(self):
4952
"""
5053
cmd.run test interface
@@ -54,6 +57,7 @@ def test_run_simple_test_true(self):
5457
)
5558
self.assertSaltNoneReturn(ret)
5659

60+
@pytest.mark.windows_whitelisted
5761
def test_run_hide_output(self):
5862
"""
5963
cmd.run with output hidden
@@ -106,6 +110,7 @@ def tearDown(self):
106110
super().tearDown()
107111

108112
@pytest.mark.slow_test
113+
@pytest.mark.windows_whitelisted
109114
def test_run_unless(self):
110115
"""
111116
test cmd.run unless
@@ -150,6 +155,7 @@ def test_run_unless_multiple_cmds(self):
150155
)
151156

152157
@pytest.mark.slow_test
158+
@pytest.mark.windows_whitelisted
153159
def test_run_creates_exists(self):
154160
"""
155161
test cmd.run creates already there
@@ -175,6 +181,7 @@ def test_run_creates_exists(self):
175181
self.assertEqual(len(ret[state_key]["changes"]), 0)
176182

177183
@pytest.mark.slow_test
184+
@pytest.mark.windows_whitelisted
178185
def test_run_creates_new(self):
179186
"""
180187
test cmd.run creates not there
@@ -201,6 +208,7 @@ def test_run_creates_new(self):
201208
self.assertEqual(len(ret[state_key]["changes"]), 4)
202209

203210
@pytest.mark.slow_test
211+
@pytest.mark.windows_whitelisted
204212
def test_run_redirect(self):
205213
"""
206214
test cmd.run with shell redirect
@@ -241,6 +249,7 @@ def tearDown(self):
241249
os.remove(self.state_file)
242250
super().tearDown()
243251

252+
@pytest.mark.windows_whitelisted
244253
def test_run_watch(self):
245254
"""
246255
test cmd.run watch

tests/pytests/functional/modules/cmd/test_enabled.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def test_cmd():
2727
# "$env:SALTY_VARIABLE='saltines'; echo $env:SALTY_VARIABLE; "
2828
# "echo duh *> $null"
2929
# )
30-
return '(echo(first & echo(second & echo(third) | find /v /c "" && echo saltines && echo duh > NUL 2>&1'
30+
return 'where find | findstr find | find /v /c "" && echo saltines && echo duh > NUL 2>&1'
3131
else:
3232
return (
3333
"printf '%s\\n' first second third | wc -l ; "
@@ -39,7 +39,7 @@ def test_cmd():
3939
@pytest.fixture
4040
def ret_disabled():
4141
if salt.utils.platform.is_windows():
42-
return "Access denied - \\"
42+
return 'ERROR: Value for default option cannot be empty.\r\nType "WHERE /?" for usage.'
4343
else:
4444
return (
4545
"first\nsecond\nthird\n|\nwc\n-l\n;\nexport\nSALTY_VARIABLE=saltines"
@@ -49,7 +49,10 @@ def ret_disabled():
4949

5050
@pytest.fixture
5151
def ret_enabled():
52-
return f"3{os.linesep}saltines"
52+
if salt.utils.platform.is_windows():
53+
return f"1{os.linesep}saltines"
54+
else:
55+
return f"3{os.linesep}saltines"
5356

5457

5558
@pytest.fixture(scope="module")
@@ -102,9 +105,13 @@ def test_template_shell(state, state_tree, test_cmd, ret_enabled):
102105
"""
103106
state_name = "template_shell_enabled"
104107
state_file_name = state_name + ".sls"
108+
if salt.utils.platform.is_windows():
109+
test_cmd = f"'{test_cmd}'"
110+
else:
111+
test_cmd = f'"{test_cmd}"'
105112
state_file_contents = textwrap.dedent(
106113
f"""
107-
{{% set shell_enabled = salt['cmd.shell']("{test_cmd}", python_shell=True).strip() %}}
114+
{{% set shell_enabled = salt['cmd.shell']({test_cmd}, python_shell=True).strip() %}}
108115
109116
shell_enabled:
110117
test.configurable_test_state:
@@ -129,9 +136,13 @@ def test_template_default_disabled(state, state_tree, test_cmd, ret_disabled):
129136
"""
130137
state_name = "template_shell_disabled"
131138
state_file_name = state_name + ".sls"
139+
if salt.utils.platform.is_windows():
140+
test_cmd = f"'{test_cmd}'"
141+
else:
142+
test_cmd = f'"{test_cmd}"'
132143
state_file_contents = textwrap.dedent(
133144
f"""
134-
{{% set shell_disabled = salt['cmd.run']("{test_cmd}") %}}
145+
{{% set shell_disabled = salt['cmd.run']({test_cmd}) %}}
135146
136147
shell_enabled:
137148
test.configurable_test_state:

tests/pytests/functional/modules/cmd/test_run_win.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,15 @@ def test_cmd_exitcode_runas(
6464
("whoami && echo foo", "foo"),
6565
("echo \"foo 'bar'\"", "\"foo 'bar'\""),
6666
('echo|set /p="foo" & echo|set /p="bar"', "foobar"),
67-
('''echo "&<>[]|{}^=;!'+,`~ "''', '''"&<>[]|{}^=;!'+,`~ "'''),
67+
('''echo ""&<>[]|{}^=;!'+,`~ ""''', '''"&<>[]|{}^=;!'+,`~ "'''),
6868
],
6969
)
7070
def test_cmd_builtins(modules, command, expected):
7171
"""
7272
Test builtin cmd.exe commands
7373
"""
74-
result = modules.cmd.run(command, shell="cmd")
74+
# Final test is failing... can't get it to pass
75+
result = modules.cmd.run(command, python_shell=True)
7576
assert expected in result
7677

7778

@@ -91,7 +92,10 @@ def test_cmd_builtins_runas(modules, account, command, expected):
9192
Test builtin cmd.exe commands with runas
9293
"""
9394
result = modules.cmd.run(
94-
cmd=command, shell="cmd", runas=account.username, password=account.password
95+
cmd=command,
96+
python_shell=True,
97+
runas=account.username,
98+
password=account.password,
9599
)
96100
assert expected in result
97101

@@ -144,7 +148,7 @@ def test_cmd_env(modules, command, env, expected):
144148
"""
145149
Test cmd.run with environment variables
146150
"""
147-
result = modules.cmd.run_all(command, shell="cmd", env=env)
151+
result = modules.cmd.run_all(command, python_shell=True, env=env)
148152
assert isinstance(result["pid"], int)
149153
assert result["retcode"] == 0
150154
assert result["stdout"] == expected

tests/pytests/functional/modules/cmd/test_script_batch.py

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
from textwrap import dedent
2+
13
import pytest
24

5+
import salt.utils.platform
6+
37
pytestmark = [
48
pytest.mark.core_test,
59
pytest.mark.windows_whitelisted,
6-
pytest.mark.skip_unless_on_windows,
710
]
811

912

@@ -15,15 +18,28 @@ def account():
1518

1619
@pytest.fixture
1720
def echo_script(state_tree):
18-
contents = """@echo off
19-
set a=%~1
20-
set b=%~2
21-
shift
22-
shift
23-
echo a: %a%, b: %b%
24-
"""
25-
with pytest.helpers.temp_file("test.bat", contents, state_tree / "echo-script"):
26-
yield
21+
if salt.utils.platform.is_windows():
22+
file_name = "test.bat"
23+
contents = dedent(
24+
"""\
25+
@echo off
26+
set a=%~1
27+
set b=%~2
28+
echo a: %a%, b: %b%
29+
"""
30+
)
31+
else:
32+
file_name = "test.sh"
33+
contents = dedent(
34+
"""\
35+
#!/bin/bash
36+
a="$1"
37+
b="$2"
38+
echo "a: $a, b: $b"
39+
"""
40+
)
41+
with pytest.helpers.temp_file(file_name, contents, state_tree / "echo-script"):
42+
yield file_name
2743

2844

2945
@pytest.mark.parametrize(
@@ -39,8 +55,8 @@ def test_echo(modules, echo_script, args, expected):
3955
"""
4056
Test argument processing with a batch script
4157
"""
42-
script = "salt://echo-script/test.bat"
43-
result = modules.cmd.script(script, args=args, shell="cmd")
58+
script = f"salt://echo-script/{echo_script}"
59+
result = modules.cmd.script(script, args=args)
4460
assert result["stdout"] == expected
4561

4662

@@ -55,13 +71,12 @@ def test_echo(modules, echo_script, args, expected):
5571
)
5672
def test_echo_runas(modules, account, echo_script, args, expected):
5773
"""
58-
Test argument processing with a batch script and runas
74+
Test argument processing with a batch/bash script and runas
5975
"""
60-
script = "salt://echo-script/test.bat"
76+
script = f"salt://echo-script/{echo_script}"
6177
result = modules.cmd.script(
6278
script,
6379
args=args,
64-
shell="cmd",
6580
runas=account.username,
6681
password=account.password,
6782
)

0 commit comments

Comments
 (0)