Avoid ResourceWarning by closing file handlers when finished

Warnings appear when running local_yaml unit tests.
This commit fixes the issues causing these warnings

```
 ResourceWarning: unclosed file <_io.TextIOWrapper
name='jenkins-job-builder/tests/yamlparser/fixtures/include_path001.conf'
mode='r' encoding='utf-8'>
  jjb_config = JJBConfig(self.conf_filename)
```

Change-Id: I8fec288cce8c9f6c7b928886d7c70f7a35d43b4a
This commit is contained in:
Philip Roche 2019-07-29 16:40:33 +01:00
parent 1d48093fe9
commit 1e901d921d
2 changed files with 5 additions and 1 deletions

View File

@ -141,6 +141,9 @@ class JJBConfig(object):
self._setup()
self._handle_deprecated_hipchat_config()
if config_fp is not None:
config_fp.close()
def _init_defaults(self):
""" Initialize default configuration values using DEFAULT_CONF
"""

View File

@ -137,7 +137,8 @@ class BaseTestCase(testtools.TestCase):
# Read XML content, assuming it is unicode encoded
xml_content = ""
for f in sorted(self.out_filenames):
xml_content += u"%s" % io.open(f, 'r', encoding='utf-8').read()
with io.open(f, 'r', encoding='utf-8') as xml_file:
xml_content += u"%s" % xml_file.read()
return xml_content
def _read_yaml_content(self, filename):