Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ name = "python-gitmojis"
version = "0.0.0"
requires-python = ">= 3.10"
dependencies = [
"click",
"requests",
]

Expand All @@ -26,11 +27,15 @@ lint = [
]
test = [
"pytest",
"pytest-click",
"pytest-cov",
"pytest-custom-exit-code",
"pytest-mock",
]

[project.scripts]
"gitmojis" = "gitmojis.__main__:main"

[tool.setuptools.packages.find]
include = ["gitmojis*"]
where = ["src"]
Expand Down
4 changes: 4 additions & 0 deletions src/gitmojis/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from gitmojis.cli import gitmojis_cli as main

if __name__ == "__main__":
main()
32 changes: 32 additions & 0 deletions src/gitmojis/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import click

from gitmojis.core import fetch_guide


def get_commands() -> list[click.Command]:
"""Return a list of `@click` commands defined in `gitmojis.cli.commands`."""
from gitmojis.cli import commands as commands_module

return [
command
for command in commands_module.__dict__.values()
if isinstance(command, click.Command)
]


@click.group(
name="gitmojis",
commands=get_commands(),
)
@click.version_option(
package_name="python-gitmojis",
prog_name="gitmojis",
)
@click.pass_context
def gitmojis_cli(context: click.Context) -> None:
"""Command-line interface for managing the official Gitmoji guide."""
# Initialize the context object
context.ensure_object(dict)

# Pass the current state of the Gitmoji guide to the group context
context.obj["guide"] = fetch_guide()
Empty file added src/gitmojis/cli/commands.py
Empty file.
47 changes: 47 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import subprocess
import sys

import click

from gitmojis.cli import commands as commands_module
from gitmojis.cli import get_commands, gitmojis_cli
from gitmojis.model import Guide


def test_gitmojis_cli_runs_as_entry_point():
result = subprocess.run(["gitmojis"])

assert result.returncode == 0


def test_gitmojis_cli_runs_as_python_script():
result = subprocess.run([sys.executable, "-m", "gitmojis"])

assert result.returncode == 0


def test_get_commands_registers_command_from_commands_module(mocker):
@click.command()
def command():
pass

mocker.patch.dict(commands_module.__dict__, {"command": command})

commands = get_commands()

assert command in commands


def test_gitmojis_cli_passes_guide_to_context(mocker, cli_runner):
mocker.patch("gitmojis.core.fetch_guide", return_value=Guide(gitmojis=[]))

@click.command()
@click.pass_context
def command(context):
assert "guide" in context.obj

gitmojis_cli.add_command(command)

result = cli_runner.invoke(gitmojis_cli, "command")

assert result.exit_code == 0