From 7dac478026ec69ca70f36485408fd8d2afa94a2d Mon Sep 17 00:00:00 2001 From: Piyush Raman Srivastava Date: Wed, 16 Mar 2016 09:46:59 -0700 Subject: [PATCH] Fix validate_args function in cliutils Fix validate_args() for more strict validation. Current logic works if only 1 required arg for functions. May fails if more than 1 required args. Refer to bug description for example failure case. Change-Id: Ie9bc54631d431f0933daf74793f0bcca1ba227f7 Closes-Bug: #1555764 --- rally/cli/cliutils.py | 11 ++++------- tests/unit/cli/test_cliutils.py | 4 ++++ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/rally/cli/cliutils.py b/rally/cli/cliutils.py index 10911f692a..d9d3d8bb42 100644 --- a/rally/cli/cliutils.py +++ b/rally/cli/cliutils.py @@ -79,7 +79,7 @@ def validate_args(fn, *args, **kwargs): MissingArgs: Missing argument(s): b, d :param fn: the function to check - :param arg: the positional arguments supplied + :param args: the positional arguments supplied :param kwargs: the keyword arguments supplied """ argspec = inspect.getargspec(fn) @@ -87,14 +87,11 @@ def validate_args(fn, *args, **kwargs): num_defaults = len(argspec.defaults or []) required_args = argspec.args[:len(argspec.args) - num_defaults] - def isbound(method): - return getattr(method, "__self__", None) is not None - - if isbound(fn): + if getattr(fn, "__self__", None): required_args.pop(0) - missing = [arg for arg in required_args if arg not in kwargs] - missing = missing[len(args):] + missing_required_args = required_args[len(args):] + missing = [arg for arg in missing_required_args if arg not in kwargs] if missing: raise MissingArgs(missing) diff --git a/tests/unit/cli/test_cliutils.py b/tests/unit/cli/test_cliutils.py index 24a477ee8e..7c6aab2cdd 100644 --- a/tests/unit/cli/test_cliutils.py +++ b/tests/unit/cli/test_cliutils.py @@ -421,6 +421,10 @@ class ValidateArgsTest(test.TestCase): self.assertRaises(cliutils.MissingArgs, self._test_lambda_with_args, y=2) + def test_lambda_missing_args4(self): + self.assertRaises(cliutils.MissingArgs, + self._test_lambda_with_args, 1, x=2) + def _test_lambda_with_default(self, *args, **kwargs): cliutils.validate_args(lambda x, y, z=3: None, *args, **kwargs)