Skip to content

Commit 2fcd547

Browse files
DevPatel-11mt798jx
authored andcommitted
add Python type hints and enable mypy checks (The-DevOps-Daily#335) (The-DevOps-Daily#369)
1 parent 76ce66f commit 2fcd547

File tree

4 files changed

+46
-56
lines changed

4 files changed

+46
-56
lines changed

cli/cli.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1+
"""
2+
CLI entry point for the 101 Linux Commands application.
3+
"""
4+
15
import typer
2-
from commands import hello, show
36

4-
# Root CLI app
5-
app = typer.Typer(help="101 Linux Commands CLI 🚀")
7+
from commands import hello
68

7-
# Register subcommands
9+
app = typer.Typer(help="101 Linux Commands CLI 🚀")
810
app.add_typer(hello.app, name="hello")
911

10-
# register show directly as command
11-
app.command()(show.show)
12+
13+
def main() -> None:
14+
"""CLI entry point."""
15+
app()
16+
1217

1318
if __name__ == "__main__":
14-
app()
19+
app()

cli/commands/hello.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
"""
2+
Hello command for the 101 Linux Commands CLI.
3+
"""
4+
15
import typer
26

37
app = typer.Typer(help="Hello command group")

cli/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
click==8.1.7
12
typer[all]==0.12.3

cli/test_cli.py

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,82 +5,62 @@
55
import subprocess
66
import 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

229
def 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

2922
def 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

3635
def 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

4348
def 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-
7861
if __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

Comments
 (0)