@@ -52,12 +52,14 @@ def joinpath(path1, path2):
5252RESULT_FILE = joinpath (RESULT_DIR , r'expected_results' )
5353FAILED_FILE = joinpath (RESULT_DIR , r'failed_results' )
5454
55+ TEMP_DIR = joinpath (MYPATH , r'tmp_test_dir/' )
56+
5557RUNSCRIPT = joinpath (MYPATH , r"../../fprettify.py" )
5658
5759fprettify .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,11 +310,183 @@ 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 ()
314+ raise
315+ else :
316+ self .removeTmpDir ()
317+
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 ()
378+
379+ def test_config_stdin (self ):
380+ outstring = []
381+ instring = "CALL alien_invasion( x)"
382+ outstring_with_config = "call alien_invasion(x)"
383+ self .createTmpDir ()
384+
385+ alien_file = joinpath (TEMP_DIR , "alien_invasion.f90" )
386+ config_file = joinpath (os .getcwd (), ".fprettify.rc" )
387+ conf_string = "case=[1,1,1,2]\n exclude=[excluded*]"
388+
389+ if os .path .exists (config_file ):
390+ raise FileException (
391+ "remove file %s" % conf_file_cwd ) # pragma: no cover
392+
393+ def create_file (fname , string ):
394+ with io .open (fname , 'w' , encoding = 'utf-8' ) as infile :
395+ infile .write (string )
396+
397+ try :
398+ create_file (alien_file , instring )
399+ create_file (config_file , conf_string )
400+
401+ # testing stdin --> stdout, with configuration file read from CWD
402+ p1 = subprocess .Popen (RUNSCRIPT ,
403+ stdout = subprocess .PIPE , stdin = subprocess .PIPE )
404+ outstr = p1 .communicate (instring .encode ('UTF-8' ))[0 ].decode ('UTF-8' )
405+ self .assertEqual (outstring_with_config , outstr .strip ())
406+
407+ except : # pragma: no cover
408+ self .removeTmpDir ()
409+ if os .path .isfile (config_file ):
410+ os .remove (config_file )
411+ raise
412+ else :
413+ self .removeTmpDir ()
414+ os .remove (config_file )
415+
416+ def test_config_file (self ):
417+ """simple test for configuration file reading"""
418+
419+ outstring = []
420+ instring = "CALL alien_invasion( x)"
421+ outstring_with_config = "call alien_invasion(x)"
422+ outstring_without_config = "CALL alien_invasion(x)"
423+
424+ self .createTmpDir ()
425+ dirname = TEMP_DIR
426+
427+ alien_file = joinpath (dirname , "alien_invasion.f90" )
428+ excluded_file = joinpath (dirname , "excluded.f90" )
429+ config_file = joinpath (dirname , ".fprettify.rc" )
430+ conf_string = "case=[1,1,1,2]\n exclude=[excluded*]"
431+
432+ excluded_subdir = joinpath (TEMP_DIR , 'excluded_subdir/' )
433+ subdir = joinpath (TEMP_DIR , 'subdir/' )
434+
435+ def create_file (fname , string ):
436+ with io .open (fname , 'w' , encoding = 'utf-8' ) as infile :
437+ infile .write (string )
438+
439+ def check_output_file (fname , str_exp ):
440+ with io .open (fname , 'r' , encoding = 'utf-8' ) as infile :
441+ self .assertEqual (str_exp , infile .read ().strip ())
442+
443+ try :
444+ create_file (alien_file , instring )
445+ create_file (excluded_file , instring )
446+ create_file (config_file , conf_string )
447+
448+ # testing stdin --> stdout
449+ # In this case, the config file will not be read,
450+ # because it is not located in CWD.
451+ p1 = subprocess .Popen (RUNSCRIPT ,
452+ stdout = subprocess .PIPE , stdin = subprocess .PIPE )
453+ outstr = p1 .communicate (instring .encode ('UTF-8' ))[0 ].decode ('UTF-8' )
454+ self .assertEqual (outstring_without_config , outstr .strip ())
455+
456+ # testing file --> stdout
457+ p1 = subprocess .Popen ([RUNSCRIPT , alien_file , '--stdout' ],
458+ stdout = subprocess .PIPE )
459+ outstr = p1 .communicate (instring .encode ('UTF-8' )[0 ])[0 ].decode ('UTF-8' )
460+ self .assertEqual (outstring_with_config , outstr .strip ())
461+
462+ # testing recursive mode
463+ os .mkdir (subdir )
464+ file_in_subdir = joinpath (subdir , 'aliens.F90' )
465+ create_file (file_in_subdir , instring )
466+ config_file_in_subdir = joinpath (subdir , ".fprettify.rc" )
467+ # Config file in subdir should take precedence.
468+ create_file (config_file_in_subdir , "case=[2,2,2,2]" )
469+
470+ os .mkdir (excluded_subdir )
471+ file_in_excluded_subdir = joinpath (excluded_subdir , 'aliens.F90' )
472+ create_file (file_in_excluded_subdir , instring )
473+
474+ p1 = subprocess .Popen ([RUNSCRIPT , '--recursive' , dirname ],
475+ stdout = subprocess .PIPE )
476+ p1 .wait ()
477+
478+ check_output_file (alien_file , outstring_with_config )
479+ # Excluded files and directories should not be touched at all.
480+ check_output_file (excluded_file , instring )
481+ check_output_file (file_in_excluded_subdir , instring )
482+ # subdir contains a different config file which should take precedence.
483+ check_output_file (file_in_subdir , outstring_without_config )
484+
485+ except : # pragma: no cover
486+ self .removeTmpDir ()
294487 raise
295488 else :
296- os . remove ( alien_file )
489+ self . removeTmpDir ( )
297490
298491 def test_multi_alias (self ):
299492 """test for issue #11 (multiple alias and alignment)"""
0 commit comments