Пример #1
0
 def test_crazy_whitespace(self):
     with temporary_dir() as workdir:
         filename = 'crazy_whitespace.proto'
         with open(os.path.join(workdir, filename), 'w') as fd:
             fd.write(
                 dedent('''
         package
            org.pantsbuild.protos; option
                java_multiple_files
                = true; option java_package =
                "com.example.foo.bar"; message
         Foo
         {
         enum
         Bar {
         BAZ = 0; } } message
         FooBar
         { }
       '''))
             fd.close()
             proto_parse = ProtobufParse(fd.name, filename)
             proto_parse.parse()
             self.assertEqual('com.example.foo.bar', proto_parse.package)
             self.assertEqual(set(['Bar']), proto_parse.enums)
             self.assertEqual(set(['Foo', 'FooBar']), proto_parse.messages)
             self.assertEqual(set(), proto_parse.services)
             self.assertEqual('CrazyWhitespace',
                              proto_parse.outer_class_name)
Пример #2
0
 def test_crazy_whitespace(self):
   with temporary_dir() as workdir:
     filename = 'crazy_whitespace.proto'
     with open(os.path.join(workdir, filename), 'w') as fd:
       fd.write(dedent('''
           package
              org.pantsbuild.protos; option
                  java_multiple_files
                  = true; option java_package =
                  "com.example.foo.bar"; message
           Foo
           {
           enum
           Bar {
           BAZ = 0; } } message
           FooBar
           { }
         '''))
       fd.close()
       proto_parse = ProtobufParse(fd.name, filename)
       proto_parse.parse()
       self.assertEqual('com.example.foo.bar', proto_parse.package)
       self.assertEqual(set(['Bar']), proto_parse.enums)
       self.assertEqual(set(['Foo', 'FooBar']), proto_parse.messages)
       self.assertEqual(set(), proto_parse.services)
       self.assertEqual('CrazyWhitespace', proto_parse.outer_class_name)
Пример #3
0
  def calculate_genfiles(self, path, source):
    protobuf_parse = ProtobufParse(path, source)
    protobuf_parse.parse()

    genfiles = defaultdict(set)
    genfiles['py'].update(self.calculate_python_genfiles(source))
    genfiles['java'].update(self.calculate_java_genfiles(protobuf_parse))
    return genfiles
Пример #4
0
def calculate_genfiles(path, source, service_prefixes):
  protobuf_parse = ProtobufParse(path, source)
  protobuf_parse.parse()

  genfiles = defaultdict(set)
  genfiles['py'].update(calculate_python_genfiles(source))
  genfiles['java'].update(calculate_java_genfiles(protobuf_parse, service_prefixes))
  return genfiles
Пример #5
0
def calculate_genfiles(path, source):
  protobuf_parse = ProtobufParse(path, source)
  protobuf_parse.parse()

  genfiles = defaultdict(set)
  genfiles['py'].update(calculate_python_genfiles(source))
  genfiles['java'].update(calculate_java_genfiles(protobuf_parse))
  return genfiles
Пример #6
0
 def test_no_newline_at_all3(self):
   with temporary_dir() as workdir:
     filename = 'no_newline_at_all3.proto'
     with open(os.path.join(workdir, filename), 'w') as fd:
       fd.write('package org.pantsbuild.protos; option java_package = "com.example.foo.bar"; message Foo { }')
       fd.close()
       proto_parse = ProtobufParse(fd.name, filename)
       proto_parse.parse()
       self.assertEqual('com.example.foo.bar', proto_parse.package)
       self.assertEqual(set(), proto_parse.enums)
       self.assertEqual(set(['Foo', ]), proto_parse.messages)
       self.assertEqual(set(), proto_parse.services)
       self.assertEqual('NoNewlineAtAll3', proto_parse.outer_class_name)
Пример #7
0
 def test_no_newline_at_all2(self):
   with temporary_dir() as workdir:
     filename = 'no_newline_at_all2.proto'
     with open(os.path.join(workdir, filename), 'w') as fd:
       fd.write('package org.pantsbuild.protos; message Foo {'
                + 'enum Bar { BAZ = 0; } } message FooBar { }')
       fd.close()
       proto_parse = ProtobufParse(fd.name, filename)
       proto_parse.parse()
       self.assertEqual('org.pantsbuild.protos', proto_parse.package)
       self.assertEqual(set(['Bar']), proto_parse.enums)
       self.assertEqual(set(['Foo', 'FooBar']), proto_parse.messages)
       self.assertEqual(set(), proto_parse.services)
       self.assertEqual('NoNewlineAtAll2', proto_parse.outer_class_name)
