Skip to content

Commit

Permalink
Merge pull request #170 from bjoernricks/missing-config-file
Browse files Browse the repository at this point in the history
Don't crash if config file is missing
  • Loading branch information
bjoernricks committed Mar 7, 2019
2 parents d5bef96 + 2afdaf7 commit 4758e69
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
5 changes: 2 additions & 3 deletions gvmtools/config.py
Expand Up @@ -22,8 +22,6 @@
import configparser
import logging

from pathlib import Path

from gvm.connections import DEFAULT_UNIX_SOCKET_PATH, DEFAULT_GVM_PORT

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -51,7 +49,8 @@ def load(self, filepath):

config = configparser.ConfigParser(default_section='main')

config.read_file(path.open())
with path.open() as f:
config.read_file(f)

if 'Auth' in config:
logger.warning(
Expand Down
8 changes: 7 additions & 1 deletion gvmtools/parser.py
Expand Up @@ -224,7 +224,13 @@ def _load_config(self, configfile):
return config

configpath = Path(configfile)
if not configpath.expanduser().resolve().exists():

try:
if not configpath.expanduser().resolve().exists():
logger.debug('Ignoring non existing config file %s', configfile)
return config
except FileNotFoundError:
# we are on python 3.5 and Path.resolve raised a FileNotFoundError
logger.debug('Ignoring non existing config file %s', configfile)
return config

Expand Down
36 changes: 36 additions & 0 deletions tests/test_parser.py
Expand Up @@ -20,6 +20,8 @@

from pathlib import Path

from gvm.connections import DEFAULT_UNIX_SOCKET_PATH, DEFAULT_TIMEOUT

from gvmtools.parser import CliParser

__here__ = Path(__file__).parent.resolve()
Expand Down Expand Up @@ -72,6 +74,40 @@ def test_tls_defaults_from_config(self):
self.assertEqual(args.port, 123)


class IgnoreConfigParserTestCase(unittest.TestCase):
def test_unkown_config_file(self):
test_config_path = __here__ / 'foo.cfg'

self.assertFalse(test_config_path.is_file())

self.parser = CliParser('TestParser', 'test.log')

args = self.parser.parse_args(
['--config', str(test_config_path), 'socket']
)

self.assertEqual(args.timeout, DEFAULT_TIMEOUT)
self.assertEqual(args.gmp_password, '')
self.assertEqual(args.gmp_username, '')
self.assertEqual(args.socketpath, DEFAULT_UNIX_SOCKET_PATH)

def test_unkown_config_file_in_unkown_dir(self):
test_config_path = __here__ / 'foo' / 'foo.cfg'

self.assertFalse(test_config_path.is_file())

self.parser = CliParser('TestParser', 'test.log')

args = self.parser.parse_args(
['--config', str(test_config_path), 'socket']
)

self.assertEqual(args.timeout, DEFAULT_TIMEOUT)
self.assertEqual(args.gmp_password, '')
self.assertEqual(args.gmp_username, '')
self.assertEqual(args.socketpath, DEFAULT_UNIX_SOCKET_PATH)


class ParserTestCase(unittest.TestCase):
def setUp(self):
self.parser = CliParser(
Expand Down

0 comments on commit 4758e69

Please sign in to comment.