Skip to content

Commit

Permalink
Fix a lot of warnings (#662)
Browse files Browse the repository at this point in the history
* Fix the use of assertEquals() in tests

* Fix the use of logging.warn()

* Fix 2 ResourceWarning: unclosed file, in test
  • Loading branch information
BoboTiG authored and lechat committed Sep 2, 2018
1 parent 51e4eac commit 0f79ccf
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 35 deletions.
20 changes: 11 additions & 9 deletions jenkinsapi_tests/systests/test_jenkins_artifacts.py
Expand Up @@ -38,19 +38,21 @@ def test_artifacts(jenkins):
text_artifact.save_to_dir(tempDir, strict_validation=True)
text_file_path = join(tempDir, text_artifact.filename)
assert os.path.exists(text_file_path)
read_back_text = open(text_file_path, 'rb').read().strip()
read_back_text = read_back_text.decode('ascii')
log.info('Text artifact: %s', read_back_text)
assert re.match(r'^PING \S+ \(127.0.0.1\)', read_back_text) is not None
assert read_back_text.endswith('ms') is True
with open(text_file_path, 'rb') as f:
read_back_text = f.read().strip()
read_back_text = read_back_text.decode('ascii')
log.info('Text artifact: %s', read_back_text)
assert re.match(r'^PING \S+ \(127.0.0.1\)', read_back_text) is not None
assert read_back_text.endswith('ms') is True

# Verify that we can hande binary artifacts
binary_artifact.save_to_dir(tempDir, strict_validation=True)
bin_file_path = join(tempDir, binary_artifact.filename)
assert os.path.exists(bin_file_path)
read_back_text = gzip.open(bin_file_path, 'rb').read().strip()
read_back_text = read_back_text.decode('ascii')
assert re.match(r'^PING \S+ \(127.0.0.1\)', read_back_text) is not None
assert read_back_text.endswith('ms') is True
with gzip.open(bin_file_path, 'rb') as f:
read_back_text = f.read().strip()
read_back_text = read_back_text.decode('ascii')
assert re.match(r'^PING \S+ \(127.0.0.1\)', read_back_text) is not None
assert read_back_text.endswith('ms') is True
finally:
shutil.rmtree(tempDir)
2 changes: 1 addition & 1 deletion jenkinsapi_tests/systests/test_nodes.py
Expand Up @@ -17,7 +17,7 @@ def test_online_offline(jenkins):
# Master node name should be case insensitive
# mn0 = jenkins.get_node('MaStEr')
mn = jenkins.get_node('master')
# self.assertEquals(mn, mn0)
# self.assertEqual(mn, mn0)

mn.set_online() # It should already be online, hence no-op
assert mn.is_online() is True
Expand Down
10 changes: 5 additions & 5 deletions jenkinsapi_tests/unittests/test_job_get_all_builds.py
Expand Up @@ -187,7 +187,7 @@ def test_get_build_dict(self):
# remaining jobs will be fetched automatically
ret = self.j.get_build_dict()
self.assertTrue(isinstance(ret, dict))
self.assertEquals(len(ret), 4)
self.assertEqual(len(ret), 4)

@mock.patch.object(JenkinsBase, 'get_data', fakeGetDataTree)
def test_incomplete_builds_list_will_call_jenkins_twice(self):
Expand All @@ -196,15 +196,15 @@ def test_incomplete_builds_list_will_call_jenkins_twice(self):
# to the Jenkins API
TestJobGetAllBuilds.__get_data_call_count = 0
self.j = Job('http://halob:8080/job/foo/', 'foo', self.J)
self.assertEquals(TestJobGetAllBuilds.__get_data_call_count, 2)
self.assertEqual(TestJobGetAllBuilds.__get_data_call_count, 2)

@mock.patch.object(JenkinsBase, 'get_data', fakeGetDataTree)
def test_complete_builds_list_will_call_jenkins_once(self):
# The job data contains all builds, so we will not gather remaining
# builds
TestJobGetAllBuilds.__get_data_call_count = 0
self.j = Job('http://halob:8080/job/fullfoo/', 'fullfoo', self.J)
self.assertEquals(TestJobGetAllBuilds.__get_data_call_count, 1)
self.assertEqual(TestJobGetAllBuilds.__get_data_call_count, 1)

@mock.patch.object(JenkinsBase, 'get_data', fakeGetDataTree)
def test_nobuilds_get_build_dict(self):
Expand All @@ -215,15 +215,15 @@ def test_nobuilds_get_build_dict(self):

ret = j.get_build_dict()
self.assertTrue(isinstance(ret, dict))
self.assertEquals(len(ret), 0)
self.assertEqual(len(ret), 0)

@mock.patch.object(JenkinsBase, 'get_data', fakeGetDataTree)
def test_get_build_ids(self):
# The job data contains only one build, so we expect that the
# remaining jobs will be fetched automatically
ret = list(self.j.get_build_ids())
self.assertTrue(isinstance(ret, list))
self.assertEquals(len(ret), 4)
self.assertEqual(len(ret), 4)


if __name__ == '__main__':
Expand Down
8 changes: 4 additions & 4 deletions jenkinsapi_tests/unittests/test_job_scm_hg.py
Expand Up @@ -98,13 +98,13 @@ def configtree_with_default_branch(self):
@mock.patch.object(Job, 'get_config', configtree_with_branch)
def test_hg_attributes(self):
expected_url = ['http://cm5/hg/sandbox/v01.0/int']
self.assertEquals(self.j.get_scm_type(), 'hg')
self.assertEquals(self.j.get_scm_url(), expected_url)
self.assertEquals(self.j.get_scm_branch(), ['testme'])
self.assertEqual(self.j.get_scm_type(), 'hg')
self.assertEqual(self.j.get_scm_url(), expected_url)
self.assertEqual(self.j.get_scm_branch(), ['testme'])

@mock.patch.object(Job, 'get_config', configtree_with_default_branch)
def test_hg_attributes_default_branch(self):
self.assertEquals(self.j.get_scm_branch(), ['default'])
self.assertEqual(self.j.get_scm_branch(), ['default'])


if __name__ == '__main__':
Expand Down
26 changes: 13 additions & 13 deletions jenkinsapi_tests/unittests/test_plugins.py
Expand Up @@ -78,21 +78,21 @@ def test_no_plugins_str(self, _poll_plugins):
_poll_plugins.return_value = {}

plugins = self.J.get_plugins()
self.assertEquals(str(plugins), "[]")
self.assertEqual(str(plugins), "[]")

@mock.patch.object(Plugins, '_poll')
def test_plugins_str(self, _poll_plugins):
_poll_plugins.return_value = self.DATA

plugins = self.J.get_plugins()
self.assertEquals(str(plugins), "['maven-plugin', 'subversion']")
self.assertEqual(str(plugins), "['maven-plugin', 'subversion']")

@mock.patch.object(Plugins, '_poll')
def test_plugins_len(self, _poll_plugins):
_poll_plugins.return_value = self.DATA

plugins = self.J.get_plugins()
self.assertEquals(len(plugins), 2)
self.assertEqual(len(plugins), 2)

@mock.patch.object(Plugins, '_poll')
def test_plugins_contains(self, _poll_plugins):
Expand Down Expand Up @@ -143,7 +143,7 @@ def test_plugins_empty(self, _poll_plugins):

# list() is required here for python 3.x compatibility
plugins = list(self.J.get_plugins().keys())
self.assertEquals([], plugins)
self.assertEqual([], plugins)

@mock.patch.object(Plugins, '_poll')
def test_plugin_get_by_name(self, _poll_plugins):
Expand All @@ -170,18 +170,18 @@ def test_plugin_get_by_name(self, _poll_plugins):
)

