diff --git a/scripts/test.sh b/scripts/test.sh index 6edfebf6c3..5440f2ce2e 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -6,3 +6,5 @@ set -x export PYTHONPATH=./docs/src pytest --cov=typer --cov=tests --cov=docs/src --cov-report=term-missing ${@} bash ./scripts/lint.sh +# Check README.md is up to date +diff --brief docs/index.md README.md diff --git a/tests/test_tutorial/test_first_steps/test_tutorial001.py b/tests/test_tutorial/test_first_steps/test_tutorial001.py index cd638359e4..d7b8473618 100644 --- a/tests/test_tutorial/test_first_steps/test_tutorial001.py +++ b/tests/test_tutorial/test_first_steps/test_tutorial001.py @@ -1,13 +1,25 @@ +import subprocess + import typer from typer.testing import CliRunner -from first_steps.tutorial001 import main +from first_steps import tutorial001 as mod runner = CliRunner() def test_cli(): app = typer.Typer() - app.command()(main) + app.command()(mod.main) result = runner.invoke(app, []) assert result.output == "Hello World\n" + + +def test_script(): + result = subprocess.run( + ["coverage", "run", "--parallel-mode", mod.__file__, "--help"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding="utf-8", + ) + assert "Usage" in result.stdout diff --git a/tests/test_tutorial/test_first_steps/test_tutorial002.py b/tests/test_tutorial/test_first_steps/test_tutorial002.py new file mode 100644 index 0000000000..9c70a3e577 --- /dev/null +++ b/tests/test_tutorial/test_first_steps/test_tutorial002.py @@ -0,0 +1,33 @@ +import subprocess + +import typer +from typer.testing import CliRunner + +from first_steps import tutorial002 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + + +def test_1(): + result = runner.invoke(app, []) + assert result.exit_code != 0 + assert 'Error: Missing argument "NAME"' in result.output + + +def test_2(): + result = runner.invoke(app, ["Camila"]) + assert result.exit_code == 0 + assert "Hello Camila" in result.output + + +def test_script(): + result = subprocess.run( + ["coverage", "run", "--parallel-mode", mod.__file__, "--help"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding="utf-8", + ) + assert "Usage" in result.stdout diff --git a/tests/test_tutorial/test_first_steps/test_tutorial003.py b/tests/test_tutorial/test_first_steps/test_tutorial003.py new file mode 100644 index 0000000000..bf0038dadf --- /dev/null +++ b/tests/test_tutorial/test_first_steps/test_tutorial003.py @@ -0,0 +1,33 @@ +import subprocess + +import typer +from typer.testing import CliRunner + +from first_steps import tutorial003 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + + +def test_1(): + result = runner.invoke(app, ["Camila"]) + assert result.exit_code != 0 + assert 'Error: Missing argument "LASTNAME"' in result.output + + +def test_2(): + result = runner.invoke(app, ["Camila", "Gutiérrez"]) + assert result.exit_code == 0 + assert "Hello Camila Gutiérrez" in result.output + + +def test_script(): + result = subprocess.run( + ["coverage", "run", "--parallel-mode", mod.__file__, "--help"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding="utf-8", + ) + assert "Usage" in result.stdout diff --git a/tests/test_tutorial/test_first_steps/test_tutorial004.py b/tests/test_tutorial/test_first_steps/test_tutorial004.py new file mode 100644 index 0000000000..e00f5a2041 --- /dev/null +++ b/tests/test_tutorial/test_first_steps/test_tutorial004.py @@ -0,0 +1,45 @@ +import subprocess + +import typer +from typer.testing import CliRunner + +from first_steps import tutorial004 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + + +def test_1(): + result = runner.invoke(app, ["Camila", "Gutiérrez"]) + assert result.exit_code == 0 + assert "Hello Camila Gutiérrez" in result.output + + +def test_formal_1(): + result = runner.invoke(app, ["Camila", "Gutiérrez", "--formal"]) + assert result.exit_code == 0 + assert "Good day Ms. Camila Gutiérrez." in result.output + + +def test_formal_2(): + result = runner.invoke(app, ["Camila", "--formal", "Gutiérrez"]) + assert result.exit_code == 0 + assert "Good day Ms. Camila Gutiérrez." in result.output + + +def test_formal_3(): + result = runner.invoke(app, ["--formal", "Camila", "Gutiérrez"]) + assert result.exit_code == 0 + assert "Good day Ms. Camila Gutiérrez." in result.output + + +def test_script(): + result = subprocess.run( + ["coverage", "run", "--parallel-mode", mod.__file__, "--help"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding="utf-8", + ) + assert "Usage" in result.stdout diff --git a/tests/test_tutorial/test_first_steps/test_tutorial005.py b/tests/test_tutorial/test_first_steps/test_tutorial005.py new file mode 100644 index 0000000000..4aa2e722e5 --- /dev/null +++ b/tests/test_tutorial/test_first_steps/test_tutorial005.py @@ -0,0 +1,52 @@ +import subprocess + +import typer +from typer.testing import CliRunner + +from first_steps import tutorial005 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + + +def test_help(): + result = runner.invoke(app, ["--help"]) + assert result.exit_code == 0 + assert "--lastname TEXT" in result.output + assert "--formal / --no-formal" in result.output + + +def test_1(): + result = runner.invoke(app, ["Camila"]) + assert result.exit_code == 0 + assert "Hello Camila" in result.output + + +def test_option_lastname(): + result = runner.invoke(app, ["Camila", "--lastname", "Gutiérrez"]) + assert result.exit_code == 0 + assert "Hello Camila Gutiérrez" in result.output + + +def test_option_lastname_2(): + result = runner.invoke(app, ["--lastname", "Gutiérrez", "Camila"]) + assert result.exit_code == 0 + assert "Hello Camila Gutiérrez" in result.output + + +def test_formal_1(): + result = runner.invoke(app, ["Camila", "--lastname", "Gutiérrez", "--formal"]) + assert result.exit_code == 0 + assert "Good day Ms. Camila Gutiérrez." in result.output + + +def test_script(): + result = subprocess.run( + ["coverage", "run", "--parallel-mode", mod.__file__, "--help"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding="utf-8", + ) + assert "Usage" in result.stdout diff --git a/typer/testing.py b/typer/testing.py index 1a07c43f2b..88cd83dc6b 100644 --- a/typer/testing.py +++ b/typer/testing.py @@ -8,7 +8,6 @@ class CliRunner(ClickCliRunner): def invoke( # type: ignore self, app: Typer, - cli: Typer, args: Optional[Union[str, Iterable[str]]] = None, input: Optional[Union[bytes, Text, IO[Any]]] = None, env: Optional[Mapping[str, str]] = None,