Skip to content

Commit 7fbff04

Browse files
committed
WIP FIX: excluded option in config file not respected
The fix in this commit only works for excluding files, not directories. Excluding directories properly requires a bit of a refactor.
1 parent cb25402 commit 7fbff04

File tree

2 files changed

+87
-4
lines changed

2 files changed

+87
-4
lines changed

fprettify/__init__.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,11 +2057,11 @@ def build_ws_dict(args):
20572057
sys.exit(1)
20582058
else:
20592059
if not os.path.exists(directory):
2060-
sys.stderr.write("directory " + directory +
2060+
sys.stderr.write("file " + directory +
20612061
" does not exist!\n")
20622062
sys.exit(1)
2063-
if not os.path.isfile(directory) and directory != '-' and not args.recursive:
2064-
sys.stderr.write("file " + directory + " does not exist!\n")
2063+
if os.path.isdir(directory) and not args.recursive:
2064+
sys.stderr.write("%s is a directory. Use --recursive option\n" % directory)
20652065
sys.exit(1)
20662066

20672067
if not args.recursive:
@@ -2078,6 +2078,7 @@ def build_ws_dict(args):
20782078
for dirpath, dirnames, files in os.walk(directory,topdown=True):
20792079

20802080
# Prune excluded patterns from list of child directories
2081+
# https://stackoverflow.com/a/19859907
20812082
dirnames[:] = [dirname for dirname in dirnames if not any(
20822083
[fnmatch(dirname,exclude_pattern) or fnmatch(os.path.join(dirpath,dirname),exclude_pattern)
20832084
for exclude_pattern in args.exclude]
@@ -2098,8 +2099,17 @@ def build_ws_dict(args):
20982099
filearguments['default_config_files'] = ['~/.fprettify.rc'] + get_config_file_list(os.path.abspath(filename) if filename != '-' else os.getcwd())
20992100
file_argparser = get_arg_parser(filearguments)
21002101
file_args = file_argparser.parse_args(argv[1:])
2101-
ws_dict = build_ws_dict(file_args)
21022102

2103+
# Skip excluded files but not if they we specified explicitly
2104+
# on the command line.
2105+
if filename != directory and any(
2106+
fnmatch(os.path.basename(filename), exclude_pattern)
2107+
for exclude_pattern in file_args.exclude):
2108+
if file_args.debug:
2109+
sys.stderr.write("Skipping excluded file %s\n" % filename)
2110+
continue
2111+
2112+
ws_dict = build_ws_dict(file_args)
21032113
case_dict = {
21042114
'keywords' : file_args.case[0],
21052115
'procedures' : file_args.case[1],

fprettify/tests/__init__.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,79 @@ def test_io(self):
295295
else:
296296
os.remove(alien_file)
297297

298+
299+
def test_config_file(self):
300+
"""simple test for configuration file reading"""
301+
302+
outstring = []
303+
instring = "CALL alien_invasion( x)"
304+
outstring_with_config = "call alien_invasion(x)"
305+
outstring_without_config = "CALL alien_invasion(x)"
306+
307+
dirname = 'test_tmp/'
308+
309+
if os.path.lexists(dirname):
310+
raise AlienInvasion(
311+
"remove directory test_tmp") # pragma: no cover
312+
313+
os.mkdir(dirname)
314+
alien_file = os.path.join(dirname, "alien_invasion.f90")
315+
excluded_file = os.path.join(dirname, "excluded.f90")
316+
config_file = os.path.join(dirname, ".fprettify.rc")
317+
configuration = "case=[1,1,1,2]\nexclude=[excluded.f90]"
318+
319+
try:
320+
with io.open(alien_file, 'w', encoding='utf-8') as infile:
321+
infile.write(instring)
322+
323+
with io.open(excluded_file, 'w', encoding='utf-8') as infile:
324+
infile.write(instring)
325+
326+
with io.open(config_file, 'w', encoding='utf-8') as infile:
327+
infile.write(configuration)
328+
329+
# testing stdin --> stdout
330+
# In this case, the config file will not be read,
331+
# because it is not located in CWD.
332+
p1 = subprocess.Popen(RUNSCRIPT,
333+
stdout=subprocess.PIPE, stdin=subprocess.PIPE)
334+
outstr = p1.communicate(instring.encode('UTF-8'))[0].decode('UTF-8')
335+
self.assertEqual(outstring_without_config, outstr.strip())
336+
337+
# testing file --> stdout
338+
p1 = subprocess.Popen([RUNSCRIPT, alien_file, '--stdout'],
339+
stdout=subprocess.PIPE)
340+
outstr = p1.communicate(instring.encode('UTF-8')[0])[0].decode('UTF-8')
341+
self.assertEqual(outstring_with_config, outstr.strip())
342+
343+
# testing recursive mode
344+
p1 = subprocess.Popen([RUNSCRIPT, '--recursive', dirname],
345+
stdout=subprocess.PIPE)
346+
p1.wait()
347+
348+
with io.open(alien_file, 'r', encoding='utf-8') as infile:
349+
self.assertEqual(outstring_with_config, infile.read().strip())
350+
351+
# Excluded file should not be touched at all.
352+
with io.open(excluded_file, 'r', encoding='utf-8') as infile:
353+
self.assertEqual(instring, infile.read().strip())
354+
355+
except: # pragma: no cover
356+
if os.path.isfile(alien_file):
357+
os.remove(alien_file)
358+
if os.path.isfile(excluded_file):
359+
os.remove(excluded_file)
360+
if os.path.isfile(config_file):
361+
os.remove(config_file)
362+
if os.path.isdir(dirname):
363+
os.rmdir(dirname)
364+
raise
365+
else:
366+
os.remove(alien_file)
367+
os.remove(excluded_file)
368+
os.remove(config_file)
369+
os.rmdir(dirname)
370+
298371
def test_multi_alias(self):
299372
"""test for issue #11 (multiple alias and alignment)"""
300373
instring="use A,only:B=>C,&\nD=>E"

0 commit comments

Comments
 (0)