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=""): 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 test_split_args_on_not_windows(args): input_arguments, expected_split_args = args[0], args[1] with mock.patch("jrnl.os_compat.on_windows", lambda: True): assert split_args(input_arguments) == expected_split_args