Exemplo n.º 1
0
    def test_ask_for_actions_and_apply(self):
        failed_actions = set()
        action = TestAction()
        do_nothing_action = DoNothingAction()
        args = [
            self.console_printer,
            Section(''),
            [do_nothing_action.get_metadata(),
             action.get_metadata()], {
                 'DoNothingAction': do_nothing_action,
                 'TestAction': action
             }, failed_actions,
            Result('origin', 'message'), {}, {}, {}
        ]

        with simulate_console_inputs('a', 'param1', 'a',
                                     'param2') as generator:
            action.apply = unittest.mock.Mock(side_effect=AssertionError)
            ask_for_action_and_apply(*args)
            self.assertEqual(generator.last_input, 1)
            self.assertIn('TestAction', failed_actions)

            action.apply = lambda *args, **kwargs: {}
            ask_for_action_and_apply(*args)
            self.assertEqual(generator.last_input, 3)
            self.assertNotIn('TestAction', failed_actions)
Exemplo n.º 2
0
    def test_default_input_apply_single_test(self):
        action = TestAction()
        do_nothing_action = DoNothingAction()
        apply_single = 'Test (A)ction'
        se = Section('cli')
        args = [self.console_printer, se,
                [do_nothing_action.get_metadata(), action.get_metadata()],
                {'DoNothingAction': do_nothing_action, 'TestAction': action},
                set(), Result('origin', 'message'), {}, {}, {}, apply_single]

        with simulate_console_inputs('a') as generator:
            self.assertFalse(ask_for_action_and_apply(*args))
    def test_default_input_apply_single_test(self):
        action = TestAction()
        do_nothing_action = DoNothingAction()
        apply_single = 'Test (A)ction'
        se = Section('cli')
        args = [self.console_printer, se,
                [do_nothing_action.get_metadata(), action.get_metadata()],
                {'DoNothingAction': do_nothing_action, 'TestAction': action},
                set(), Result('origin', 'message'), {}, {}, {}, apply_single]

        with simulate_console_inputs('a') as generator:
            self.assertTrue(ask_for_action_and_apply(*args))
Exemplo n.º 4
0
    def test_default_input2(self):
        action = TestAction()
        do_nothing_action = DoNothingAction()
        args = [
            self.console_printer,
            Section(''),
            [do_nothing_action.get_metadata(),
             action.get_metadata()], {
                 'DoNothingAction': do_nothing_action,
                 'TestAction': action
             },
            set(),
            Result('origin', 'message'), {}, {}, {}
        ]

        with simulate_console_inputs(1, 1) as generator:
            self.assertTrue(ask_for_action_and_apply(*args))
Exemplo n.º 5
0
    def test_ask_for_actions_and_apply(self):
        failed_actions = set()
        action = TestAction()
        do_nothing_action = DoNothingAction()
        args = [self.console_printer, Section(''),
                [do_nothing_action.get_metadata(), action.get_metadata()],
                {'DoNothingAction': do_nothing_action, 'TestAction': action},
                failed_actions, Result('origin', 'message'), {}, {}, {}]

        with simulate_console_inputs('a', 'param1', 'a', 'param2') as generator:
            action.apply = unittest.mock.Mock(side_effect=AssertionError)
            ask_for_action_and_apply(*args)
            self.assertEqual(generator.last_input, 1)
            self.assertIn('TestAction', failed_actions)

            action.apply = lambda *args, **kwargs: {}
            ask_for_action_and_apply(*args)
            self.assertEqual(generator.last_input, 3)
            self.assertNotIn('TestAction', failed_actions)
