Skip to content

Commit bea75ad

Browse files
committed
Test refactor and add test for recursive mode.
1 parent 7fbff04 commit bea75ad

File tree

1 file changed

+115
-41
lines changed

1 file changed

+115
-41
lines changed

fprettify/tests/__init__.py

Lines changed: 115 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,14 @@ def joinpath(path1, path2):
5252
RESULT_FILE = joinpath(RESULT_DIR, r'expected_results')
5353
FAILED_FILE = joinpath(RESULT_DIR, r'failed_results')
5454

55+
TEMP_DIR = joinpath(MYPATH, r'tmp_test_dir/')
56+
5557
RUNSCRIPT = joinpath(MYPATH, r"../../fprettify.py")
5658

5759
fprettify.set_fprettify_logger(logging.ERROR)
5860

5961

60-
class AlienInvasion(Exception):
62+
class FileException(Exception):
6163
"""Should not happen"""
6264
pass
6365

@@ -85,6 +87,27 @@ def setUp(self):
8587
"""
8688
self.maxDiff = None
8789

90+
def createTmpDir(self):
91+
"""
92+
Create a temporary directory for IO tests
93+
"""
94+
if os.path.lexists(TEMP_DIR):
95+
raise FileException(
96+
"remove directory %s" % TEMP_DIR) # pragma: no cover
97+
os.mkdir(TEMP_DIR)
98+
99+
def removeTmpDir(self):
100+
"""
101+
Remove the temporary test directory and all its content.
102+
"""
103+
if not os.path.isdir(TEMP_DIR):
104+
return
105+
106+
for dirpath, _, files in os.walk(TEMP_DIR, topdown=False):
107+
for f in files:
108+
os.remove(joinpath(dirpath, f))
109+
os.rmdir(dirpath)
110+
88111
@classmethod
89112
def setUpClass(cls):
90113
"""
@@ -258,10 +281,8 @@ def test_io(self):
258281
instring = "CALL alien_invasion( 👽 )"
259282
outstring_exp = "CALL alien_invasion(👽)"
260283

261-
alien_file = "alien_invasion.f90"
262-
if os.path.isfile(alien_file):
263-
raise AlienInvasion(
264-
"remove file alien_invasion.f90") # pragma: no cover
284+
self.createTmpDir()
285+
alien_file = joinpath(TEMP_DIR, "alien_invasion.f90")
265286

266287
try:
267288
with io.open(alien_file, 'w', encoding='utf-8') as infile:
@@ -289,12 +310,71 @@ def test_io(self):
289310
for outstr in outstring:
290311
self.assertEqual(outstring_exp, outstr.strip())
291312
except: # pragma: no cover
292-
if os.path.isfile(alien_file):
293-
os.remove(alien_file)
313+
self.removeTmpDir()
294314
raise
295315
else:
296-
os.remove(alien_file)
316+
self.removeTmpDir()
297317

318+
def test_recursive_mode(self):
319+
"""test recursive mode which finds all fortran files in the tree"""
320+
321+
instring = "CALL alien_invasion( x)"
322+
formatted_string = "CALL alien_invasion(x)"
323+
324+
self.createTmpDir()
325+
326+
# We will create the following paths inside TEMP_DIR
327+
# - alien_file.f90
328+
# - excluded_alien_file.f90
329+
# - subdir/alien_invasion.f90
330+
# - subdir/excluded_alien_invasion.f90
331+
# - excluded_subdir/alien_invasion.f90
332+
alien_file = "alien_invasion.f90"
333+
excluded_file = "excluded_alien_invasion.f90"
334+
subdir = joinpath(TEMP_DIR, "subdir/")
335+
excluded_subdir = joinpath(TEMP_DIR, "excluded_subdir/")
336+
os.mkdir(subdir)
337+
os.mkdir(excluded_subdir)
338+
339+
def create_file(fname):
340+
with io.open(fname, 'w', encoding='utf-8') as infile:
341+
infile.write(instring)
342+
343+
def check_output_file(fname, str_exp):
344+
with io.open(fname, 'r', encoding='utf-8') as infile:
345+
self.assertEqual(str_exp, infile.read().strip())
346+
347+
try:
348+
create_file(joinpath(TEMP_DIR, alien_file))
349+
create_file(joinpath(TEMP_DIR, excluded_file))
350+
create_file(joinpath(subdir, alien_file))
351+
create_file(joinpath(subdir, excluded_file))
352+
create_file(joinpath(excluded_subdir, alien_file))
353+
354+
p1 = subprocess.Popen([
355+
RUNSCRIPT,
356+
'--recursive',
357+
'-e', 'excluded_*',
358+
TEMP_DIR],
359+
stdout=subprocess.PIPE)
360+
p1.wait()
361+
362+
# Check files that should be formatted.
363+
check_output_file(joinpath(TEMP_DIR, alien_file), formatted_string)
364+
check_output_file(joinpath(subdir, alien_file), formatted_string)
365+
366+
# Check excluded files.
367+
check_output_file(joinpath(TEMP_DIR, excluded_file), instring)
368+
check_output_file(joinpath(subdir, excluded_file), instring)
369+
370+
# Check excluded directory.
371+
check_output_file(joinpath(excluded_subdir, alien_file), instring)
372+
373+
except: # pragma: no cover
374+
self.removeTmpDir()
375+
raise
376+
else:
377+
self.removeTmpDir()
298378

