Skip to content

Commit

Permalink
Add test helpers, rewrite integration tests (#100)
Browse files Browse the repository at this point in the history
* Add test helpers, rewrite integration tests

* Remove custom proctest, use tmp_path pytest fixture

* Fix linting and tests

* set shell=True when args is a str

* Add tmp_run fixture

Reduces the boilerplate of having to specify cwd every time when
commands are 99% of the time run in the tmp_path.

* Add typing for tmp_run

* Use typing_extensions for Protocol

Protocol was only introduced in Python 3.8. typing_extensions makes
it available for older Pythons.

* Remove unused fixtures

Co-authored-by: Brett Langdon <me@brett.is>

Co-authored-by: Brett Langdon <me@brett.is>
  • Loading branch information
Kyle-Verhoog and brettlangdon committed May 25, 2021
1 parent 6d064ca commit 5ad6e6d
Show file tree
Hide file tree
Showing 3 changed files with 541 additions and 86 deletions.
1 change: 1 addition & 0 deletions riotfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"pytest": latest,
"pytest-cov": latest,
"mock": latest,
"typing-extensions": latest,
},
),
Venv(
Expand Down
86 changes: 0 additions & 86 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,72 +57,6 @@ def assert_args(args):
)


def test_main(cli: click.testing.CliRunner) -> None:
"""Running main with no command returns usage."""
result = cli.invoke(riot.cli.main)
assert result.exit_code == 0
assert result.stdout.startswith("Usage: main")


def test_main_help(cli: click.testing.CliRunner) -> None:
"""Running main with --help returns usage."""
result = cli.invoke(riot.cli.main, ["--help"])
assert result.exit_code == 0
assert result.stdout.startswith("Usage: main")


def test_main_version(cli: click.testing.CliRunner) -> None:
"""Running main with --version returns version string."""
result = cli.invoke(riot.cli.main, ["--version"])
assert result.exit_code == 0
assert result.stdout.startswith("main, version ")


def test_list_empty(cli: click.testing.CliRunner) -> None:
"""Running list with an empty riotfile prints nothing."""
with with_riotfile(cli, "empty_riotfile.py"):
result = cli.invoke(riot.cli.main, ["list"])
assert result.exit_code == 0
assert result.stdout == ""


def test_list_no_riotfile(cli: click.testing.CliRunner) -> None:
"""Running list with no riotfile fails with an error."""
with without_riotfile(cli):
result = cli.invoke(riot.cli.main, ["list"])
assert result.exit_code == 2
assert result.stdout.startswith("Usage: main")
assert result.stdout.endswith(
"Error: Invalid value for '-f' / '--file': Path 'riotfile.py' does not exist.\n"
)


def test_list_default_pattern(cli: click.testing.CliRunner) -> None:
"""Running list with no pattern passes through the default pattern."""
with mock.patch("riot.cli.Session.list_venvs") as list_venvs:
with with_riotfile(cli, "empty_riotfile.py"):
result = cli.invoke(riot.cli.main, ["list"])
# Success, but no output because we don't have a matching pattern
assert result.exit_code == 0
assert result.stdout == ""

list_venvs.assert_called_once()
assert list_venvs.call_args.args[0].pattern == ".*"


def test_list_with_pattern(cli: click.testing.CliRunner) -> None:
"""Running list with a pattern passes through the pattern."""
with mock.patch("riot.cli.Session.list_venvs") as list_venvs:
with with_riotfile(cli, "empty_riotfile.py"):
result = cli.invoke(riot.cli.main, ["list", "^pattern.*"])
# Success, but no output because we don't have a matching pattern
assert result.exit_code == 0
assert result.stdout == ""

list_venvs.assert_called_once()
assert list_venvs.call_args.args[0].pattern == "^pattern.*"


def test_list_with_venv_pattern(cli: click.testing.CliRunner) -> None:
"""Running list with a venv pattern passes."""
with with_riotfile(cli, "simple_riotfile.py"):
Expand Down Expand Up @@ -170,26 +104,6 @@ def test_list_with_python(cli: click.testing.CliRunner) -> None:
)


def test_run(cli: click.testing.CliRunner) -> None:
"""Running run with default options."""
with mock.patch("riot.cli.Session.run") as run:
with with_riotfile(cli, "empty_riotfile.py"):
result = cli.invoke(riot.cli.main, ["run"])
# Success, but no output because we mock run
assert result.exit_code == 0
assert result.stdout == ""

run.assert_called_once()
kwargs = run.call_args.kwargs
assert_args(kwargs)
assert kwargs["pattern"].pattern == ".*"
assert kwargs["venv_pattern"].pattern == ".*"
assert kwargs["recreate_venvs"] is False
assert kwargs["skip_base_install"] is False
assert kwargs["pass_env"] is False
assert kwargs["exit_first"] is False


def test_run_with_long_args(cli: click.testing.CliRunner) -> None:
"""Running run with long option names uses those options."""
with mock.patch("riot.cli.Session.run") as run:
Expand Down
Loading

0 comments on commit 5ad6e6d

Please sign in to comment.