def test_assign_temporary_file_sets_correct_chmod(self): manager = TempManager() path = manager.assign_temporary_file() try: st = os.stat(path) self.assertEqual('0755', str(oct(st.st_mode))[-4:]) finally: manager.finally_clean_up()
def test_assign_temporary_file_creates_a_file_and_finally_clean_up_removes_file( self): manager = TempManager() with self.subTest('Creation'): path = manager.assign_temporary_file() self.assertTrue(os.path.isfile(path)) with self.subTest('Deletion'): manager.finally_clean_up() self.assertFalse(os.path.isfile(path))
def execute_mocked_task_and_get_output(self, task: TaskInterface, args=None, env=None) -> str: """ Run a single task, capturing it's output in a simplified way. There is no whole RKD bootstrapped in this operation. :param TaskInterface task: :param dict args: :param dict env: :return: """ if args is None: args = {} if env is None: env = {} ctx = ApplicationContext([], [], '') ctx.io = BufferedSystemIO() task.internal_inject_dependencies( io=ctx.io, ctx=ctx, executor=OneByOneTaskExecutor(ctx=ctx), temp_manager=TempManager()) merged_env = deepcopy(os.environ) merged_env.update(env) r_io = IO() str_io = StringIO() defined_args = {} for arg, arg_value in args.items(): defined_args[arg] = {'default': ''} with r_io.capture_descriptors(enable_standard_out=True, stream=str_io): try: # noinspection PyTypeChecker result = task.execute( ExecutionContext(TaskDeclaration(task), args=args, env=merged_env, defined_args=defined_args)) except Exception: self._restore_standard_out() print(ctx.io.get_value() + "\n" + str_io.getvalue()) raise return ctx.io.get_value() + "\n" + str_io.getvalue( ) + "\nTASK_EXIT_RESULT=" + str(result)
def test_directory_is_created_when_it_does_not_exist(self): temp_chdir = '/tmp/' + str(uuid4()) manager = TempManager(temp_chdir) try: manager.assign_temporary_file() self.assertTrue(os.path.isdir(temp_chdir)) finally: manager.finally_clean_up() subprocess.call(['rmdir', temp_chdir])
def _prepare_test_for_forking_process(task: TaskInterface = None): if not task: task = TestTask() io = IO() string_io = StringIO() temp = TempManager(chdir='/tmp/') container = ApplicationContext([], [], '') container.io = BufferedSystemIO() executor = OneByOneTaskExecutor(container) declaration = get_test_declaration(task) task._io = io task._io.set_log_level('debug') ctx = ExecutionContext(declaration) return string_io, task, executor, io, ctx, temp
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
def test_chdir_is_considered(self): manager = TempManager('/tmp/') self.assertTrue(manager.create_tmp_file_path()[0].startswith('/tmp/'))
def test_create_tmp_file_path_assigns_an_unique_filename(self): path, filename = TempManager().create_tmp_file_path() self.assertFalse(os.path.isfile(path))