Пример #8
0
 def test_extend(self):
   with temporary_dir() as workdir:
     filename = 'testextend.proto'
     with open(os.path.join(workdir, filename), 'w') as fd:
       fd.write(dedent('''
           package org.pantsbuild.protos;
           extend Foo {
             optional int32 bar = 126;
           }
         '''))
       fd.close()
       proto_parse = ProtobufParse(fd.name, filename)
       proto_parse.parse()
       self.assertEqual(set(['Foo']), proto_parse.extends)
Пример #9
0
 def test_no_newline_at_all2(self):
     with temporary_dir() as workdir:
         filename = 'no_newline_at_all2.proto'
         with open(os.path.join(workdir, filename), 'w') as fd:
             fd.write('package org.pantsbuild.protos; message Foo {' +
                      'enum Bar { BAZ = 0; } } message FooBar { }')
             fd.close()
             proto_parse = ProtobufParse(fd.name, filename)
             proto_parse.parse()
             self.assertEqual('org.pantsbuild.protos', proto_parse.package)
             self.assertEqual(set(['Bar']), proto_parse.enums)
             self.assertEqual(set(['Foo', 'FooBar']), proto_parse.messages)
             self.assertEqual(set(), proto_parse.services)
             self.assertEqual('NoNewlineAtAll2',
                              proto_parse.outer_class_name)
Пример #10
0
 def test_extend(self):
     with temporary_dir() as workdir:
         filename = 'testextend.proto'
         with open(os.path.join(workdir, filename), 'w') as fd:
             fd.write(
                 dedent('''
         package org.pantsbuild.protos;
         extend Foo {
           optional int32 bar = 126;
         }
       '''))
             fd.close()
             proto_parse = ProtobufParse(fd.name, filename)
             proto_parse.parse()
             self.assertEqual(set(['Foo']), proto_parse.extends)
Пример #11
0
  def calculate_genfiles(self, path, source, service_writer):
    protobuf_parse = ProtobufParse(path, source)
    protobuf_parse.parse()

    types = protobuf_parse.messages | protobuf_parse.enums
    if service_writer:
      types |= protobuf_parse.services

    # Wire generates a single type for all of the 'extends' declarations in this file.
    if protobuf_parse.extends:
      types |= {"Ext_{0}".format(protobuf_parse.filename)}

    java_files = self.calculate_java_genfiles(protobuf_parse.package, types)
    logger.debug('Path {path} yielded types {types} got files {java_files}'
                 .format(path=path, types=types, java_files=java_files))
    return set(java_files)
Пример #12
0
  def test_update_type_list(self):
    match = ProtobufParse._message_parser_re().match('message Temperature {')

    expected_value = set()
    expected_value.add('Temperature')
    actual_value = set()
    update_type_list(match, 0, actual_value)
    self.assertEqual(expected_value, actual_value)
Пример #13
0
 def test_no_newline_at_all3(self):
     with temporary_dir() as workdir:
         filename = 'no_newline_at_all3.proto'
         with open(os.path.join(workdir, filename), 'w') as fd:
             fd.write(
                 'package org.pantsbuild.protos; option java_package = "com.example.foo.bar"; message Foo { }'
             )
             fd.close()
             proto_parse = ProtobufParse(fd.name, filename)
             proto_parse.parse()
             self.assertEqual('com.example.foo.bar', proto_parse.package)
             self.assertEqual(set(), proto_parse.enums)
             self.assertEqual(set([
                 'Foo',
             ]), proto_parse.messages)
             self.assertEqual(set(), proto_parse.services)
             self.assertEqual('NoNewlineAtAll3',
                              proto_parse.outer_class_name)
