From 9e109acada8c6952592acf038ca7107d002b3d19 Mon Sep 17 00:00:00 2001 From: Arne Welzel Date: Wed, 10 Dec 2025 15:16:20 +0100 Subject: [PATCH] Skip interpolation checks for non-selected alternative sections In #122 there's spurious stderr output for Spicy's test suite reported. This is due to the new code from #121 eagerly reading all keys in all sections, even if some, like [environment-] sections for alternatives, would never be used if not selected explicitly. Move the interpolation check below Options.alternative wrangling and skip sections that won't be used. This squelches the spurious stderr output. I've added a test, though it's a bit opaque. --- btest | 44 ++++++++++++------- .../output | 13 ++++++ .../test.out | 6 +++ .../btest-cfg-skip-non-selected-env.test | 29 ++++++++++++ 4 files changed, 77 insertions(+), 15 deletions(-) create mode 100644 testing/Baseline/tests.btest-cfg-skip-non-selected-env/output create mode 100644 testing/Baseline/tests.btest-cfg-skip-non-selected-env/test.out create mode 100644 testing/tests/btest-cfg-skip-non-selected-env.test diff --git a/btest b/btest index 94ca3c42..36604119 100755 --- a/btest +++ b/btest @@ -3123,20 +3123,6 @@ if __name__ == "__main__": Config = getcfgparser(defaults) Config.read(btest_cfg, encoding="utf-8") - # Make sure at this point that any Python-level %()s substitution patterns - # resolve. Cleaner to error now than in mid-processing when we retrieve an - # option. We use itemsNoDefaults() for this since falling back on defaults - # pulls in all of os.environ, some of which may well not be formatted for - # Python substitution. - try: - for section in Config.sections(): - for _, _ in Config.itemsNoDefaults(section): - pass - except configparser.InterpolationError as err: - error( - f"{btest_cfg} contains invalid Python configparser interpolation in {err.section}.{err.option}: {err}" - ) - defaults["baselinedir"] = getOption("BaselineDir", defaults["baselinedir"]) min_version = getOption("MinVersion", None) @@ -3144,7 +3130,7 @@ if __name__ == "__main__": validate_version_requirement(min_version, VERSION) if Options.alternatives: - # Preprocess to split into list. "-" refers to the default setup, as a + # Preprocess to split into list. "-" refers to the default alternative, as a # shorthand for "default", to allow combination with select alternatives. Options.alternatives = [alt.strip() for alt in Options.alternatives.split(",")] Options.alternatives = [ @@ -3188,6 +3174,34 @@ if __name__ == "__main__": None, transform=lambda x: normalize_path(x), ) + else: + Options.alternatives = [] + + # Make sure at this point that any Python-level %()s substitution patterns + # resolve. Cleaner to error now than in mid-processing when we retrieve an + # option. We use itemsNoDefaults() for this since falling back on defaults + # pulls in all of os.environ, some of which may well not be formatted for + # Python substitution. + try: + for section in Config.sections(): + # Skip interpolating sections of alternatives that aren't selected + # via -a / --alternatives. They aren't needed to run tests + # and aren't expected to properly interpolatable. + if ( + section.startswith("environment-") + or section.startswith("filter-") + or section.startswith("substitution-") + ): + tag = section.split("-", 1)[-1] + if tag not in Options.alternatives: + continue + + for _, _ in Config.itemsNoDefaults(section): + pass + except configparser.InterpolationError as err: + error( + f"{btest_cfg} contains invalid Python configparser interpolation in {err.section}.{err.option}: {err}" + ) orig_cwd = pathlib.Path.cwd() os.chdir(TestBase) diff --git a/testing/Baseline/tests.btest-cfg-skip-non-selected-env/output b/testing/Baseline/tests.btest-cfg-skip-non-selected-env/output new file mode 100644 index 00000000..0a791e00 --- /dev/null +++ b/testing/Baseline/tests.btest-cfg-skip-non-selected-env/output @@ -0,0 +1,13 @@ +### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. +X default +X default +test ... ok +all 1 tests successful +X default +X installation +Y installation +X default +X installation +Y installation +test [installation] ... ok +all 1 tests successful diff --git a/testing/Baseline/tests.btest-cfg-skip-non-selected-env/test.out b/testing/Baseline/tests.btest-cfg-skip-non-selected-env/test.out new file mode 100644 index 00000000..dc235279 --- /dev/null +++ b/testing/Baseline/tests.btest-cfg-skip-non-selected-env/test.out @@ -0,0 +1,6 @@ +### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. +default +MYTEST_X=Xdefault +installation +MYTEST_X=Xinstallation +MYTEST_Y=Yinstallation diff --git a/testing/tests/btest-cfg-skip-non-selected-env.test b/testing/tests/btest-cfg-skip-non-selected-env.test new file mode 100644 index 00000000..4eabab9e --- /dev/null +++ b/testing/tests/btest-cfg-skip-non-selected-env.test @@ -0,0 +1,29 @@ +# %TEST-DOC: Validates that a environment- section isn't interpolated/evaluated +# it is not not selected. +# +# %TEST-EXEC: echo "default" >>test.out +# %TEST-EXEC: btest -dt test >output 2>&1 +# %TEST-EXEC: echo "installation" >>test.out +# %TEST-EXEC: btest -dt -a installation test >>output 2>&1 +# %TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff output +# %TEST-EXEC: btest-diff test.out + +# %TEST-START-FILE btest.cfg +[DEFAULT] +val1 = abcd + +[btest] +option1=%(val1)s + +[environment] +MYTEST_X=`echo X default >&2; echo Xdefault` + +[environment-installation] +MYTEST_X=`echo X installation >&2; echo Xinstallation` +MYTEST_Y=`echo Y installation >&2; echo Yinstallation` +# %TEST-END-FILE + +# %TEST-START-FILE test +# # This writes into the outer test directory. +# @TEST-EXEC: env | grep MYTEST | sort >> ../../test.out +# %TEST-END-FILE