55import subprocess
66import sys
77
8- CLI = [sys .executable , "cli.py" ]
9- CWD = os .path .dirname (__file__ )
10-
11-
12- def run_cli (args ):
13- """Helper to run CLI with subprocess."""
14- return subprocess .run (
15- CLI + args ,
16- capture_output = True ,
17- text = True ,
18- cwd = CWD ,
19- )
20-
218
229def test_cli_help ():
2310 """Test that the CLI shows help."""
24- result = run_cli (["--help" ])
11+ result = subprocess .run (
12+ [sys .executable , "cli.py" , "--help" ],
13+ capture_output = True ,
14+ text = True ,
15+ cwd = os .path .dirname (__file__ ),
16+ check = True ,
17+ )
2518 assert result .returncode == 0
2619 assert "101 Linux Commands CLI" in result .stdout
2720
2821
2922def test_hello_command ():
3023 """Test the hello command."""
31- result = run_cli (["hello" , "greet" ])
24+ result = subprocess .run (
25+ [sys .executable , "cli.py" , "hello" , "greet" ],
26+ capture_output = True ,
27+ text = True ,
28+ cwd = os .path .dirname (__file__ ),
29+ check = True ,
30+ )
3231 assert result .returncode == 0
3332 assert "Hello, World!" in result .stdout
3433
3534
3635def test_hello_command_with_name ():
3736 """Test the hello command with a custom name."""
38- result = run_cli (["hello" , "greet" , "--name" , "Linux" ])
37+ result = subprocess .run (
38+ [sys .executable , "cli.py" , "hello" , "greet" , "--name" , "Linux" ],
39+ capture_output = True ,
40+ text = True ,
41+ cwd = os .path .dirname (__file__ ),
42+ check = True ,
43+ )
3944 assert result .returncode == 0
4045 assert "Hello, Linux!" in result .stdout
4146
4247
4348def test_hello_help ():
4449 """Test the hello command help."""
45- result = run_cli (["hello" , "--help" ])
50+ result = subprocess .run (
51+ [sys .executable , "cli.py" , "hello" , "--help" ],
52+ capture_output = True ,
53+ text = True ,
54+ cwd = os .path .dirname (__file__ ),
55+ check = True ,
56+ )
4657 assert result .returncode == 0
4758 assert "Hello command group" in result .stdout
4859
4960
50- # ----------------------------
51- # Tests for `show` subcommand
52- # ----------------------------
53-
54- def test_show_ls ():
55- """Test the show command with ls."""
56- result = run_cli (["show" , "ls" ])
57- assert result .returncode == 0
58- assert "# ls" in result .stdout or "Command: ls" in result .stdout
59- assert "List" in result .stdout
60-
61-
62- def test_show_grep ():
63- """Test the show command with grep."""
64- result = run_cli (["show" , "grep" ])
65- assert result .returncode == 0
66- assert "grep" in result .stdout
67- assert "Search" in result .stdout or "Print" in result .stdout
68-
69-
70- def test_show_invalid ():
71- """Test the show command with an invalid command."""
72- result = run_cli (["show" , "foobar" ])
73- assert result .returncode == 1
74- combined_output = result .stdout + result .stderr
75- assert "Unknown command" in combined_output
76-
77-
7861if __name__ == "__main__" :
7962 test_cli_help ()
8063 test_hello_command ()
8164 test_hello_command_with_name ()
8265 test_hello_help ()
83- test_show_ls ()
84- test_show_grep ()
85- test_show_invalid ()
86- print ("✅ All tests passed!" )
66+ print ("✅ All tests passed!" )
0 commit comments