Skip to content

Commit 272d72b

Browse files
committed
Adding testcase for Add(..., subst)
Signed-off-by: Mats Wichmann <[email protected]>
1 parent 18b45e4 commit 272d72b

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

RELEASE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ CHANGED/ENHANCED EXISTING FUNCTIONALITY
5050
Now matches the annotation and docstring (which were prematurely
5151
updated in 4.6). All SCons usage except unit test was already fully
5252
consistent with a bool.
53-
- The Variables object Add method now accepts a do_subst keyword argument
53+
- The Variables object Add method now accepts a subst keyword argument
5454
(defaults to True) which can be set to inhibit substitution prior to
5555
calling the variable's converter and validator.
5656

SCons/Variables/VariablesTests.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,28 @@ def test_Update(self) -> None:
150150
opts.Update(env, {})
151151
assert env['ANSWER'] == 54
152152

153+
# Test that the value is not substituted if 'subst' is False
154+
def check_subst(key, value, env) -> None:
155+
"""Check that variable was not substituted before we get called."""
156+
assert value == "$ORIGIN", \
157+
f"Validator: '$ORIGIN' was substituted to {value!r}"
158+
159+
def conv_subst(value) -> None:
160+
"""Check that variable was not substituted before we get called."""
161+
assert value == "$ORIGIN", \
162+
f"Converter: '$ORIGIN' was substituted to {value!r}"
163+
return value
164+
165+
opts.Add('NOSUB',
166+
help='Variable whose value will not be substituted',
167+
default='$ORIGIN',
168+
validator=check_subst,
169+
converter=conv_subst,
170+
subst=False)
171+
env = Environment()
172+
opts.Update(env)
173+
assert env['NOSUB'] == "$ORIGIN"
174+
153175
# Test that a bad value from the file is used and
154176
# validation fails correctly.
155177
test = TestSCons.TestSCons()

SCons/Variables/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ def _do_add(
114114
"""Create a Variable and add it to the list.
115115
116116
Internal routine, not public API.
117+
118+
.. versionadded:: 4.8.0
119+
*subst* keyword argument is now recognized.
117120
"""
118121
option = Variable()
119122

@@ -252,7 +255,7 @@ def Update(self, env, args: Optional[dict] = None) -> None:
252255
for option in self.options:
253256
if option.converter and option.key in values:
254257
if option.do_subst:
255-
value = env.subst(f'${option.key}')
258+
value = env.subst('${%s}' % option.key)
256259
else:
257260
value = env[option.key]
258261
try:
@@ -270,7 +273,7 @@ def Update(self, env, args: Optional[dict] = None) -> None:
270273
for option in self.options:
271274
if option.validator and option.key in values:
272275
if option.do_subst:
273-
value = env.subst('${%s}'%option.key)
276+
value = env.subst('${%s}' % option.key)
274277
else:
275278
value = env[option.key]
276279
option.validator(option.key, value, env)

doc/man/scons.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4835,7 +4835,7 @@ not to any stored-values files.
48354835

48364836
<variablelist>
48374837
<varlistentry id="v-Add">
4838-
<term><replaceable>vars</replaceable>.<function>Add</function>(<parameter>key, [help, default, validator, converter, do_subst]</parameter>)</term>
4838+
<term><replaceable>vars</replaceable>.<function>Add</function>(<parameter>key, [help, default, validator, converter, subst]</parameter>)</term>
48394839
<listitem>
48404840
<para>Add a customizable &consvar; to the &Variables; object.
48414841
<parameter>key</parameter>
@@ -4889,12 +4889,12 @@ it can raise a <exceptionname>ValueError</exceptionname>.
48894889

48904890
<para>
48914891
Substitution will be performed on the variable value
4892-
as it is added, before the converter and validator are called,
4893-
unless the optional <parameter>do_subst</parameter> parameter
4892+
before the converter and validator are called,
4893+
unless the optional <parameter>subst</parameter> parameter
48944894
is false (default <literal>True</literal>).
48954895
Suppressing substitution may be useful if the variable value
4896-
looks like a &consvar; reference (<literal>$VAR</literal>)
4897-
to be expanded later.
4896+
looks like a &consvar; reference (e.g. <literal>$VAR</literal>)
4897+
and the validator and/or converter should see it unexpanded.
48984898
</para>
48994899

49004900
<para>
@@ -4932,7 +4932,7 @@ vars.Add('COLOR', validator=valid_color)
49324932

49334933
<para>
49344934
<emphasis>Changed in version 4.8.0:</emphasis>
4935-
added the <parameter>do_subst</parameter> parameter.
4935+
added the <parameter>subst</parameter> parameter.
49364936
</para>
49374937
</listitem>
49384938
</varlistentry>

0 commit comments

Comments
 (0)