Skip to content

Commit

Permalink
Merge pull request #166 from bjoernricks/load-config
Browse files Browse the repository at this point in the history
Fix loading config file
  • Loading branch information
bjoernricks committed Mar 4, 2019
2 parents 17eb5ba + f8c0d9c commit d3dec02
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
6 changes: 3 additions & 3 deletions gvmtools/config.py
Expand Up @@ -46,8 +46,8 @@ def __init__(self):

self._defaults = dict()

def load(self, filename):
path = Path(filename).expanduser()
def load(self, filepath):
path = filepath.expanduser()

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

Expand All @@ -57,7 +57,7 @@ def load(self, filename):
logger.warning(
"Warning: Loaded config file %s contains deprecated 'Auth' "
"section. This section will be ignored in future.",
filename,
str(filepath),
)
gmp_username = config.get('Auth', 'gmp_username', fallback='')
gmp_password = config.get('Auth', 'gmp_password', fallback='')
Expand Down
9 changes: 8 additions & 1 deletion gvmtools/parser.py
Expand Up @@ -22,6 +22,8 @@
import argparse
import logging

from pathlib import Path

from gvm import get_version as get_gvm_version
from gvm.connections import (
DEFAULT_TIMEOUT,
Expand Down Expand Up @@ -221,8 +223,13 @@ def _load_config(self, configfile):
if not configfile:
return config

configpath = Path(configfile)
if not configpath.expanduser().resolve().exists():
logger.debug('Ignoring non existing config file %s', configfile)
return config

try:
config.load(configfile)
config.load(configpath)
logger.debug('Loaded config %s', configfile)
except Exception as e: # pylint: disable=broad-except
raise RuntimeError(
Expand Down
14 changes: 12 additions & 2 deletions tests/test_config.py
Expand Up @@ -54,7 +54,7 @@ def test_load(self):
self.assertTrue(test_config_path.is_file())

config = Config()
config.load(str(test_config_path))
config.load(test_config_path)

self.assertEqual(config.get('gmp', 'username'), 'bar')
self.assertEqual(config.get('gmp', 'password'), 'bar')
Expand Down Expand Up @@ -85,9 +85,19 @@ def test_load_auth(self):
self.assertTrue(test_config_path.is_file())

config = Config()
config.load(str(test_config_path))
config.load(test_config_path)

self.assertEqual(config.get('gmp', 'username'), 'foo')
self.assertEqual(config.get('gmp', 'password'), 'bar')

root.disabled = False

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

self.assertFalse(test_config_path.is_file())

config = Config()

with self.assertRaises(FileNotFoundError):
config.load(test_config_path)

0 comments on commit d3dec02

Please sign in to comment.