Skip to content

Commit 28b2f89

Browse files
committed
Allow executing tests from outside the suite
If btest.cfg is in the `testing/btest` relative directory, this allows the user to call btest with `btest testing/btest/tests`, which will use the btest.cfg file in testing/btest. It will also check its parents, so you may use `btest testing/btest/language/while.zeek` - for example. The first complication is that previously-valid uses of btest should remain the same. This is done by always checking the current directory for btest.cfg first. The second is if there is a conflict between two tests' btest.cfg file. For example, the following may choose two different btest.cfg files: . └── multiple-cfg ├── Baseline ├── btest.cfg └── tests ├── no-cfg │   └── test2.test └── with-cfg ├── btest.cfg └── tests └── test1.test If there is a conflicting config file, then btest should raise an error, as it can currently only execute with one config file per run.
1 parent 6e11c84 commit 28b2f89

File tree

7 files changed

+122
-13
lines changed

7 files changed

+122
-13
lines changed

btest

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2713,18 +2713,24 @@ def outputDocumentation(tests, fmt):
27132713
print()
27142714

27152715

2716-
# Finds a file, recursing to parent directories if not found
2717-
def find_file(filename, *, recurse):
2718-
current_dir = pathlib.Path.cwd()
2716+
# Finds the config file 'filename', recursing to parent directories from the
2717+
# 'start_dir' if not found.
2718+
def find_config_file(filename, *, start_dir, recurse):
2719+
# First check current dir
2720+
f = pathlib.Path(filename)
2721+
if f.is_file():
2722+
return str(f.absolute())
2723+
2724+
if not recurse:
2725+
return None
2726+
2727+
current_dir = start_dir.absolute()
27192728

27202729
for d in [current_dir, *current_dir.parents]:
27212730
f = d / filename
27222731
if f.is_file():
27232732
return str(f)
27242733

2725-
if not recurse:
2726-
break
2727-
27282734
return None
27292735

27302736

@@ -3008,20 +3014,50 @@ if __name__ == "__main__":
30083014

30093015
(Options, args) = parse_options()
30103016

3011-
btest_cfg = find_file(Options.config, recurse=Options.config == DEFAULT_CONFIG_NAME)
3012-
if not btest_cfg:
3017+
found_configs = set()
3018+
3019+
if args:
3020+
for arg in args:
3021+
if found_config := find_config_file(
3022+
Options.config,
3023+
start_dir=pathlib.Path(arg).absolute(),
3024+
recurse=Options.config == DEFAULT_CONFIG_NAME,
3025+
):
3026+
found_configs.add(found_config)
3027+
else:
3028+
if found_config := find_config_file(
3029+
Options.config,
3030+
start_dir=pathlib.Path.cwd(),
3031+
recurse=Options.config == DEFAULT_CONFIG_NAME,
3032+
):
3033+
found_configs.add(found_config)
3034+
3035+
if len(found_configs) == 0:
30133036
error(f"configuration file '{Options.config}' not found")
30143037

3038+
if len(found_configs) > 1:
3039+
conflicting_configs = ", ".join(
3040+
sorted(f"'{config}'" for config in found_configs)
3041+
)
3042+
error(
3043+
f"cannot execute tests using different configuration files together: {conflicting_configs}"
3044+
)
3045+
3046+
btest_cfg = found_configs.pop()
3047+
3048+
dirname = os.path.dirname(btest_cfg)
3049+
3050+
# Special case for providing just a single path to btest that happens
3051+
# to be the TestBase. Reset args, assuming the user wants to run all tests.
3052+
if len(args) == 1 and os.path.abspath(args[0]) == dirname:
3053+
args = []
3054+
30153055
# The defaults come from environment variables, plus a few additional items.
30163056
defaults = {}
30173057
# Changes to defaults should not change os.environ
30183058
defaults.update(os.environ)
30193059
defaults["default_path"] = os.environ["PATH"]
30203060

3021-
dirname = os.path.dirname(btest_cfg)
3022-
if not dirname:
3023-
dirname = os.getcwd()
3024-
30253061
# If the BTEST_TEST_BASE envirnoment var is set, we'll use that as the testbase.
30263062
# If not, we'll use the config file's directory.
30273063
TestBase = normalize_path(os.environ.get("BTEST_TEST_BASE", dirname))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
2+
## RUNNING NO CFG TEST ALONE
3+
tests.no-cfg.no-cfg ...
4+
> exit 0
5+
... tests.no-cfg.no-cfg ok
6+
all 1 tests successful
7+
## RUNNING WITH CFG TEST ALONE
8+
tests.with-cfg ...
9+
> exit 0
10+
... tests.with-cfg ok
11+
all 1 tests successful
12+
## RUNNING TESTS TOGETHER
13+
cannot execute tests using different configuration files together: '<...>/btest.cfg', '<...>/btest.cfg'
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
2+
## RUNNING DIRECTLY ON DIRECTORY WITH CONFIG:
3+
tests.one.a-relative-test ...
4+
> exit 0
5+
... tests.one.a-relative-test ok
6+
tests.two.another-relative-test ...
7+
> exit 0
8+
... tests.two.another-relative-test ok
9+
all 2 tests successful
10+
## RUNNING DIRECTLY ON TESTS DIRECTORY:
11+
tests.one.a-relative-test ...
12+
> exit 0
13+
... tests.one.a-relative-test ok
14+
tests.two.another-relative-test ...
15+
> exit 0
16+
... tests.two.another-relative-test ok
17+
all 2 tests successful

