Skip to content
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
40 changes: 15 additions & 25 deletions reframe/frontend/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ def __getattr__(self, name):
f'cannot convert environment variable {envvar!r} '
f'to {arg_type.__name__!r}'
) from err
else:
ret = default

if _undefined(ret):
ret = default

return ret

Expand Down Expand Up @@ -154,7 +155,6 @@ def __repr__(self):
class _ArgumentHolder:
def __init__(self, holder, shared_options=None):
self._holder = holder
self._defaults = argparse.Namespace()

# Map command-line options to environment variables and configuration
# options. Values are tuples of the form (envvar, configvar)
Expand Down Expand Up @@ -235,12 +235,8 @@ def add_argument(self, *flags, **kwargs):
kwargs['const'] = True if action == 'store_true' else False
kwargs['const'] = False if action == 'store_false' else True

# Remove defaults
try:
self._defaults.__dict__[opt_name] = kwargs['default']
del kwargs['default']
except KeyError:
self._defaults.__dict__[opt_name] = None
# Remove defaults; they are applied by `_Namespace` on access
kwargs.pop('default', None)

if not flags:
return None
Expand Down Expand Up @@ -291,10 +287,6 @@ def _resolve_attr(self, attr, namespaces):

return None

def _update_defaults(self):
for g in self._groups:
self._defaults.__dict__.update(g._defaults.__dict__)

def parse_args(self, args=None, namespace=None, suppress_required=False):
'''Convert argument strings to objects and return them as attributes of
a namespace.
Expand All @@ -304,10 +296,13 @@ def parse_args(self, args=None, namespace=None, suppress_required=False):

If `namespace` is not `None` and an attribute has not been assigned a
value during the parsing process of argument strings `args`, a value
for it will be looked up first in `namespace` and if not found there,
it will be assigned the default value as specified in its corresponding
`add_argument()` call. If no default value was specified either, the
attribute will be set to `None`.
for it will be looked up in `namespace`.

Defaults are not assigned during parsing, so that the raw parsed
options contain only the options actually passed. The default value
specified in the corresponding `add_argument()` call is applied when
the option is accessed in the returned namespace, if it is not set
otherwise.

If `suppress_required` is true, required mutually-exclusive groups will
be treated as optional for this parsing operation.
Expand Down Expand Up @@ -348,16 +343,11 @@ def __exit__(this, *args, **kwargs):
if isinstance(namespace, _Namespace):
namespace = namespace.cmd_options

# Update parser's defaults with groups' defaults
self._update_defaults()

# Update the parsed options of those from the given namespace and/or
# the defaults
# Update the parsed options of those from the given namespace; defaults
# are not assigned here, they are applied by `_Namespace` on access
for attr, val in options.__dict__.items():
if val is None:
resolved = self._resolve_attr(attr,
[namespace, self._defaults])
options.__dict__[attr] = resolved
options.__dict__[attr] = self._resolve_attr(attr, [namespace])
elif self._option_map[attr][2] == 'append':
# 'append' options are combined with those from the given
# namespace, but *not* with the defaults (important)
Expand Down
15 changes: 15 additions & 0 deletions unittests/test_argparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ def test_parsing(argparser, foo_options, bar_options):
assert options.barfoo


def test_reparse_does_not_append_defaults(argparser, foo_options,
bar_options):
argparser.add_argument('--alist', action='append', default=['X'])
options = argparser.parse_args(['--foo', 'name'])
assert options.cmd_options.alist is None
assert options.cmd_options.bar is None
assert ['X'] == options.alist
assert 'BAR' == options.bar

options = argparser.parse_args(['--alist', 'Y'], options)
assert ['Y'] == options.alist
assert 'name' == options.foo
assert 'BAR' == options.bar


@pytest.fixture
def extended_parser():
parser = ArgumentParser()
Expand Down
19 changes: 19 additions & 0 deletions unittests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,25 @@ def test_execution_modes_envvar_expansion(run_reframe, monkeypatch):
assert 'Found 1 check(s)' in stdout


def test_execution_modes_prgenv_filter(run_reframe, monkeypatch):
# The default of `-p` must not leak into the command-line filter
monkeypatch.setenv('TEST_NAME_PATTERN', '^HelloTest$')
monkeypatch.setenv('VAR', 'x')
monkeypatch.setenv('VAL', '1')
returncode, stdout, stderr = run_reframe(
system='testsys:gpu',
environs=['PrgEnv-gnu'],
mode='env_vars',
action='list_concretized',
local=False
)
assert returncode == 0
assert 'Traceback' not in stdout
assert 'Traceback' not in stderr
assert 'testsys:gpu+PrgEnv-gnu' in stdout
assert 'testsys:gpu+builtin' not in stdout


def test_invalid_mode_error(run_reframe):
mode = 'invalid'
returncode, stdout, stderr = run_reframe(
Expand Down
Loading