11#!/usr/bin/env python3
22"""Test cfg file documentation matches cfg init"""
33
4- from os import system
54from os import stat
6- from os .path import join
7- from os .path import exists
8- from os .path import basename
9- from os .path import relpath
5+ import os .path as op
6+ from pathlib import Path
107from sys import argv
118import filecmp
129from pmidcite .cfg import Cfg
1310from pmidcite .cfgini import wr_rcfile
1411from tests .icite import DIR_REPO
12+ from tests .utils import cat
1513
1614
1715# pylint: disable=line-too-long
1816def test_cfg_desecriptive ():
1917 """Test default cfg file that contains detailed comments"""
20- file_cfg = join (DIR_REPO , 'test_icite.cfg' )
18+ file_cfg = op . join (DIR_REPO , 'test_icite.cfg' )
2119
2220 # Remove test configuration file and test that it does not exist
23- system ( f'rm -f { file_cfg } ' )
24- assert not exists (file_cfg )
21+ Path ( file_cfg ). unlink ( missing_ok = True )
22+ assert not op . exists (file_cfg )
2523
2624 # Test that non-existing configuration file can not be read by Cfg
2725 obj = Cfg (check = False , prt_fullname = False )
2826 obj .set_cfg (file_cfg )
2927 assert not obj .rd_rc ()
3028
3129 # Write configuration file. Test that an exiting cfg file is not overwritten
32- system ( f' cat { file_cfg } ' )
30+ cat ( file_cfg )
3331 assert wr_rcfile (file_cfg , force = False )
34- system ( f' cat { file_cfg } ' )
35- assert exists (file_cfg )
32+ cat ( file_cfg )
33+ assert op . exists (file_cfg )
3634 assert not wr_rcfile (file_cfg , force = False )
3735
3836 # Read configuration file, file_cfg
@@ -42,46 +40,46 @@ def test_cfg_desecriptive():
4240 f'UNEXPECTED FILENAME: EXP({ file_cfg } ) ACT({ next (iter (cfg ))} )'
4341
4442 # Test that the values for the new cfg file are the default values
45- fin_base = basename (file_cfg )
43+ fin_base = op . basename (file_cfg )
4644 for key , actual in obj .cfgparser ['pmidcite' ].items ():
4745 print (f'{ fin_base } : { key } { actual } ' )
4846 expected = Cfg .dfltdct ['pmidcite' ][key ]
4947 assert actual == expected , \
5048 f'{ file_cfg } : KEY({ key } ) ACTUAL({ actual } ) != EXPECTED({ expected } )'
5149 print (stat (file_cfg ))
52- system ( f'rm { file_cfg } ' )
50+ Path ( file_cfg ). unlink ( missing_ok = True )
5351 print ('PASSED: cfg init with comments' )
5452
5553
5654def test_cfg_example (update_example = False ):
5755 """Test that the config file example is up-to-date"""
58- fin_cfg = join (DIR_REPO , 'test_icite.cfg' )
59- fin_ex = join (DIR_REPO , 'doc/example_cfg/.pmidciterc' )
56+ fin_cfg = op . join (DIR_REPO , 'test_icite.cfg' )
57+ fin_ex = op . join (DIR_REPO , 'doc/example_cfg/.pmidciterc' )
6058 wr_rcfile (fin_cfg , force = True )
6159 wr_rcfile (fin_ex , force = update_example )
6260 # Compare the contents of the two files
6361 assert filecmp .cmp (fin_cfg , fin_ex , shallow = False ), \
64- f'EXP({ relpath (fin_ex )} ) != ACT({ relpath (fin_cfg )} )'
65- system ( 'rm { fin_cfg}' )
62+ f'EXP({ op . relpath (fin_ex )} ) != ACT({ op . relpath (fin_cfg )} )'
63+ Path ( fin_cfg ). unlink ( missing_ok = True )
6664 print ('PASSED: Cfg example matches cfg default' )
6765
6866
6967def test_cfg_icite ():
7068 """Test cfg file documentation matches cfg init"""
7169 # NIH iCite configuration file
72- file_cfg = join (DIR_REPO , 'test_icite.cfg' )
70+ file_cfg = op . join (DIR_REPO , 'test_icite.cfg' )
7371
7472 # Remove test configuration file and test that it can no longer be read by Cfg
75- system ( f'rm -f { file_cfg } ' )
73+ Path ( file_cfg ). unlink ( missing_ok = True )
7674 obj = Cfg (check = False , prt_fullname = False )
7775 obj .set_cfg (file_cfg )
7876 assert not obj .rd_rc ()
7977
8078 # Write configuration file
81- system ( f' cat { file_cfg } ' )
79+ cat ( file_cfg )
8280 assert obj .wr_rc ()
83- system ( ' cat { file_cfg}' )
84- assert exists (file_cfg )
81+ cat ( file_cfg )
82+ assert op . exists (file_cfg )
8583 assert not obj .wr_rc ()
8684
8785 # Read configuration file
@@ -95,21 +93,21 @@ def test_cfg_icite():
9593 f"EXP({ Cfg .dfltdct ['pmidcite' ]['dir_icite_py' ]} ) "
9694 f"ACT({ obj .cfgparser ['pmidcite' ]['dir_icite_py' ]} )" )
9795 assert obj .cfgparser ['pmidcite' ]['dir_pubmed_txt' ] == Cfg .dfltdct ['pmidcite' ]['dir_pubmed_txt' ]
98- system ( f'rm { file_cfg } ' )
96+ Path ( file_cfg ). unlink ( missing_ok = True )
9997 print ('PASSED: cfg init with no comments' )
10098
10199
102100def test_cfg_eutils ():
103101 """Test writing and reading the configuration file"""
104102 # NIH iCite configuration file
105- file_cfg = join (DIR_REPO , 'test_eutils.cfg' )
103+ file_cfg = op . join (DIR_REPO , 'test_eutils.cfg' )
106104 obj = Cfg (check = False )
107105 obj .set_cfg (file_cfg )
108106
109107 # Write configuration file
110- system ( f'rm -f { file_cfg } ' )
108+ Path ( file_cfg ). unlink ( missing_ok = True )
111109 assert obj .wr_rc ()
112- assert exists (file_cfg )
110+ assert op . exists (file_cfg )
113111 assert not obj .wr_rc ()
114112
115113 # Newly created cfg file should have default values for private data
@@ -119,7 +117,7 @@ def test_cfg_eutils():
119117 assert obj .cfgparser ['pmidcite' ]['apikey' ] == Cfg .dfltdct ['pmidcite' ]['apikey' ]
120118 assert obj .cfgparser ['pmidcite' ]['tool' ] == Cfg .dfltdct ['pmidcite' ]['tool' ]
121119
122- system ( f'rm { file_cfg } ' )
120+ Path ( file_cfg ). unlink ( missing_ok = True )
123121 print ('PASSED: cfg init private values are default' )
124122
125123
0 commit comments