Ensure tests run with ConfigParser object
Should always use a ConfigParser object when testing to allow unit tests to check that calls behave as expected. Read in the default configuration to align with expected usage, and fix some issues caused by usage of dicts resulting in the option name requested being taken as the default value. Since '.get()' is a valid method for a dictionary object, take care to ensure that we do not use a dummy dict object as this will not throw the expected errors if a config file is not provided and will also treat the option name as a default value resulting in the wrong data appearing in the outputted XML. Change-Id: I6abb85f0363a48f220914c52b74e5a5fb65b0faa
This commit is contained in:
parent
7e51c13c80
commit
dc464d943f
@ -36,6 +36,8 @@ try:
|
||||
from unittest import mock
|
||||
except ImportError:
|
||||
import mock # noqa
|
||||
|
||||
from jenkins_jobs.cmd import DEFAULT_CONF
|
||||
import jenkins_jobs.local_yaml as yaml
|
||||
from jenkins_jobs.parser import YamlParser
|
||||
from jenkins_jobs.xml_config import XmlJob
|
||||
@ -119,15 +121,19 @@ class BaseTestCase(object):
|
||||
yaml_content = yaml.load(yaml_file)
|
||||
return yaml_content
|
||||
|
||||
def _get_config(self):
|
||||
config = configparser.ConfigParser()
|
||||
config.readfp(StringIO(DEFAULT_CONF))
|
||||
if self.conf_filename is not None:
|
||||
with io.open(self.conf_filename, 'r', encoding='utf-8') as cf:
|
||||
config.readfp(cf)
|
||||
return config
|
||||
|
||||
def test_yaml_snippet(self):
|
||||
if not self.in_filename:
|
||||
return
|
||||
|
||||
if self.conf_filename is not None:
|
||||
config = configparser.ConfigParser()
|
||||
config.readfp(io.open(self.conf_filename, 'r', encoding='utf-8'))
|
||||
else:
|
||||
config = {}
|
||||
config = self._get_config()
|
||||
|
||||
expected_xml = self._read_utf8_content()
|
||||
yaml_content = self._read_yaml_content(self.in_filename)
|
||||
@ -178,13 +184,10 @@ class BaseTestCase(object):
|
||||
|
||||
class SingleJobTestCase(BaseTestCase):
|
||||
def test_yaml_snippet(self):
|
||||
config = self._get_config()
|
||||
|
||||
expected_xml = self._read_utf8_content()
|
||||
|
||||
if self.conf_filename:
|
||||
config = configparser.ConfigParser()
|
||||
config.readfp(io.open(self.conf_filename, 'r', encoding='utf-8'))
|
||||
else:
|
||||
config = None
|
||||
parser = YamlParser(config)
|
||||
parser.parse(self.in_filename)
|
||||
|
||||
|
5
tests/hipchat/fixtures/hipchat001.conf
Normal file
5
tests/hipchat/fixtures/hipchat001.conf
Normal file
@ -0,0 +1,5 @@
|
||||
[hipchat]
|
||||
authtoken=blue
|
||||
send-as=Jenkins
|
||||
[jenkins]
|
||||
url=green
|
@ -14,8 +14,8 @@
|
||||
</properties>
|
||||
<publishers>
|
||||
<jenkins.plugins.hipchat.HipChatNotifier>
|
||||
<jenkinsUrl>url</jenkinsUrl>
|
||||
<authToken>authtoken</authToken>
|
||||
<jenkinsUrl>green</jenkinsUrl>
|
||||
<authToken>blue</authToken>
|
||||
<room/>
|
||||
</jenkins.plugins.hipchat.HipChatNotifier>
|
||||
</publishers>
|
||||
|
5
tests/hipchat/fixtures/hipchat002.conf
Normal file
5
tests/hipchat/fixtures/hipchat002.conf
Normal file
@ -0,0 +1,5 @@
|
||||
[hipchat]
|
||||
authtoken=blue
|
||||
send-as=Jenkins
|
||||
[jenkins]
|
||||
url=green
|
@ -8,8 +8,8 @@
|
||||
</properties>
|
||||
<publishers>
|
||||
<jenkins.plugins.hipchat.HipChatNotifier>
|
||||
<jenkinsUrl>url</jenkinsUrl>
|
||||
<authToken>authtoken</authToken>
|
||||
<jenkinsUrl>green</jenkinsUrl>
|
||||
<authToken>blue</authToken>
|
||||
<room/>
|
||||
</jenkins.plugins.hipchat.HipChatNotifier>
|
||||
</publishers>
|
||||
|
@ -14,9 +14,9 @@
|
||||
</properties>
|
||||
<publishers>
|
||||
<jenkins.plugins.hipchat.HipChatNotifier>
|
||||
<buildServerUrl>url</buildServerUrl>
|
||||
<sendAs>send-as</sendAs>
|
||||
<authToken>authtoken</authToken>
|
||||
<buildServerUrl>http://localhost:8080/</buildServerUrl>
|
||||
<sendAs>Jenkins</sendAs>
|
||||
<authToken>dummy</authToken>
|
||||
<room/>
|
||||
</jenkins.plugins.hipchat.HipChatNotifier>
|
||||
</publishers>
|
||||
|
@ -14,9 +14,9 @@
|
||||
</properties>
|
||||
<publishers>
|
||||
<jenkins.plugins.hipchat.HipChatNotifier>
|
||||
<buildServerUrl>url</buildServerUrl>
|
||||
<sendAs>send-as</sendAs>
|
||||
<authToken>authtoken</authToken>
|
||||
<buildServerUrl>http://localhost:8080/</buildServerUrl>
|
||||
<sendAs>Jenkins</sendAs>
|
||||
<authToken>dummy</authToken>
|
||||
<room/>
|
||||
</jenkins.plugins.hipchat.HipChatNotifier>
|
||||
</publishers>
|
||||
|
Loading…
Reference in New Issue
Block a user