report the correct location for an option updated with set_defaults()

Change-Id: Ida9259b56f3f293d9f3cc6848051ab6895310c25
Signed-off-by: Doug Hellmann <doug@doughellmann.com>
This commit is contained in:
Doug Hellmann 2018-01-23 16:19:48 -05:00
parent a9625c78d3
commit 4010c4b002
2 changed files with 13 additions and 2 deletions

View File

@ -808,6 +808,7 @@ def set_defaults(opts, **kwargs):
for opt in opts:
if opt.dest in kwargs:
opt.default = kwargs[opt.dest]
opt._set_location = LocationInfo(Locations.set_default, '')
def _normalize_group_name(group_name):
@ -957,6 +958,7 @@ class Opt(object):
self.deprecated_reason = deprecated_reason
self.deprecated_since = deprecated_since
self._logged_deprecation = False
self._set_location = LocationInfo(Locations.opt_default, '')
self.deprecated_opts = copy.deepcopy(deprecated_opts) or []
for o in self.deprecated_opts:
@ -2972,6 +2974,7 @@ class ConfigOpts(collections.Mapping):
info = self._get_opt_info(name, group)
opt = info['opt']
loc = opt._set_location
if isinstance(opt, SubCommandOpt):
return (self.SubCommandAttr(self, group, opt.dest), None)
@ -3016,8 +3019,7 @@ class ConfigOpts(collections.Mapping):
% (opt.name, str(e)))
if opt.default is not None:
return (convert(opt.default),
LocationInfo(Locations.opt_default, ''))
return (convert(opt.default), loc)
return (None, None)

View File

@ -75,6 +75,15 @@ class GetLocationTestCase(base.BaseTestCase):
loc.location,
)
def test_set_defaults_func(self):
cfg.set_defaults([self.normal_opt], normal_opt=self.id())
self.conf([])
loc = self.conf.get_location('normal_opt')
self.assertEqual(
cfg.Locations.set_default,
loc.location,
)
def test_set_override(self):
self.conf.set_override('normal_opt', self.id())
self.conf([])