コード例 #1
0
def main(command_executor: InteractiveCommandExecutor) -> None:
    """Entry point for this executable python module."""
    args = handle_args.setup_args_for_update()
    store_file_path = command_store.get_file_path(args.save_dir)
    store = command_store.load_command_store(store_file_path)
    print('Looking for all past commands with: ' + ", ".join(args.query))
    start_time = time.time()
    search_results = store.search_commands(
        args.query,
        args.startswith,
    )
    end_time = time.time()
    print(f"Search time: {end_time - start_time} seconds")
    print(f"Number of results found: {str(len(search_results))}")
    if len(search_results) > args.max:
        print(f"Results truncated to the first: {args.max}")
    search_results = search_results[:args.max]
    if len(search_results) > 0:
        if args.delete:
            print("Delete mode")
            command_store.print_commands(search_results, args.query)
            command_executor.delete_interaction(store, search_results)
        if args.updateinfo:
            print("Updating Info mode")
            command_executor.command_info_interaction(search_results, store)
コード例 #2
0
 def test_command_update_info_should_fail_set_info_because_exit(self) -> None:
     interactive_command = InteractiveCommandExecutor()
     command = Command("git rest --hard HEAD")
     user_input = ['exit']
     store = mock.Mock()
     with patch('builtins.input', side_effect=user_input):
         self.assertFalse(interactive_command.command_info_interaction([command], store))
     self.assertEqual(command.get_command_info(), "")
     store.update_command_info.assert_not_called()
コード例 #3
0
 def test_run_whenCommandChosen_shouldWriteToHistFile(self, sub_process_mock) -> None:
     command_str = "Command to write to history file"
     command = Command(command_str)
     interactive_command = InteractiveCommandExecutor('SomeHistoryFile.txt')
     with patch('builtins.input', side_effect=['1']):
         with patch('builtins.open', mock_open(read_data=b'some command')) as m:
             self.assertTrue(interactive_command.run([command]))
             handle = m()
             handle.write.assert_called_with(command_str + '\n')
     sub_process_mock.assert_called_once()
コード例 #4
0
 def test_command_update_info_should_correctly_set_info(self) -> None:
     interactive_command = InteractiveCommandExecutor()
     store = command_store_lib.SqlCommandStore()
     command = Command("git rest --hard HEAD")
     command_info = 'command info'
     user_input = ['1', command_info]
     store.add_command(command)
     with patch('builtins.input', side_effect=user_input):
         interactive_command.command_info_interaction([command], store)
     self.assertEqual(command.get_command_info(), command_info)
     self.assertEqual(command_info, store.search_commands(['git'])[0].get_command_info())
コード例 #5
0
 def test_run_whenCommandChosenInZsh_shouldWriteToHistFile(self) -> None:
     command_str = "Command to write to history file"
     command = Command(command_str)
     interactive_command = InteractiveCommandExecutor('SomeHistoryFile.txt')
     user_input = ['1']
     hist_file_content = b': 1573535428:0;vim ~/.histfile\n'
     expected = ': 1573535429:0;Command to write to history file\n'
     with patch('builtins.input', side_effect=user_input):
         with patch('remember.interactive.open', mock_open(read_data=hist_file_content)) as m:
             self.assertTrue(interactive_command.run([command]))
             handle = m()
             handle.write.assert_called_with(expected)
コード例 #6
0
 def test_delete_command_from_store_with_invalid_should_remove_1(self) -> None:
     store = command_store_lib.SqlCommandStore(':memory:')
     command = Command("testing delete this command")
     command2 = Command("remove this also")
     self.assertEqual(store.get_num_commands(), 0)
     store.add_command(command)
     store.add_command(command2)
     self.assertEqual(store.get_num_commands(), 2)
     interactive_command = InteractiveCommandExecutor()
     user_input = ['1, 9', 'y']
     with patch('builtins.input', side_effect=user_input):
         self.assertTrue(interactive_command.delete_interaction(store, [command, command2]))
     self.assertEqual(store.get_num_commands(), 1)
