Update assertTrue/False to Python 3 precise asserts (#8792)

* Update assertTrue/False to Python 3 precise asserts

* Fix test failures

* Fix test failures

* More replacements

* Update to Python 3 asserts in acme-module

* Fix Windows test failure

* Fix failures

* Fix test failure

* More replacements

* Don't include the semgrep rules

* Fix test failure
This commit is contained in:
Mads Jensen
2021-04-29 10:45:08 +10:00
committed by GitHub
parent f339d23e54
commit 2cf1775864
41 changed files with 444 additions and 452 deletions
+3 -3
View File
@@ -174,7 +174,7 @@ class InstallerTest(test_util.ConfigTestCase):
def test_current_file_hash_in_all_hashes(self):
from certbot._internal.constants import ALL_SSL_DHPARAMS_HASHES
self.assertTrue(self._current_ssl_dhparams_hash() in ALL_SSL_DHPARAMS_HASHES,
self.assertIn(self._current_ssl_dhparams_hash(), ALL_SSL_DHPARAMS_HASHES,
"Constants.ALL_SSL_DHPARAMS_HASHES must be appended"
" with the sha256 hash of self.config.ssl_dhparams when it is updated.")
@@ -330,7 +330,7 @@ class InstallVersionControlledFileTest(test_util.TempDirTestCase):
mod_ssl_conf.write("a new line for the wrong hash\n")
with mock.patch("certbot.plugins.common.logger") as mock_logger:
self._call()
self.assertFalse(mock_logger.warning.called)
self.assertIs(mock_logger.warning.called, False)
self.assertTrue(os.path.isfile(self.dest_path))
self.assertEqual(crypto_util.sha256sum(self.source_path),
self._current_file_hash())
@@ -352,7 +352,7 @@ class InstallVersionControlledFileTest(test_util.TempDirTestCase):
# only print warning once
with mock.patch("certbot.plugins.common.logger") as mock_logger:
self._call()
self.assertFalse(mock_logger.warning.called)
self.assertIs(mock_logger.warning.called, False)
if __name__ == "__main__":
unittest.main() # pragma: no cover
+39 -41
View File
@@ -75,7 +75,7 @@ class PluginEntryPointTest(unittest.TestCase):
name, PluginEntryPoint.entry_point_to_plugin_name(entry_point, with_prefix=True))
def test_description(self):
self.assertTrue("temporary webserver" in self.plugin_ep.description)
self.assertIn("temporary webserver", self.plugin_ep.description)
def test_description_with_name(self):
self.plugin_ep.plugin_cls = mock.MagicMock(description="Desc")
@@ -101,31 +101,31 @@ class PluginEntryPointTest(unittest.TestCase):
interfaces.IInstaller, interfaces.IAuthenticator)))
def test__init__(self):
self.assertFalse(self.plugin_ep.initialized)
self.assertFalse(self.plugin_ep.prepared)
self.assertFalse(self.plugin_ep.misconfigured)
self.assertFalse(self.plugin_ep.available)
self.assertTrue(self.plugin_ep.problem is None)
self.assertTrue(self.plugin_ep.entry_point is EP_SA)
self.assertIs(self.plugin_ep.initialized, False)
self.assertIs(self.plugin_ep.prepared, False)
self.assertIs(self.plugin_ep.misconfigured, False)
self.assertIs(self.plugin_ep.available, False)
self.assertIsNone(self.plugin_ep.problem)
self.assertIs(self.plugin_ep.entry_point, EP_SA)
self.assertEqual("sa", self.plugin_ep.name)
self.assertTrue(self.plugin_ep.plugin_cls is standalone.Authenticator)
self.assertIs(self.plugin_ep.plugin_cls, standalone.Authenticator)
def test_init(self):
config = mock.MagicMock()
plugin = self.plugin_ep.init(config=config)
self.assertTrue(self.plugin_ep.initialized)
self.assertTrue(plugin.config is config)
self.assertIs(self.plugin_ep.initialized, True)
self.assertIs(plugin.config, config)
# memoize!
self.assertTrue(self.plugin_ep.init() is plugin)
self.assertTrue(plugin.config is config)
self.assertIs(self.plugin_ep.init(), plugin)
self.assertIs(plugin.config, config)
# try to give different config
self.assertTrue(self.plugin_ep.init(123) is plugin)
self.assertTrue(plugin.config is config)
self.assertIs(self.plugin_ep.init(123), plugin)
self.assertIs(plugin.config, config)
self.assertFalse(self.plugin_ep.prepared)
self.assertFalse(self.plugin_ep.misconfigured)
self.assertFalse(self.plugin_ep.available)
self.assertIs(self.plugin_ep.prepared, False)
self.assertIs(self.plugin_ep.misconfigured, False)
self.assertIs(self.plugin_ep.available, False)
def test_verify(self):
iface1 = mock.MagicMock(__name__="iface1")
@@ -155,7 +155,7 @@ class PluginEntryPointTest(unittest.TestCase):
self.plugin_ep.init(config=config)
self.plugin_ep.prepare()
self.assertTrue(self.plugin_ep.prepared)
self.assertFalse(self.plugin_ep.misconfigured)
self.assertIs(self.plugin_ep.misconfigured, False)
# output doesn't matter that much, just test if it runs
str(self.plugin_ep)
@@ -165,12 +165,11 @@ class PluginEntryPointTest(unittest.TestCase):
plugin.prepare.side_effect = errors.MisconfigurationError
# pylint: disable=protected-access
self.plugin_ep._initialized = plugin
self.assertTrue(isinstance(self.plugin_ep.prepare(),
errors.MisconfigurationError))
self.assertIsInstance(self.plugin_ep.prepare(),
errors.MisconfigurationError)
self.assertTrue(self.plugin_ep.prepared)
self.assertTrue(self.plugin_ep.misconfigured)
self.assertTrue(isinstance(self.plugin_ep.problem,
errors.MisconfigurationError))
self.assertIsInstance(self.plugin_ep.problem, errors.MisconfigurationError)
self.assertTrue(self.plugin_ep.available)
def test_prepare_no_installation(self):
@@ -178,21 +177,20 @@ class PluginEntryPointTest(unittest.TestCase):
plugin.prepare.side_effect = errors.NoInstallationError
# pylint: disable=protected-access
self.plugin_ep._initialized = plugin
self.assertTrue(isinstance(self.plugin_ep.prepare(),
errors.NoInstallationError))
self.assertTrue(self.plugin_ep.prepared)
self.assertFalse(self.plugin_ep.misconfigured)
self.assertFalse(self.plugin_ep.available)
self.assertIsInstance(self.plugin_ep.prepare(), errors.NoInstallationError)
self.assertIs(self.plugin_ep.prepared, True)
self.assertIs(self.plugin_ep.misconfigured, False)
self.assertIs(self.plugin_ep.available, False)
def test_prepare_generic_plugin_error(self):
plugin = mock.MagicMock()
plugin.prepare.side_effect = errors.PluginError
# pylint: disable=protected-access
self.plugin_ep._initialized = plugin
self.assertTrue(isinstance(self.plugin_ep.prepare(), errors.PluginError))
self.assertIsInstance(self.plugin_ep.prepare(), errors.PluginError)
self.assertTrue(self.plugin_ep.prepared)
self.assertFalse(self.plugin_ep.misconfigured)
self.assertFalse(self.plugin_ep.available)
self.assertIs(self.plugin_ep.misconfigured, False)
self.assertIs(self.plugin_ep.available, False)
def test_repr(self):
self.assertEqual("PluginEntryPoint#sa", repr(self.plugin_ep))
@@ -226,14 +224,14 @@ class PluginsRegistryTest(unittest.TestCase):
standalone.Authenticator, webroot.Authenticator,
null.Installer, null.Installer]
plugins = PluginsRegistry.find_all()
self.assertTrue(plugins["sa"].plugin_cls is standalone.Authenticator)
self.assertTrue(plugins["sa"].entry_point is EP_SA)
self.assertTrue(plugins["wr"].plugin_cls is webroot.Authenticator)
self.assertTrue(plugins["wr"].entry_point is EP_WR)
self.assertTrue(plugins["ep1"].plugin_cls is null.Installer)
self.assertTrue(plugins["ep1"].entry_point is self.ep1)
self.assertTrue(plugins["p1:ep1"].plugin_cls is null.Installer)
self.assertTrue(plugins["p1:ep1"].entry_point is self.ep1)
self.assertIs(plugins["sa"].plugin_cls, standalone.Authenticator)
self.assertIs(plugins["sa"].entry_point, EP_SA)
self.assertIs(plugins["wr"].plugin_cls, webroot.Authenticator)
self.assertIs(plugins["wr"].entry_point, EP_WR)
self.assertIs(plugins["ep1"].plugin_cls, null.Installer)
self.assertIs(plugins["ep1"].entry_point, self.ep1)
self.assertIs(plugins["p1:ep1"].plugin_cls, null.Installer)
self.assertIs(plugins["p1:ep1"].entry_point, self.ep1)
def test_getitem(self):
self.assertEqual(self.plugin_ep, self.reg["mock"])
@@ -296,10 +294,10 @@ class PluginsRegistryTest(unittest.TestCase):
self.assertEqual({}, self.reg.available()._plugins)
def test_find_init(self):
self.assertTrue(self.reg.find_init(mock.Mock()) is None)
self.assertIsNone(self.reg.find_init(mock.Mock()))
self.plugin_ep.initialized = True
self.assertTrue(
self.reg.find_init(self.plugin_ep.init()) is self.plugin_ep)
self.assertIs(
self.reg.find_init(self.plugin_ep.init()), self.plugin_ep)
def test_repr(self):
self.plugin_ep.__repr__ = lambda _: "PluginEntryPoint#mock"
+6 -6
View File
@@ -211,20 +211,20 @@ class CredentialsConfigurationRequireTest(test_util.TempDirTestCase):
class DomainNameGuessTest(unittest.TestCase):
def test_simple_case(self):
self.assertTrue(
'example.com' in
self.assertIn(
'example.com',
dns_common.base_domain_name_guesses("example.com")
)
def test_sub_domain(self):
self.assertTrue(
'example.com' in
self.assertIn(
'example.com',
dns_common.base_domain_name_guesses("foo.bar.baz.example.com")
)
def test_second_level_domain(self):
self.assertTrue(
'example.co.uk' in
self.assertIn(
'example.co.uk',
dns_common.base_domain_name_guesses("foo.bar.baz.example.co.uk")
)
+4 -5
View File
@@ -52,7 +52,7 @@ class AuthenticatorTest(test_util.TempDirTestCase):
self.assertRaises(errors.HookCommandNotFound, self.auth.prepare)
def test_more_info(self):
self.assertTrue(isinstance(self.auth.more_info(), str))
self.assertIsInstance(self.auth.more_info(), str)
def test_get_chall_pref(self):
self.assertEqual(self.auth.get_chall_pref('example.org'),
@@ -96,9 +96,8 @@ class AuthenticatorTest(test_util.TempDirTestCase):
[achall.response(achall.account_key) for achall in self.achalls])
for i, (args, kwargs) in enumerate(mock_get_utility().notification.call_args_list):
achall = self.achalls[i]
self.assertTrue(
achall.validation(achall.account_key) in args[0])
self.assertFalse(kwargs['wrap'])
self.assertIn(achall.validation(achall.account_key), args[0])
self.assertIs(kwargs['wrap'], False)
def test_cleanup(self):
self.config.manual_auth_hook = ('{0} -c "import sys; sys.stdout.write(\'foo\')"'
@@ -119,7 +118,7 @@ class AuthenticatorTest(test_util.TempDirTestCase):
os.environ['CERTBOT_TOKEN'],
achall.chall.encode('token'))
else:
self.assertFalse('CERTBOT_TOKEN' in os.environ)
self.assertNotIn('CERTBOT_TOKEN', os.environ)
if __name__ == '__main__':
+1 -1
View File
@@ -15,7 +15,7 @@ class InstallerTest(unittest.TestCase):
self.installer = Installer(config=mock.MagicMock(), name="null")
def test_it(self):
self.assertTrue(isinstance(self.installer.more_info(), str))
self.assertIsInstance(self.installer.more_info(), str)
self.assertEqual([], self.installer.get_all_names())
self.assertEqual([], self.installer.supported_enhancements())
+6 -6
View File
@@ -70,7 +70,7 @@ class PickPluginTest(unittest.TestCase):
self.assertEqual(1, self.reg.visible().ifaces.call_count)
def test_no_candidate(self):
self.assertTrue(self._call() is None)
self.assertIsNone(self._call())
def test_single(self):
plugin_ep = mock.MagicMock()
@@ -88,7 +88,7 @@ class PickPluginTest(unittest.TestCase):
self.reg.visible().ifaces().verify().available.return_value = {
"bar": plugin_ep}
self.assertTrue(self._call() is None)
self.assertIsNone(self._call())
def test_multiple(self):
plugin_ep = mock.MagicMock()
@@ -111,7 +111,7 @@ class PickPluginTest(unittest.TestCase):
with mock.patch("certbot._internal.plugins.selection.choose_plugin") as mock_choose:
mock_choose.return_value = None
self.assertTrue(self._call() is None)
self.assertIsNone(self._call())
class ChoosePluginTest(unittest.TestCase):
@@ -153,7 +153,7 @@ class ChoosePluginTest(unittest.TestCase):
@test_util.patch_get_utility("certbot._internal.plugins.selection.z_util")
def test_no_choice(self, mock_util):
mock_util().menu.return_value = (display_util.CANCEL, 0)
self.assertTrue(self._call() is None)
self.assertIsNone(self._call())
class GetUnpreparedInstallerTest(test_util.ConfigTestCase):
@@ -180,7 +180,7 @@ class GetUnpreparedInstallerTest(test_util.ConfigTestCase):
def test_no_installer_defined(self):
self.config.configurator = None
self.assertEqual(self._call(), None)
self.assertIsNone(self._call())
def test_no_available_installers(self):
self.config.configurator = "apache"
@@ -190,7 +190,7 @@ class GetUnpreparedInstallerTest(test_util.ConfigTestCase):
def test_get_plugin(self):
self.config.configurator = "apache"
installer = self._call()
self.assertTrue(installer is self.mock_apache_plugin)
self.assertIs(installer, self.mock_apache_plugin)
def test_multiple_installers_returned(self):
self.config.configurator = "apache"
+5 -6
View File
@@ -29,9 +29,8 @@ class ServerManagerTest(unittest.TestCase):
self.mgr = ServerManager(self.certs, self.http_01_resources)
def test_init(self):
self.assertTrue(self.mgr.certs is self.certs)
self.assertTrue(
self.mgr.http_01_resources is self.http_01_resources)
self.assertIs(self.mgr.certs, self.certs)
self.assertIs(self.mgr.http_01_resources, self.http_01_resources)
def _test_run_stop(self, challenge_type):
server = self.mgr.run(port=0, challenge_type=challenge_type)
@@ -48,7 +47,7 @@ class ServerManagerTest(unittest.TestCase):
port = server.getsocknames()[0][1]
server2 = self.mgr.run(port=port, challenge_type=challenges.HTTP01)
self.assertEqual(self.mgr.running(), {port: server})
self.assertTrue(server is server2)
self.assertIs(server, server2)
self.mgr.stop(port)
self.assertEqual(self.mgr.running(), {})
@@ -89,7 +88,7 @@ class AuthenticatorTest(unittest.TestCase):
self.auth.servers = mock.MagicMock()
def test_more_info(self):
self.assertTrue(isinstance(self.auth.more_info(), str))
self.assertIsInstance(self.auth.more_info(), str)
def test_get_chall_pref(self):
self.assertEqual(self.auth.get_chall_pref(domain=None),
@@ -126,7 +125,7 @@ class AuthenticatorTest(unittest.TestCase):
def _assert_correct_yesno_call(self, mock_yesno):
yesno_args, yesno_kwargs = mock_yesno.call_args
self.assertTrue("in use" in yesno_args[0])
self.assertIn("in use", yesno_args[0])
self.assertFalse(yesno_kwargs.get("default", True))
def test_perform_eacces(self):
+5 -5
View File
@@ -49,7 +49,7 @@ class PluginStorageTest(test_util.ConfigTestCase):
self.assertRaises(KeyError,
nocontent.storage.fetch, "value")
self.assertTrue(mock_log.called)
self.assertTrue("no values loaded" in mock_log.call_args[0][0])
self.assertIn("no values loaded", mock_log.call_args[0][0])
def test_load_errors_corrupted(self):
with open(os.path.join(self.config.config_dir,
@@ -61,7 +61,7 @@ class PluginStorageTest(test_util.ConfigTestCase):
self.assertRaises(errors.PluginError,
corrupted.storage.fetch,
"value")
self.assertTrue("is corrupted" in mock_log.call_args[0][0])
self.assertIn("is corrupted", mock_log.call_args[0][0])
def test_save_errors_cant_serialize(self):
with mock.patch("certbot.plugins.storage.logger.error") as mock_log:
@@ -71,7 +71,7 @@ class PluginStorageTest(test_util.ConfigTestCase):
self.plugin.storage._data = self.plugin_cls # pylint: disable=protected-access
self.assertRaises(errors.PluginStorageError,
self.plugin.storage.save)
self.assertTrue("Could not serialize" in mock_log.call_args[0][0])
self.assertIn("Could not serialize", mock_log.call_args[0][0])
def test_save_errors_unable_to_write_file(self):
mock_open = mock.mock_open()
@@ -83,7 +83,7 @@ class PluginStorageTest(test_util.ConfigTestCase):
self.plugin.storage._storagepath = "/tmp/whatever"
self.assertRaises(errors.PluginStorageError,
self.plugin.storage.save)
self.assertTrue("Could not write" in mock_log.call_args[0][0])
self.assertIn("Could not write", mock_log.call_args[0][0])
def test_save_uninitialized(self):
with mock.patch("certbot.reverter.util"):
@@ -114,7 +114,7 @@ class PluginStorageTest(test_util.ConfigTestCase):
".pluginstorage.json"), 'r') as fh:
psdata = fh.read()
psjson = json.loads(psdata)
self.assertTrue("mockplugin" in psjson.keys())
self.assertIn("mockplugin", psjson.keys())
self.assertEqual(len(psjson), 1)
self.assertEqual(psjson["mockplugin"]["testkey"], "testvalue")
+4 -4
View File
@@ -30,7 +30,7 @@ class PathSurgeryTest(unittest.TestCase):
with mock.patch.dict('os.environ', all_path):
with mock.patch('certbot.util.exe_exists') as mock_exists:
mock_exists.return_value = True
self.assertEqual(path_surgery("eg"), True)
self.assertIs(path_surgery("eg"), True)
self.assertEqual(mock_debug.call_count, 0)
self.assertEqual(os.environ["PATH"], all_path["PATH"])
if os.name != 'nt':
@@ -39,9 +39,9 @@ class PathSurgeryTest(unittest.TestCase):
with mock.patch.dict('os.environ', no_path):
path_surgery("thingy")
self.assertEqual(mock_debug.call_count, 2 if os.name != 'nt' else 1)
self.assertTrue("Failed to find" in mock_debug.call_args[0][0])
self.assertTrue("/usr/local/bin" in os.environ["PATH"])
self.assertTrue("/tmp" in os.environ["PATH"])
self.assertIn("Failed to find", mock_debug.call_args[0][0])
self.assertIn("/usr/local/bin", os.environ["PATH"])
self.assertIn("/tmp", os.environ["PATH"])
if __name__ == "__main__":
+4 -4
View File
@@ -58,8 +58,8 @@ class AuthenticatorTest(unittest.TestCase):
def test_more_info(self):
more_info = self.auth.more_info()
self.assertTrue(isinstance(more_info, str))
self.assertTrue(self.path in more_info)
self.assertIsInstance(more_info, str)
self.assertIn(self.path, more_info)
def test_add_parser_arguments(self):
add = mock.MagicMock()
@@ -79,7 +79,7 @@ class AuthenticatorTest(unittest.TestCase):
self.auth.perform([self.achall])
self.assertTrue(mock_display.menu.called)
for call in mock_display.menu.call_args_list:
self.assertTrue(self.achall.domain in call[0][0])
self.assertIn(self.achall.domain, call[0][0])
self.assertTrue(all(
webroot in call[0][1]
for webroot in self.config.webroot_map.values()))
@@ -96,7 +96,7 @@ class AuthenticatorTest(unittest.TestCase):
self.assertRaises(errors.PluginError, self.auth.perform, [self.achall])
self.assertTrue(mock_display.menu.called)
for call in mock_display.menu.call_args_list:
self.assertTrue(self.achall.domain in call[0][0])
self.assertIn(self.achall.domain, call[0][0])
self.assertTrue(all(
webroot in call[0][1]
for webroot in self.config.webroot_map.values()))