Skip to content

Commit

Permalink
docs with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick91 committed Dec 2, 2024
1 parent 79bf9fb commit aceae4e
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 38 deletions.
46 changes: 8 additions & 38 deletions docs/tutorial/splitting-apps.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,60 +31,30 @@ This application will have the following commands:

Let's start by creating a simple module that prints the version of the application.

```python
import typer
{* docs_src/splitting_apps/version.py hl[3,6,7,8] *}

app = typer.Typer()

@app.command()
def version():
typer.echo("MyCLI version 1.0.0")
```

In this example, we are creating a new `Typer` app instance for the `version` module. This is not required for a single-file application but is necessary for a multi-file application, as it will allow us to include this command in the main app using `add_typer`.
In this file we are creating a new Typer app instance for the version command. This is not required in single-file applications, but in the case of multi-file applications it will allow us to include this command in the main application using `add_typer`.

### Main Module (`main.py`)

The main module will be the entry point of the application. It will import the version module and the users module.
The main module will be the entry point of the application. It will import the version module and the users module. We'll see how to implement the user module in the next section.

```python
import typer

from version import app as version_app
from users import app as users_app

app = typer.Typer()

app.add_typer(version_app)
app.add_typer(users_app, name="users")
```
{* docs_src/splitting_apps/main.py hl[8,9] *}

In this module, we import the `version` and `users` modules and add them to the main app using `add_typer`. For the `users` module, we specify the name as `users` to group the commands under the `users` namespace.

Let's now create the `users` module with the `add` and `delete` commands.

### Users Add Command (`users/add.py`)

```python
import typer

app = typer.Typer()

@app.command()
def add(name: str):
typer.echo(f"Adding user: {name}")
```
{* docs_src/splitting_apps/users/add.py *}

### Users Delete Command (`users/delete.py`)

```python
import typer
{* docs_src/splitting_apps/users/delete.py *}

app = typer.Typer()
### Users' app

@app.command()
def delete(name: str):
typer.echo(f"Deleting user: {name
```
{* docs_src/splitting_apps/users/__init__.py *}

Similar to the `version` module, we create a new `Typer` app instance for the `users` module. This allows us to include the `add` and `delete` commands in the users app.
Empty file.
9 changes: 9 additions & 0 deletions docs_src/splitting_apps/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import typer

from .users import app as users_app
from .version import app as version_app

app = typer.Typer()

app.add_typer(version_app)
app.add_typer(users_app, name="users")
9 changes: 9 additions & 0 deletions docs_src/splitting_apps/users/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import typer

from .add import app as add_app
from .delete import app as delete_app

app = typer.Typer()

app.add_typer(add_app)
app.add_typer(delete_app)
8 changes: 8 additions & 0 deletions docs_src/splitting_apps/users/add.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import typer

app = typer.Typer()


@app.command()
def add(name: str):
typer.echo(f"Adding user: {name}")
8 changes: 8 additions & 0 deletions docs_src/splitting_apps/users/delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import typer

app = typer.Typer()


@app.command()
def delete(name: str):
typer.echo(f"Deleting user: {name}")
8 changes: 8 additions & 0 deletions docs_src/splitting_apps/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import typer

app = typer.Typer()


@app.command()
def version():
typer.echo("My CLI Version 1.0")
Empty file.
48 changes: 48 additions & 0 deletions tests/test_tutorial/test_splitting_apps/test_tutorial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import os
import subprocess
import sys

from typer.testing import CliRunner

from docs_src.splitting_apps import main as mod

runner = CliRunner()


def test_help():
result = runner.invoke(mod.app, ["--help"])

assert result.exit_code == 0

assert "version" in result.output
assert "users" in result.output


def test_version():
result = runner.invoke(mod.app, ["version"])

assert result.exit_code == 0
assert "My CLI Version 1.0" in result.output


def test_users_help():
result = runner.invoke(mod.app, ["users", "--help"])

assert result.exit_code == 0

assert "add" in result.output
assert "delete" in result.output


def test_add_user():
result = runner.invoke(mod.app, ["users", "add", "Camila"])

assert result.exit_code == 0
assert "Adding user: Camila" in result.output


def test_delete_user():
result = runner.invoke(mod.app, ["users", "delete", "Camila"])

assert result.exit_code == 0
assert "Deleting user: Camila" in result.output

0 comments on commit aceae4e

Please sign in to comment.