def test_cli_runscript(monkeypatch, tmp_path): """Test that running napari script.py runs a script""" script = tmp_path / 'test.py' script.write_text('import napari; v = napari.Viewer(show=False)') with monkeypatch.context() as m: m.setattr(sys, 'argv', ['napari', str(script)]) with pytest.raises(SystemExit): __main__.main()
def test_cli_raises(monkeypatch): """test that unknown kwargs raise the correct errors.""" with monkeypatch.context() as m: m.setattr(sys, 'argv', ['napari', 'path/to/file', '--nonsense']) with pytest.raises(SystemExit) as e: __main__.main() assert str(e.value) == 'error: unrecognized arguments: --nonsense' with monkeypatch.context() as m: m.setattr(sys, 'argv', ['napari', 'path/to/file', '--gamma']) with pytest.raises(SystemExit) as e: __main__.main() assert str(e.value) == 'error: argument --gamma expected one argument'
def test_cli_parses_unknowns(monkeypatch): """test that we can parse layer keyword arg variants""" def assert_kwargs(*args, **kwargs): assert args == (["file"], ) assert kwargs['contrast_limits'] == (0, 1) @contextmanager def gui_qt(**kwargs): yield # testing all the variants of literal_evals monkeypatch.setattr(__main__, 'view_path', assert_kwargs) monkeypatch.setattr(__main__, 'gui_qt', gui_qt) with monkeypatch.context() as m: m.setattr(sys, 'argv', ['n', 'file', '--contrast-limits', '(0, 1)']) __main__.main() with monkeypatch.context() as m: m.setattr(sys, 'argv', ['n', 'file', '--contrast-limits', '(0,1)']) __main__.main() with monkeypatch.context() as m: m.setattr(sys, 'argv', ['n', 'file', '--contrast-limits=(0, 1)']) __main__.main() with monkeypatch.context() as m: m.setattr(sys, 'argv', ['n', 'file', '--contrast-limits=(0,1)']) __main__.main()
def test_cli_passes_kwargs(monkeypatch): """test that we can parse layer keyword arg variants""" @contextmanager def gui_qt(**kwargs): yield viewer_mock = MagicMock(napari.Viewer) monkeypatch.setattr(napari, 'Viewer', viewer_mock) monkeypatch.setattr(__main__, 'gui_qt', gui_qt) with monkeypatch.context() as m: m.setattr(sys, 'argv', ['n', 'file', '--name', 'some name']) __main__.main() expected = call().open( path=['file'], stack=False, plugin=None, layer_type=None, name='some name', ) assert expected in viewer_mock.mock_calls
import sys import os import site # patch. see https://github.com/napari/napari/pull/2030 nap = os.path.join(site.getsitepackages()[0], "napari") build_res = os.path.join(nap, "resources", "build_icons.py") if os.path.exists(build_res): with open(build_res, "r") as f: text = f.read() text = text.replace("check_call([name,", "check_call([sys.executable, name,") with open(build_res, "w") as f: f.write(text) # probably mac specific for now bin = os.path.join(os.path.dirname(sys.exec_prefix), "app_packages", "bin") os.environ["PATH"] = bin + os.pathsep + os.environ["PATH"] from napari.__main__ import main sys.exit(main()) except ImportError: from .bootstrap import BootStrap BootStrap().run() except Exception: from .error import ErrorDialog ErrorDialog().run()
def test_cli_shows_plugins(monkeypatch, capsys): """Test the cli --info runs and shows plugins""" monkeypatch.setattr(sys, 'argv', ['napari', '--info']) with pytest.raises(SystemExit): __main__.main() assert 'svg' in str(capsys.readouterr())
def test_cli_works(monkeypatch, capsys): """Test the cli runs and shows help""" monkeypatch.setattr(sys, 'argv', ['napari', '-h']) with pytest.raises(SystemExit): __main__.main() assert 'napari command line viewer.' in str(capsys.readouterr())