def open_editor_and_enter(context, method, text=""): text = text or context.text or "" if method == "enter": file_method = "w+" elif method == "append": file_method = "a" else: file_method = "r+" def _mock_editor(command): context.editor_command = command tmpfile = command[-1] with open(tmpfile, file_method) as f: f.write(text) return tmpfile if "password" in context: password = context.password else: password = "" # fmt: off # see: https://github.com/psf/black/issues/664 with \ patch("subprocess.call", side_effect=_mock_editor) as mock_editor, \ patch("getpass.getpass", side_effect=_mock_getpass(password)) as mock_getpass, \ patch("sys.stdin.isatty", return_value=True) \ : context.editor = mock_editor context.getpass = mock_getpass cli(["--edit"])
def run_with_input(context, command, inputs=""): # create an iterator through all inputs. These inputs will be fed one by one # to the mocked calls for 'input()', 'util.getpass()' and 'sys.stdin.read()' if context.text: text = iter(context.text.split("\n")) else: text = iter([inputs]) args = ushlex(command)[1:] # fmt: off # see: https://github.com/psf/black/issues/664 with \ patch("builtins.input", side_effect=_mock_input(text)) as mock_input, \ patch("getpass.getpass", side_effect=_mock_getpass(text)) as mock_getpass, \ patch("sys.stdin.read", side_effect=text) as mock_read \ : try: cli(args or []) context.exit_status = 0 except SystemExit as e: context.exit_status = e.code # at least one of the mocked input methods got called assert mock_input.called or mock_getpass.called or mock_read.called # all inputs were used try: next(text) assert False, "Not all inputs were consumed" except StopIteration: pass
def open_editor_and_enter(context, method, text=""): text = text or context.text or "" if method == "enter": file_method = "w+" elif method == "append": file_method = "a" else: file_method = "r+" def _mock_editor_function(command): context.editor_command = command tmpfile = command[-1] with open(tmpfile, file_method) as f: f.write(text) return tmpfile # fmt: off # see: https://github.com/psf/black/issues/664 with \ patch("subprocess.call", side_effect=_mock_editor_function), \ patch("sys.stdin.isatty", return_value=True) \ : cli(["--edit"])
def run(context, command, text=""): text = text or context.text or "" if "config_path" in context and context.config_path is not None: with open(context.config_path) as f: context.jrnl_config = yaml.load(f, Loader=yaml.FullLoader) else: context.jrnl_config = None if "cache_dir" in context and context.cache_dir is not None: cache_dir = os.path.join("features", "cache", context.cache_dir) command = command.format(cache_dir=cache_dir) if "config_path" in context and context.config_path is not None: with open(context.config_path, "r") as f: cfg = yaml.load(f, Loader=FullLoader) context.jrnl_config = cfg args = split_args(command) context.args = args[1:] def _mock_editor(command): context.editor_command = command tmpfile = command[-1] with open(tmpfile, "r") as editor_file: file_content = editor_file.read() context.editor_file = {"name": tmpfile, "content": file_content} Path(tmpfile).touch() if "password" in context: password = context.password else: password = iter(text) try: # fmt: off # see: https://github.com/psf/black/issues/664 with \ patch("sys.argv", args), \ patch("getpass.getpass", side_effect=_mock_getpass(password)) as mock_getpass, \ patch("subprocess.call", side_effect=_mock_editor) as mock_editor, \ patch("sys.stdin.read", side_effect=lambda: text), \ patch("jrnl.time.parse", side_effect=_mock_time_parse(context)), \ patch("jrnl.config.get_config_path", side_effect=lambda: context.config_path), \ patch("jrnl.install.get_config_path", side_effect=lambda: context.config_path) \ : context.editor = mock_editor context.getpass = mock_getpass cli(args[1:]) context.exit_status = 0 # fmt: on except SystemExit as e: context.exit_status = e.code
def run_with_input(context, command, inputs=""): # create an iterator through all inputs. These inputs will be fed one by one # to the mocked calls for 'input()', 'util.getpass()' and 'sys.stdin.read()' if context.text: text = iter(context.text.split("\n")) else: text = iter([inputs]) args = split_args(command)[1:] context.args = args def _mock_editor(command): context.editor_command = command tmpfile = command[-1] with open(tmpfile, "r") as editor_file: file_content = editor_file.read() context.editor_file = {"name": tmpfile, "content": file_content} Path(tmpfile).touch() if "password" in context: password = context.password else: password = text # fmt: off # see: https://github.com/psf/black/issues/664 with \ patch("builtins.input", side_effect=_mock_input(text)) as mock_input, \ patch("getpass.getpass", side_effect=_mock_getpass(password)) as mock_getpass, \ patch("sys.stdin.read", side_effect=text) as mock_read, \ patch("subprocess.call", side_effect=_mock_editor) as mock_editor, \ patch("jrnl.time.parse", side_effect=_mock_time_parse(context)), \ patch("jrnl.config.get_config_path", side_effect=lambda: context.config_path), \ patch("jrnl.install.get_config_path", side_effect=lambda: context.config_path) \ : try: cli(args or []) context.exit_status = 0 except SystemExit as e: context.exit_status = e.code # put mocks into context so they can be checked later in "then" statements context.editor = mock_editor context.input = mock_input context.getpass = mock_getpass context.read = mock_read context.iter_text = text context.execute_steps(''' Then all input was used And at least one input method was called ''')
def run(context, command, text="", cache_dir=None): text = text or context.text or "" if cache_dir is not None: cache_dir = os.path.join("features", "cache", cache_dir) command = command.format(cache_dir=cache_dir) args = ushlex(command) def _mock_editor(command): context.editor_command = command try: with patch("sys.argv", args), patch("subprocess.call", side_effect=_mock_editor), patch( "sys.stdin.read", side_effect=lambda: text): cli(args[1:]) context.exit_status = 0 except SystemExit as e: context.exit_status = e.code
def open_editor_and_enter(context, method, text=""): text = text or context.text or "" if method == "enter": file_method = "w+" elif method == "append": file_method = "a" else: file_method = "r+" def _mock_editor(command): context.editor_command = command tmpfile = command[-1] with open(tmpfile, file_method) as f: f.write(text) return tmpfile if "password" in context: password = context.password else: password = "" # fmt: off # see: https://github.com/psf/black/issues/664 with \ patch("subprocess.call", side_effect=_mock_editor) as mock_editor, \ patch("getpass.getpass", side_effect=_mock_getpass(password)) as mock_getpass, \ patch("sys.stdin.isatty", return_value=True), \ patch("jrnl.time.parse", side_effect=_mock_time_parse(context)), \ patch("jrnl.config.get_config_path", side_effect=lambda: context.config_path), \ patch("jrnl.install.get_config_path", side_effect=lambda: context.config_path) \ : context.editor = mock_editor context.getpass = mock_getpass try: cli(["--edit"]) context.exit_status = 0 except SystemExit as e: context.exit_status = e.code
def run(context, command, text=""): text = text or context.text or "" if "cache_dir" in context and context.cache_dir is not None: cache_dir = os.path.join("features", "cache", context.cache_dir) command = command.format(cache_dir=cache_dir) args = ushlex(command) def _mock_editor(command): context.editor_command = command tmpfile = command[-1] with open(tmpfile, "r") as editor_file: file_content = editor_file.read() context.editor_file = {"name": tmpfile, "content": file_content} Path(tmpfile).touch() if "password" in context: password = context.password else: password = iter(text) try: # fmt: off # see: https://github.com/psf/black/issues/664 with \ patch("sys.argv", args), \ patch("getpass.getpass", side_effect=_mock_getpass(password)) as mock_getpass, \ patch("subprocess.call", side_effect=_mock_editor) as mock_editor, \ patch("sys.stdin.read", side_effect=lambda: text) \ : context.editor = mock_editor context.getpass = mock_getpass cli(args[1:]) context.exit_status = 0 # fmt: on except SystemExit as e: context.exit_status = e.code