Browse Source

fix up assertEqual(None...) check to catch more cases

To finally close out bug #1280522 it turned out that our hacking check
to eliminate assertEqual(None, ...) was pretty sub optimal. The
perfect fix would actually use the tokenizer, however this is a better
regex which catches a few more missing bits in our codebase. It's
probably close enough for most of what we need.

Fix the relevant tests that would fail the updating hacking check.

Change-Id: Ic8cb1192e001409d827c8da55fe536681895944b
Closes-Bug: #1280522
tags/2014.2.rc1
Sean Dague 4 years ago
parent
commit
5dd34c93e2

+ 4
- 4
nova/hacking/checks.py View File

@@ -51,9 +51,9 @@ asse_equal_type_re = re.compile(
51 51
                        r"(.)*assertEqual\(type\((\w|\.|\'|\"|\[|\])+\), "
52 52
                        "(\w|\.|\'|\"|\[|\])+\)")
53 53
 asse_equal_end_with_none_re = re.compile(
54
-                           r"(.)*assertEqual\((\w|\.|\'|\"|\[|\])+, None\)")
54
+                           r"assertEqual\(.*?,\s+None\)$")
55 55
 asse_equal_start_with_none_re = re.compile(
56
-                           r"(.)*assertEqual\(None, (\w|\.|\'|\"|\[|\])+\)")
56
+                           r"assertEqual\(None,")
57 57
 conf_attribute_set_re = re.compile(r"CONF\.[a-z0-9_.]+\s*=\s*\w")
58 58
 log_translation = re.compile(
59 59
     r"(.)*LOG\.(audit|error|info|warn|warning|critical|exception)\(\s*('|\")")
@@ -250,8 +250,8 @@ def assert_equal_none(logical_line):
250 250
 
251 251
     N318
