From 16db5206c3be2c20dbf3ecad9f8fe0b1097a17e5 Mon Sep 17 00:00:00 2001 From: Mats Wichmann Date: Thu, 26 Oct 2023 14:48:16 -0600 Subject: [PATCH] Beef up _stripixes unittests Also fix some bad typing markup. Signed-off-by: Mats Wichmann --- SCons/Environment.py | 16 ++++++++++------ SCons/EnvironmentTests.py | 38 +++++++++++++++++++++++++------------- SCons/Subst.py | 10 ++++++---- 3 files changed, 41 insertions(+), 23 deletions(-) diff --git a/SCons/Environment.py b/SCons/Environment.py index 6327d86206..64d38b0768 100644 --- a/SCons/Environment.py +++ b/SCons/Environment.py @@ -37,6 +37,7 @@ import shlex from collections import UserDict, deque from subprocess import PIPE, DEVNULL +from typing import Optional import SCons.Action import SCons.Builder @@ -679,7 +680,7 @@ def gvars(self): def lvars(self): return {} - def subst(self, string, raw: int=0, target=None, source=None, conv=None, executor=None, overrides: bool=False): + def subst(self, string, raw: int=0, target=None, source=None, conv=None, executor=None, overrides: Optional[dict] = None): """Recursively interpolates construction variables from the Environment into the specified string, returning the expanded result. Construction variables are specified by a $ prefix @@ -705,9 +706,11 @@ def subst_kw(self, kw, raw: int=0, target=None, source=None): nkw[k] = v return nkw - def subst_list(self, string, raw: int=0, target=None, source=None, conv=None, executor=None, overrides: bool=False): - """Calls through to SCons.Subst.scons_subst_list(). See - the documentation for that function.""" + def subst_list(self, string, raw: int=0, target=None, source=None, conv=None, executor=None, overrides: Optional[dict] = None): + """Calls through to SCons.Subst.scons_subst_list(). + + See the documentation for that function. + """ gvars = self.gvars() lvars = self.lvars() lvars['__env__'] = self @@ -716,9 +719,10 @@ def subst_list(self, string, raw: int=0, target=None, source=None, conv=None, ex return SCons.Subst.scons_subst_list(string, self, raw, target, source, gvars, lvars, conv, overrides=overrides) def subst_path(self, path, target=None, source=None): - """Substitute a path list, turning EntryProxies into Nodes - and leaving Nodes (and other objects) as-is.""" + """Substitute a path list. + Turns EntryProxies into Nodes, leaving Nodes (and other objects) as-is. + """ if not is_List(path): path = [path] diff --git a/SCons/EnvironmentTests.py b/SCons/EnvironmentTests.py index b1b6a0d648..c2287e79d1 100644 --- a/SCons/EnvironmentTests.py +++ b/SCons/EnvironmentTests.py @@ -1528,29 +1528,41 @@ def test__concat_nested(self) -> None: # function is in Defaults.py, tested here to use TestEnvironment def test__stripixes(self) -> None: """Test _stripixes()""" - # LIBPREFIXES and LIBSUFFIXES are stripped, - # except if an entry begins with LIBLITERAL + # LIBPREFIXES and LIBSUFFIXES are stripped, except if an entry + # begins with LIBLITERAL. Check this with and without that + # argument being passed, and with and without the var in the env. e = self.TestEnvironment( PRE='pre', SUF='suf', LIST=['xxx-a', 'b.yyy', 'zzxxx-c.yyy'], LIBPREFIXES=['xxx-'], LIBSUFFIXES=['.yyy'], - LIBLITERAL='zz', ) - x = e.subst('$( ${_stripixes(PRE, LIST, SUF, LIBPREFIXES, LIBSUFFIXES,__env__, LIBLITERAL)} $)') - self.assertEqual(x, 'preasuf prebsuf prezzxxx-c.yyysuf') - # Test that setting literal_prefix (in this case LIBLITERAL) - # same as os.pathsep disables the literal protection - e['LIBLITERAL'] = os.pathsep - x = e.subst('$( ${_stripixes(PRE, LIST, SUF, LIBPREFIXES, LIBSUFFIXES,__env__, LIBLITERAL)} $)') - self.assertEqual(x, 'preasuf prebsuf prezzxxx-csuf') + with self.subTest(): + x = e.subst('$( ${_stripixes(PRE, LIST, SUF, LIBPREFIXES, LIBSUFFIXES,__env__, LIBLITERAL)} $)') + self.assertEqual('preasuf prebsuf prezzxxx-c.yyysuf', x) + + with self.subTest(): + x = e.subst('$( ${_stripixes(PRE, LIST, SUF, LIBPREFIXES, LIBSUFFIXES,__env__)} $)') + self.assertEqual('preasuf prebsuf prezzxxx-csuf', x) + + # add it to the env: + e['LIBLITERAL'] = 'zz' - # Test that setting not settingliteral_prefix doesn't fail - x = e.subst('$( ${_stripixes(PRE, LIST, SUF, LIBPREFIXES, LIBSUFFIXES,__env__)} $)') - self.assertEqual(x, 'preasuf prebsuf prezzxxx-csuf') + with self.subTest(): + x = e.subst('$( ${_stripixes(PRE, LIST, SUF, LIBPREFIXES, LIBSUFFIXES,__env__, LIBLITERAL)} $)') + self.assertEqual('preasuf prebsuf prezzxxx-c.yyysuf', x) + + with self.subTest(): + x = e.subst('$( ${_stripixes(PRE, LIST, SUF, LIBPREFIXES, LIBSUFFIXES,__env__)} $)') + self.assertEqual('preasuf prebsuf prezzxxx-csuf', x) + # And special case: LIBLITERAL is the same as os.pathsep: + e['LIBLITERAL'] = os.pathsep + with self.subTest(): + x = e.subst('$( ${_stripixes(PRE, LIST, SUF, LIBPREFIXES, LIBSUFFIXES,__env__, LIBLITERAL)} $)') + self.assertEqual('preasuf prebsuf prezzxxx-csuf', x) def test_gvars(self) -> None: diff --git a/SCons/Subst.py b/SCons/Subst.py index 4046ca6b53..b04ebe50cd 100644 --- a/SCons/Subst.py +++ b/SCons/Subst.py @@ -26,6 +26,7 @@ import collections import re from inspect import signature, Parameter +from typing import Optional import SCons.Errors from SCons.Util import is_String, is_Sequence @@ -448,11 +449,12 @@ def substitute(self, args, lvars): This serves as a wrapper for splitting up a string into separate tokens. """ + def sub_match(match): + return self.conv(self.expand(match.group(1), lvars)) + if is_String(args) and not isinstance(args, CmdStringHolder): args = str(args) # In case it's a UserString. try: - def sub_match(match): - return self.conv(self.expand(match.group(1), lvars)) result = _dollar_exps.sub(sub_match, args) except TypeError: # If the internal conversion routine doesn't return @@ -805,7 +807,7 @@ def _remove_list(list): _space_sep = re.compile(r'[\t ]+(?![^{]*})') -def scons_subst(strSubst, env, mode=SUBST_RAW, target=None, source=None, gvars={}, lvars={}, conv=None, overrides: bool=False): +def scons_subst(strSubst, env, mode=SUBST_RAW, target=None, source=None, gvars={}, lvars={}, conv=None, overrides: Optional[dict] = None): """Expand a string or list containing construction variable substitutions. @@ -887,7 +889,7 @@ def scons_subst(strSubst, env, mode=SUBST_RAW, target=None, source=None, gvars={ return result -def scons_subst_list(strSubst, env, mode=SUBST_RAW, target=None, source=None, gvars={}, lvars={}, conv=None,overrides: bool=False): +def scons_subst_list(strSubst, env, mode=SUBST_RAW, target=None, source=None, gvars={}, lvars={}, conv=None, overrides: Optional[dict] = None): """Substitute construction variables in a string (or list or other object) and separate the arguments into a command list.