Skip to content

Commit

Permalink
Setting limit to number of lines per file (to speedup cron tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
pseewald committed May 11, 2024
1 parent 72bc6ce commit 24e71d9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
2 changes: 1 addition & 1 deletion fortran_tests/testsuites.config
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
22 changes: 18 additions & 4 deletions fprettify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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:

Expand Down
25 changes: 19 additions & 6 deletions fprettify/tests/fortrantests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 24e71d9

Please sign in to comment.