コード例 #7
0
 def test_run_when_command_is_executed(self, mock_subproc) -> None:
     store = command_store_lib.SqlCommandStore(':memory:')
     command_str = "testing delete this command"
     command = Command(command_str)
     command2 = Command("remove this also")
     self.assertEqual(store.get_num_commands(), 0)
     store.add_command(command)
     store.add_command(command2)
     self.assertEqual(store.get_num_commands(), 2)
     interactive_command = InteractiveCommandExecutor()
     user_input = ['1']
     with patch('builtins.input', side_effect=user_input):
         self.assertTrue(interactive_command.run([command, command2]))
     shell_env = os.getenv('SHELL')
     call_args = [shell_env, '-i', '-c', command_str]
     mock_subproc.assert_called_once_with(call_args)
コード例 #8
0
 def test_command_update_info_should_fail_set_info_becuase_exit(self):
     interactive = InteractiveCommandExecutor()
     command = Command("git rest --hard HEAD")
     self.set_input('exit')
     self.assertFalse(interactive.set_command_info([command]))
     self.assertEqual(command.get_command_info(), "")
     self.reset_input()
コード例 #9
0
 def test_command_update_info_should_correctly_set_info(self):
     interactive = InteractiveCommandExecutor()
     command = Command("git rest --hard HEAD")
     command_info = 'command info'
     self.set_input('1', command_info)
     interactive.set_command_info([command])
     self.assertEqual(command.get_command_info(), command_info)
     self.reset_input()
コード例 #10
0
 def test_delete_command_from_store_when_no_should_not_delete(self) -> None:
     store = command_store_lib.SqlCommandStore(':memory:')
     command = Command("testing delete this command")
     store.add_command(command)
     self.assertEqual(store.get_num_commands(), 1)
     user_input = ['1', 'n']
     with patch('builtins.input', side_effect=user_input):
         self.assertFalse(InteractiveCommandExecutor.delete_interaction(store, [command]))
     self.assertEqual(store.get_num_commands(), 1)
コード例 #11
0
 def test_delete_command_from_store_with_exit_input_should_not_delete(self):
     store = command_store_lib.CommandStore()
     command = Command("testing delete this command")
     store.add_command(command)
     self.assertEqual(store.get_num_commands(), 1)
     interactive = InteractiveCommandExecutor()
     self.set_input('quit')
     self.assertFalse(interactive.delete_interaction(store, [command]))
     self.assertEqual(store.get_num_commands(), 1)
     self.reset_input()
コード例 #12
0
 def test_delete_command_from_store_with_1_2_should_remove_all(self):
     store = command_store_lib.CommandStore()
     command = Command("testing delete this command")
     command2 = Command("remove this also")
     self.assertEqual(store.get_num_commands(), 0)
     store.add_command(command)
     store.add_command(command2)
     self.assertEqual(store.get_num_commands(), 2)
     interactive = InteractiveCommandExecutor()
     self.set_input('1, 2', 'y')
     self.assertTrue(interactive.delete_interaction(store, [command, command2]))
     self.assertEqual(store.get_num_commands(), 0)
     self.reset_input()
コード例 #13
0
 def test_setup_args_for_update_but_nothing_happens_because_nothing_updated(
         self, load_store_mock: mock.Mock) -> None:
     with mock.patch('argparse.ArgumentParser.parse_args',
                     return_value=argparse.Namespace(json=True,
                                                     sql=False,
                                                     all=True,
                                                     startswith=True,
                                                     execute=False,
                                                     save_dir='save_dir',
                                                     history_file_path='hist',
                                                     delete=False,
                                                     updateinfo=False,
                                                     max=1000,
                                                     query='query')):
         update_store.main(InteractiveCommandExecutor())
         load_store_mock.assert_called()
