@@ -703,6 +703,30 @@ def test_config_precedence():
703703 assert cfg (f = LOCAL )['pytest' ]['precedence' ] == 'local'
704704
705705
706+ @pytest .mark .skipif (
707+ WINDOWS or (os .geteuid () == 0 ),
708+ reason = "Non-readable files do not exist on Windows, and root user can always read files" ,
709+ )
710+ def test_config_non_readable_file (config_tmpdir ):
711+ # test to read a config file without read permission
712+ cwd = pathlib .Path .cwd ()
713+
714+ # create a readable file
715+ config_readable = cwd / 'readable'
716+ config_readable .touch ()
717+
718+ # create a non-readable file
719+ config_non_readable = cwd / 'non-readable'
720+ config_non_readable .touch ()
721+ config_non_readable .chmod (0o000 )
722+
723+ # trying to use a non-readable config should result in according error
724+ with update_env ({'WEST_CONFIG_GLOBAL' : f'{ config_readable } { os .pathsep } { config_non_readable } ' }):
725+ _ , stderr = cmd_raises ('config --global some.section' , MalformedConfig )
726+ expected = f"Error while reading one of '{ [str (config_readable ), str (config_non_readable )]} '"
727+ assert expected in stderr
728+
729+
706730def test_config_multiple (config_tmpdir ):
707731 # Verify that local settings take precedence over global ones,
708732 # but that both values are still available, and that setting
@@ -747,16 +771,6 @@ def run_and_assert(expected_values: dict[str, dict[str, str]]):
747771 write_config (config_l1 , 'sec' , 'l' , '1' , 'l1' , '1' )
748772 write_config (config_l2 , 'sec' , 'l' , '2' , 'l2' , '2' )
749773
750- # config file without read permission (does not work on Windows)
751- if not WINDOWS :
752- config_non_readable = config_dir / 'non-readable'
753- config_non_readable .touch ()
754- config_non_readable .chmod (0o000 )
755- with update_env ({'WEST_CONFIG_GLOBAL' : f'{ config_g1 } { os .pathsep } { config_non_readable } ' }):
756- _ , stderr = cmd_raises ('config --global some.section' , MalformedConfig )
757- expected = f"Error while reading one of '{ [str (config_g1 ), str (config_non_readable )]} '"
758- assert expected in stderr
759-
760774 # specify multiple configs for each config level (separated by os.pathsep)
761775 os .environ ["WEST_CONFIG_GLOBAL" ] = f'{ config_g1 } { os .pathsep } { config_g2 } '
762776 os .environ ["WEST_CONFIG_SYSTEM" ] = f'{ config_s1 } { os .pathsep } { config_s2 } '
0 commit comments