def test_task_run(self, d): os.chdir(d.path) file_content = """ file = open("test-target-file.txt", "w") file.write("{0}") file.close() """ file_content = file_content.format(Constants.PROJECT_NAME) file_path = os.path.join(d.path, 'test-file.py') target_file_path = os.path.join(d.path, 'test-target-file.txt') d.write(file_path, file_content.encode('utf-8')) task = Task(task_type=Task.TYPE_RUN, task_name='Sample run task', task_params={'args': ['python', file_path]}) process_data = ProcessData() template_data = {} task.parse(process_data) task.run(process_data=process_data, template_data=template_data, working_dir=d.path) content = FileUtil.read_file(target_file_path) self.assertEqual(Constants.PROJECT_NAME, content)
def test_task_copy_files(self, d): os.chdir(d.path) file_path1 = os.path.join(d.path, 'test-copy-1.txt') file_path2 = os.path.join(d.path, 'test-copy-2.txt') to_path = os.path.join(d.path, 'files') d.write(file_path1, 'sample data'.encode('utf-8')) d.write(file_path2, 'sample data'.encode('utf-8')) FileUtil.create_dir(to_path) task = Task(task_type=Task.TYPE_COPY_FILES, task_name='Sample copy files task', task_params={ 'from': os.path.join(d.path, '*.txt'), 'to': to_path }) process_data = ProcessData() template_data = {} task.parse(process_data) task.run(process_data=process_data, template_data=template_data, working_dir=d.path) self.assertTrue( os.path.exists(os.path.join(to_path, 'test-copy-1.txt'))) self.assertTrue( os.path.exists(os.path.join(to_path, 'test-copy-2.txt')))
def test_task_run_invalid_type(self): task = Task(task_name='Sample task', task_type='invalid_type_xyz') with pytest.raises(SystemExit) as error: task.run(process_data=ProcessData(), template_data={}, working_dir=None) self.assertEqual(error.type, SystemExit) self.assertEqual(error.value.code, 1)
def test_task_parse_file(self, d): os.chdir(d.path) file_path = os.path.join(d.path, '*.txt') d.write(file_path, '{{ name }}'.encode('utf-8')) task = Task(task_type=Task.TYPE_PARSE_FILE, task_name='Sample parse file task', task_params={'file': file_path}) process_data = ProcessData() template_data = {'name': Constants.PROJECT_NAME} task.parse(process_data) task.run(process_data=process_data, template_data=template_data, working_dir=d.path) content = FileUtil.read_file(file_path) self.assertEqual(Constants.PROJECT_NAME, content)
def test_task_run_generate_error(self, d): os.chdir(d.path) file_content = """ print("Sample task") raise Exception('Sample task') """ file_path = os.path.join(d.path, 'test-file.py') d.write(file_path, file_content.encode('utf-8')) task = Task(task_type=Task.TYPE_RUN, task_name='Sample run task', task_params={'args': ['python', file_path]}) with pytest.raises(SystemExit) as error: task.run(process_data=ProcessData(), template_data={}, working_dir=d.path) self.assertEqual(error.type, SystemExit) self.assertEqual(error.value.code, 1)
def test_task_run_invalid_binary(self, d): os.chdir(d.path) task = Task(task_type=Task.TYPE_RUN, task_name='Sample run task - invalid binary', task_params={'args': ['dont_exists_xyz']}) process_data = ProcessData() template_data = {} task.parse(process_data) error_type = OSError if sys.version_info >= (3, ): error_type = FileNotFoundError with pytest.raises(error_type) as error: task.run(process_data=process_data, template_data=template_data, working_dir=d.path) self.assertEqual(error.type, error_type)
def test_task_copy_file(self, d): os.chdir(d.path) from_path = os.path.join(d.path, 'test-copy.txt') to_path = os.path.join(d.path, 'test-copy2.txt') d.write(from_path, 'sample data'.encode('utf-8')) task = Task(task_type=Task.TYPE_COPY_FILE, task_name='Sample copy file task', task_params={ 'from': from_path, 'to': to_path }) process_data = ProcessData() template_data = {} task.parse(process_data) task.run(process_data=process_data, template_data=template_data, working_dir=d.path) self.assertTrue(os.path.exists(to_path))
def test_task_run_invalid_process_data(self): task = Task(task_name='Sample task', task_type=Task.TYPE_RUN) task.run(process_data=None, template_data={}, working_dir=None)