Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unknown variables from Variables file are now reported #4653

Merged
merged 2 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ RELEASE VERSION/DATE TO BE FILLED IN LATER
types depending on whether zero, one, or multiple construction
variable names are given.
- Update Clean and NoClean documentation.
- Make sure unknown variables from a Variables file are recognized
as such (issue #4645)


RELEASE 4.8.1 - Tue, 03 Sep 2024 17:22:20 -0700
Expand Down
3 changes: 3 additions & 0 deletions RELEASE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ FIXES
- Skip running a few validation tests if the user is root and the test is
not designed to work for the root user.

- Make sure unknown variables from a Variables file are recognized
as such (issue #4645)

IMPROVEMENTS
------------

Expand Down
30 changes: 17 additions & 13 deletions SCons/Variables/VariablesTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,41 +654,45 @@ def test_unknown(self) -> None:
self.assertEqual('answer', env['ANSWER'])

def test_AddOptionUpdatesUnknown(self) -> None:
"""Test updating of the 'unknown' dict"""
opts = SCons.Variables.Variables()

opts.Add('A',
'A test variable',
"1")
"""Test updating of the 'unknown' dict.

Get one unknown from args and one from a variables file.
Add these later, making sure they no longer appear in unknowns
after the subsequent Update().
"""
test = TestSCons.TestSCons()
var_file = test.workpath('vars.py')
test.write('vars.py', 'FROMFILE="added"')
opts = SCons.Variables.Variables(files=var_file)
opts.Add('A', 'A test variable', "1")
args = {
'A' : 'a',
'ADDEDLATER' : 'notaddedyet',
}

env = Environment()
opts.Update(env,args)

r = opts.UnknownVariables()
with self.subTest():
self.assertEqual({'ADDEDLATER': 'notaddedyet'}, r)
self.assertEqual('notaddedyet', r['ADDEDLATER'])
self.assertEqual('added', r['FROMFILE'])
self.assertEqual('a', env['A'])

opts.Add('ADDEDLATER',
'An option not present initially',
"1")

opts.Add('ADDEDLATER', 'An option not present initially', "1")
opts.Add('FROMFILE', 'An option from a file also absent', "1")
args = {
'A' : 'a',
'ADDEDLATER' : 'added',
}

opts.Update(env, args)

r = opts.UnknownVariables()
with self.subTest():
self.assertEqual(0, len(r))
self.assertNotIn('ADDEDLATER', r)
self.assertEqual('added', env['ADDEDLATER'])
self.assertNotIn('FROMFILE', r)
self.assertEqual('added', env['FROMFILE'])

def test_AddOptionWithAliasUpdatesUnknown(self) -> None:
"""Test updating of the 'unknown' dict (with aliases)"""
Expand Down
27 changes: 19 additions & 8 deletions SCons/Variables/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,19 +235,30 @@ def Update(self, env, args: dict | None = None) -> None:
for filename in self.files:
# TODO: issue #816 use Node to access saved-variables file?
if os.path.exists(filename):
# lint: W0622: Redefining built-in 'dir'
dir = os.path.split(os.path.abspath(filename))[0]
if dir:
sys.path.insert(0, dir)
# issue #4645: don't exec directly into values,
# so we can iterate through for unknown variables.
temp_values = {}
dirname = os.path.split(os.path.abspath(filename))[0]
if dirname:
sys.path.insert(0, dirname)
try:
values['__name__'] = filename
temp_values['__name__'] = filename
with open(filename) as f:
contents = f.read()
exec(contents, {}, values)
exec(contents, {}, temp_values)
finally:
if dir:
if dirname:
del sys.path[0]
del values['__name__']
del temp_values['__name__']

for arg, value in temp_values.items():
added = False
for option in self.options:
if arg in option.aliases + [option.key,]:
values[option.key] = value
added = True
if not added:
self.unknown[arg] = value

# set the values specified on the command line
if args is None:
Expand Down
Loading