Exemplo n.º 6
0
def ask_for_action_and_apply(console_printer,
                             section,
                             metadata_list,
                             action_dict,
                             failed_actions,
                             result,
                             file_diff_dict,
                             file_dict,
                             applied_actions,
                             apply_single=False):
    """
    Asks the user for an action and applies it.

    :param console_printer: Object to print messages on the console.
    :param section:         Currently active section.
    :param metadata_list:   Contains metadata for all the actions.
    :param action_dict:     Contains the action names as keys and their
                            references as values.
    :param failed_actions:  A set of all actions that have failed. A failed
                            action remains in the list until it is successfully
                            executed.
    :param result:          Result corresponding to the actions.
    :param file_diff_dict:  If it is an action which applies a patch, this
                            contains the diff of the patch to be applied to
                            the file with filename as keys.
    :param file_dict:       Dictionary with filename as keys and its contents
                            as values.
    :param apply_single:    The action that should be applied for all results.
                            If it's not selected, has a value of False.
    :param applied_actions: A dictionary that contains the result, file_dict,
                            file_diff_dict and the section for an action.
    :return:                Returns a boolean value. True will be returned, if
                            it makes sense that the user may choose to execute
                            another action, False otherwise.
                            If apply_single isn't set, always return False.
    """
    do_nothing_action = DoNothingAction()
    metadata_list.insert(0, do_nothing_action.get_metadata())
    action_dict[do_nothing_action.get_metadata().id] = DoNothingAction()

    actions_desc, actions_id = choose_action(console_printer, metadata_list,
                                             apply_single)

    if apply_single:
        for index, action_details in enumerate(metadata_list, 1):
            if apply_single == action_details.desc:
                action_name, section = get_action_info(
                    section, metadata_list[index - 1], failed_actions)
                chosen_action = action_dict[action_details.id]
                try_to_apply_action(action_name, chosen_action,
                                    console_printer, section, metadata_list,
                                    action_dict, failed_actions, result,
                                    file_diff_dict, file_dict, applied_actions)
                break
        return False
    else:
        for action_choice, action_choice_id in zip(actions_desc, actions_id):
            chosen_action = action_dict[action_choice_id]
            action_choice_made = action_choice
            for index, action_details in enumerate(metadata_list, 1):
                if action_choice_made == action_details.desc:
                    action_name, section = get_action_info(
                        section, metadata_list[index - 1], failed_actions)
                    try_to_apply_action(action_name, chosen_action,
                                        console_printer, section,
                                        metadata_list, action_dict,
                                        failed_actions, result, file_diff_dict,
                                        file_dict, applied_actions)

            if action_choice == 'Do (N)othing':
                return False
    return True
Exemplo n.º 7
0
def ask_for_action_and_apply(console_printer,
                             section,
                             metadata_list,
                             action_dict,
                             failed_actions,
                             result,
                             file_diff_dict,
                             file_dict,
                             applied_actions,
                             apply_single=False):
    """
    Asks the user for an action and applies it.

    :param console_printer: Object to print messages on the console.
    :param section:         Currently active section.
    :param metadata_list:   Contains metadata for all the actions.
    :param action_dict:     Contains the action names as keys and their
                            references as values.
    :param failed_actions:  A set of all actions that have failed. A failed
                            action remains in the list until it is successfully
                            executed.
    :param result:          Result corresponding to the actions.
    :param file_diff_dict:  If it is an action which applies a patch, this
                            contains the diff of the patch to be applied to
                            the file with filename as keys.
    :param file_dict:       Dictionary with filename as keys and its contents
                            as values.
    :param apply_single:    The action that should be applied for all results.
                            If it's not selected, has a value of False.
    :param applied_actions: A dictionary that contains the result, file_dict,
                            file_diff_dict and the section for an action.
    :return:                Returns a boolean value. True will be returned, if
                            it makes sense that the user may choose to execute
                            another action, False otherwise.
                            If apply_single isn't set, always return False.
    """
    do_nothing_action = DoNothingAction()
    metadata_list.insert(0, do_nothing_action.get_metadata())
    action_dict[do_nothing_action.get_metadata().name] = DoNothingAction()

    actions_desc, actions_name = choose_action(console_printer, metadata_list,
                                               apply_single)

    if apply_single:
        for index, action_details in enumerate(metadata_list, 1):
            if apply_single == action_details.desc:
                action_name, section = get_action_info(
                    section, metadata_list[index - 1], failed_actions)
                chosen_action = action_dict[action_details.name]
                try_to_apply_action(action_name,
                                    chosen_action,
                                    console_printer,
                                    section,
                                    metadata_list,
                                    action_dict,
                                    failed_actions,
                                    result,
                                    file_diff_dict,
                                    file_dict,
                                    applied_actions)
        return False
    else:
        for action_choice, action_choice_name in zip(actions_desc,
                                                     actions_name):
            chosen_action = action_dict[action_choice_name]
            action_choice_made = action_choice
            for index, action_details in enumerate(metadata_list, 1):
                if action_choice_made in action_details.desc:
                    action_name, section = get_action_info(
                        section, metadata_list[index-1], failed_actions)
                    try_to_apply_action(action_name,
                                        chosen_action,
                                        console_printer,
                                        section,
                                        metadata_list,
                                        action_dict,
                                        failed_actions,
                                        result,
                                        file_diff_dict,
                                        file_dict,
                                        applied_actions)

            if action_choice == 'Do (N)othing':
                return False
    return True