Exemplo n.º 1
0
    def test_should_sort_tasks_by_name(self):
        task_a = Task("a_name", lambda: None, "dependency", "description")
        task_b = Task("b_name", lambda: None, "dependency", "description")

        task_list = [task_b, task_a]

        self.assertEquals(["a_name", "b_name"],
                          [task.name for task in sorted(task_list)])
Exemplo n.º 2
0
    def test_should_extend_task_with_values_from_other_task(self):
        def callable_one():
            pass

        def callable_two(param):
            pass

        task = Task("task", callable_one, "dependency", "description")
        replacement = Task("replacement", callable_two, "another_dependency", "replacement description")

        task.extend(replacement)

        self.assertEquals("task", task.name)
        self.assertEquals(["dependency", "another_dependency"], task.dependencies)
        self.assertEquals(["description", "replacement description"], task.description)
Exemplo n.º 3
0
    def test_should_raise_exception_when_callable_argument_cannot_be_satisfied(
            self):
        def callable(spam):
            pass

        executable = Task("callable", callable)
        self.assertRaises(ValueError, executable.execute, mock(), {})
Exemplo n.º 4
0
    def collect_tasks_and_actions_and_initializers(self, project_module):
        for name in dir(project_module):
            candidate = getattr(project_module, name)

            if hasattr(candidate, NAME_ATTRIBUTE):
                name = getattr(candidate, NAME_ATTRIBUTE)
            elif hasattr(candidate, "__name__"):
                name = candidate.__name__
            description = getattr(candidate, DESCRIPTION_ATTRIBUTE) if hasattr(
                candidate, DESCRIPTION_ATTRIBUTE) else ""

            if hasattr(candidate, TASK_ATTRIBUTE) and getattr(
                    candidate, TASK_ATTRIBUTE):
                dependencies = getattr(
                    candidate, DEPENDS_ATTRIBUTE) if hasattr(
                        candidate, DEPENDS_ATTRIBUTE) else None
                required_dependencies = []
                optional_dependencies = []
                if dependencies:
                    dependencies = list(as_list(dependencies))
                    for d in dependencies:
                        if type(d) is optional:
                            d = as_list(d())
                            optional_dependencies += d
                        else:
                            required_dependencies.append(d)
                self.logger.debug(
                    "Found task '%s' with required dependencies %s and optional dependencies %s",
                    name, required_dependencies, optional_dependencies)
                self.execution_manager.register_task(
                    Task(name, candidate, required_dependencies, description,
                         optional_dependencies))

            elif hasattr(candidate, ACTION_ATTRIBUTE) and getattr(
                    candidate, ACTION_ATTRIBUTE):
                before = getattr(candidate, BEFORE_ATTRIBUTE) if hasattr(
                    candidate, BEFORE_ATTRIBUTE) else None
                after = getattr(candidate, AFTER_ATTRIBUTE) if hasattr(
                    candidate, AFTER_ATTRIBUTE) else None

                only_once = False
                if hasattr(candidate, ONLY_ONCE_ATTRIBUTE):
                    only_once = getattr(candidate, ONLY_ONCE_ATTRIBUTE)
                teardown = False
                if hasattr(candidate, TEARDOWN_ATTRIBUTE):
                    teardown = getattr(candidate, TEARDOWN_ATTRIBUTE)

                self.logger.debug("Found action %s", name)
                self.execution_manager.register_action(
                    Action(name, candidate, before, after, description,
                           only_once, teardown))

            elif hasattr(candidate, INITIALIZER_ATTRIBUTE) and getattr(
                    candidate, INITIALIZER_ATTRIBUTE):
                environments = []
                if hasattr(candidate, ENVIRONMENTS_ATTRIBUTE):
                    environments = getattr(candidate, ENVIRONMENTS_ATTRIBUTE)

                self.execution_manager.register_initializer(
                    Initializer(name, candidate, environments, description))
Exemplo n.º 5
0
    def test_should_initialize_fields(self):
        def callable():
            pass

        task = Task("callable", callable, "dependency", "description")

        self.assertEquals(["dependency"], task.dependencies)
        self.assertEquals(["description"], task.description)
Exemplo n.º 6
0
    def test_should_extend_task_with_values_from_other_task(self):
        def callable_one():
            pass

        def callable_two(param):
            pass

        task = Task("task", callable_one, "dependency", "description")
        replacement = Task("replacement", callable_two, "another_dependency",
                           "replacement description")

        task.extend(replacement)

        self.assertEquals("task", task.name)
        self.assertEquals(["dependency", "another_dependency"],
                          task.dependencies)
        self.assertEquals(["description", "replacement description"],
                          task.description)
Exemplo n.º 7
0
    def test_should_execute_callable_without_arguments(self):
        def callable():
            callable.called = True

        callable.called = False

        Task("callable", callable).execute(mock(), {})

        self.assertTrue(callable.called)
Exemplo n.º 8
0
    def test_should_execute_both_callables_when_extending_task(self):
        def callable_one():
            callable_one.called = True

        callable_one.called = False

        def callable_two(param):
            callable_two.called = True

        callable_two.called = False

        task_one = Task("task", callable_one)
        task_two = Task("task", callable_two)
        task_one.extend(task_two)

        task_one.execute(mock(), {"param": "spam"})

        self.assertTrue(callable_one.called)
        self.assertTrue(callable_two.called)