testing/Scripts/diff-remove-abspath

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@
22
#
33
# Replace absolute paths with the basename.
44

5-
sed 's#[a-zA-Z:]*/\([^/]\{1,\}/\)\{1,\}\([^/]\{1,\}\)#<...>/\2#g'
5+
# The drive letter portion of the Windows regex below is adapted from
6+
# https://github.com/stdlib-js/stdlib/blob/develop/lib/node_modules/%40stdlib/regexp/basename-windows/lib/regexp.js
7+
sed -E 's#/+#/#g' |
8+
sed -E 's#([a-zA-Z]:|[\\/]{2}[^\\/]+[\\/]+[^\\/]+)([\\/])([^ :\\/]{1,}[\\/]){1,}([^ :\\/]{1,})#<...>/\4#g' |
9+
sed -E 's#/([^ :/]{1,}/){1,}([^ :/]{1,})#<...>/\2#g'

testing/btest.tests.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ override=normal
1010
[btest]
1111
TmpDir = `echo .tmp`
1212
BaselineDir = %(testbase)s/Baseline
13+
TestDirs = tests
1314

1415
[environment]
1516
ORIGPATH=%(default_path)s

testing/tests/multiple-cfg-fail.test

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# %TEST-DOC: Test that you can run btest from outside of the directory with btest.cfg
2+
#
3+
# %TEST-EXEC: mkdir -p base/tests/no-cfg
4+
# %TEST-EXEC: mkdir -p base/tests/with-cfg/tests
5+
# %TEST-EXEC: cat %INPUT >> base/tests/no-cfg/no-cfg.test
6+
# %TEST-EXEC: mv btest.cfg base/
7+
# %TEST-EXEC: echo "## RUNNING NO CFG TEST ALONE" >>output
8+
# %TEST-EXEC: btest -v base/tests/no-cfg/no-cfg.test >>output 2>&1
9+
#
10+
# %TEST-EXEC: cp base/btest.cfg base/tests/with-cfg
11+
# %TEST-EXEC: cat %INPUT >> base/tests/with-cfg/tests/with-cfg.test
12+
# %TEST-EXEC: echo "## RUNNING WITH CFG TEST ALONE" >>output
13+
# %TEST-EXEC: btest -v base/tests/with-cfg/tests/with-cfg.test >>output 2>&1
14+
#
15+
# But fail together
16+
# %TEST-EXEC: echo "## RUNNING TESTS TOGETHER" >>output
17+
# %TEST-EXEC-FAIL: btest -v base/tests/no-cfg/no-cfg.test base/tests/with-cfg/tests/with-cfg.test >>output 2>&1
18+
# %TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff output
19+
20+
@TEST-EXEC: exit 0

testing/tests/outside-btest-dir.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# %TEST-DOC: Test that you can run btest from outside of the directory with btest.cfg
2+
#
3+
# %TEST-EXEC: mkdir -p base/tests/one
4+
# %TEST-EXEC: mkdir -p base/tests/two
5+
# %TEST-EXEC: cat %INPUT >> base/tests/one/a-relative-test.test
6+
# %TEST-EXEC: cat %INPUT >> base/tests/two/another-relative-test.test
7+
# %TEST-EXEC: mv btest.cfg base/
8+
# %TEST-EXEC: btest base/tests/one/a-relative-test.test
9+
# %TEST-EXEC: btest base/tests/one/a-relative-test.test base/tests/two/another-relative-test.test
10+
#
11+
# Test the special case of running all tests from outside
12+
# %TEST-EXEC: echo "## RUNNING DIRECTLY ON DIRECTORY WITH CONFIG:" >>output
13+
# %TEST-EXEC: btest -v base/ >>output 2>&1
14+
# %TEST-EXEC: echo "## RUNNING DIRECTLY ON TESTS DIRECTORY:" >>output
15+
# %TEST-EXEC: btest -v base/tests >>output 2>&1
16+
# %TEST-EXEC: btest-diff output
17+
18+
@TEST-EXEC: exit 0

0 commit comments

Comments
 (0)