Skip to content

Commit 10938f7

Browse files
Add version command to CLI with tests (#371)
* Add version command with tests - Create __version__.py to store version string (0.1.0) - Add version command that prints current version - Support both 'version' and 'version show' subcommands - Add tests for version command Fixes #321 * fix: removed unnecessary blank lines * fix: added a blank line * fix: ran test for black, isort, flake8 and pytest but failing in mypy (need to resolve * fix: Succesfully ran test for black, isort, flake8, mypy and pytest * Remove extra newline in main function * Fix formatting issues in cli.py --------- Co-authored-by: Bobby Iliev <bobby@bobbyiliev.com>
1 parent 8fd776b commit 10938f7

File tree

5 files changed

+82
-3
lines changed

5 files changed

+82
-3
lines changed

cli/__version__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""Version information for 101-linux-commands CLI."""
2+
3+
__version__ = "0.1.0"

cli/cli.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
import typer
66

7-
from commands import hello
7+
from commands import hello, version
88

99
app = typer.Typer(help="101 Linux Commands CLI 🚀")
1010
app.add_typer(hello.app, name="hello")
11+
app.add_typer(version.app, name="version")
1112

1213

1314
def main() -> None:

cli/commands/version.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Version command for 101-linux-commands CLI."""
2+
3+
import sys
4+
from pathlib import Path
5+
6+
import typer
7+
8+
sys.path.insert(0, str(Path(__file__).parent.parent))
9+
10+
from __version__ import __version__
11+
12+
app = typer.Typer(help="version command")
13+
14+
15+
@app.callback(invoke_without_command=True)
16+
def main(ctx: typer.Context):
17+
"""Show the current version of 101-linux CLI"""
18+
if ctx.invoked_subcommand is None:
19+
typer.echo(f"101-linux v{__version__}")
20+
21+
22+
@app.command()
23+
def show():
24+
"""Show the current version of 101-linux CLI"""
25+
typer.echo(f"101-linux v{__version__}")

cli/setup.cfg

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,44 @@ extend-ignore =
55
E203,
66
# W503: line break before binary operator (conflicts with black)
77
W503
8+
# E402: module level import not at top (needed for sys.path manipulation)
9+
E402
810
exclude =
911
.git,
1012
__pycache__,
1113
.venv,
1214
venv,
1315
.env,
14-
env
16+
env,
17+
.mypy_cache,
18+
build,
19+
dist
1520

1621
[isort]
1722
profile = black
1823
multi_line_output = 3
1924
line_length = 88
2025

2126
[mypy]
22-
python_version = 3.8
27+
python_version = 3.9
2328
warn_return_any = True
2429
warn_unused_configs = True
2530
ignore_missing_imports = True
31+
disallow_untyped_defs = False
32+
show_error_codes = True
33+
explicit_package_bases = True
34+
35+
[mypy-__version__]
36+
ignore_missing_imports = True
37+
38+
[mypy-commands]
39+
ignore_missing_imports = True
40+
41+
[mypy-commands.*]
42+
ignore_missing_imports = True
43+
44+
[mypy-typer.*]
45+
ignore_missing_imports = True
46+
47+
[mypy-pytest.*]
48+
ignore_missing_imports = True

cli/test_cli.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,36 @@ def test_hello_help():
5858
assert "Hello command group" in result.stdout
5959

6060

61+
def test_version_command():
62+
"""Test the version command"""
63+
result = subprocess.run(
64+
[sys.executable, "cli.py", "version"],
65+
capture_output=True,
66+
text=True,
67+
cwd=os.path.dirname(__file__),
68+
)
69+
assert result.returncode == 0
70+
assert "101-linux v" in result.stdout
71+
assert "0.1.0" in result.stdout
72+
73+
74+
def test_version_show_command():
75+
"""Test the version show subcommand."""
76+
result = subprocess.run(
77+
[sys.executable, "cli.py", "version", "show"],
78+
capture_output=True,
79+
text=True,
80+
cwd=os.path.dirname(__file__),
81+
)
82+
assert result.returncode == 0
83+
assert "101-linux v0.1.0" in result.stdout
84+
85+
6186
if __name__ == "__main__":
6287
test_cli_help()
6388
test_hello_command()
6489
test_hello_command_with_name()
6590
test_hello_help()
91+
test_version_command()
92+
test_version_show_command()
6693
print("✅ All tests passed!")

0 commit comments

Comments
 (0)