def test_check_is_file_and_correct(): random_name = '.{}'.format(hash(os.times())) test_filename = "./" + random_name + ".txt" with pytest.raises(RuntimeError): common.check_folder(test_filename) assert common.check_is_file_and_correct_path( __get_this_filename()) == os.path.abspath(__get_this_filename())
def test_check_folder(): # current_dir = os.path.split(__get_this_filename())[0] # root = os.path.abspath(os.path.join(current_dir, "./../../")) # assert check_folder(current_dir + "/././../") == root + "\\python" # assert check_folder(current_dir + "/././../../") == root assert common.check_folder("C:/") == "C:\\" with pytest.raises(RuntimeError): common.check_folder(__file__) with pytest.raises(RuntimeError): common.check_folder("AA:/")
def create_result_trans_file_inside(function_test, new_extension_for_result, filename_ext=".md", folder_search=get_test_folder(), force_creation=False): logging.info('Create result for %s', new_extension_for_result) folder_search = common.check_folder(folder_search) len_end_of_filename = len(new_extension_for_result) def __create_result__(filename): filename = common.check_is_file_and_correct_path(filename) result_filename = common.set_correct_path(os.path.splitext(filename)[ 0] + new_extension_for_result) if (len(filename) > len_end_of_filename) and \ (filename[-len_end_of_filename:] == new_extension_for_result): return if (os.path.isfile(result_filename)) and (not force_creation): return logging.info('Create result for the file %s', filename) shutil.copyfile(filename, result_filename) function_test(result_filename) new_result_filename = "%s.%s-" % (result_filename, common.get_today()) if os.path.isfile(new_result_filename + "000.bak"): os.remove(new_result_filename + "000.bak") if os.path.isfile(new_result_filename + "001.bak"): os.remove(new_result_filename + "001.bak") common.apply_function_in_folder( folder_search, __create_result__, filename_ext=filename_ext)
def create_result_transform_file(function_test, new_extension_for_result, filename_ext=".md", folder_search=get_test_folder(), force_creation=False): logging.info('Create result for %s', new_extension_for_result) folder_search = common.check_folder(folder_search) len_end_of_filename = len(new_extension_for_result) def __create_result__(filename): filename = common.check_is_file_and_correct_path(filename) result_filename = common.set_correct_path(os.path.splitext(filename)[ 0] + new_extension_for_result) if (len(filename) > len_end_of_filename) and \ (filename[-len_end_of_filename:] == new_extension_for_result): return if (os.path.isfile(result_filename)) and (not force_creation): return logging.info('Create result for the file %s', filename) function_test(filename) common.check_is_file_and_correct_path(result_filename) common.apply_function_in_folder( folder_search, __create_result__, filename_ext=filename_ext)
def find_test_file_couple(new_extension_for_result, filename_ext=".md", folder_search=get_test_folder()): logging.info('Search test files for %s', new_extension_for_result) folder_search = common.check_folder(folder_search) len_end_of_filename = len(new_extension_for_result) result = [] def __search_test_file__(filename): filename = common.check_is_file_and_correct_path(filename) result_filename = common.set_correct_path(os.path.splitext(filename)[ 0] + new_extension_for_result) # case when the filename is a result filename if (len(filename) > len_end_of_filename) \ and (filename[-len_end_of_filename:] == new_extension_for_result): return if os.path.isfile(result_filename): result.append((filename, result_filename)) common.apply_function_in_folder( folder_search, __search_test_file__, filename_ext=filename_ext) return result
def test_check_create_folder(): current_folder = os.path.abspath("./") assert common.check_create_folder("./") == current_folder random_name = '.{}'.format(hash(os.times())) test_foldername = "./" + random_name with pytest.raises(RuntimeError): common.check_folder(test_foldername) assert common.check_create_folder(test_foldername) == os.path.abspath( test_foldername) assert common.check_folder(test_foldername) == os.path.abspath( test_foldername) os.rmdir(test_foldername) with pytest.raises(RuntimeError): common.check_folder(test_foldername) with pytest.raises(RuntimeError): common.check_create_folder(__file__)
def rename_file_result(test_folder, extension, new_extension): logging.info('rename_file_result in %s from %s to %s', test_folder, extension, new_extension) folder_search = common.check_folder(os.path.join(get_test_folder(), test_folder)) len_end_of_filename = len(extension) def __rename_test_file__(filename): filename = common.check_is_file_and_correct_path(filename) if (len(filename) > len_end_of_filename) \ and (filename[-len_end_of_filename:] == extension): shutil.move(filename, filename[:-len_end_of_filename] + new_extension) print(filename) return common.apply_function_in_folder( folder_search, __rename_test_file__, filename_ext=".md") return
def test_get_new_temp_dir(): temp1 = common.get_new_temp_dir() temp2 = common.get_new_temp_dir() assert temp1 != temp2 assert common.check_folder(temp1) == os.path.abspath(temp1) assert common.check_folder(temp2) == os.path.abspath(temp2) assert len(os.listdir(temp1)) == 0 assert len(os.listdir(temp2)) == 0 if os.path.isdir(temp1): os.rmdir(temp1) if os.path.isdir(temp2): os.rmdir(temp2) with pytest.raises(RuntimeError): common.check_folder(temp1) with pytest.raises(RuntimeError): common.check_folder(temp2)
def get_test_folder(): if get_test_folder.__folder_md_test__ is None: get_test_folder.__folder_md_test__ = common.check_folder(os.path.join( os.path.split(__get_this_filename())[0], "data")) return get_test_folder.__folder_md_test__