@@ -52,12 +52,14 @@ def joinpath(path1, path2):
52
52
RESULT_FILE = joinpath (RESULT_DIR , r'expected_results' )
53
53
FAILED_FILE = joinpath (RESULT_DIR , r'failed_results' )
54
54
55
+ TEMP_DIR = joinpath (MYPATH , r'tmp_test_dir/' )
56
+
55
57
RUNSCRIPT = joinpath (MYPATH , r"../../fprettify.py" )
56
58
57
59
fprettify .set_fprettify_logger (logging .ERROR )
58
60
59
61
60
- class AlienInvasion (Exception ):
62
+ class FileException (Exception ):
61
63
"""Should not happen"""
62
64
pass
63
65
@@ -85,6 +87,27 @@ def setUp(self):
85
87
"""
86
88
self .maxDiff = None
87
89
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
+
88
111
@classmethod
89
112
def setUpClass (cls ):
90
113
"""
@@ -258,10 +281,8 @@ def test_io(self):
258
281
instring = "CALL alien_invasion( 👽 )"
259
282
outstring_exp = "CALL alien_invasion(👽)"
260
283
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" )
265
286
266
287
try :
267
288
with io .open (alien_file , 'w' , encoding = 'utf-8' ) as infile :
@@ -289,11 +310,146 @@ def test_io(self):
289
310
for outstr in outstring :
290
311
self .assertEqual (outstring_exp , outstr .strip ())
291
312
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_file (self ):
380
+ """simple test for configuration file reading"""
381
+
382
+ outstring = []
383
+ instring = "CALL alien_invasion( x)"
384
+ outstring_with_config = "call alien_invasion(x)"
385
+ outstring_without_config = "CALL alien_invasion(x)"
386
+
387
+ self .createTmpDir ()
388
+ dirname = TEMP_DIR
389
+
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]\n exclude=[excluded*]"
394
+
395
+ excluded_subdir = joinpath (TEMP_DIR , 'excluded_subdir/' )
396
+ subdir = joinpath (TEMP_DIR , 'subdir/' )
397
+
398
+ def create_file (fname , string ):
399
+ with io .open (fname , 'w' , encoding = 'utf-8' ) as infile :
400
+ infile .write (string )
401
+
402
+ def check_output_file (fname , str_exp ):
403
+ with io .open (fname , 'r' , encoding = 'utf-8' ) as infile :
404
+ self .assertEqual (str_exp , infile .read ().strip ())
405
+
406
+ try :
407
+ create_file (alien_file , instring )
408
+ create_file (excluded_file , instring )
409
+ create_file (config_file , conf_string )
410
+
411
+ # testing stdin --> stdout
412
+ # In this case, the config file will not be read,
413
+ # because it is not located in CWD.
414
+ p1 = subprocess .Popen (RUNSCRIPT ,
415
+ stdout = subprocess .PIPE , stdin = subprocess .PIPE )
416
+ outstr = p1 .communicate (instring .encode ('UTF-8' ))[0 ].decode ('UTF-8' )
417
+ self .assertEqual (outstring_without_config , outstr .strip ())
418
+
419
+ # testing file --> stdout
420
+ p1 = subprocess .Popen ([RUNSCRIPT , alien_file , '--stdout' ],
421
+ stdout = subprocess .PIPE )
422
+ outstr = p1 .communicate (instring .encode ('UTF-8' )[0 ])[0 ].decode ('UTF-8' )
423
+ self .assertEqual (outstring_with_config , outstr .strip ())
424
+
425
+ # testing recursive mode
426
+ os .mkdir (subdir )
427
+ file_in_subdir = joinpath (subdir , 'aliens.F90' )
428
+ create_file (file_in_subdir , instring )
429
+ config_file_in_subdir = joinpath (subdir , ".fprettify.rc" )
430
+ # Config file in subdir should take precedence.
431
+ create_file (config_file_in_subdir , "case=[2,2,2,2]" )
432
+
433
+ os .mkdir (excluded_subdir )
434
+ file_in_excluded_subdir = joinpath (excluded_subdir , 'aliens.F90' )
435
+ create_file (file_in_excluded_subdir , instring )
436
+
437
+ p1 = subprocess .Popen ([RUNSCRIPT , '--recursive' , dirname ],
438
+ stdout = subprocess .PIPE )
439
+ p1 .wait ()
440
+
441
+ check_output_file (alien_file , outstring_with_config )
442
+ # Excluded files and directories should not be touched at all.
443
+ check_output_file (excluded_file , instring )
444
+ check_output_file (file_in_excluded_subdir , instring )
445
+ # subdir contains a different config file which should take precedence.
446
+ check_output_file (file_in_subdir , outstring_without_config )
447
+
448
+ except : # pragma: no cover
449
+ self .removeTmpDir ()
294
450
raise
295
451
else :
296
- os . remove ( alien_file )
452
+ self . removeTmpDir ( )
297
453
298
454
def test_multi_alias (self ):
299
455
"""test for issue #11 (multiple alias and alignment)"""
0 commit comments