Пример #14
0
    def calculate_genfiles(self, path, source, service_writer):
        protobuf_parse = ProtobufParse(path, source)
        protobuf_parse.parse()

        types = protobuf_parse.messages | protobuf_parse.enums
        if service_writer:
            types |= protobuf_parse.services

        # Wire generates a single type for all of the 'extends' declarations in this file.
        if protobuf_parse.extends:
            types |= {"Ext_{0}".format(protobuf_parse.filename)}

        java_files = self.calculate_java_genfiles(protobuf_parse.package,
                                                  types)
        logger.debug(
            'Path {path} yielded types {types} got files {java_files}'.format(
                path=path, types=types, java_files=java_files))
        return set(java_files)
Пример #15
0
 def test_inner_class_no_newline(self):
   with temporary_dir() as workdir:
     filename = 'inner_class_no_newline.proto'
     with open(os.path.join(workdir, filename), 'w') as fd:
       fd.write(dedent('''
           package org.pantsbuild.protos;
           option java_multiple_files = true;
           message Foo {
              enum Bar { BAZ = 0; }
           }
         '''))
       fd.close()
       proto_parse = ProtobufParse(fd.name, filename)
       proto_parse.parse()
       self.assertEqual('org.pantsbuild.protos', proto_parse.package)
       self.assertEqual(set(['Bar']), proto_parse.enums)
       self.assertEqual(set(['Foo']), proto_parse.messages)
       self.assertEqual(set(), proto_parse.services)
       self.assertEqual('InnerClassNoNewline', proto_parse.outer_class_name)
Пример #16
0
  def test_parse_for(self):
    with temporary_dir() as workdir:
      filename = 'temperatures.proto'
      with open(os.path.join(workdir, filename), 'w') as fd:
        fd.write(dedent('''
            package org.pantsbuild.example.temperature;
            message Temperature {
              optional string unit = 1;
              required int64 number = 2;
            }
          '''))
        fd.close()

        proto_parser = ProtobufParse(fd.name, filename)
        proto_parser.parse()
        self.assertEqual('org.pantsbuild.example.temperature', proto_parser.package)
        self.assertEqual(set(), proto_parser.enums)
        self.assertEqual(set(['Temperature']), proto_parser.messages)
        self.assertEqual(set(), proto_parser.services)
        self.assertEqual('Temperatures', proto_parser.outer_class_name)
Пример #17
0
 def test_inner_class_no_newline(self):
     with temporary_dir() as workdir:
         filename = 'inner_class_no_newline.proto'
         with open(os.path.join(workdir, filename), 'w') as fd:
             fd.write(
                 dedent('''
         package org.pantsbuild.protos;
         option java_multiple_files = true;
         message Foo {
            enum Bar { BAZ = 0; }
         }
       '''))
             fd.close()
             proto_parse = ProtobufParse(fd.name, filename)
             proto_parse.parse()
             self.assertEqual('org.pantsbuild.protos', proto_parse.package)
             self.assertEqual(set(['Bar']), proto_parse.enums)
             self.assertEqual(set(['Foo']), proto_parse.messages)
             self.assertEqual(set(), proto_parse.services)
             self.assertEqual('InnerClassNoNewline',
                              proto_parse.outer_class_name)
Пример #18
0
    def test_parse_for(self):
        with temporary_dir() as workdir:
            filename = 'temperatures.proto'
            with open(os.path.join(workdir, filename), 'w') as fd:
                fd.write(
                    dedent('''
            package org.pantsbuild.example.temperature;
            message Temperature {
              optional string unit = 1;
              required int64 number = 2;
            }
          '''))
                fd.close()

                proto_parser = ProtobufParse(fd.name, filename)
                proto_parser.parse()
                self.assertEqual('org.pantsbuild.example.temperature',
                                 proto_parser.package)
                self.assertEqual(set(), proto_parser.enums)
                self.assertEqual(set(['Temperature']), proto_parser.messages)
                self.assertEqual(set(), proto_parser.services)
                self.assertEqual('Temperatures', proto_parser.outer_class_name)
