def setUp(self):
     L = descriptor_pb2.SourceCodeInfo.Location
     # fmt: off
     locations = [
         L(path=(
             4,
             0,
         ), span=(1, 2, 3, 4)),
         # Enum will add (2, 1) for each EnumValue in the path.
         L(path=(4, 0, 2, 1), span=(2, 3, 4, 5)),
     ]
     self.enum_foo = make_enum(
         name="Foo",
         values=(("VALUE1", 1), ),
         proto_file_name="test.proto",
         locations=locations,
         path=(
             4,
             0,
         ),
     )
     self.enum_bar = make_enum(
         name="Bar",
         values=(
             ("VALUE1", 1),
             ("VALUE2", 2),
         ),
         proto_file_name="test_update.proto",
         locations=locations,
         path=(
             4,
             0,
         ),
     )
     self.finding_container = FindingContainer()
Exemplo n.º 2
0
class FindingContainerTest(unittest.TestCase):
    # The unit tests are executed in alphabetical order by test name
    # automatically, so test_reset() will be run in the middle which will break us.
    # This is a monolithic test, so we give numbers to indicate
    # the steps and also ensure the execution orders.
    finding_container = FindingContainer()

    def test1_add_findings(self):
        self.finding_container.add_finding(
            category=FindingCategory.METHOD_REMOVAL,
            proto_file_name="my_proto.proto",
            source_code_line=12,
            change_type=ChangeType.MAJOR,
            subject="subject",
            context="context",
        )
        self.assertEqual(len(self.finding_container.get_all_findings()), 1)

    def test2_get_actionable_findings(self):
        self.finding_container.add_finding(
            category=FindingCategory.FIELD_ADDITION,
            proto_file_name="my_proto.proto",
            source_code_line=15,
            change_type=ChangeType.MINOR,
        )
        self.assertEqual(len(self.finding_container.get_actionable_findings()),
                         1)

    def test3_to_dict_arr(self):
        dict_arr_output = self.finding_container.to_dict_arr()
        self.assertEqual(dict_arr_output[0]["category"], "METHOD_REMOVAL")
        self.assertEqual(dict_arr_output[1]["category"], "FIELD_ADDITION")

    def test4_to_human_readable_message(self):
        self.finding_container.add_finding(
            category=FindingCategory.RESOURCE_DEFINITION_REMOVAL,
            proto_file_name="my_proto.proto",
            source_code_line=5,
            change_type=ChangeType.MAJOR,
            subject="subject",
        )
        self.finding_container.add_finding(
            category=FindingCategory.METHOD_SIGNATURE_REMOVAL,
            proto_file_name="my_other_proto.proto",
            source_code_line=-1,
            change_type=ChangeType.MAJOR,
            type="type",
            subject="subject",
            context="context",
        )
        self.assertEqual(
            self.finding_container.to_human_readable_message(),
            "my_other_proto.proto: An existing method_signature `type` is removed from method `subject` in service `context`.\n"
            +
            "my_proto.proto L5: An existing resource_definition `subject` is removed.\n"
            +
            "my_proto.proto L12: An existing method `subject` is removed from service `context`.\n",
        )
Exemplo n.º 3
0
 def __init__(
     self,
     descriptor_set_original: desc.FileDescriptorSet,
     descriptor_set_update: desc.FileDescriptorSet,
     opts: Optional[Options] = None,
 ):
     self.descriptor_set_original = descriptor_set_original
     self.descriptor_set_update = descriptor_set_update
     self.opts = opts
     self.finding_container = FindingContainer()
Exemplo n.º 4
0
class FindingContainerTest(unittest.TestCase):
    # The unit tests are executed in alphabetical order by test name
    # automatically, so test_reset() will be run in the middle which will break us.
    # This is a monolithic test, so we give numbers to indicate
    # the steps and also ensure the execution orders.
    finding_container = FindingContainer()

    def test1_add_findings(self):
        self.finding_container.addFinding(
            category=FindingCategory.METHOD_REMOVAL,
            proto_file_name="my_proto.proto",
            source_code_line=12,
            message="An rpc method bar is removed.",
            change_type=ChangeType.MAJOR,
        )
        self.assertEqual(len(self.finding_container.getAllFindings()), 1)

    def test2_get_actionable_findings(self):
        self.finding_container.addFinding(
            category=FindingCategory.FIELD_ADDITION,
            proto_file_name="my_proto.proto",
            source_code_line=15,
            message="Not breaking change.",
            change_type=ChangeType.MINOR,
        )
        self.assertEqual(len(self.finding_container.getActionableFindings()),
                         1)

    def test3_toDictArr(self):
        dict_arr_output = self.finding_container.toDictArr()
        self.assertEqual(dict_arr_output[0]["category"], "METHOD_REMOVAL")
        self.assertEqual(dict_arr_output[1]["category"], "FIELD_ADDITION")

    def test4_toHumanReadableMessage(self):
        self.finding_container.addFinding(
            category=FindingCategory.RESOURCE_DEFINITION_CHANGE,
            proto_file_name="my_proto.proto",
            source_code_line=5,
            message="An existing file-level resource definition has changed.",
            change_type=ChangeType.MAJOR,
        )
        self.finding_container.addFinding(
            category=FindingCategory.METHOD_SIGNATURE_CHANGE,
            proto_file_name="my_other_proto.proto",
            source_code_line=-1,
            message=
            "An existing method_signature is changed from 'sig1' to 'sig2'.",
            change_type=ChangeType.MAJOR,
        )
        self.assertEqual(
            self.finding_container.toHumanReadableMessage(),
            "my_proto.proto L5: An existing file-level resource definition has changed.\n"
            + "my_proto.proto L12: An rpc method bar is removed.\n" +
            "my_other_proto.proto: An existing method_signature is changed from 'sig1' to 'sig2'.\n",
        )
Exemplo n.º 5
0
 def setUp(self):
     L = descriptor_pb2.SourceCodeInfo.Location
     locations = [L(path=(2, 1), span=(1, 2))]
     self.enum_foo = make_enum_value(
         name="FOO",
         number=1,
         proto_file_name="test.proto",
         locations=locations,
         path=(2, 1),
     )
     self.enum_bar = make_enum_value(
         name="BAR",
         number=1,
         proto_file_name="test_update.proto",
         locations=locations,
         path=(2, 1),
     )
     self.finding_container = FindingContainer()
Exemplo n.º 6
0
 def setUp(self):
     self.finding_container = FindingContainer()
 def setUp(self):
     self.message_foo = make_message("Message")
     self.finding_container = FindingContainer()
Exemplo n.º 8
0
 def setUp(self):
     self.service_foo = make_service(name="Foo")
     self.finding_container = FindingContainer()