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)
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))
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'] = ''
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'] = ''
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)