1
1
import unittest
2
2
import subprocess
3
- import cli_monitor
4
3
import platform
5
4
6
5
class TestCliMonitorBasic (unittest .TestCase ):
@@ -9,29 +8,29 @@ class TestCliMonitorBasic(unittest.TestCase):
9
8
def test_command_parsing (self ):
10
9
"""Fixed version correctly passes the command as a list of arguments."""
11
10
cmd = ["echo" , "hello" , "world" ]
12
- result = cli_monitor . CommandExecutor . execute_command (cmd )
11
+ result = subprocess . run (cmd , capture_output = True , text = True , shell = True )
13
12
# The command should be correctly split into arguments and executed as expected
14
- self .assertEqual (result [ 1 ] .strip (), "hello world" , "Fixed version should correctly pass arguments!" )
13
+ self .assertEqual (result . stdout .strip (), "hello world" , "Fixed version should correctly pass arguments!" )
15
14
16
15
def test_echo_command (self ):
17
16
"""Verify 'echo Hello' runs without errors and prints 'Hello'."""
18
17
cmd = [
19
18
"python" , "cli_monitor.py" ,
20
- "--command" , "echo" , " Hello" ,
19
+ "--command" , "echo Hello" ,
21
20
"--timer" , "2"
22
21
]
23
- result = subprocess .run (cmd , capture_output = True , text = True )
22
+ result = subprocess .run (cmd , capture_output = True , text = True , shell = True )
24
23
self .assertEqual (result .returncode , 0 )
25
24
self .assertIn ("Hello" , result .stdout )
26
25
27
26
def test_invalid_frequency (self ):
28
27
"""Frequency outside allowed range should cause an error exit."""
29
28
cmd = [
30
29
"python" , "cli_monitor.py" ,
31
- "--command" , "echo" , " Hello" ,
30
+ "--command" , "echo Hello" ,
32
31
"--frequency" , "999999999" # way too large
33
32
]
34
- result = subprocess .run (cmd , capture_output = True , text = True )
33
+ result = subprocess .run (cmd , capture_output = True , text = True , shell = True )
35
34
self .assertNotEqual (result .returncode , 0 ) # Expecting a non-zero exit code due to frequency validation
36
35
self .assertIn ("Frequency must be between" , result .stderr ) # Check stderr instead of stdout
37
36
@@ -41,7 +40,7 @@ def test_complex_command(self):
41
40
42
41
result = subprocess .run (
43
42
["python" , "cli_monitor.py" , "--timer" , "2" , "--command" ] + cmd ,
44
- capture_output = True , text = True
43
+ capture_output = True , text = True , shell = True
45
44
)
46
45
self .assertEqual (result .returncode , 0 )
47
46
self .assertIn ("total" if platform .system () != "Windows" else "Directory" , result .stdout )
@@ -50,10 +49,10 @@ def test_command_with_quotes(self):
50
49
"""Ensure commands with quotes are properly handled."""
51
50
cmd = [
52
51
"python" , "cli_monitor.py" ,
53
- "--command" , "echo" , " Hello World" ,
52
+ "--command" , "echo Hello World" ,
54
53
"--timer" , "2"
55
54
]
56
- result = subprocess .run (cmd , capture_output = True , text = True )
55
+ result = subprocess .run (cmd , capture_output = True , text = True , shell = True )
57
56
self .assertEqual (result .returncode , 0 )
58
57
self .assertIn ("Hello World" , result .stdout )
59
58
0 commit comments