Exemplo n.º 9
0
    def test_should_execute_callable_with_single_arguments(self):
        def callable(spam):
            callable.called = True
            callable.spam = spam

        callable.called = False

        Task("callable", callable).execute(mock(), {"spam": "spam"})

        self.assertTrue(callable.called)
        self.assertEquals("spam", callable.spam)
Exemplo n.º 10
0
    def collect_tasks_and_actions_and_initializers(self, project_module):
        for name in dir(project_module):
            candidate = getattr(project_module, name)

            if hasattr(candidate, NAME_ATTRIBUTE):
                name = getattr(candidate, NAME_ATTRIBUTE)
            elif hasattr(candidate, "__name__"):
                name = candidate.__name__
            description = getattr(candidate, DESCRIPTION_ATTRIBUTE) if hasattr(
                candidate, DESCRIPTION_ATTRIBUTE) else ""

            if hasattr(candidate, TASK_ATTRIBUTE) and getattr(
                    candidate, TASK_ATTRIBUTE):
                dependencies = getattr(
                    candidate, DEPENDS_ATTRIBUTE) if hasattr(
                        candidate, DEPENDS_ATTRIBUTE) else None

                self.logger.debug("Found task %s", name)
                self.execution_manager.register_task(
                    Task(name, candidate, dependencies, description))

            elif hasattr(candidate, ACTION_ATTRIBUTE) and getattr(
                    candidate, ACTION_ATTRIBUTE):
                before = getattr(candidate, BEFORE_ATTRIBUTE) if hasattr(
                    candidate, BEFORE_ATTRIBUTE) else None
                after = getattr(candidate, AFTER_ATTRIBUTE) if hasattr(
                    candidate, AFTER_ATTRIBUTE) else None

                only_once = False
                if hasattr(candidate, ONLY_ONCE_ATTRIBUTE):
                    only_once = getattr(candidate, ONLY_ONCE_ATTRIBUTE)

                self.logger.debug("Found action %s", name)
                self.execution_manager.register_action(
                    Action(name, candidate, before, after, description,
                           only_once))

            elif hasattr(candidate, INITIALIZER_ATTRIBUTE) and getattr(
                    candidate, INITIALIZER_ATTRIBUTE):
                environments = []
                if hasattr(candidate, ENVIRONMENTS_ATTRIBUTE):
                    environments = getattr(candidate, ENVIRONMENTS_ATTRIBUTE)

                self.execution_manager.register_initializer(
                    Initializer(name, candidate, environments, description))
Exemplo n.º 11
0
    def test_should_execute_both_callables_when_extending_task(self):
        def callable_one():
            callable_one.called = True

        callable_one.called = False

        def callable_two(param):
            callable_two.called = True

        callable_two.called = False

        task_one = Task("task", callable_one)
        task_two = Task("task", callable_two)
        task_one.extend(task_two)

        task_one.execute(mock(), {"param": "spam"})

        self.assertTrue(callable_one.called)
        self.assertTrue(callable_two.called)
