Skip to content

Commit

Permalink
Beef up _stripixes unittests
Browse files Browse the repository at this point in the history
Also fix some bad typing markup.

Signed-off-by: Mats Wichmann <[email protected]>
  • Loading branch information
mwichmann committed Oct 26, 2023
1 parent 07e3ddf commit 16db520
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 23 deletions.
16 changes: 10 additions & 6 deletions SCons/Environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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]

Expand Down
38 changes: 25 additions & 13 deletions SCons/EnvironmentTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 6 additions & 4 deletions SCons/Subst.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 16db520

Please sign in to comment.