299379
def test_config_file(self):
300380
"""simple test for configuration file reading"""
@@ -304,27 +384,31 @@ def test_config_file(self):
304384
outstring_with_config = "call alien_invasion(x)"
305385
outstring_without_config = "CALL alien_invasion(x)"
306386

307-
dirname = 'test_tmp/'
387+
self.createTmpDir()
388+
dirname = TEMP_DIR
308389

309-
if os.path.lexists(dirname):
310-
raise AlienInvasion(
311-
"remove directory test_tmp") # pragma: no cover
390+
alien_file = joinpath(dirname, "alien_invasion.f90")
391+
excluded_file = joinpath(dirname, "excluded.f90")
392+
config_file = joinpath(dirname, ".fprettify.rc")
393+
conf_string = "case=[1,1,1,2]\nexclude=[excluded*]"
312394

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]"
395+
def create_file(fname, string):
396+
with io.open(fname, 'w', encoding='utf-8') as infile:
397+
infile.write(string)
318398

319-
try:
320-
with io.open(alien_file, 'w', encoding='utf-8') as infile:
321-
infile.write(instring)
399+
def check_output_file(fname, str_exp):
400+
with io.open(fname, 'r', encoding='utf-8') as infile:
401+
self.assertEqual(str_exp, infile.read().strip())
322402

323-
with io.open(excluded_file, 'w', encoding='utf-8') as infile:
324-
infile.write(instring)
403+
try:
404+
create_file(alien_file, instring)
405+
create_file(excluded_file, instring)
406+
create_file(config_file, conf_string)
325407

326-
with io.open(config_file, 'w', encoding='utf-8') as infile:
327-
infile.write(configuration)
408+
excluded_subdir = joinpath(TEMP_DIR, 'excluded_subdir/')
409+
os.mkdir(excluded_subdir)
410+
file_in_excluded_subdir = joinpath(excluded_subdir, 'aliens.F90')
411+
create_file(file_in_excluded_subdir, instring)
328412

329413
# testing stdin --> stdout
330414
# In this case, the config file will not be read,
@@ -345,28 +429,18 @@ def test_config_file(self):
345429
stdout=subprocess.PIPE)
346430
p1.wait()
347431

348-
with io.open(alien_file, 'r', encoding='utf-8') as infile:
349-
self.assertEqual(outstring_with_config, infile.read().strip())
350-
432+
check_output_file(alien_file, outstring_with_config)
351433
# 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())
434+
check_output_file(excluded_file, instring)
435+
# File in excluded directory should not be touched at all.
436+
# TODO: Make this work!
437+
#check_output_file(file_in_excluded_subdir, instring)
354438

355439
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)
440+
self.removeTmpDir()
364441
raise
365442
else:
366-
os.remove(alien_file)
367-
os.remove(excluded_file)
368-
os.remove(config_file)
369-
os.rmdir(dirname)
443+
self.removeTmpDir()
370444

371445
def test_multi_alias(self):
372446
"""test for issue #11 (multiple alias and alignment)"""

0 commit comments

Comments
 (0)