@@ -61,10 +61,9 @@ VERSION = "1.2-9" # Automatically filled in.
61
61
Name = "btest"
62
62
Config = None
63
63
64
- try :
65
- ConfigDefault = os .environ ["BTEST_CFG" ]
66
- except KeyError :
67
- ConfigDefault = "btest.cfg"
64
+ DEFAULT_CONFIG_NAME = "btest.cfg"
65
+
66
+ ConfigDefault = os .environ .get ("BTEST_CFG" , DEFAULT_CONFIG_NAME )
68
67
69
68
70
69
def normalize_path (path ):
@@ -2708,6 +2707,21 @@ def outputDocumentation(tests, fmt):
2708
2707
print ()
2709
2708
2710
2709
2710
+ # Finds a file, recursing to parent directories if not found
2711
+ def find_file (filename , * , recurse ):
2712
+ current_dir = pathlib .Path .cwd ()
2713
+
2714
+ for d in [current_dir , * current_dir .parents ]:
2715
+ f = d / filename
2716
+ if f .is_file ():
2717
+ return str (f )
2718
+
2719
+ if not recurse :
2720
+ break
2721
+
2722
+ return None
2723
+
2724
+
2711
2725
def parse_options ():
2712
2726
optparser = optparse .OptionParser (
2713
2727
usage = "%prog [options] <directories>" , version = VERSION
@@ -2950,9 +2964,6 @@ def parse_options():
2950
2964
warning ("ignoring requested parallelism in interactive-update mode" )
2951
2965
options .threads = 1
2952
2966
2953
- if not os .path .exists (options .config ):
2954
- error (f"configuration file '{ options .config } ' not found" )
2955
-
2956
2967
return options , parsed_args
2957
2968
2958
2969
@@ -2991,18 +3002,22 @@ if __name__ == "__main__":
2991
3002
2992
3003
(Options , args ) = parse_options ()
2993
3004
3005
+ btest_cfg = find_file (Options .config , recurse = Options .config == DEFAULT_CONFIG_NAME )
3006
+ if not btest_cfg :
3007
+ error (f"configuration file '{ Options .config } ' not found" )
3008
+
2994
3009
# The defaults come from environment variables, plus a few additional items.
2995
3010
defaults = {}
2996
3011
# Changes to defaults should not change os.environ
2997
3012
defaults .update (os .environ )
2998
3013
defaults ["default_path" ] = os .environ ["PATH" ]
2999
3014
3000
- dirname = os .path .dirname (Options . config )
3015
+ dirname = os .path .dirname (btest_cfg )
3001
3016
if not dirname :
3002
3017
dirname = os .getcwd ()
3003
3018
3004
3019
# If the BTEST_TEST_BASE envirnoment var is set, we'll use that as the testbase.
3005
- # If not, we'll use the current directory.
3020
+ # If not, we'll use the config file's directory.
3006
3021
TestBase = normalize_path (os .environ .get ("BTEST_TEST_BASE" , dirname ))
3007
3022
defaults ["testbase" ] = TestBase
3008
3023
defaults ["baselinedir" ] = normalize_path (
@@ -3012,7 +3027,7 @@ if __name__ == "__main__":
3012
3027
3013
3028
# Parse our config
3014
3029
Config = getcfgparser (defaults )
3015
- Config .read (Options . config , encoding = "utf-8" )
3030
+ Config .read (btest_cfg , encoding = "utf-8" )
3016
3031
3017
3032
defaults ["baselinedir" ] = getOption ("BaselineDir" , defaults ["baselinedir" ])
3018
3033
@@ -3044,7 +3059,7 @@ if __name__ == "__main__":
3044
3059
# At this point, our defaults have changed, so we
3045
3060
# reread the configuration.
3046
3061
new_config = getcfgparser (defaults )
3047
- new_config .read (Options . config )
3062
+ new_config .read (btest_cfg )
3048
3063
return new_config , value
3049
3064
3050
3065
return Config , default
0 commit comments