Esempio n. 1
0
    def compare(self):
        # 1. If the original EnumDescriptor is None,
        # then a new EnumDescriptor is added.
        if self.enum_original is None:
            self.finding_container.add_finding(
                category=FindingCategory.ENUM_ADDITION,
                proto_file_name=self.enum_update.proto_file_name,
                source_code_line=self.enum_update.source_code_line,
                subject=self.enum_update.name,
                context=self.context,
                change_type=ChangeType.MINOR,
            )

        # 2. If the updated EnumDescriptor is None,
        # then the original EnumDescriptor is removed.
        elif self.enum_update is None:
            self.finding_container.add_finding(
                category=FindingCategory.ENUM_REMOVAL,
                proto_file_name=self.enum_original.proto_file_name,
                source_code_line=self.enum_original.source_code_line,
                subject=self.enum_original.name,
                context=self.context,
                change_type=ChangeType.MAJOR,
            )

        # 3. If the EnumDescriptors have the same name, check the values
        # of them stay the same. Enum values are identified by number.
        else:
            enum_values_dict_original = self.enum_original.values
            enum_values_dict_update = self.enum_update.values
            enum_values_keys_set_original = set(enum_values_dict_original.keys())
            enum_values_keys_set_update = set(enum_values_dict_update.keys())
            # Compare Enum values that only exist in the original version.
            for number in enum_values_keys_set_original - enum_values_keys_set_update:
                EnumValueComparator(
                    enum_values_dict_original[number],
                    None,
                    self.finding_container,
                    context=self.enum_update.name,
                ).compare()
            # Compare Enum values that only exist in the update version.
            for number in enum_values_keys_set_update - enum_values_keys_set_original:
                EnumValueComparator(
                    None,
                    enum_values_dict_update[number],
                    self.finding_container,
                    context=self.enum_update.name,
                ).compare()
            # Compare Enum values that exist in both original and update versions.
            for number in enum_values_keys_set_original & enum_values_keys_set_update:
                EnumValueComparator(
                    enum_values_dict_original[number],
                    enum_values_dict_update[number],
                    self.finding_container,
                    context=self.enum_update.name,
                ).compare()
    def compare(self):
        # 1. If original EnumDescriptor is None, then a new EnumDescriptor is added.
        if self.enum_original is None:
            msg = 'A new Enum {} is added.'.format(self.enum_update.name)
            FindingContainer.addFinding(FindingCategory.ENUM_ADDITION, "", msg,
                                        False)

        # 2. If updated EnumDescriptor is None, then the original EnumDescriptor is removed.
        elif self.enum_update is None:
            msg = 'An Enum {} is removed'.format(self.enum_original.name)
            FindingContainer.addFinding(FindingCategory.ENUM_REMOVAL, "", msg,
                                        True)

        # 3. If both EnumDescriptors are existing, check if the name is changed.
        elif self.enum_original.name != self.enum_update.name:
            msg = 'Name of the Enum is changed, the original is {}, but the updated is {}'.format(
                self.enum_original.name, self.enum_update.name)
            FindingContainer.addFinding(FindingCategory.ENUM_NAME_CHANGE, "",
                                        msg, True)

        # 4. If the EnumDescriptors have the same name, check the values of them stay the same.
        # Enum values are identified by number, not by name.
        else:
            enum_values_dict_original = {
                x.number: x
                for x in self.enum_original.values
            }
            enum_values_dict_update = {
                x.number: x
                for x in self.enum_update.values
            }
            # Compare Enum values that only exist in original version
            for number in list(
                    set(enum_values_dict_original.keys()) -
                    set(enum_values_dict_update.keys())):
                EnumValueComparator(enum_values_dict_original[number],
                                    None).compare()
            # Compare Enum values that only exist in update version
            for number in list(
                    set(enum_values_dict_update.keys()) -
                    set(enum_values_dict_original.keys())):
                EnumValueComparator(None,
                                    enum_values_dict_update[number]).compare()
            # Compare Enum values that exist both in original and update versions
            for number in list(
                    set(enum_values_dict_update.keys())
                    & set(enum_values_dict_original.keys())):
                EnumValueComparator(enum_values_dict_original[number],
                                    enum_values_dict_update[number]).compare()
