66import sys
77
88
9- def test_cli_help ( ):
10- """Test that the CLI shows help ."""
9+ def run_cli ( args ):
10+ """Helper to run CLI with subprocess and capture output ."""
1111 result = subprocess .run (
12- [sys .executable , "cli.py" , "--help" ],
12+ [sys .executable , "cli.py" , * args ],
1313 capture_output = True ,
1414 text = True ,
1515 cwd = os .path .dirname (__file__ ),
16- check = True ,
1716 )
17+ return result
18+
19+
20+ def test_cli_help ():
21+ """Test that the CLI shows help."""
22+ result = run_cli (["--help" ])
1823 assert result .returncode == 0
1924 assert "101 Linux Commands CLI" in result .stdout
2025
2126
2227def test_hello_command ():
2328 """Test the hello command."""
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- )
29+ result = run_cli (["hello" , "greet" ])
3130 assert result .returncode == 0
3231 assert "Hello, World!" in result .stdout
3332
3433
3534def test_hello_command_with_name ():
3635 """Test the hello command with a custom name."""
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- )
36+ result = run_cli (["hello" , "greet" , "--name" , "Linux" ])
4437 assert result .returncode == 0
4538 assert "Hello, Linux!" in result .stdout
4639
4740
4841def test_hello_help ():
4942 """Test the hello command 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- )
43+ result = run_cli (["hello" , "--help" ])
5744 assert result .returncode == 0
5845 assert "Hello command group" in result .stdout
5946
6047
6148def test_list_command ():
6249 """Test the list command."""
63- result = subprocess .run (
64- [sys .executable , "cli.py" , "list" ],
65- capture_output = True ,
66- text = True ,
67- cwd = os .path .dirname (__file__ ),
68- )
50+ result = run_cli (["list" ])
51+ assert result .returncode == 0
6952 assert "ls - List directory contents." in result .stdout
7053 assert "cd - Change directory." in result .stdout
7154 assert "pwd - Print working directory." in result .stdout
@@ -74,29 +57,47 @@ def test_list_command():
7457
7558def test_version_command ():
7659 """Test the version command"""
77- result = subprocess .run (
78- [sys .executable , "cli.py" , "version" ],
79- capture_output = True ,
80- text = True ,
81- cwd = os .path .dirname (__file__ ),
82- )
60+ result = run_cli (["version" ])
8361 assert result .returncode == 0
8462 assert "101-linux v" in result .stdout
8563 assert "0.1.0" in result .stdout
8664
8765
8866def test_version_show_command ():
8967 """Test the version show subcommand."""
90- result = subprocess .run (
91- [sys .executable , "cli.py" , "version" , "show" ],
92- capture_output = True ,
93- text = True ,
94- cwd = os .path .dirname (__file__ ),
95- )
68+ result = run_cli (["version" , "show" ])
9669 assert result .returncode == 0
9770 assert "101-linux v0.1.0" in result .stdout
9871
9972
73+ # ----------------------------
74+ # Tests for `show` subcommand
75+ # ----------------------------
76+
77+ def test_show_ls ():
78+ """Test the show command with ls."""
79+ result = run_cli (["show" , "ls" ])
80+ assert result .returncode == 0
81+ assert "ls" in result .stdout
82+ assert "List" in result .stdout
83+
84+
85+ def test_show_grep ():
86+ """Test the show command with grep."""
87+ result = run_cli (["show" , "grep" ])
88+ assert result .returncode == 0
89+ assert "grep" in result .stdout
90+ assert "Search" in result .stdout or "Print" in result .stdout
91+
92+
93+ def test_show_invalid ():
94+ """Test the show command with an invalid command."""
95+ result = run_cli (["show" , "foobar" ])
96+ assert result .returncode != 0
97+ combined_output = result .stdout + result .stderr
98+ assert "Unknown" in combined_output or "Error" in combined_output
99+
100+
100101if __name__ == "__main__" :
101102 test_cli_help ()
102103 test_hello_command ()
@@ -105,4 +106,7 @@ def test_version_show_command():
105106 test_list_command ()
106107 test_version_command ()
107108 test_version_show_command ()
109+ test_show_ls ()
110+ test_show_grep ()
111+ test_show_invalid ()
108112 print ("✅ All tests passed!" )
0 commit comments