1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
import sys
import pytest
from typer.testing import CliRunner
from npe2 import __version__
from npe2.cli import app, main
from npe2.manifest.schema import PluginManifest
runner = CliRunner()
@pytest.mark.parametrize("debug", ["--debug", ""])
@pytest.mark.parametrize("imports", ["--imports", "--no-imports"])
def test_cli_validate_ok(sample_path, debug, imports, monkeypatch):
cmd = ["validate", str(sample_path / "my_plugin" / "napari.yaml"), imports]
if debug:
cmd += [debug]
with monkeypatch.context() as m:
m.setattr(sys, "path", [*sys.path, str(sample_path)])
result = runner.invoke(app, cmd)
assert "✔ Manifest for 'My Plugin' valid!" in result.stdout
assert result.exit_code == 0
def test_cli_validate_invalid(tmp_path, capsys):
(tmp_path / "manifest.yaml").write_text("name: hi??\n")
cmd = ["validate", str(tmp_path / "manifest.yaml")]
result = runner.invoke(app, cmd)
assert "'hi??' is not a valid python package name." in result.stdout
def test_cli_validate_load_err(tmp_path):
non_existent = str(tmp_path / "manifest.yaml")
result = runner.invoke(app, ["validate", non_existent])
assert result.exit_code == 1
assert "🅇 Unexpected error in" in result.stdout
assert "Could not find manifest for" in result.stdout
result = runner.invoke(app, ["validate", non_existent, "--debug"])
assert "Could not find manifest for" in result.stdout
@pytest.mark.parametrize("format", ["json", "yaml", "toml", "csv"])
@pytest.mark.parametrize("to_file", [True, False])
def test_cli_parse(sample_path, format, tmp_path, to_file):
cmd = ["parse", str(sample_path / "my_plugin" / "napari.yaml")]
if to_file:
dest = tmp_path / f"output.{format}"
cmd.extend(["-o", str(dest)])
else:
cmd.extend(["-f", format])
result = runner.invoke(app, cmd)
if format == "csv":
assert result.exit_code
return
assert result.exit_code == 0
if to_file:
assert dest.exists()
assert PluginManifest.from_file(dest)
else:
assert "my-plugin" in result.stdout # just prints the yaml
@pytest.mark.parametrize("format", ["json", "yaml", "toml", "csv"])
@pytest.mark.parametrize("to_file", [True, False])
@pytest.mark.parametrize("include_meta", [True, False])
def test_cli_fetch(format, tmp_path, to_file, include_meta):
cmd = ["fetch", "napari-omero"]
if to_file:
dest = tmp_path / f"output.{format}"
cmd.extend(["-o", str(dest)])
else:
cmd.extend(["-f", format])
if include_meta:
cmd.extend(["--include-package-meta", "--indent=2"])
result = runner.invoke(app, cmd)
if format == "csv":
assert result.exit_code
return
assert result.exit_code == 0
if to_file:
assert dest.exists()
assert PluginManifest.from_file(dest)
else:
assert "napari-omero" in result.stdout # just prints the yaml
if include_meta:
assert "package_metadata" in result.stdout
@pytest.mark.filterwarnings("default:Failed to convert")
def test_cli_convert_repo(npe1_repo, mock_npe1_pm_with_plugin):
result = runner.invoke(app, ["convert", str(npe1_repo)])
assert result.exit_code == 0
assert "✔ Conversion complete!" in result.stdout
@pytest.mark.filterwarnings("default:Failed to convert")
def test_cli_convert_repo_dry_run(npe1_repo, mock_npe1_pm_with_plugin):
result = runner.invoke(app, ["convert", str(npe1_repo), "-n"])
assert result.exit_code == 0
assert "# Manifest would be written to" in result.stdout
@pytest.mark.filterwarnings("ignore:The distutils package is deprecated")
@pytest.mark.filterwarnings("default:Failed to convert napari_get_writer")
def test_cli_convert_svg():
result = runner.invoke(app, ["convert", "napari-svg"])
assert "Some issues occured:" in result.stdout
assert "Found a multi-layer writer in 'napari-svg'" in result.stdout
assert result.exit_code == 0
def test_cli_convert_repo_fails(npe1_repo, mock_npe1_pm_with_plugin):
(npe1_repo / "setup.cfg").unlink()
result = runner.invoke(app, ["convert", str(npe1_repo)])
assert result.exit_code == 1
assert "Could not detect first gen napari plugin package" in result.stdout
@pytest.mark.filterwarnings("default:Failed to convert")
def test_cli_convert_package_name(npe1_repo, mock_npe1_pm_with_plugin):
result = runner.invoke(app, ["convert", "npe1-plugin"])
assert "name: npe1-plugin" in result.stdout # just prints the yaml
assert result.exit_code == 0
def test_cli_main(monkeypatch, sample_path):
cmd = ["npe2", "validate", str(sample_path / "my_plugin" / "napari.yaml")]
monkeypatch.setattr(sys, "argv", cmd)
with pytest.raises(SystemExit) as e:
main()
assert e.value.code == 0
def test_cli_cache_list_empty():
result = runner.invoke(app, ["cache", "--list"])
assert "Nothing cached" in result.stdout
assert result.exit_code == 0
def test_cli_cache_list_full(uses_npe1_plugin, mock_cache):
(mock_cache / "npe1-plugin.yaml").write_text("name: npe1-plugin\n")
result = runner.invoke(app, ["cache", "--list"])
assert result.stdout == "npe1-plugin: 0.1.0\n"
assert result.exit_code == 0
def test_cli_cache_list_named(uses_npe1_plugin, mock_cache):
(mock_cache / "npe1-plugin.yaml").write_text("name: npe1-plugin\n")
result = runner.invoke(app, ["cache", "--list", "not-a-plugin"])
assert result.stdout == "Nothing cached for plugins: not-a-plugin\n"
assert result.exit_code == 0
def test_cli_cache_clear_empty():
result = runner.invoke(app, ["cache", "--clear"])
assert "Nothing to clear" in result.stdout
assert result.exit_code == 0
def test_cli_cache_clear_full(mock_cache):
(mock_cache / "npe1-plugin.yaml").write_text("name: npe1-plugin\n")
result = runner.invoke(app, ["cache", "--clear"])
assert "Cleared these files from cache" in result.stdout
assert "- npe1-plugin.yaml" in result.stdout
assert result.exit_code == 0
def test_cli_cache_clear_named(mock_cache):
(mock_cache / "npe1-plugin.yaml").write_text("name: npe1-plugin\n")
result = runner.invoke(app, ["cache", "--clear", "not-a-plugin"])
assert result.stdout == "Nothing to clear for plugins: not-a-plugin\n"
assert result.exit_code == 0
@pytest.mark.parametrize("format", ["table", "compact", "yaml", "json"])
@pytest.mark.parametrize("fields", [None, "name,version,author"])
def test_cli_list(format, fields, uses_npe1_plugin):
result = runner.invoke(app, ["list", "-f", format, "--fields", fields])
assert result.exit_code == 0
assert "npe1-plugin" in result.output
if fields and "author" in fields and format != "compact":
assert "author" in result.output.lower()
else:
assert "author" not in result.output.lower()
def test_cli_list_sort(uses_npe1_plugin):
result = runner.invoke(app, ["list", "--sort", "version"])
assert result.exit_code == 0
result = runner.invoke(app, ["list", "--sort", "7"])
assert result.exit_code
assert "Invalid sort value '7'" in result.output
result = runner.invoke(app, ["list", "--sort", "notaname"])
assert result.exit_code
assert "Invalid sort value 'notaname'" in result.output
def test_cli_version():
result = runner.invoke(app, ["--version"])
assert result.exit_code == 0
assert __version__ in result.output
def test_compile(compiled_plugin_dir):
result = runner.invoke(app, ["compile", str(compiled_plugin_dir)])
assert "id: my_compiled_plugin.my-plugin.generate_random_data" in result.output
|