def test_valid_zuliprc_but_no_connection(capsys, mocker, minimal_zuliprc, server_connection_error="some_error"): mocker.patch('zulipterminal.core.Controller.__init__', side_effect=ServerConnectionFailure(server_connection_error)) with pytest.raises(SystemExit) as e: main(["-c", minimal_zuliprc]) assert str(e.value) == '1' captured = capsys.readouterr() lines = captured.out.strip().split("\n") expected_lines = [ "Loading with:", " theme 'default' specified with no config.", " autohide setting 'no_autohide' specified with no config.", "\x1b[91m", ("Error connecting to Zulip server: {}.\x1b[0m".format( server_connection_error)), ] assert lines == expected_lines assert captured.err == ""
def test_valid_zuliprc_but_no_connection(capsys, mocker, tmpdir, server_connection_error="some_error"): mocker.patch('zulipterminal.core.Controller.__init__', side_effect=ServerConnectionFailure(server_connection_error)) zuliprc_path = str(tmpdir) + "/zuliprc" with open(zuliprc_path, "w") as f: f.write("[api]") # minimal to avoid Exception with pytest.raises(SystemExit) as e: main(["-c", zuliprc_path]) assert str(e.value) == '1' captured = capsys.readouterr() lines = captured.out.strip().split("\n") expected_lines = [ "Loading with:", " theme 'default' specified with no config.", " autohide setting 'autohide' specified with no config.", "", ("\x1b[91mError connecting to Zulip server: {}.".format( server_connection_error)), ] assert lines == expected_lines assert captured.err == ""
def test_main_cannot_write_zuliprc_given_good_credentials( monkeypatch: pytest.MonkeyPatch, capsys: CaptureFixture[str], mocker: MockerFixture, unreadable_dir: Tuple[Path, Path], path_to_use: str, expected_exception: str, ) -> None: tmp_path, unusable_path = unreadable_dir # This is default base path to use zuliprc_path = os.path.join(str(tmp_path), path_to_use) monkeypatch.setenv("HOME", zuliprc_path) # Give some arbitrary input and fake that it's always valid mocker.patch.object(builtins, "input", lambda _: "text\n") response = mocker.Mock(json=lambda: dict(api_key=""), status_code=200) mocker.patch(MODULE + ".get_api_key", return_value=(response, None)) with pytest.raises(SystemExit): main([]) captured = capsys.readouterr() lines = captured.out.strip().split("\n") expected_line = ( "\x1b[91m" f"{expected_exception}: zuliprc could not be created " f"at {os.path.join(zuliprc_path, 'zuliprc')}" "\x1b[0m" ) assert lines[-1] == expected_line
def test_successful_main_function_with_config( capsys, mocker, parameterized_zuliprc, config_key, config_value, footlinks_output, ): config = { "theme": "default", "autohide": "autohide", "notify": "enabled", "color-depth": "256", } config[config_key] = config_value zuliprc = parameterized_zuliprc(config) mocker.patch("zulipterminal.core.Controller.__init__", return_value=None) mocker.patch("zulipterminal.core.Controller.main", return_value=None) with pytest.raises(SystemExit): main(["-c", zuliprc]) captured = capsys.readouterr() lines = captured.out.strip().split("\n") expected_lines = [ 'Loading with:', " theme 'zt_dark' specified in zuliprc file (by alias 'default').", " autohide setting 'autohide' specified in zuliprc file.", f" maximum footlinks value {footlinks_output}", " color depth setting '256' specified in zuliprc file." ] assert lines == expected_lines
def test_warning_regarding_incomplete_theme(capsys, mocker, monkeypatch, minimal_zuliprc, bad_theme, server_connection_error="sce"): mocker.patch('zulipterminal.core.Controller.__init__', side_effect=ServerConnectionFailure(server_connection_error)) monkeypatch.setitem(THEMES, bad_theme, []) mocker.patch('zulipterminal.cli.run.all_themes', return_value=('a', 'b', 'c', 'd')) mocker.patch('zulipterminal.cli.run.complete_and_incomplete_themes', return_value=(['a', 'b'], ['c', 'd'])) with pytest.raises(SystemExit) as e: main(["-c", minimal_zuliprc, "-t", bad_theme]) assert str(e.value) == '1' captured = capsys.readouterr() lines = captured.out.strip().split("\n") expected_lines = [ "Loading with:", " theme '{}' specified on command line.".format(bad_theme), "\x1b[93m" " WARNING: Incomplete theme; results may vary!", " (you could try: {}, {})" "\x1b[0m".format('a', 'b'), " autohide setting 'no_autohide' specified with no config.", "\x1b[91m", ("Error connecting to Zulip server: {}.\x1b[0m". format(server_connection_error)), ] assert lines == expected_lines assert captured.err == ""
def test_main_help(capsys, options): with pytest.raises(SystemExit): main([options]) captured = capsys.readouterr() lines = captured.out.strip().split("\n") assert lines[0].startswith('usage: ') required_arguments = { '--theme THEME, -t THEME', '--autohide', '--no-autohide', '-h, --help', '-d, --debug', '--profile', '--config-file CONFIG_FILE, -c CONFIG_FILE', '-v, --version' } optional_argument_lines = {line[2:] for line in lines if len(line) > 2 and line[2] == '-'} for line in optional_argument_lines: assert any(line.startswith(arg) for arg in required_arguments) assert captured.err == ""
def test_main_cannot_write_zuliprc_given_good_credentials( monkeypatch, capsys, mocker, unreadable_dir, path_to_use, expected_exception, ): tmpdir, unusable_path = unreadable_dir # This is default base path to use zuliprc_path = os.path.join(str(tmpdir), path_to_use) monkeypatch.setenv("HOME", zuliprc_path) # Give some arbitrary input and fake that it's always valid mocker.patch.object(builtins, 'input', lambda _: 'text\n') mocker.patch("zulipterminal.cli.run.get_api_key", return_value=(mocker.Mock(status_code=200), None)) with pytest.raises(SystemExit): main([]) captured = capsys.readouterr() lines = captured.out.strip().split("\n") expected_line = ("\x1b[91m" "{}: zuliprc could not be created at {}" "\x1b[0m".format(expected_exception, os.path.join(zuliprc_path, "zuliprc"))) assert lines[-1] == expected_line
def test_valid_zuliprc_but_no_connection(capsys, mocker, minimal_zuliprc, server_connection_error="some_error"): mocker.patch( "zulipterminal.core.Controller.__init__", side_effect=ServerConnectionFailure(server_connection_error), ) with pytest.raises(SystemExit) as e: main(["-c", minimal_zuliprc]) assert str(e.value) == "1" captured = capsys.readouterr() lines = captured.out.strip().split("\n") expected_lines = [ "Loading with:", " theme 'zt_dark' specified with no config.", " autohide setting 'no_autohide' specified with no config.", " maximum footlinks value '3' specified with no config.", " color depth setting '256' specified with no config.", " notify setting 'disabled' specified with no config.", "\x1b[91m", f"Error connecting to Zulip server: {server_connection_error}.\x1b[0m", ] assert lines == expected_lines assert captured.err == ""
def test_show_error_if_loading_zuliprc_with_open_permissions( capsys: CaptureFixture[str], minimal_zuliprc: str, mode: int ) -> None: mode += 0o600 os.chmod(minimal_zuliprc, mode) current_mode = stat.filemode(os.stat(minimal_zuliprc).st_mode) with pytest.raises(SystemExit) as e: main(["-c", minimal_zuliprc]) assert str(e.value) == "1" captured = capsys.readouterr() lines = captured.out.split("\n")[:-1] expected_last_lines = [ f"(it currently has permissions '{current_mode}')", "This can often be achieved with a command such as:", f" chmod og-rwx {minimal_zuliprc}", "Consider regenerating the [api] part of your zuliprc to ensure " "your account is secure." "\x1b[0m", ] assert lines[-4:] == expected_last_lines assert captured.err == ""
def test_successful_main_function_with_config( capsys: CaptureFixture[str], mocker: MockerFixture, parameterized_zuliprc: Callable[[Dict[str, str]], str], config_key: str, config_value: str, footlinks_output: str, ) -> None: config = { "theme": "default", "autohide": "autohide", "notify": "enabled", "color-depth": "256", } config[config_key] = config_value zuliprc = parameterized_zuliprc(config) mocker.patch(CONTROLLER + ".__init__", return_value=None) mocker.patch(CONTROLLER + ".main", return_value=None) with pytest.raises(SystemExit): main(["-c", zuliprc]) captured = capsys.readouterr() lines = captured.out.strip().split("\n") expected_lines = [ "Loading with:", " theme 'zt_dark' specified in zuliprc file (by alias 'default').", " autohide setting 'autohide' specified in zuliprc file.", f" maximum footlinks value {footlinks_output}", " color depth setting '256' specified in zuliprc file.", " notify setting 'enabled' specified in zuliprc file.", ] assert lines == expected_lines
def test_show_error_if_loading_zuliprc_with_open_permissions( capsys, minimal_zuliprc, mode, ): mode += 0o600 os.chmod(minimal_zuliprc, mode) current_mode = stat.filemode(os.stat(minimal_zuliprc).st_mode) with pytest.raises(SystemExit) as e: main([ "-c", minimal_zuliprc, ]) assert str(e.value) == '1' captured = capsys.readouterr() lines = captured.out.split('\n')[:-1] expected_last_lines = [ "(it currently has permissions '{}')".format(current_mode), "This can often be achieved with a command such as:", " chmod og-rwx {}".format(minimal_zuliprc), "Consider regenerating the [api] part of your zuliprc to ensure " "your account is secure." "\x1b[0m" ] assert lines[-4:] == expected_last_lines assert captured.err == ""
def test_main_help(capsys, options): with pytest.raises(SystemExit): main([options]) captured = capsys.readouterr() lines = captured.out.strip().split("\n") assert lines[0].startswith("usage: ") required_arguments = { "--theme THEME, -t THEME", "-h, --help", "-d, --debug", "--list-themes", "--profile", "--config-file CONFIG_FILE, -c CONFIG_FILE", "--autohide", "--no-autohide", "-v, --version", "-e, --explore", "--color-depth", "--notify", "--no-notify", } optional_argument_lines = { line[2:] for line in lines if len(line) > 2 and line[2] == "-" } for line in optional_argument_lines: assert any(line.startswith(arg) for arg in required_arguments) assert captured.err == ""
def test_main_multiple_autohide_options(capsys, options): with pytest.raises(SystemExit) as e: main(options) assert str(e.value) == "2" captured = capsys.readouterr() lines = captured.err.strip('\n') lines = lines.split("pytest: ", 1)[1] expected = ("error: argument {}: not allowed " "with argument {}".format(options[1], options[0])) assert lines == expected
def test_main_multiple_notify_options(capsys, options): with pytest.raises(SystemExit) as e: main(options) assert str(e.value) == "2" captured = capsys.readouterr() lines = captured.err.strip("\n") lines = lines.split("pytest: ", 1)[1] expected = f"error: argument {options[1]}: not allowed with argument {options[0]}" assert lines == expected
def test_zt_version(capsys, options): with pytest.raises(SystemExit) as e: main([options]) assert str(e.value) == "0" captured = capsys.readouterr() lines = captured.out.strip('\n') expected = 'Zulip Terminal ' + ZT_VERSION assert lines == expected assert captured.err == ""
def test_zt_version(capsys: CaptureFixture[str], options: str) -> None: with pytest.raises(SystemExit) as e: main([options]) assert str(e.value) == "0" captured = capsys.readouterr() lines = captured.out.strip("\n") expected = "Zulip Terminal " + ZT_VERSION assert lines == expected assert captured.err == ""
def test_main_error_with_invalid_zuliprc_options( capsys, mocker, parameterized_zuliprc, zulip_config, error_message, ): zuliprc = parameterized_zuliprc(zulip_config) mocker.patch("zulipterminal.core.Controller.__init__", return_value=None) mocker.patch("zulipterminal.core.Controller.main", return_value=None) with pytest.raises(SystemExit) as e: main(["-c", zuliprc]) assert str(e.value) == "1" captured = capsys.readouterr() lines = captured.out.strip() expected_lines = f"\033[91m{error_message}\033[0m" assert lines == expected_lines
def test_warning_regarding_incomplete_theme( capsys, mocker, monkeypatch, minimal_zuliprc, bad_theme, server_connection_error="sce", ): mocker.patch( "zulipterminal.core.Controller.__init__", side_effect=ServerConnectionFailure(server_connection_error), ) monkeypatch.setitem(THEMES, bad_theme, []) mocker.patch("zulipterminal.cli.run.all_themes", return_value=("a", "b", "c", "d")) mocker.patch( "zulipterminal.cli.run.complete_and_incomplete_themes", return_value=(["a", "b"], ["c", "d"]), ) with pytest.raises(SystemExit) as e: main(["-c", minimal_zuliprc, "-t", bad_theme]) assert str(e.value) == "1" captured = capsys.readouterr() lines = captured.out.strip().split("\n") expected_lines = [ "Loading with:", f" theme '{bad_theme}' specified on command line.", "\x1b[93m WARNING: Incomplete theme; results may vary!", f" (you could try: {'a'}, {'b'})\x1b[0m", " autohide setting 'no_autohide' specified with no config.", " maximum footlinks value '3' specified with no config.", " color depth setting '256' specified with no config.", " notify setting 'disabled' specified with no config.", "\x1b[91m", f"Error connecting to Zulip server: {server_connection_error}.\x1b[0m", ] assert lines == expected_lines assert captured.err == ""
def test_warning_regarding_incomplete_theme( capsys: CaptureFixture[str], mocker: MockerFixture, minimal_zuliprc: str, bad_theme: str, expected_complete_incomplete_themes: Tuple[List[str], List[str]], expected_warning: str, server_connection_error: str = "sce", ) -> None: mocker.patch( CONTROLLER + ".__init__", side_effect=ServerConnectionFailure(server_connection_error), ) mocker.patch(MODULE + ".all_themes", return_value=("a", "b", "c", "d")) mocker.patch( MODULE + ".complete_and_incomplete_themes", return_value=expected_complete_incomplete_themes, ) mocker.patch(MODULE + ".generate_theme") with pytest.raises(SystemExit) as e: main(["-c", minimal_zuliprc, "-t", bad_theme]) assert str(e.value) == "1" captured = capsys.readouterr() lines = captured.out.strip().split("\n") expected_lines = [ "Loading with:", f" theme '{bad_theme}' specified on command line.", "\x1b[93m WARNING: Incomplete theme; results may vary!", f" {expected_warning}\x1b[0m", " autohide setting 'no_autohide' specified with no config.", " maximum footlinks value '3' specified with no config.", " color depth setting '256' specified with no config.", " notify setting 'disabled' specified with no config.", "\x1b[91m", f"Error connecting to Zulip server: {server_connection_error}.\x1b[0m", ] assert lines == expected_lines assert captured.err == ""
def test_main_error_with_invalid_zuliprc_options( capsys: CaptureFixture[str], mocker: MockerFixture, parameterized_zuliprc: Callable[[Dict[str, str]], str], zulip_config: Dict[str, str], error_message: str, ) -> None: zuliprc = parameterized_zuliprc(zulip_config) mocker.patch(CONTROLLER + ".__init__", return_value=None) mocker.patch(CONTROLLER + ".main", return_value=None) with pytest.raises(SystemExit) as e: main(["-c", zuliprc]) assert str(e.value) == "1" captured = capsys.readouterr() lines = captured.out.strip() expected_lines = f"\033[91m{error_message}\033[0m" assert lines == expected_lines