diff --git a/test/py/testing.py b/test/py/testing.py index 366ac78..c4c8017 100644 --- a/test/py/testing.py +++ b/test/py/testing.py @@ -16,13 +16,13 @@ ExtensionsOfInterest = ['*.OBK', '*.OLB', '*.DSN', '*.DBK'] -def get_files_of_interest(path: str, file_extensions: List[str]) -> List[str]: +def get_files_of_interest(path: Path, file_extensions: List[str]) -> List[Path]: """ Get a list of absolute paths to files with specified extension. This function searches recursively through the path. Usage e.g. - >>> files = get_files_of_interest('/home/some_dir', ['*.txt', '*.log']) + >>> files = get_files_of_interest(Path('/home/some_dir'), ['*.txt', '*.log']) >>> print(files) >>> ['/home/som_dir/sub_dir/foo.txt', '/home/some_dir/bar.log'] """ @@ -30,21 +30,19 @@ def get_files_of_interest(path: str, file_extensions: List[str]) -> List[str]: files = [] for extension in file_extensions: - for file in Path(path).rglob(extension): + for file in path.rglob(extension): files += [file] return files -def add_new_repo_to_database(db: FileErrorDatabase, url: str, base_path: str) -> None: +def add_new_repo_to_database(db: FileErrorDatabase, url: str, base_path: Path) -> None: author = url.split('.git')[0].split('/')[-2] project = url.split('.git')[0].split('/')[-1] - base_path = os.path.join(base_path, author, project) + base_path = base_path / author / project - exists_on_fs = os.path.exists(base_path) - - if exists_on_fs: + if base_path.exists(): print(f'Repository {base_path} does already exist, do not clone.') fs_repo = Repo(base_path) else: @@ -60,7 +58,7 @@ def add_new_repo_to_database(db: FileErrorDatabase, url: str, base_path: str) -> files = get_files_of_interest(base_path, ExtensionsOfInterest) for file in files: - file = os.path.join('.', os.path.relpath(file, base_path)) + file = Path(os.path.join('.', os.path.relpath(file, base_path))) repo_file = RepoFile(path=file, errors=99999) if not db.isFileInRepo(repo, repo_file): @@ -68,8 +66,8 @@ def add_new_repo_to_database(db: FileErrorDatabase, url: str, base_path: str) -> db.addRepo(repo) -def write_back_err_count(db: FileErrorDatabase, path_err_cnt_log: str, path_thirdparty_designs: str) -> None: - with open(path_err_cnt_log, 'r') as f: +def write_back_err_count(db: FileErrorDatabase, path_err_cnt_log: Path, path_thirdparty_designs: Path) -> None: + with path_err_cnt_log.open() as f: log = f.readlines() for line in log: @@ -81,14 +79,14 @@ def write_back_err_count(db: FileErrorDatabase, path_err_cnt_log: str, path_thir expect_err = int(matches[1]) file_path = str(matches[2]) - author = file_path[len(path_thirdparty_designs):].split(os.path.sep)[1] - project = file_path[len(path_thirdparty_designs):].split(os.path.sep)[2] + author = file_path[len(str(path_thirdparty_designs)):].split(os.path.sep)[1] + project = file_path[len(str(path_thirdparty_designs)):].split(os.path.sep)[2] - path_repo = os.path.join(path_thirdparty_designs, author, project) + path_repo = path_thirdparty_designs / author / project # Make path relative file_path = os.path.relpath(file_path, path_repo) - file_path = os.path.join('.', file_path) + file_path = Path(os.path.join('.', file_path)) repo = db.getRepoByNames(author, project) file = db.getFileByPath(repo, file_path) @@ -96,8 +94,8 @@ def write_back_err_count(db: FileErrorDatabase, path_err_cnt_log: str, path_thir db.updateErrorCounter(file, actual_err) -def create_repo_cpp_unit_test(repo: Repository, path_repo: str, path_unit_test: str) -> None: - with open(path_unit_test, 'w') as f: +def create_repo_cpp_unit_test(repo: Repository, path_repo: Path, path_unit_test: Path) -> None: + with path_unit_test.open('w', encoding='utf-8') as f: preamble = """// THIS FILE IS AUTOMATICALLY GENERATED! DO NOT MODIFY IT! @@ -198,8 +196,8 @@ def create_repo_cpp_unit_test(repo: Repository, path_repo: str, path_unit_test: do_write_back = args.write_back do_download = args.do_download - path_thirdparty_designs = args.path_thirdparty_designs - path_unit_tests = args.path_unit_tests + path_thirdparty_designs = Path(args.path_thirdparty_designs) + path_unit_tests = Path(args.path_unit_tests) path_err_cnt_log = args.path_err_cnt_log path_db = args.path_db @@ -207,10 +205,10 @@ def create_repo_cpp_unit_test(repo: Repository, path_repo: str, path_unit_test: if not do_generate_unit_tests and not do_write_back and not do_download: parser.print_help() - def generate_unit_tests(db: FileErrorDatabase, path_thirdparty_designs: str, path_unit_tests: str) -> None: + def generate_unit_tests(db: FileErrorDatabase, path_thirdparty_designs: Path, path_unit_tests: Path) -> None: for repo in db.data.repositories: - path_repo = os.path.join(path_thirdparty_designs, repo.author, repo.project) - path_unit_test = os.path.join(path_unit_tests, f'Test_{repo.author}_{repo.project}.cpp') + path_repo = path_thirdparty_designs / repo.author / repo.project + path_unit_test = path_unit_tests / f'Test_{repo.author}_{repo.project}.cpp' create_repo_cpp_unit_test(repo, path_repo, path_unit_test)