Skip to content

Commit

Permalink
Fix unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpakkane committed Aug 7, 2024
1 parent b068804 commit 13ab6b8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
43 changes: 35 additions & 8 deletions mesonbuild/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -849,26 +849,37 @@ def sanitize_dir_option_value(self, prefix: str, option: OptionKey, value: T.Any
return value.as_posix()


def set_value(self, key: T.Union[OptionKey, str], new_value: 'T.Any') -> bool:
def set_value(self, key: T.Union[OptionKey, str], new_value: 'T.Any', first_invocation: bool = False) -> bool:
key = self.ensure_and_validate_key(key)
if key.name == 'prefix':
new_value = self.sanitize_prefix(new_value)
elif self.is_builtin_option(key):
prefix = self.get_value_for('prefix')
new_value = self.sanitize_dir_option_value(prefix, key, new_value)
prefix = self.get_value_for('prefix')
new_value = self.sanitize_dir_option_value(prefix, key, new_value)
if key not in self.options:
raise MesonException(f'Unknown options: "{key.name}" not found.')

valobj = self.options[key]
old_value = valobj.value
changed = valobj.set_value(new_value)

if valobj.readonly and changed:
raise MesonException(f'Tried modify read only option {str(key)!r}')

if key.name == 'prefix' and first_invocation and changed:
self.reset_prefixed_options(old_value, new_value)

return changed

def set_option(self, name: str, subproject: T.Optional[str], new_value: str):
def set_option(self, name: str, subproject: T.Optional[str], new_value: str, first_invocation:bool = False):
key = OptionKey(name, subproject)
# FIRXME, dupe ofr the on in set_value.
# FIXME, dupe of set_value
# Remove one of the two before merging to master.
if key.name == 'prefix':
new_value = self.sanitize_prefix(new_value)
elif self.is_builtin_option(key):
prefix = self.get_value_for('prefix')
new_value = self.sanitize_dir_option_value(prefix, key, new_value)
opt = self.get_value_object_for(key)
if opt.deprecated is True:
mlog.deprecation(f'Option {key.name!r} is deprecated')
Expand All @@ -892,14 +903,29 @@ def replace(v):
dirty |= opt.set_value(new_value)
return dirty

old_value = opt.value
changed = opt.set_value(new_value)

if opt.readonly and changed:
raise MesonException(f'Tried modify read only option {str(key)!r}')

if key.name == 'prefix' and first_invocation and changed:
self.reset_prefixed_options(old_value, new_value)

return changed

def reset_prefixed_options(self, old_prefix, new_prefix):
for optkey, prefix_mapping in BUILTIN_DIR_NOPREFIX_OPTIONS.items():
if new_prefix not in prefix_mapping:
continue
valobj = self.options[optkey]
if old_prefix in prefix_mapping:
# Only reset the value if it has not been changed from the default.
if prefix_mapping[old_prefix] == valobj.value:
valobj.set_value(prefix_mapping[new_prefix])
else:
valobj.set_value(prefix_mapping[new_prefix])

# FIXME, this should be removed.or renamed to "change_type_of_existing_object" or something like that
def set_value_object(self, key: T.Union[OptionKey, str], new_object: 'UserOption[T.Any]') -> bool:
key = self.ensure_and_validate_key(key)
Expand Down Expand Up @@ -1060,6 +1086,7 @@ def optlist2optdict(self, optlist):
return optdict

def set_from_top_level_project_call(self, project_default_options, cmd_line_options, native_file_options):
first_invocation = True
if isinstance(project_default_options, str):
project_default_options = [project_default_options]
if isinstance(project_default_options, list):
Expand All @@ -1076,7 +1103,7 @@ def set_from_top_level_project_call(self, project_default_options, cmd_line_opti
#self.pending_project_options[key] = valstr
raise MesonException(f'Can not set subproject option {keystr} in machine files.')
elif key in self.options:
self.options[key].set_value(valstr)
self.set_value(key, valstr, first_invocation)
else:
proj_key = key.evolve(subproject='')
if proj_key in self.options:
Expand All @@ -1099,7 +1126,7 @@ def set_from_top_level_project_call(self, project_default_options, cmd_line_opti
if key.subproject is not None:
self.pending_project_options[key] = valstr
elif key in self.options:
self.set_option(key.name, key.subproject, valstr)
self.set_option(key.name, key.subproject, valstr, first_invocation)
else:
# Setting a project option with default_options.
# Argubly this should be a hard error, the default
Expand All @@ -1118,7 +1145,7 @@ def set_from_top_level_project_call(self, project_default_options, cmd_line_opti
if key.subproject is None:
projectkey = key.evolve(subproject='')
if key in self.options:
self.set_value(key, valstr)
self.set_value(key, valstr, True)
elif projectkey in self.options:
self.options[projectkey].set_value(valstr)
else:
Expand Down
10 changes: 6 additions & 4 deletions unittests/allplatformstests.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,25 +338,27 @@ def test_default_options_prefix_dependent_defaults(self):
'sysconfdir': '/etc',
'localstatedir': '/var',
'sharedstatedir': '/sharedstate'},

'--sharedstatedir=/var/state':
{'prefix': '/usr',
'sysconfdir': '/etc',
'localstatedir': '/var',
'sharedstatedir': '/var/state'},

'--sharedstatedir=/var/state --prefix=/usr --sysconfdir=sysconf':
{'prefix': '/usr',
'sysconfdir': 'sysconf',
'localstatedir': '/var',
'sharedstatedir': '/var/state'},
}
for args in expected:
self.init(testdir, extra_args=args.split(), default_args=False)
for argument_string, expected_values in expected.items():
self.init(testdir, extra_args=argument_string.split(), default_args=False)
opts = self.introspect('--buildoptions')
for opt in opts:
name = opt['name']
value = opt['value']
if name in expected[args]:
self.assertEqual(value, expected[args][name])
if name in expected_values:
self.assertEqual(value, expected_values[name], f'For option {name}, Meson arg: {argument_string}')
self.wipe()

def test_clike_get_library_dirs(self):
Expand Down

0 comments on commit 13ab6b8

Please sign in to comment.