Skip to content

Commit dba9ad9

Browse files
authored
πŸ§‘β€πŸ’» Set up the package's CLI for adding commands (#11)
1 parent fb9155a commit dba9ad9

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed

β€Žpyproject.tomlβ€Ž

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ name = "python-gitmojis"
1010
version = "0.0.0"
1111
requires-python = ">= 3.10"
1212
dependencies = [
13+
"click",
1314
"requests",
1415
]
1516

@@ -26,11 +27,15 @@ lint = [
2627
]
2728
test = [
2829
"pytest",
30+
"pytest-click",
2931
"pytest-cov",
3032
"pytest-custom-exit-code",
3133
"pytest-mock",
3234
]
3335

36+
[project.scripts]
37+
"gitmojis" = "gitmojis.__main__:main"
38+
3439
[tool.setuptools.packages.find]
3540
include = ["gitmojis*"]
3641
where = ["src"]

β€Žsrc/gitmojis/__main__.pyβ€Ž

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from gitmojis.cli import gitmojis_cli as main
2+
3+
if __name__ == "__main__":
4+
main()

β€Žsrc/gitmojis/cli/__init__.pyβ€Ž

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import click
2+
3+
from gitmojis.core import fetch_guide
4+
5+
6+
def get_commands() -> list[click.Command]:
7+
"""Return a list of `@click` commands defined in `gitmojis.cli.commands`."""
8+
from gitmojis.cli import commands as commands_module
9+
10+
return [
11+
command
12+
for command in commands_module.__dict__.values()
13+
if isinstance(command, click.Command)
14+
]
15+
16+
17+
@click.group(
18+
name="gitmojis",
19+
commands=get_commands(),
20+
)
21+
@click.version_option(
22+
package_name="python-gitmojis",
23+
prog_name="gitmojis",
24+
)
25+
@click.pass_context
26+
def gitmojis_cli(context: click.Context) -> None:
27+
"""Command-line interface for managing the official Gitmoji guide."""
28+
# Initialize the context object
29+
context.ensure_object(dict)
30+
31+
# Pass the current state of the Gitmoji guide to the group context
32+
context.obj["guide"] = fetch_guide()

β€Žsrc/gitmojis/cli/commands.pyβ€Ž

Whitespace-only changes.

β€Žtests/test_cli.pyβ€Ž

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import subprocess
2+
import sys
3+
4+
import click
5+
6+
from gitmojis.cli import commands as commands_module
7+
from gitmojis.cli import get_commands, gitmojis_cli
8+
from gitmojis.model import Guide
9+
10+
11+
def test_gitmojis_cli_runs_as_entry_point():
12+
result = subprocess.run(["gitmojis"])
13+
14+
assert result.returncode == 0
15+
16+
17+
def test_gitmojis_cli_runs_as_python_script():
18+
result = subprocess.run([sys.executable, "-m", "gitmojis"])
19+
20+
assert result.returncode == 0
21+
22+
23+
def test_get_commands_registers_command_from_commands_module(mocker):
24+
@click.command()
25+
def command():
26+
pass
27+
28+
mocker.patch.dict(commands_module.__dict__, {"command": command})
29+
30+
commands = get_commands()
31+
32+
assert command in commands
33+
34+
35+
def test_gitmojis_cli_passes_guide_to_context(mocker, cli_runner):
36+
mocker.patch("gitmojis.core.fetch_guide", return_value=Guide(gitmojis=[]))
37+
38+
@click.command()
39+
@click.pass_context
40+
def command(context):
41+
assert "guide" in context.obj
42+
43+
gitmojis_cli.add_command(command)
44+
45+
result = cli_runner.invoke(gitmojis_cli, "command")
46+
47+
assert result.exit_code == 0

0 commit comments

Comments
Β (0)