Exemplo n.º 12
0
    def collect_tasks_and_actions_and_initializers(self, project_module):
        injected_task_dependencies = dict()

        def normalize_candidate_name(candidate):
            if hasattr(candidate, NAME_ATTRIBUTE):
                return getattr(candidate, NAME_ATTRIBUTE)
            elif hasattr(candidate, "__name__"):
                return candidate.__name__

        def add_task_dependency(names, depends_on, optional):
            for name in as_list(names):
                if not isinstance(name, basestring):
                    name = normalize_candidate_name(name)
                if name not in injected_task_dependencies:
                    injected_task_dependencies[name] = list()
                injected_task_dependencies[name].append(
                    TaskDependency(depends_on, optional))

        for name in dir(project_module):
            candidate = getattr(project_module, name)

            if hasattr(candidate, TASK_ATTRIBUTE) and getattr(
                    candidate, TASK_ATTRIBUTE):
                dependents = getattr(
                    candidate, DEPENDENTS_ATTRIBUTE) if hasattr(
                        candidate, DEPENDENTS_ATTRIBUTE) else None
                if dependents:
                    dependents = list(as_list(dependents))
                    for d in dependents:
                        if isinstance(d, optional):
                            d = d()
                            add_task_dependency(d, candidate, True)
                        else:
                            add_task_dependency(d, candidate, False)

        for name in dir(project_module):
            candidate = getattr(project_module, name)
            name = normalize_candidate_name(candidate)

            description = getattr(candidate, DESCRIPTION_ATTRIBUTE) if hasattr(
                candidate, DESCRIPTION_ATTRIBUTE) else ""

            if hasattr(candidate, TASK_ATTRIBUTE) and getattr(
                    candidate, TASK_ATTRIBUTE):
                dependencies = getattr(
                    candidate, DEPENDS_ATTRIBUTE) if hasattr(
                        candidate, DEPENDS_ATTRIBUTE) else None

                task_dependencies = list()
                if dependencies:
                    dependencies = list(as_list(dependencies))
                    for d in dependencies:
                        if isinstance(d, optional):
                            d = as_list(d())
                            task_dependencies.extend(
                                [TaskDependency(item, True) for item in d])
                        else:
                            task_dependencies.append(TaskDependency(d))

                # Add injected
                if name in injected_task_dependencies:
                    task_dependencies.extend(injected_task_dependencies[name])

                self.logger.debug("Found task '%s' with dependencies %s", name,
                                  task_dependencies)
                self.execution_manager.register_task(
                    Task(name, candidate, task_dependencies, description))

            elif hasattr(candidate, ACTION_ATTRIBUTE) and getattr(
                    candidate, ACTION_ATTRIBUTE):
                before = getattr(candidate, BEFORE_ATTRIBUTE) if hasattr(
                    candidate, BEFORE_ATTRIBUTE) else None
                after = getattr(candidate, AFTER_ATTRIBUTE) if hasattr(
                    candidate, AFTER_ATTRIBUTE) else None

                only_once = False
                if hasattr(candidate, ONLY_ONCE_ATTRIBUTE):
                    only_once = getattr(candidate, ONLY_ONCE_ATTRIBUTE)
                teardown = False
                if hasattr(candidate, TEARDOWN_ATTRIBUTE):
                    teardown = getattr(candidate, TEARDOWN_ATTRIBUTE)

                self.logger.debug("Found action %s", name)
                self.execution_manager.register_action(
                    Action(name, candidate, before, after, description,
                           only_once, teardown))

            elif hasattr(candidate, INITIALIZER_ATTRIBUTE) and getattr(
                    candidate, INITIALIZER_ATTRIBUTE):
                environments = []
                if hasattr(candidate, ENVIRONMENTS_ATTRIBUTE):
                    environments = getattr(candidate, ENVIRONMENTS_ATTRIBUTE)

                self.execution_manager.register_initializer(
                    Initializer(name, candidate, environments, description))
Exemplo n.º 13
0
    def collect_project_annotations(self, project_module):
        injected_task_dependencies = {}

        def add_task_dependency(names, depends_on, optional):
            for name in as_list(names):
                if not isinstance(name, basestring):
                    name = self.normalize_candidate_name(name)
                if name not in injected_task_dependencies:
                    injected_task_dependencies[name] = list()
                injected_task_dependencies[name].append(
                    TaskDependency(depends_on, optional))

        for name in dir(project_module):
            candidate = getattr(project_module, name)
            name = self.normalize_candidate_name(candidate)

            if getattr(candidate, TASK_ATTRIBUTE, None):
                dependents = getattr(candidate, DEPENDENTS_ATTRIBUTE, None)

                if dependents:
                    dependents = list(as_list(dependents))
                    for d in dependents:
                        if isinstance(d, optional):
                            d = d()
                            add_task_dependency(d, name, True)
                        else:
                            add_task_dependency(d, name, False)

        for name in dir(project_module):
            candidate = getattr(project_module, name)
            name = self.normalize_candidate_name(candidate)

            description = getattr(candidate, DESCRIPTION_ATTRIBUTE, "")

            if getattr(candidate, TASK_ATTRIBUTE, None):
                dependencies = getattr(candidate, DEPENDS_ATTRIBUTE, None)

                task_dependencies = list()
                if dependencies:
                    dependencies = list(as_list(dependencies))
                    for d in dependencies:
                        if isinstance(d, optional):
                            d = as_list(d())
                            task_dependencies.extend(
                                [TaskDependency(item, True) for item in d])
                        else:
                            task_dependencies.append(TaskDependency(d))

                # Add injected
                if name in injected_task_dependencies:
                    task_dependencies.extend(injected_task_dependencies[name])
                    del injected_task_dependencies[name]

                self.logger.debug("Found task '%s' with dependencies %s", name,
                                  task_dependencies)
                self.execution_manager.register_task(
                    Task(name, candidate, task_dependencies, description))

            elif getattr(candidate, ACTION_ATTRIBUTE, None):
                before = getattr(candidate, BEFORE_ATTRIBUTE, None)
                after = getattr(candidate, AFTER_ATTRIBUTE, None)

                only_once = getattr(candidate, ONLY_ONCE_ATTRIBUTE, False)
                teardown = getattr(candidate, TEARDOWN_ATTRIBUTE, False)

                self.logger.debug("Found action %s", name)
                self.execution_manager.register_action(
                    Action(name, candidate, before, after, description,
                           only_once, teardown))

            elif getattr(candidate, INITIALIZER_ATTRIBUTE, None):
                environments = getattr(candidate, ENVIRONMENTS_ATTRIBUTE, [])

                self.execution_manager.register_initializer(
                    Initializer(name, candidate, environments, description))
            elif getattr(candidate, FINALIZER_ATTRIBUTE, None):
                environments = getattr(candidate, ENVIRONMENTS_ATTRIBUTE, [])

                self.execution_manager.register_finalizer(
                    Finalizer(name, candidate, environments, description))

        self.execution_manager.register_late_task_dependencies(
            injected_task_dependencies)