252 252
     """
253
-    res = (asse_equal_start_with_none_re.match(logical_line) or
254
-           asse_equal_end_with_none_re.match(logical_line))
253
+    res = (asse_equal_start_with_none_re.search(logical_line) or
254
+           asse_equal_end_with_none_re.search(logical_line))
255 255
     if res:
256 256
         yield (0, "N318: assertEqual(A, None) or assertEqual(None, A) "
257 257
                "sentences not allowed")

+ 1
- 1
nova/tests/api/openstack/test_wsgi.py View File

@@ -125,7 +125,7 @@ class RequestTest(test.NoDBTestCase):
125 125
                 compute_nodes[1])
126 126
         self.assertEqual(request.get_db_compute_node('id2'),
127 127
                 compute_nodes[2])
128
-        self.assertEqual(request.get_db_compute_node('id3'), None)
128
+        self.assertIsNone(request.get_db_compute_node('id3'))
129 129
         self.assertEqual(request.get_db_compute_nodes(),
130 130
                 {'id0': compute_nodes[0],
131 131
                  'id1': compute_nodes[1],

+ 3
- 3
nova/tests/network/test_api.py View File

@@ -517,9 +517,9 @@ class ApiTestCase(test.TestCase):
517 517
     @mock.patch('nova.objects.FixedIP.get_by_floating_address')
518 518
     def test_get_instance_by_floating_address_none(self, mock_get_by_floating):
519 519
         mock_get_by_floating.return_value = None
520
-        self.assertEqual(None,
521
-                         self.network_api.get_instance_id_by_floating_address(
522
-                             self.context, mock.sentinel.floating))
520
+        self.assertIsNone(
521
+            self.network_api.get_instance_id_by_floating_address(
522
+                self.context, mock.sentinel.floating))
523 523
         mock_get_by_floating.assert_called_once_with(self.context,
524 524
                                                      mock.sentinel.floating)
525 525
 

+ 2
- 2
nova/tests/pci/test_pci_stats.py View File

@@ -237,7 +237,7 @@ class PciDeviceStatsWithTagsTestCase(test.NoDBTestCase):
237 237
         pci_dev_obj = objects.PciDevice.create(pci_dev)
238 238
         self.pci_stats.add_device(pci_dev_obj)
239 239
         # There should be no change
240
-        self.assertEqual(None,
240
+        self.assertIsNone(
241 241
             self.pci_stats._create_pool_keys_from_dev(pci_dev_obj))
242 242
         self._assertPools()
243 243
 
@@ -253,7 +253,7 @@ class PciDeviceStatsWithTagsTestCase(test.NoDBTestCase):
253 253
         pci_dev_obj = objects.PciDevice.create(pci_dev)
254 254
         self.pci_stats.remove_device(pci_dev_obj)
255 255
         # There should be no change
256
-        self.assertEqual(None,
256
+        self.assertIsNone(
257 257
             self.pci_stats._create_pool_keys_from_dev(pci_dev_obj))
258 258
         self._assertPools()
259 259
 

+ 2
- 2
nova/tests/virt/libvirt/test_blockinfo.py View File

@@ -725,10 +725,10 @@ class LibvirtBlockInfoTest(test.TestCase):
725 725
         self.assertEqual('/dev/vda', blockinfo.get_device_name(driver_bdm))
726 726
 
727 727
         bdm_obj.device_name = None
728
-        self.assertEqual(None, blockinfo.get_device_name(bdm_obj))
728
+        self.assertIsNone(blockinfo.get_device_name(bdm_obj))
729 729
 
730 730
         driver_bdm = driver_block_device.DriverVolumeBlockDevice(bdm_obj)
731
-        self.assertEqual(None, blockinfo.get_device_name(driver_bdm))
731
+        self.assertIsNone(blockinfo.get_device_name(driver_bdm))
732 732
 
733 733
     @mock.patch('nova.virt.libvirt.blockinfo.find_disk_dev_for_disk_bus',
734 734
                 return_value='vda')

+ 1
- 1
nova/tests/virt/libvirt/test_imagebackend.py View File

@@ -1287,6 +1287,6 @@ class UtilTestCase(test.NoDBTestCase):
1287 1287
     def test_get_hw_disk_discard(self):
1288 1288
         self.assertEqual('unmap', imagebackend.get_hw_disk_discard("unmap"))
1289 1289
         self.assertEqual('ignore', imagebackend.get_hw_disk_discard("ignore"))
1290
-        self.assertEqual(None, imagebackend.get_hw_disk_discard(None))
1290
+        self.assertIsNone(imagebackend.get_hw_disk_discard(None))
1291 1291
         self.assertRaises(RuntimeError, imagebackend.get_hw_disk_discard,
1292 1292
                           "fake")

+ 4
- 7
nova/tests/virt/xenapi/test_volume_utils.py View File

@@ -37,9 +37,8 @@ class SROps(stubs.XenAPITestBaseNoDB):
37 37
         self.session = mock.Mock()
38 38
         self.session.XenAPI.Failure = UUIDException
39 39
         self.session.call_xenapi.side_effect = UUIDException
40
-        self.assertEqual(volume_utils.find_sr_by_uuid(self.session,
41
-                                                      'sr_uuid'),
42
-                         None)
40
+        self.assertIsNone(
41
+            volume_utils.find_sr_by_uuid(self.session, 'sr_uuid'))
43 42
 
44 43
     def test_find_sr_from_vdi(self):
45 44
         vdi_ref = 'fake-ref'
@@ -81,11 +80,9 @@ class ISCSIParametersTestCase(stubs.XenAPITestBaseNoDB):
81 80
                          'host')
82 81
 
83 82
         # There is no default value
84
-        self.assertEqual(volume_utils._get_target_host(':port'),
85
-                         None)
83
+        self.assertIsNone(volume_utils._get_target_host(':port'))
86 84
 
87
-        self.assertEqual(volume_utils._get_target_host(None),
88
-                         None)
85
+        self.assertIsNone(volume_utils._get_target_host(None))
89 86
 
90 87
     def test_target_port(self):
91 88
         self.assertEqual(volume_utils._get_target_port('host:port'),

Loading…
Cancel
Save