Skip to content

Commit 70d42c7

Browse files
committed
Check parent directories for btest.cfg
1 parent 1092e9c commit 70d42c7

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

README

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -414,10 +414,11 @@ Configuration
414414
-------------
415415

416416
Specifics of ``btest``'s execution can be tuned with a configuration
417-
file, which by default is ``btest.cfg`` if that's found in the
418-
current directory. It can alternatively be specified with the
419-
``--config`` command line option, or a ``BTEST_CFG`` environment
420-
variable. The configuration file is
417+
file, which by default is ``btest.cfg`` if that's found in the current
418+
directory or parent directories. It can alternatively be specified
419+
with the ``--config`` command line option (which will not search
420+
parent directories), or a ``BTEST_CFG`` environment variable.
421+
The configuration file is
421422
"INI-style", and an example comes with the distribution, see
422423
``btest.cfg.example``. A configuration file has one main section,
423424
``btest``, that defines most options; as well as an optional section

btest

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,9 @@ VERSION = "1.2-9" # Automatically filled in.
6161
Name = "btest"
6262
Config = None
6363

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)
6867

6968

7069
def normalize_path(path):
@@ -2708,6 +2707,21 @@ def outputDocumentation(tests, fmt):
27082707
print()
27092708

27102709

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+
27112725
def parse_options():
27122726
optparser = optparse.OptionParser(
27132727
usage="%prog [options] <directories>", version=VERSION
@@ -2950,9 +2964,6 @@ def parse_options():
29502964
warning("ignoring requested parallelism in interactive-update mode")
29512965
options.threads = 1
29522966

2953-
if not os.path.exists(options.config):
2954-
error(f"configuration file '{options.config}' not found")
2955-
29562967
return options, parsed_args
29572968

29582969

@@ -2991,18 +3002,22 @@ if __name__ == "__main__":
29913002

29923003
(Options, args) = parse_options()
29933004

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+
29943009
# The defaults come from environment variables, plus a few additional items.
29953010
defaults = {}
29963011
# Changes to defaults should not change os.environ
29973012
defaults.update(os.environ)
29983013
defaults["default_path"] = os.environ["PATH"]
29993014

3000-
dirname = os.path.dirname(Options.config)
3015+
dirname = os.path.dirname(btest_cfg)
30013016
if not dirname:
30023017
dirname = os.getcwd()
30033018

30043019
# 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.
30063021
TestBase = normalize_path(os.environ.get("BTEST_TEST_BASE", dirname))
30073022
defaults["testbase"] = TestBase
30083023
defaults["baselinedir"] = normalize_path(
@@ -3012,7 +3027,7 @@ if __name__ == "__main__":
30123027

30133028
# Parse our config
30143029
Config = getcfgparser(defaults)
3015-
Config.read(Options.config, encoding="utf-8")
3030+
Config.read(btest_cfg, encoding="utf-8")
30163031

30173032
defaults["baselinedir"] = getOption("BaselineDir", defaults["baselinedir"])
30183033

@@ -3044,7 +3059,7 @@ if __name__ == "__main__":
30443059
# At this point, our defaults have changed, so we
30453060
# reread the configuration.
30463061
new_config = getcfgparser(defaults)
3047-
new_config.read(Options.config)
3062+
new_config.read(btest_cfg)
30483063
return new_config, value
30493064

30503065
return Config, default

testing/Baseline/tests.btest-cfg/nopath

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ btest-cfg ... ok
55
all 1 tests successful
66
btest-cfg ... ok
77
all 1 tests successful
8-
configuration file 'btest.cfg' not found
8+
configuration file 'nonexistant' not found

testing/tests/btest-cfg.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# %TEST-EXEC: btest -c myfile %INPUT > nopath 2>&1
33
# %TEST-EXEC: BTEST_CFG=myfile btest %INPUT >> nopath 2>&1
44
# %TEST-EXEC: BTEST_CFG=notexist btest -c myfile %INPUT >> nopath 2>&1
5-
# %TEST-EXEC-FAIL: btest %INPUT >> nopath 2>&1
5+
# %TEST-EXEC-FAIL: btest -c nonexistant %INPUT >> nopath 2>&1
66
# %TEST-EXEC: btest-diff nopath
77
# %TEST-EXEC: mkdir z
88
# %TEST-EXEC: mv myfile z/btest.cfg

0 commit comments

Comments
 (0)