Merge "Throw exception if --config-dir doesn't exist"
This commit is contained in:
commit
16541c554e
@ -387,6 +387,16 @@ class ConfigFilesNotFoundError(Error):
|
||||
",".join(self.config_files))
|
||||
|
||||
|
||||
class ConfigDirNotFoundError(Error):
|
||||
"""Raised if the requested config-dir is not found."""
|
||||
|
||||
def __init__(self, config_dir):
|
||||
self.config_dir = config_dir
|
||||
|
||||
def __str__(self):
|
||||
return ('Failed to read config file directory: %s' % self.config_dir)
|
||||
|
||||
|
||||
class ConfigFileParseError(Error):
|
||||
"""Raised if there is an error parsing a config file."""
|
||||
|
||||
@ -1045,10 +1055,16 @@ class _ConfigDirOpt(Opt):
|
||||
def __call__(self, parser, namespace, values, option_string=None):
|
||||
"""Handle a --config-dir command line argument.
|
||||
|
||||
:raises: ConfigFileParseError, ConfigFileValueError
|
||||
:raises: ConfigFileParseError, ConfigFileValueError,
|
||||
ConfigDirNotFoundError
|
||||
"""
|
||||
setattr(namespace, self.dest, values)
|
||||
|
||||
values = os.path.expanduser(values)
|
||||
|
||||
if not os.path.exists(values):
|
||||
raise ConfigDirNotFoundError(values)
|
||||
|
||||
config_dir_glob = os.path.join(values, '*.conf')
|
||||
|
||||
for config_file in sorted(glob.glob(config_dir_glob)):
|
||||
|
@ -75,6 +75,10 @@ class ExceptionsTestCase(utils.BaseTestCase):
|
||||
msg = str(cfg.ConfigFilesNotFoundError(['foo', 'bar']))
|
||||
self.assertEqual(msg, 'Failed to read some config files: foo,bar')
|
||||
|
||||
def test_config_dir_not_found_error(self):
|
||||
msg = str(cfg.ConfigDirNotFoundError('foobar'))
|
||||
self.assertEqual(msg, 'Failed to read config file directory: foobar')
|
||||
|
||||
def test_config_file_parse_error(self):
|
||||
msg = str(cfg.ConfigFileParseError('foo', 'foobar'))
|
||||
self.assertEqual(msg, 'Failed to parse foo: foobar')
|
||||
@ -2011,6 +2015,14 @@ class ConfigDirTestCase(BaseTestCase):
|
||||
self.assertTrue(hasattr(self.conf.snafu, 'bell'))
|
||||
self.assertEqual(self.conf.snafu.bell, 'whistle-11')
|
||||
|
||||
def test_config_dir_doesnt_exist(self):
|
||||
tmpdir = '/tmp/foo'
|
||||
|
||||
self.assertRaises(cfg.ConfigDirNotFoundError,
|
||||
self.conf,
|
||||
['--config-dir', tmpdir]
|
||||
)
|
||||
|
||||
|
||||
class ReparseTestCase(BaseTestCase):
|
||||
|
||||
@ -2762,27 +2774,29 @@ class TildeExpansionTestCase(BaseTestCase):
|
||||
|
||||
def test_config_dir_tilde(self):
|
||||
homedir = os.path.expanduser('~')
|
||||
tmpdir = tempfile.mktemp(dir=homedir,
|
||||
prefix='cfg-',
|
||||
suffix='.d')
|
||||
tmpfile = os.path.join(tmpdir, 'foo.conf')
|
||||
tmpbase = os.path.basename(tmpfile)
|
||||
|
||||
self.useFixture(fixtures.MonkeyPatch(
|
||||
'glob.glob',
|
||||
lambda p: [tmpfile]))
|
||||
|
||||
try:
|
||||
self.conf(['--config-dir',
|
||||
os.path.join('~', os.path.basename(tmpdir))])
|
||||
except cfg.ConfigFilesNotFoundError as cfnfe:
|
||||
self.assertTrue(os.path.expanduser('~') in str(cfnfe))
|
||||
tmpdir = tempfile.mkdtemp(dir=homedir,
|
||||
prefix='cfg-',
|
||||
suffix='.d')
|
||||
tmpfile = os.path.join(tmpdir, 'foo.conf')
|
||||
|
||||
self.useFixture(fixtures.MonkeyPatch(
|
||||
'os.path.exists',
|
||||
lambda p: p == tmpfile))
|
||||
self.useFixture(fixtures.MonkeyPatch(
|
||||
'glob.glob',
|
||||
lambda p: [tmpfile]))
|
||||
|
||||
self.assertEqual(self.conf.find_file(tmpbase), tmpfile)
|
||||
e = self.assertRaises(cfg.ConfigFilesNotFoundError,
|
||||
self.conf,
|
||||
['--config-dir',
|
||||
os.path.join('~',
|
||||
os.path.basename(tmpdir))]
|
||||
)
|
||||
self.assertIn(tmpdir, str(e))
|
||||
finally:
|
||||
try:
|
||||
shutil.rmtree(tmpdir)
|
||||
except OSError as exc:
|
||||
if exc.errno != 2:
|
||||
raise
|
||||
|
||||
|
||||
class SubCommandTestCase(BaseTestCase):
|
||||
|
Loading…
Reference in New Issue
Block a user