Пример #19
0
    def test_whitespace(self):
        with temporary_dir() as workdir:
            filename = 'jack_spratt_no_whitespace.proto'
            with open(os.path.join(workdir, filename), 'w') as fd:
                fd.write(
                    dedent('''
            package com.twitter.lean;
            option java_multiple_files = true;
            enum Jake { FOO=1;}
            message joe_bob {}
          '''))
                fd.close()
                proto_parse_no_whitespace = ProtobufParse(fd.name, filename)
                proto_parse_no_whitespace.parse()
                self.assertEqual('com.twitter.lean',
                                 proto_parse_no_whitespace.package)
                self.assertEqual(set(['Jake']),
                                 proto_parse_no_whitespace.enums)
                self.assertEqual(set(['joe_bob']),
                                 proto_parse_no_whitespace.messages)
                self.assertEqual(set(), proto_parse_no_whitespace.services)
                self.assertEqual('JackSprattNoWhitespace',
                                 proto_parse_no_whitespace.outer_class_name)

            filename = 'jack_spratt.proto'
            with open(os.path.join(workdir, filename), 'w') as fd:
                fd.write(
                    dedent('''
            package com.twitter.lean;
            option java_multiple_files = true;

            enum Jake { FOO=1;
            }
            message joe_bob {
            }
          '''))
                fd.close()
                proto_parse_with_whitespace = ProtobufParse(fd.name, filename)
                proto_parse_with_whitespace.parse()
                self.assertEqual('com.twitter.lean',
                                 proto_parse_with_whitespace.package)
                self.assertEqual(set(['Jake']),
                                 proto_parse_with_whitespace.enums)
                self.assertEqual(set(['joe_bob']),
                                 proto_parse_with_whitespace.messages)
                self.assertEqual('JackSpratt',
                                 proto_parse_with_whitespace.outer_class_name)
Пример #20
0
 def test_filename(self):
     with temporary_dir() as workdir:
         filename = 'foobar/testfile.proto'
         os.makedirs(os.path.join(workdir, 'foobar'))
         with open(os.path.join(workdir, filename), 'w') as fd:
             fd.write(
                 dedent('''
         package org.pantsbuild.protos;
         message Foo {
            optional string name = 1;
         }
       '''))
             fd.close()
             proto_parse = ProtobufParse(fd.name, filename)
             self.assertEquals('testfile', proto_parse.filename)
Пример #21
0
  def test_whitespace(self):
    with temporary_dir() as workdir:
      filename = 'jack_spratt_no_whitespace.proto'
      with open(os.path.join(workdir, filename), 'w') as fd:
        fd.write(
          '''
            package com.twitter.lean;
            option java_multiple_files = true;
            enum Jake { FOO=1;}
            message joe_bob {}
          '''
        )
        fd.close()
        proto_parse_no_whitespace =  ProtobufParse(fd.name, filename)
        proto_parse_no_whitespace.parse()
        self.assertEqual('com.twitter.lean', proto_parse_no_whitespace.package)
        self.assertEqual(set(['Jake']), proto_parse_no_whitespace.enums)
        self.assertEqual(set(['joe_bob']), proto_parse_no_whitespace.messages)
        self.assertEqual(set(), proto_parse_no_whitespace.services)
        self.assertEqual('JackSprattNoWhitespace', proto_parse_no_whitespace.outer_class_name)

      filename = 'jack_spratt.proto'
      with open(os.path.join(workdir, filename), 'w') as fd:
        fd.write(
          '''
            package com.twitter.lean;
            option java_multiple_files = true;

            enum Jake { FOO=1;
            }
            message joe_bob {
            }
          '''
        )
        fd.close()
        proto_parse_with_whitespace =  ProtobufParse(fd.name, filename)
        proto_parse_with_whitespace.parse()
        self.assertEqual('com.twitter.lean', proto_parse_with_whitespace.package)
        self.assertEqual(set(['Jake']), proto_parse_with_whitespace.enums)
        self.assertEqual(set(['joe_bob']), proto_parse_with_whitespace.messages)
        self.assertEqual('JackSpratt',proto_parse_with_whitespace.outer_class_name)
Пример #22
0
 def calculate_genfiles(self, path, source):
   protobuf_parse = ProtobufParse(path, source)
   protobuf_parse.parse()
   return OrderedSet(self.calculate_java_genfiles(protobuf_parse))
Пример #23
0
 def calculate_genfiles(self, path, source):
     protobuf_parse = ProtobufParse(path, source)
     protobuf_parse.parse()
     return OrderedSet(self.calculate_java_genfiles(protobuf_parse))