def test_validate_target_dir(self): # assert not ArgumentValidator.validate_target_dir(f'{TestIntegrationArgumentValidator.TEST_DIR}') # assert not ArgumentValidator.validate_target_dir(f'{TestIntegrationArgumentValidator.TEST_DIR}_x') assert not ArgumentValidator.validate_target_dir('{test_dir}'.format( test_dir=TestIntegrationArgumentValidator.TEST_DIR)) assert not ArgumentValidator.validate_target_dir('{test_dir}_x'.format( test_dir=TestIntegrationArgumentValidator.TEST_DIR))
def test_validate_input_dir(self): assert not ArgumentValidator.validate_input_dir( TestIntegrationArgumentValidator.TEST_DIR) with pytest.raises(AssertionError): # ArgumentValidator.validate_input_dir(f'{TestIntegrationArgumentValidator.TEST_DIR}_x') ArgumentValidator.validate_input_dir('{test_dir}_x'.format( test_dir=TestIntegrationArgumentValidator.TEST_DIR))
def test_validate_target_dir(os): os.path.exists = lambda x: False os.access = lambda x, y: True ArgumentValidator.validate_target_dir('test') os.access = lambda x, y: False with pytest.raises(AssertionError): ArgumentValidator.validate_target_dir('test')
def copy_files(cls, file_groups, target_dir, force_overwrite): if not os.path.exists(target_dir): os.makedirs(target_dir) # TODO: not covered total_count = Count() for group in file_groups: group_count = Count() # group_dir = f'{target_dir}{os.sep}{group}' # 3.6 group_dir = '{target_dir}{os_sep}{group}'.format( target_dir=target_dir, os_sep=os.sep, group=group) ArgumentValidator.validate_target_dir(group_dir) if not os.path.exists(group_dir): os.makedirs(group_dir) # log_message(f'Created directory: {group_dir}') # 3.6 log_message('Created directory: {group_dir}'.format( group_dir=group_dir)) # log_message(f'Copying {len(file_groups[group])} files to {group_dir}') # 3.6 log_message('Moving {group_size} files to {group_dir}'.format( group_size=len(file_groups[group]), group_dir=group_dir)) for file in file_groups[group]: # file_path = f'{group_dir}{os.sep}{os.path.basename(file)}' # 3.6 file_path = '{group_dir}{os_sep}{file_name}'.format( group_dir=group_dir, os_sep=os.sep, file_name=os.path.basename(file)) if force_overwrite and os.path.exists(file_path): os.remove(file_path) if not os.path.exists(file_path): shutil.copy2(file, group_dir) group_count.add_copied(count=1) else: group_count.add_skipped(count=1) # TODO: not covered total_count.add_files(count=len(file_groups[group])) total_count.add_copied(count=group_count.copied) total_count.add_skipped(count=group_count.skipped) # log_message(f'Copied {group_count.copied}, skipped {group_count.skipped}') # 3.6 log_message( 'Copied {local_copied_count}, skipped {local_skipped_count}'. format(local_copied_count=group_count.copied, local_skipped_count=group_count.skipped)) log_message( # f'Total files count {total_count.files}, total copied {total_count.copied}, total skipped {total_count.skipped}') # 3.6 'Total files count {total_files_count}, total copied {total_copied_count}, total skipped {total_skipped_count}' .format(total_files_count=total_count.files, total_copied_count=total_count.copied, total_skipped_count=total_count.skipped)) return total_count
def run(*, args=None): command_line_argument_parser = CommandLineArgumentParser() try: arguments = ArgumentValidator.validate_arguments( command_line_argument_parser.parse(args=args)) except AssertionError as assertion_error: command_line_argument_parser.print_help() raise assertion_error file_mover = FileCopierService(input_dir=arguments.input_dir, target_dir=arguments.target_dir, force_overwrite=arguments.force_overwrite) file_mover.copy_files() # print(f'Copying files within {arguments.input_dir} to {arguments.target_dir} in YYYYMM formatted subdirectories') # 3.6 print( 'Copying files within {input_dir} to {target_dir} in YYYYMM formatted subdirectories' .format(input_dir=arguments.input_dir, target_dir=arguments.target_dir))
def test_validate_target_dir_with_exception(self, os): os.access = lambda x, y: False with pytest.raises(AssertionError): ArgumentValidator.validate_target_dir('test')