Esempio n. 3
0
 def test_enum_value_addition(self):
     EnumValueComparator(None, self.enum_foo,
                         self.finding_container).compare()
     finding = self.finding_container.getAllFindings()[0]
     self.assertEqual(finding.message, "A new EnumValue `FOO` is added.")
     self.assertEqual(finding.category.name, "ENUM_VALUE_ADDITION")
     self.assertEqual(finding.change_type.name, "MINOR")
     self.assertEqual(finding.location.proto_file_name, "test.proto")
     self.assertEqual(finding.location.source_code_line, 2)
 def enumValueNameChange(self):
     EnumValueComparator(self.enumValue_mobile,
                         self.enumValue_home).compare()
     finding = FindingContainer.getAllFindings()[0]
     self.assertEqual(
         finding.message,
         'Name of the EnumValue is changed, the original is MOBILE, but the updated is HOME'
     )
     self.assertEqual(finding.category.name, 'ENUM_VALUE_NAME_CHANGE')
Esempio n. 5
0
    def compare(self):
        # 1. If original EnumDescriptor is None, then a new
        # EnumDescriptor is added.
        if self.enum_original is None:
            FindingContainer.addFinding(
                category=FindingCategory.ENUM_ADDITION,
                location=
                f"{self.enum_update.proto_file_name} Line: {self.enum_update.source_code_line}",
                message=f"A new Enum {self.enum_update.name} is added.",
                actionable=False,
            )

        # 2. If updated EnumDescriptor is None, then the original
        # EnumDescriptor is removed.
        elif self.enum_update is None:
            FindingContainer.addFinding(
                category=FindingCategory.ENUM_REMOVAL,
                location=
                f"{self.enum_original.proto_file_name} Line: {self.enum_original.source_code_line}",
                message=f"An Enum {self.enum_original.name} is removed",
                actionable=True,
            )

        # 3. If the EnumDescriptors have the same name, check the values
        # of them stay the same. Enum values are identified by number,
        # not by name.
        else:
            enum_values_dict_original = self.enum_original.values
            enum_values_dict_update = self.enum_update.values
            enum_values_keys_set_original = set(
                enum_values_dict_original.keys())
            enum_values_keys_set_update = set(enum_values_dict_update.keys())
            # Compare Enum values that only exist in original version
            for number in enum_values_keys_set_original - enum_values_keys_set_update:
                EnumValueComparator(enum_values_dict_original[number],
                                    None).compare()
            # Compare Enum values that only exist in update version
            for number in enum_values_keys_set_update - enum_values_keys_set_original:
                EnumValueComparator(None,
                                    enum_values_dict_update[number]).compare()
            # Compare Enum values that exist both in original and update versions
            for number in enum_values_keys_set_original & enum_values_keys_set_update:
                EnumValueComparator(enum_values_dict_original[number],
                                    enum_values_dict_update[number]).compare()
 def test_name_change(self):
     EnumValueComparator(self.enum_foo,
                         self.enum_bar,
                         self.finding_container,
                         context="ctx").compare()
     finding = self.finding_container.get_all_findings()[0]
     self.assertEqual(finding.category.name, "ENUM_VALUE_NAME_CHANGE")
     self.assertEqual(finding.change_type.name, "MAJOR")
     self.assertEqual(finding.location.proto_file_name, "test_update.proto")
     self.assertEqual(finding.location.source_code_line, 2)
 def test_enum_value_addition(self):
     EnumValueComparator(None,
                         self.enum_foo,
                         self.finding_container,
                         context="ctx").compare()
     finding = self.finding_container.get_all_findings()[0]
     self.assertEqual(finding.category.name, "ENUM_VALUE_ADDITION")
     self.assertEqual(finding.change_type.name, "MINOR")
     self.assertEqual(finding.location.proto_file_name, "test.proto")
     self.assertEqual(finding.location.source_code_line, 2)
 def test_name_change(self):
     EnumValueComparator(self.enumValue_mobile,
                         self.enumValue_home).compare()
     finding = FindingContainer.getAllFindings()[0]
     self.assertEqual(
         finding.message,
         "Name of the EnumValue is changed, the original "
         "is MOBILE, but the updated is HOME",
     )
     self.assertEqual(finding.category.name, "ENUM_VALUE_NAME_CHANGE")
     self.assertEqual(finding.location.path, "message_v1.proto Line: 12")
