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:
Darragh Bailey 2015-05-04 16:50:21 +01:00 committed by Darragh Bailey
parent 7e51c13c80
commit dc464d943f
7 changed files with 33 additions and 20 deletions

View File

@ -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)

View File

@ -0,0 +1,5 @@
[hipchat]
authtoken=blue
send-as=Jenkins
[jenkins]
url=green

View File

@ -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>

View File

@ -0,0 +1,5 @@
[hipchat]
authtoken=blue
send-as=Jenkins
[jenkins]
url=green

View File

@ -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>

View File

@ -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>

View File

@ -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>