plugin = self.J.get_plugins()['subversion']
self.assertEquals(p, plugin)
self.assertEqual(p, plugin)

@mock.patch.object(Plugins, '_poll')
def test_get_plugin_details(self, _poll_plugins):
_poll_plugins.return_value = self.DATA
plugin = self.J.get_plugins()['subversion']
self.assertEquals('1.45', plugin.version)
self.assertEquals('subversion', plugin.shortName)
self.assertEquals('Jenkins Subversion Plug-in', plugin.longName)
self.assertEquals('http://wiki.jenkins-ci.org/display/JENKINS/'
'Subversion+Plugin',
plugin.url)
self.assertEqual('1.45', plugin.version)
self.assertEqual('subversion', plugin.shortName)
self.assertEqual('Jenkins Subversion Plug-in', plugin.longName)
self.assertEqual('http://wiki.jenkins-ci.org/display/JENKINS/'
'Subversion+Plugin',
plugin.url)

@mock.patch.object(Requester, 'post_xml_and_confirm_status')
def test_install_plugin_bad_input(self, _post):
Expand Down Expand Up @@ -253,7 +253,7 @@ def test_get_plugin_dependencies(self, _poll_plugins):
_poll_plugins.return_value = self.DATA
dependencies = self.J.plugins._get_plugin_dependencies(
downloaded_plugin)
self.assertEquals(len(dependencies), 2)
self.assertEqual(len(dependencies), 2)
for dep in dependencies:
self.assertIsInstance(dep, Plugin)

Expand Down Expand Up @@ -309,7 +309,7 @@ def test_plugin_repr(self):
'shortName': 'subversion',
}
)
self.assertEquals(repr(p), '<jenkinsapi.plugin.Plugin subversion>')
self.assertEqual(repr(p), '<jenkinsapi.plugin.Plugin subversion>')


if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion jenkinsapi_tests/unittests/test_result_set.py
Expand Up @@ -69,7 +69,7 @@ def testName(self):
with self.assertRaises(AttributeError):
self.rs.id()

self.assertEquals(self.rs.name, 'Test Result for FooBuild')
self.assertEqual(self.rs.name, 'Test Result for FooBuild')

def testBuildComponents(self):
self.assertTrue(self.rs.items())
Expand Down
4 changes: 2 additions & 2 deletions jenkinsapi_utils/jenkins_launcher.py
Expand Up @@ -194,7 +194,7 @@ def start(self, timeout=60):
StreamThread('out', self.queue, self.jenkins_process.stdout,
log.info),
StreamThread('err', self.queue, self.jenkins_process.stderr,
log.warn)
log.warning)
]

# Start the threads
Expand Down Expand Up @@ -223,7 +223,7 @@ def start(self, timeout=60):
log.info(line)
return
else:
log.warn('Stream %s has terminated', streamName)
log.warning('Stream %s has terminated', streamName)

self.block_until_jenkins_ready(timeout)

Expand Down

0 comments on commit 0f79ccf

Please sign in to comment.