Esempio n. 9
0
 def test_name_change(self):
     EnumValueComparator(self.enum_foo, self.enum_bar,
                         self.finding_container).compare()
     finding = self.finding_container.getAllFindings()[0]
     self.assertEqual(
         finding.message,
         "Name of the EnumValue is changed from `FOO` to `BAR`.")
     self.assertEqual(finding.category.name, "ENUM_VALUE_NAME_CHANGE")
     self.assertEqual(finding.change_type.name, "MAJOR")
     self.assertEqual(finding.location.proto_file_name, "test_update.proto")
     self.assertEqual(finding.location.source_code_line, 2)
Esempio n. 10
0
 def test_enum_value_removal(self):
     EnumValueComparator(
         self.enum_foo,
         None,
         self.finding_container,
     ).compare()
     finding = self.finding_container.getAllFindings()[0]
     self.assertEqual(finding.message,
                      "An existing EnumValue `FOO` is removed.")
     self.assertEqual(finding.category.name, "ENUM_VALUE_REMOVAL")
     self.assertEqual(finding.change_type.name, "MAJOR")
     self.assertEqual(finding.location.proto_file_name, "test.proto")
     self.assertEqual(finding.location.source_code_line, 2)
 def noApiChange(self):
     EnumValueComparator(self.enumValue_mobile,
                         self.enumValue_mobile).compare()
     self.assertEqual(len(FindingContainer.getAllFindings()), 0)
 def enumValueAddition(self):
     EnumValueComparator(None, self.enumValue_home).compare()
     finding = FindingContainer.getAllFindings()[0]
     self.assertEqual(finding.message, 'A new EnumValue HOME is added.')
     self.assertEqual(finding.category.name, 'ENUM_VALUE_ADDITION')
 def test_enum_value_addition(self):
     EnumValueComparator(None, self.enumValue_home).compare()
     finding = FindingContainer.getAllFindings()[0]
     self.assertEqual(finding.message, "A new EnumValue HOME is added.")
     self.assertEqual(finding.category.name, "ENUM_VALUE_ADDITION")
     self.assertEqual(finding.location.path, "message_v1.proto Line: 12")
 def test_enum_value_removal(self):
     EnumValueComparator(self.enumValue_mobile, None).compare()
     finding = FindingContainer.getAllFindings()[0]
     self.assertEqual(finding.message, "An EnumValue MOBILE is removed")
     self.assertEqual(finding.category.name, "ENUM_VALUE_REMOVAL")
     self.assertEqual(finding.location.path, "message_v1.proto Line: 11")
 def test_no_api_change(self):
     EnumValueComparator(self.enum_foo,
                         self.enum_foo,
                         self.finding_container,
                         context="ctx").compare()
     self.assertEqual(len(self.finding_container.get_all_findings()), 0)
Esempio n. 16
0
 def test_no_api_change(self):
     EnumValueComparator(self.enum_foo, self.enum_foo,
                         self.finding_container).compare()
     self.assertEqual(len(self.finding_container.getAllFindings()), 0)
 def enumValueRemoval(self):
     EnumValueComparator(self.enumValue_mobile, None).compare()
     finding = FindingContainer.getAllFindings()[0]
     self.assertEqual(finding.message, 'An EnumValue MOBILE is removed')
     self.assertEqual(finding.category.name, 'ENUM_VALUE_REMOVAL')