Skip to content
This repository has been archived by the owner on Nov 6, 2024. It is now read-only.

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DragaDoncila committed Nov 6, 2024
1 parent c799109 commit 9b75e70
Showing 1 changed file with 8 additions and 114 deletions.
122 changes: 8 additions & 114 deletions tests/test_create_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,117 +3,11 @@
--------------------
"""


import os
import subprocess

import pytest


def run_tox(plugin):
"""Run the tox suite of the newly created plugin."""
try:
subprocess.check_call(
["tox", "-c", os.path.join(plugin, "tox.ini"), "-e", "py", "--", plugin]
)
except subprocess.CalledProcessError as e:
pytest.fail("Subprocess fail", pytrace=True)

@pytest.mark.parametrize("include_reader_plugin", ["y", "n"])
@pytest.mark.parametrize("include_writer_plugin", ["y", "n"])
@pytest.mark.parametrize("include_sample_data_plugin", ["y", "n"])
@pytest.mark.parametrize("include_widget_plugin", ["y", "n"])
def test_run_cookiecutter_and_plugin_tests(cookies, capsys, include_reader_plugin, include_writer_plugin, include_sample_data_plugin, include_widget_plugin):
"""Create a new plugin via cookiecutter and run its tests."""
result = cookies.bake(extra_context={
"plugin_name": "foo-bar",
"include_reader_plugin": include_reader_plugin,
"include_writer_plugin": include_writer_plugin,
"include_sample_data_plugin": include_sample_data_plugin,
"include_widget_plugin": include_widget_plugin,
}
)

assert result.exit_code == 0
assert result.exception is None
assert result.project_path.name == "foo-bar"
assert result.project_path.is_dir()
assert result.project_path.joinpath("src").is_dir()
assert result.project_path.joinpath("src", "foo_bar", "__init__.py").is_file()

test_path = result.project_path.joinpath("src", "foo_bar", "_tests")
if include_reader_plugin == "y":
assert (test_path / "test_reader.py").is_file()
if include_writer_plugin == "y":
assert (test_path / "test_writer.py").is_file()
if include_sample_data_plugin == "y":
assert (test_path / "test_sample_data.py").is_file()
if include_widget_plugin == "y":
assert (test_path / "test_widget.py").is_file()

# if all are `n` there are no modules or tests
if "y" in {include_reader_plugin, include_writer_plugin, include_sample_data_plugin, include_widget_plugin}:
run_tox(str(result.project_path))


def test_run_cookiecutter_and_plugin_tests_with_napari_prefix(cookies, capsys):
"""make sure it's also ok to use napari prefix."""
result = cookies.bake(extra_context={"plugin_name": "napari-foo"})

assert result.exit_code == 0
assert result.exception is None
assert result.project_path.name == "napari-foo"
assert result.project_path.is_dir()
assert result.project_path.joinpath("src").is_dir()
assert result.project_path.joinpath("src", "napari_foo", "__init__.py").is_file()
assert result.project_path.joinpath("src", "napari_foo", "_tests", "test_reader.py").is_file()


def test_run_cookiecutter_select_plugins(cookies, capsys):
"""make sure it's also ok to use napari prefix."""
result = cookies.bake(
extra_context={
"plugin_name": "anything",
"include_widget_plugin": "n",
"include_writer_plugin": "n",
}
)

assert result.exit_code == 0
assert result.exception is None
assert result.project_path.name == "anything"
assert result.project_path.is_dir()
assert result.project_path.joinpath("src").is_dir()
assert result.project_path.joinpath("src", "anything", "__init__.py").is_file()
assert result.project_path.joinpath("src", "anything", "_tests", "test_reader.py").is_file()

assert not result.project_path.joinpath("src", "anything", "_widget.py").is_file()
assert not result.project_path.joinpath(
"src", "anything", "_tests", "test_widget.py"
).is_file()
assert not result.project_path.joinpath("src", "anything", "_writer.py").is_file()
assert not result.project_path.joinpath(
"src", "anything", "_tests", "test_writer.py"
).is_file()


@pytest.mark.parametrize("include_reader_plugin", ["y", "n"])
@pytest.mark.parametrize("include_writer_plugin", ["y", "n"])
@pytest.mark.parametrize("include_sample_data_plugin", ["y", "n"])
@pytest.mark.parametrize("include_widget_plugin", ["y", "n"])
def test_pre_commit_validity(cookies, include_reader_plugin, include_writer_plugin, include_sample_data_plugin, include_widget_plugin):
result = cookies.bake(
extra_context={
"plugin_name": "anything",
"include_reader_plugin": include_reader_plugin,
"include_writer_plugin": include_writer_plugin,
"include_sample_data_plugin": include_sample_data_plugin,
"include_widget_plugin": include_widget_plugin,
"install_precommit": "y",
}
)
result.project_path.joinpath("setup.cfg").is_file()
try:
subprocess.run(["pre-commit", "run", "--all", "--show-diff-on-failure"], cwd=str(result.project_path), check=True, capture_output=True)
except subprocess.CalledProcessError as e:
pytest.fail(f"pre-commit failed with output:\n{e.stdout.decode()}\nerror:\n{e.stderr.decode()}")
def test_pre_prompt_exit(cookies, capfd):
result = cookies.bake()
captured = capfd.readouterr()
assert "The napari cookiecutter plugin template has been replaced with a copier template!" in captured.out
assert "For more information, refer to the template README:" in captured.out
assert "https://github.com/napari/napari-plugin-template" in captured.out
assert result.exit_code == -1
assert str(result.exception) == 'Pre-Prompt Hook script failed'

0 comments on commit 9b75e70

Please sign in to comment.