Ejemplo n.º 1
0
    def test_distint_imports_on_py_file(self):
        """Case: PY file can define a variable IMPORTS that can contain both TaskDeclaration and TaskAliasDeclaration
        """

        with NamedTemporaryFile() as tmp_file:
            tmp_file.write(b'''
            from rkd.syntax import TaskAliasDeclaration as Task
            
            IMPORTS = [
                TaskAliasDeclaration(':hello', [':test'])
            ]
            ''')

            with unittest.mock.patch('rkd.context.os.path.isfile',
                                     return_value=True):
                with unittest.mock.patch(
                        'rkd.context.SourceFileLoader.load_module'
                ) as src_loader_method:

                    class TestImported:
                        IMPORTS = []
                        TASKS = []

                    src_loader_method.return_value = TestImported()
                    src_loader_method.return_value.IMPORTS = [
                        TaskAliasDeclaration(':hello', [':test'])
                    ]
                    src_loader_method.return_value.TASKS = []

                    ctx_factory = ContextFactory(NullSystemIO())
                    ctx = ctx_factory._load_from_py(tmp_file.name)

                    self.assertIn(':hello', ctx._task_aliases)
Ejemplo n.º 2
0
    def test_loads_internal_context(self) -> None:
        """Test if internal context (RKD by default has internal context) is loaded properly
        """
        discovery = ContextFactory(NullSystemIO())
        ctx = discovery._load_context_from_directory(CURRENT_SCRIPT_PATH +
                                                     '/../rkd/misc/internal')

        self.assertTrue(isinstance(ctx, ApplicationContext))
Ejemplo n.º 3
0
    def _common_test_loads_task_from_file(self, path: str, task: str,
                                          filename: str):
        os.environ['RKD_PATH'] = path
        ctx = None

        try:
            discovery = ContextFactory(NullSystemIO())
            ctx = discovery.create_unified_context()
        except:
            raise
        finally:
            self.assertIn(task,
                          ctx.find_all_tasks().keys(),
                          msg='Expected that %s task would be loaded from %s' %
                          (task, filename))

            os.environ['RKD_PATH'] = ''
Ejemplo n.º 4
0
    def satisfy_task_dependencies(task: TaskInterface,
                                  io: IO = None) -> TaskInterface:
        """
        Inserts required dependencies to your task that implements rkd.api.contract.TaskInterface

        :param task:
        :param io:
        :return:
        """

        if io is None:
            io = NullSystemIO()

        ctx = ApplicationContext([], [], '')
        ctx.io = io

        task.internal_inject_dependencies(io=io,
                                          ctx=ctx,
                                          executor=OneByOneTaskExecutor(ctx),
                                          temp_manager=TempManager())

        return task
Ejemplo n.º 5
0
    def test_parse_tasks_successful_case(self):
        """Successful case with description, arguments and bash steps
        """

        input_tasks = {
            ':resistentia': {
                'description':
                'Against moving the costs of the crisis to the workers!',
                'arguments': {
                    '--picket': {
                        'help': 'Picket form',
                        'required': False,
                        'action': 'store_true'
                    }
                },
                'steps': ['echo "Resistentia!"']
            }
        }

        io = IO()
        out = StringIO()
        factory = YamlSyntaxInterpreter(io, YamlFileLoader([]))
        parsed_tasks = factory.parse_tasks(input_tasks, '', './makefile.yaml',
                                           {})

        self.assertEqual(':resistentia',
                         parsed_tasks[0].to_full_name(),
                         msg='Expected that the task name will be present')

        declaration = parsed_tasks[0]
        declaration.get_task_to_execute()._io = NullSystemIO()

        with io.capture_descriptors(stream=out, enable_standard_out=False):
            declaration.get_task_to_execute().execute(
                ExecutionContext(declaration))

        self.assertIn('Resistentia!',
                      out.getvalue(),
                      msg='Expected that echo contents will be visible')
Ejemplo n.º 6
0
    def test_loads_internal_context_in_unified_context(self) -> None:
        """Check if application loads context including paths from RKD_PATH
        """

        os.environ[
            'RKD_PATH'] = CURRENT_SCRIPT_PATH + '/../docs/examples/makefile-like/.rkd'
        ctx = None

        try:
            discovery = ContextFactory(NullSystemIO())
            ctx = discovery.create_unified_context()
        except:
            raise
        finally:
            self.assertIn(
                ':find-images',
                ctx.find_all_tasks().keys(),
                msg=
                ':find-images is defined in docs/examples/makefile-like/.rkd/makefile.py as an alias type task'
                +
                ', expected that it would be loaded from path placed at RKD_PATH'
            )

            os.environ['RKD_PATH'] = ''
Ejemplo n.º 7
0
    def test_distinct_imports_on_yaml_file(self):
        """Case: YAML file can import a module that contains imports() method
        And that method returns list of Union[TaskDeclaration, TaskAliasDeclaration]
        """

        with NamedTemporaryFile() as tmp_file:
            tmp_file.write(b'''
            version: org.riotkit.rkd/yaml/v1
            imports:
                - fictional
            ''')

            with unittest.mock.patch(
                    'rkd.context.YamlSyntaxInterpreter.parse') as parse_method:
                parse_method.return_value = ([
                    TaskAliasDeclaration(':hello', [':test'])
                ], [])

                ctx_factory = ContextFactory(NullSystemIO())
                ctx = ctx_factory._load_from_yaml(
                    os.path.dirname(tmp_file.name),
                    os.path.basename(tmp_file.name))

                self.assertIn(':hello', ctx._task_aliases)