diff --git a/scripts/test.sh b/scripts/test.sh index 6edfebf..5440f2c 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 cd63835..d7b8473 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 0000000..9c70a3e --- /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 0000000..bf0038d --- /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 0000000..e00f5a2 --- /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 0000000..4aa2e72 --- /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