From 24e71d922afedbbffb76f75e0fb7528b5be9d767 Mon Sep 17 00:00:00 2001 From: Patrick Seewald Date: Sat, 11 May 2024 03:28:53 +0200 Subject: [PATCH] =?UTF-8?q?Setting=20limit=20to=20number=20of=20lines=20pe?= =?UTF-8?q?r=20file=C2=A0(to=20speedup=20cron=20tests)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fortran_tests/testsuites.config | 2 +- fprettify/__init__.py | 22 ++++++++++++++++++---- fprettify/tests/fortrantests.py | 25 +++++++++++++++++++------ 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/fortran_tests/testsuites.config b/fortran_tests/testsuites.config index b5a8de7..6597c74 100644 --- a/fortran_tests/testsuites.config +++ b/fortran_tests/testsuites.config @@ -21,7 +21,7 @@ suite: regular [cp2k] obtain: git.Repo.clone_from("https://github.com/cp2k/cp2k.git", "cp2k", branch="v2024.1") path: cp2k/src -options: --whitespace 2 --indent 3 --case 2 2 2 2 +options: --whitespace 2 --indent 3 --case 2 2 2 2 --exclude-max-lines 10000 suite: cron [flap] diff --git a/fprettify/__init__.py b/fprettify/__init__.py index b003c44..8dfe73b 100644 --- a/fprettify/__init__.py +++ b/fprettify/__init__.py @@ -2036,9 +2036,11 @@ def str2bool(str): help="Paths to files to be formatted inplace. If no paths are given, stdin (-) is used by default. Path can be a directory if --recursive is used.", default=['-']) parser.add_argument('-r', '--recursive', action='store_true', default=False, help="Recursively auto-format all Fortran files in subdirectories of specified path; recognized filename extensions: {}". format(", ".join(FORTRAN_EXTENSIONS))) - parser.add_argument('-e', '--exclude', action='append', + parser.add_argument('-e', '--exclude-pattern', '--exclude', action='append', default=[], type=str, help="File or directory patterns to be excluded when searching for Fortran files to format") + parser.add_argument('-m', '--exclude-max-lines', type=int, + help="Exclude large files when searching for Fortran files to format by specifying the maximum number of lines per file") parser.add_argument('-f', '--fortran', type=str, action='append', default=[], help="Overrides default fortran extensions recognized by --recursive. Repeat this option to specify more than one extension.") parser.add_argument('--version', action='version', @@ -2109,15 +2111,27 @@ def get_config_file_list(filename): # Prune excluded patterns from list of child directories dirnames[:] = [dirname for dirname in dirnames if not any( [fnmatch(dirname,exclude_pattern) or fnmatch(os.path.join(dirpath,dirname),exclude_pattern) - for exclude_pattern in args.exclude] + for exclude_pattern in args.exclude_pattern] )] for ffile in [os.path.join(dirpath, f) for f in files if any(f.endswith(_) for _ in ext) and not any([ fnmatch(f,exclude_pattern) - for exclude_pattern in args.exclude])]: - filenames.append(ffile) + for exclude_pattern in args.exclude_pattern])]: + + include_file = True + if args.exclude_max_lines is not None: + line_count = 0 + with open(ffile) as f: + for i in f: + line_count += 1 + if line_count > args.exclude_max_lines: + include_file = False + break + + if include_file: + filenames.append(ffile) for filename in filenames: diff --git a/fprettify/tests/fortrantests.py b/fprettify/tests/fortrantests.py index 5602ef0..b57168c 100644 --- a/fprettify/tests/fortrantests.py +++ b/fprettify/tests/fortrantests.py @@ -157,12 +157,29 @@ def normalize_line(line): def addtestcode(code_path, options): print(f"creating test cases from {code_path} ...") # dynamically create test cases from fortran files in test directory + + parser = fprettify.get_arg_parser() + args = parser.parse_args(shlex.split(options)) + fprettify_args = fprettify.process_args(args) + for dirpath, _, filenames in os.walk(joinpath(TEST_EXT_DIR, code_path)): for example in [f for f in filenames if any(f.endswith(_) for _ in fprettify.FORTRAN_EXTENSIONS)]: rel_dirpath = os.path.relpath(dirpath, start=TEST_EXT_DIR) - addtestmethod(FprettifyIntegrationTestCase, rel_dirpath, example, options) -def addtestmethod(testcase, fpath, ffile, options): + include_file = True + if args.exclude_max_lines is not None: + line_count = 0 + with open(joinpath(dirpath, example)) as f: + for i in f: + line_count += 1 + if line_count > args.exclude_max_lines: + include_file = False + break + + if include_file: + addtestmethod(FprettifyIntegrationTestCase, rel_dirpath, example, fprettify_args) + +def addtestmethod(testcase, fpath, ffile, args): """add a test method for each example.""" def testmethod(testcase): @@ -191,10 +208,6 @@ def test_result(path, info): # apply fprettify try: - parser = fprettify.get_arg_parser() - args = parser.parse_args(shlex.split(options)) - args = fprettify.process_args(args) - fprettify.reformat_inplace(example, **args) # update outstring