コード例 #14
0
 def test_run_when_command_is_executed(self):
     old_call = subprocess.call
     store = command_store_lib.CommandStore()
     command_str = "testing delete this command"
     command = Command(command_str)
     command2 = Command("remove this also")
     test_call = partial(self.subprocess_call_mock, expected=command_str)
     subprocess.call = test_call
     self.assertEqual(store.get_num_commands(), 0)
     store.add_command(command)
     store.add_command(command2)
     self.assertEqual(store.get_num_commands(), 2)
     interactive = InteractiveCommandExecutor()
     self.set_input('1')
     self.assertTrue(interactive.run([command, command2]))
     self.reset_input()
     subprocess.call = old_call
コード例 #15
0
 def test_delete_command_whenUserTypesQuit_shouldReturnFalse(self) -> None:
     user_input = ['quit']
     with patch('builtins.input', side_effect=user_input):
         self.assertFalse(InteractiveCommandExecutor.delete_interaction(Mock(), []))
コード例 #16
0
 def test_run_whenResultsAreEmpty_shouldReturnFalse(self) -> None:
     interactive_command = InteractiveCommandExecutor()
     user_input = ['1']
     with patch('builtins.input', side_effect=user_input):
         self.assertFalse(interactive_command.run([]))
コード例 #17
0
class Test(unittest.TestCase):
    def test_command_update_info_should_correctly_set_info(self) -> None:
        interactive_command = InteractiveCommandExecutor()
        store = command_store_lib.SqlCommandStore()
        command = Command("git rest --hard HEAD")
        command_info = 'command info'
        user_input = ['1', command_info]
        store.add_command(command)
        with patch('builtins.input', side_effect=user_input):
            interactive_command.command_info_interaction([command], store)
        self.assertEqual(command.get_command_info(), command_info)
        self.assertEqual(command_info, store.search_commands(['git'])[0].get_command_info())

    def test_command_update_info_should_fail_set_info_because_exit(self) -> None:
        interactive_command = InteractiveCommandExecutor()
        command = Command("git rest --hard HEAD")
        user_input = ['exit']
        store = mock.Mock()
        with patch('builtins.input', side_effect=user_input):
            self.assertFalse(interactive_command.command_info_interaction([command], store))
        self.assertEqual(command.get_command_info(), "")
        store.update_command_info.assert_not_called()

    def test_delete_command_from_store_should_delete(self) -> None:
        store = command_store_lib.SqlCommandStore(':memory:')
        command = Command("testing delete this command")
        store.add_command(command)
        self.assertEqual(store.get_num_commands(), 1)
        user_input = ['1', 'y']
        with patch('builtins.input', side_effect=user_input):
            self.assertTrue(InteractiveCommandExecutor.delete_interaction(store, [command]))
        self.assertEqual(store.get_num_commands(), 0)

    def test_delete_command_from_store_when_no_should_not_delete(self) -> None:
        store = command_store_lib.SqlCommandStore(':memory:')
        command = Command("testing delete this command")
        store.add_command(command)
        self.assertEqual(store.get_num_commands(), 1)
        user_input = ['1', 'n']
        with patch('builtins.input', side_effect=user_input):
            self.assertFalse(InteractiveCommandExecutor.delete_interaction(store, [command]))
        self.assertEqual(store.get_num_commands(), 1)

    def test_delete_command_from_store_with_all_should_remove_all(self) -> None:
        store = command_store_lib.SqlCommandStore(':memory:')
        command = Command("testing delete this command")
        command2 = Command("remove this also")
        self.assertEqual(store.get_num_commands(), 0)
        store.add_command(command)
        store.add_command(command2)
        self.assertEqual(store.get_num_commands(), 2)
        user_input = ['allofthem', 'y']
        with patch('builtins.input', side_effect=user_input):
            self.assertTrue(InteractiveCommandExecutor.delete_interaction(store,
                                                                          [command, command2]))
        self.assertEqual(store.get_num_commands(), 0)

    def test_delete_command_from_store_with_1_2_should_remove_all(self) -> None:
        store = command_store_lib.SqlCommandStore(':memory:')
        command = Command("testing delete this command")
        command2 = Command("remove this also")
        self.assertEqual(store.get_num_commands(), 0)
        store.add_command(command)
        store.add_command(command2)
        self.assertEqual(store.get_num_commands(), 2)
        interactive_command = InteractiveCommandExecutor()
        user_input = ['1, 2', 'y']
        with patch('builtins.input', side_effect=user_input):
            self.assertTrue(interactive_command.delete_interaction(store, [command, command2]))
        self.assertEqual(store.get_num_commands(), 0)

    def test_delete_command_from_store_with_invalid_should_remove_1(self) -> None:
        store = command_store_lib.SqlCommandStore(':memory:')
        command = Command("testing delete this command")
        command2 = Command("remove this also")
        self.assertEqual(store.get_num_commands(), 0)
        store.add_command(command)
        store.add_command(command2)
        self.assertEqual(store.get_num_commands(), 2)
        interactive_command = InteractiveCommandExecutor()
        user_input = ['1, 9', 'y']
        with patch('builtins.input', side_effect=user_input):
            self.assertTrue(interactive_command.delete_interaction(store, [command, command2]))
        self.assertEqual(store.get_num_commands(), 1)

    def test_delete_command_from_store_with_both_invalid_should_remove_0(self) -> None:
        store = command_store_lib.SqlCommandStore(':memory:')
        command = Command("testing delete this command")
        command2 = Command("remove this also")
        self.assertEqual(store.get_num_commands(), 0)
        store.add_command(command)
        store.add_command(command2)
        self.assertEqual(store.get_num_commands(), 2)
        user_input = ['8, 9']
        with patch('builtins.input', side_effect=user_input):
            self.assertFalse(InteractiveCommandExecutor.delete_interaction(store, [command, command2]))
        self.assertEqual(store.get_num_commands(), 2)

    def test_delete_command_whenUserTypesQuit_shouldReturnFalse(self) -> None:
        user_input = ['quit']
        with patch('builtins.input', side_effect=user_input):
            self.assertFalse(InteractiveCommandExecutor.delete_interaction(Mock(), []))

    @patch('subprocess.call')
    def test_run_when_command_is_executed(self, mock_subproc) -> None:
        store = command_store_lib.SqlCommandStore(':memory:')
        command_str = "testing delete this command"
        command = Command(command_str)
        command2 = Command("remove this also")
        self.assertEqual(store.get_num_commands(), 0)
        store.add_command(command)
        store.add_command(command2)
        self.assertEqual(store.get_num_commands(), 2)
        interactive_command = InteractiveCommandExecutor()
        user_input = ['1']
        with patch('builtins.input', side_effect=user_input):
            self.assertTrue(interactive_command.run([command, command2]))
        shell_env = os.getenv('SHELL')
        call_args = [shell_env, '-i', '-c', command_str]
        mock_subproc.assert_called_once_with(call_args)

    def test_run_whenResultsAreEmpty_shouldReturnFalse(self) -> None:
        interactive_command = InteractiveCommandExecutor()
        user_input = ['1']
        with patch('builtins.input', side_effect=user_input):
            self.assertFalse(interactive_command.run([]))

    @patch('subprocess.call')
    def test_run_whenCommandChosen_shouldWriteToHistFile(self, sub_process_mock) -> None:
        command_str = "Command to write to history file"
        command = Command(command_str)
        interactive_command = InteractiveCommandExecutor('SomeHistoryFile.txt')
        with patch('builtins.input', side_effect=['1']):
            with patch('builtins.open', mock_open(read_data=b'some command')) as m:
                self.assertTrue(interactive_command.run([command]))
                handle = m()
                handle.write.assert_called_with(command_str + '\n')
        sub_process_mock.assert_called_once()

    def test_run_whenCommandChosenInZsh_shouldWriteToHistFile(self) -> None:
        command_str = "Command to write to history file"
        command = Command(command_str)
        interactive_command = InteractiveCommandExecutor('SomeHistoryFile.txt')
        user_input = ['1']
        hist_file_content = b': 1573535428:0;vim ~/.histfile\n'
        expected = ': 1573535429:0;Command to write to history file\n'
        with patch('builtins.input', side_effect=user_input):
            with patch('remember.interactive.open', mock_open(read_data=hist_file_content)) as m:
                self.assertTrue(interactive_command.run([command]))
                handle = m()
                handle.write.assert_called_with(expected)

    @patch('remember.interactive.load_user_interactor')
    @patch('remember.command_store_lib.save_last_search')
    def test_display_and_interact_whenSaveAndExecute_shouldDoBoth(
            self, save_search: Mock, load_interactor: Mock) -> None:
        results = [Command('grep command'), Command('vim command')]
        rr = display_and_interact_results(results, 5, 'save_dir', 'history_file', ['grep'], True)
        self.assertEqual(rr, None)
        expected_path = os.path.join("save_dir", command_store_lib.DEFAULT_LAST_SAVE_FILE_NAME)
        save_search.assert_called_once_with(expected_path, results)
        load_interactor.assert_called_once_with('history_file')

    @patch('remember.interactive.load_user_interactor')
    @patch('remember.command_store_lib.save_last_search')
    def test_display_and_interact_whenTruncateResult_shouldOnlySaveTruncatedResult(
            self, save_search: Mock, load_interactor: Mock) -> None:
        results = [Command('grep command'), Command('vim command')]
        rr = display_and_interact_results(results, 1, 'save_dir', 'history_file', ['grep'], True)
        self.assertEqual(rr, None)
        expected_path = os.path.join("save_dir", command_store_lib.DEFAULT_LAST_SAVE_FILE_NAME)
        results = results[:1]
        save_search.assert_called_once_with(expected_path, results)
        load_interactor.assert_called_once_with('history_file')

    @patch('remember.command_store_lib.print_commands')
    @patch('subprocess.call')
    @patch('remember.interactive.load_user_interactor', return_value=InteractiveCommandExecutor())
    @patch('remember.command_store_lib.save_last_search')
    def test_display_nd_interact_whenUserChooses1_shouldDo1(
            self, save_search: Mock, load_interactor: Mock, subprocess_mock: Mock,
            print_mock: Mock) -> None:
        command = Command('grep command')
        results = [command, Command('vim command')]
        user_input = ['1']
        with patch('builtins.input', side_effect=user_input):
            rr = display_and_interact_results(
                results, 1, 'save_dir', 'history_file', ['grep'], True)
        self.assertEqual(rr, None)
        expected_path = os.path.join("save_dir", command_store_lib.DEFAULT_LAST_SAVE_FILE_NAME)
        results = results[:1]
        save_search.assert_called_once_with(expected_path, results)
        load_interactor.assert_called_once_with('history_file')
        shell_env = os.getenv('SHELL')
        call_args = [shell_env, '-i', '-c', command.get_unique_command_id()]
        subprocess_mock.assert_called_once_with(call_args)
        print_mock.assert_called_once()

    def test_load_interactive_whenCallingLoad_shouldHaveCorrectHistoryFile(self) -> None:
        interactive = load_user_interactor('history_file_path')
        self.assertEqual(interactive._history_file_path, 'history_file_path')
コード例 #18
0
def main(command_executor: InteractiveCommandExecutor) -> None:
    """Entry point for this executable python module."""
    args = handle_args.setup_args_for_update()
    store_file_path = command_store.get_file_path(args.save_dir)
    store = command_store.load_command_store(store_file_path)
    print('Looking for all past commands with: ' + ", ".join(args.query))
    start_time = time.time()
    search_results = store.search_commands(
        args.query,
        args.startswith,
    )
    end_time = time.time()
    print(f"Search time: {end_time - start_time} seconds")
    print(f"Number of results found: {str(len(search_results))}")
    if len(search_results) > args.max:
        print(f"Results truncated to the first: {args.max}")
    search_results = search_results[:args.max]
    if len(search_results) > 0:
        if args.delete:
            print("Delete mode")
            command_store.print_commands(search_results, args.query)
            command_executor.delete_interaction(store, search_results)
        if args.updateinfo:
            print("Updating Info mode")
            command_executor.command_info_interaction(search_results, store)


if __name__ == "__main__":
    main(InteractiveCommandExecutor())