def test_ignore_empty_dirs_in_self(self):
        with self.setup_two_child_poms() as tmpdir:
            PomToBuild().convert_pom(
                'child1/pom.xml',
                generation_context=GenerationContext(print_headers=False))

            # Since all the dirs are empty, we should only have a project level pom file,
            # the others should be blank
            triple_quote_string = """
        target(name='lib')


        target(name='test',
          dependencies = [
            ':lib'
          ],
        )
      """
            self.assert_file_contents('child1/BUILD.gen',
                                      smart_dedent(triple_quote_string))

            self.assertEquals([], os.listdir('child1/src/main/java'))
            self.assertEquals([], os.listdir('child1/src/main/proto'))
            self.assertEquals([], os.listdir('child1/src/main/resources'))
            self.assertEquals([], os.listdir('child1/src/test/java'))
            self.assertEquals([], os.listdir('child1/src/test/proto'))
            self.assertEquals([], os.listdir('child1/src/test/resources'))
 def test_signed_jars(self):
     with self._setup_signed_jar_test() as tmp_dir:
         PomToBuild().convert_pom(
             'project/pom.xml',
             rootdir=tmp_dir,
             generation_context=GenerationContext(print_headers=False))
         self.assert_file_contents('project/BUILD.gen',
                                   self._signed_jar_expected_text)
 def test_shading_rules(self):
     with self._setup_single_module('shading-test',
                                    self._shading_pom_contents()) as pom:
         PomToBuild().convert_pom(
             pom,
             rootdir=os.path.abspath('.'),
             generation_context=GenerationContext(print_headers=False))
         self.assert_file_contents('{}/BUILD.gen'.format(
             os.path.dirname(pom)),
                                   self._shading_rules_expected(),
                                   ignore_leading_spaces=True)
    def test_jvm_binary_target(self):
        with temporary_dir() as tmp_dir:
            os.chdir(tmp_dir)
            self.make_file(
                'example-app/src/main/java/com/example/ExampleApp.java',
                '/* java file */')

            extra_contents = """
      <properties>
        <project.mainclass>com.example.ExampleApp</project.mainclass>
      </properties>
      """
            self.create_pom_with_modules(tmp_dir, ['example-app'],
                                         extra_project_contents=extra_contents)

            PomToBuild().convert_pom(
                os.path.join('example-app', 'pom.xml'),
                rootdir=tmp_dir,
                generation_context=GenerationContext(print_headers=False))

            expected_contents = """
        jvm_binary(name='example-app',
          main = 'com.example.ExampleApp',
          basename= 'example-app',
          dependencies = [
            ':lib'
          ],
          manifest_entries = square_manifest(),
        )

        # This target's sole purpose is just to invalidate the cache if loose files (eg app-manifest.yaml)
        # for the jvm_binary change.
        fingerprint(name='extra-files',
          sources = [],
          dependencies = [],
        )

        target(name='lib',
          dependencies = [
            'example-app/src/main/java:lib'
          ],
        )

        target(name='test',
          dependencies = [
            ':lib'
          ],
        )
      """
            self.assert_file_contents('example-app/BUILD.gen',
                                      expected_contents,
                                      ignore_leading_spaces=True)
 def test_default_test_target(self):
     with temporary_dir() as tmp_dir:
         os.chdir(tmp_dir)
         proto_file = 'project1/src/main/proto/foo.proto'
         java_file = 'project2/src/main/java/Foo.java'
         self.make_file(proto_file, '/* proto file */')
         self.make_file(java_file, '/* java file */')
         projects = ['project1', 'project2']
         self.create_pom_with_modules(tmp_dir, projects)
         for project in projects:
             PomToBuild().convert_pom(
                 os.path.join(project, 'pom.xml'),
                 rootdir=tmp_dir,
                 generation_context=GenerationContext(print_headers=False))
         triple_quote_string = """
     target(name='proto',
       dependencies = [
         'project1/src/main/proto:proto'
       ],
     )
     target(name='lib',
       dependencies = [
         ':proto'
       ],
     )
     target(name='test',
       dependencies = [
         ':lib'
       ],
     )
   """
         self.assert_file_contents('project1/BUILD.gen',
                                   triple_quote_string,
                                   ignore_leading_spaces=True)
         triple_quote_string = """
     target(name='lib',
       dependencies = [
         'project2/src/main/java:lib'
       ],
     )
     target(name='test',
       dependencies = [
         ':lib'
       ],
     )
   """
         self.assert_file_contents('project2/BUILD.gen',
                                   triple_quote_string,
                                   ignore_leading_spaces=True)
 def test_system_path(self):
     with self._setup_single_module(
             'criteriabuilders',
             self._system_path_module(),
             touch_files=['criteriabuilders/src/main/java/Foo.java'
                          ]) as pom:
         PomToBuild().convert_pom(
             pom,
             rootdir=os.path.abspath('.'),
             generation_context=GenerationContext(print_headers=False))
         path = os.path.join(os.path.dirname(pom),
                             'src/main/java/BUILD.gen')
         self.assert_file_contents(path,
                                   self._system_path_expected(),
                                   ignore_leading_spaces=True)
 def test_write_build_gen(self):
     gen_context = GenerationContext(print_headers=False)
     is_aux = gen_context.is_aux
     infer_target_name = gen_context.infer_target_name
     infer_build_name = gen_context.infer_build_name
     write_build_file = gen_context.write_build_file
     with temporary_dir() as build_dir:
         self.assertFalse(is_aux(build_dir))
         self.assertEquals('foo', infer_target_name(build_dir, 'foo'))
         self.assertEquals(os.path.join(build_dir, 'BUILD.gen'),
                           infer_build_name(build_dir))
         write_build_file(build_dir, 'contents of BUILD.gen')
         self.assertTrue(
             os.path.exists(os.path.join(build_dir, 'BUILD.gen')))
         with open(os.path.join(build_dir, 'BUILD.gen')) as build_file:
             self.assertEquals('contents of BUILD.gen', build_file.read())
    def test_system_specific_properties(self):
        dependencies = self._system_specific_properties_dependencies_text
        profiles = self._system_specific_properties_profiles_text
        with temporary_dir() as tmp_dir:
            os.chdir(tmp_dir)
            self.make_file('project/src/main/java/Foobar.java',
                           '/* nothing to see here */')
            self.create_pom_with_modules(tmp_dir, ['project'],
                                         extra_project_contents=dependencies +
                                         profiles)
            PomToBuild().convert_pom(
                'project/pom.xml',
                rootdir=tmp_dir,
                generation_context=GenerationContext(print_headers=False))

            self.assert_file_contents(
                'project/src/main/java/BUILD.gen',
                self._system_specific_properties_expected_text,
                ignore_leading_spaces=True,
                ignore_trailing_spaces=True,
                ignore_blanklines=True)
    def test_external_jar_ref(self):
        with temporary_dir() as tmpdir:
            os.chdir(tmpdir)
            with open(os.path.join('pom.xml'), 'w') as pomfile:
                triple_quote_string = """<?xml version="1.0" encoding="UTF-8"?>
                      <project>

                        <groupId>com.example</groupId>
                        <artifactId>parent</artifactId>
                        <version>HEAD-SNAPSHOT</version>

                        <modules>
                          <module>child1</module>
                        </modules>
                      </project>
                    """
                pomfile.write(smart_dedent(triple_quote_string))

            os.makedirs(os.path.join('parents', 'base'))
            with open(os.path.join('parents', 'base', 'pom.xml'),
                      'w') as base_pom:
                triple_quote_string = """<?xml version="1.0" encoding="UTF-8"?>
                    <project>

                      <groupId>com.example</groupId>
                      <artifactId>child1</artifactId>
                      <version>HEAD-SNAPSHOT</version>

                      <dependencyManagement>
                      </dependencyManagement>
                    </project>"""
                base_pom.write(smart_dedent(triple_quote_string))

            child1_path_name = 'child1'
            # Make some empty directories to hold BUILD.gen files
            os.makedirs(os.path.join(child1_path_name, 'src', 'main', 'java'))
            child1_pom_name = os.path.join(child1_path_name, 'pom.xml')
            with open(child1_pom_name, 'w') as child1_pomfile:
                triple_quote_string = """<?xml version="1.0" encoding="UTF-8"?>
                    <project>

                      <groupId>com.example</groupId>
                      <artifactId>child1</artifactId>
                      <version>HEAD-SNAPSHOT</version>

                      <dependencies>
                        <dependency>
                          <groupId>com.example.external</groupId>
                          <artifactId>foo</artifactId>
                          <version>1.2.3</version>
                          <classifier>shaded</classifier>
                          <type>tar.gz</type>
                        </dependency>

                      </dependencies>
                    </project>
                  """
                child1_pomfile.write(smart_dedent(triple_quote_string))
            self.make_file('child1/src/main/java/Foo.java', 'class Foo { }')
            PomToBuild().convert_pom(
                'child1/pom.xml',
                rootdir=tmpdir,
                generation_context=GenerationContext(print_headers=False))
            triple_quote_string = """
target(name='lib',
  dependencies = [
    'child1/src/main/java:lib'
  ],
)
target(name='test',
  dependencies = [
    ':lib'
  ],
)
"""
            self.assert_file_contents('child1/BUILD.gen', triple_quote_string)
            triple_quote_string = """
java_library(name='lib',
  sources = rglobs('*.java'),
  resources = [],
  dependencies = [
    ':jar_files'
  ],
  provides = artifact(org='com.example',
                      name='child1',
                      repo=square,),  # see squarepants/plugin/repo/register.py
)

jar_library(name='jar_files',
  jars=[
    sjar(org='com.example.external', name='foo', rev='1.2.3', classifier='shaded', ext='tar.gz',)
  ],
)
"""
            self.assert_file_contents('child1/src/main/java/BUILD.gen',
                                      triple_quote_string)
    def test_nonempty_self_and_dep(self):
        with self.setup_two_child_poms() as tmpdir:
            # Create some sources in child1 and child2 so we get BUILD.gen files in both.
            self.make_file('child1/src/main/java/Foo.java', 'class Foo { }')
            self.make_file('child1/src/main/proto/foo.proto',
                           'message Foo_Message {}')
            self.make_file('child1/src/main/resources/foo.txt', "Foo bar baz.")
            self.make_file('child1/src/test/java/FooTest.java',
                           'class FooTest { }')
            self.make_file('child1/src/test/proto/foo_test.proto',
                           'message FooTest_Message {}')
            self.make_file('child1/src/test/resources/foo_test.txt',
                           "Testing: Foo bar baz.")
            self.make_file('child2/src/main/java/Bar.java', "class Bar { }")
            self.make_file('child2/src/main/proto/bar.proto',
                           "message Bar_Message {}")
            self.make_file('child2/src/main/resources/bar.txt', "Foo bar baz.")
            self.make_file('child2/src/test/java/BarTest.java',
                           'class BarTest { }')
            self.make_file('child2/src/test/proto/bar_test.proto',
                           'message BarTest_Message {}')
            self.make_file('child2/src/test/resources/bar_test.txt',
                           "Testing: Foo bar baz.")
            PomToBuild().convert_pom(
                'child1/pom.xml',
                rootdir=tmpdir,
                generation_context=GenerationContext(print_headers=False))
            triple_quote_string = """
target(name='proto',
  dependencies = [
    'child1/src/main/proto:proto'
  ],
)
target(name='lib',
  dependencies = [
   'child1/src/main/java:lib'
  ],
)
target(name='test',
  dependencies = [
    'child1/src/test/java:test'
  ],
)
"""
            self.assert_file_contents('child1/BUILD.gen',
                                      triple_quote_string,
                                      ignore_leading_spaces=True)
            triple_quote_string = """
java_library(name='lib',
  sources = rglobs('*.java'),
  resources = [
    'child1/src/main/resources:resources'
  ],
  dependencies = [
    'child1/src/main/proto',
    'child2/src/main/java:lib'
  ],
  provides = artifact(org='com.example',
                      name='child1',
                      repo=square,),  # see squarepants/plugin/repo/register.py
)
"""
            self.assert_file_contents('child1/src/main/java/BUILD.gen',
                                      triple_quote_string)

            triple_quote_string = """
java_protobuf_library(name='proto',
  sources = rglobs('*.proto'),
  imports = [],
  dependencies = [
    ':proto-sources',
    'child2/src/main/java:lib'
  ],
  provides = artifact(org='com.example',
                      name='child1-proto',
                      repo=square,),  # see squarepants/plugin/repo/register.py
)
wire_proto_path(name='path',
  sources=rglobs('*.proto'),
  dependencies=[],
)
resources(name='proto-sources',
  sources = rglobs('*.proto'),
)
"""
            self.assert_file_contents('child1/src/main/proto/BUILD.gen',
                                      triple_quote_string)
            triple_quote_string = """
resources(name='resources',
  sources = rglobs('*', exclude=[globs('BUILD*')]),
  dependencies = [],
)
"""
            self.assert_file_contents('child1/src/main/resources/BUILD.gen',
                                      triple_quote_string)

            triple_quote_string = """
junit_tests(name='test',
  # TODO: Ideally, sources between :test, :integration-tests  and :lib should not intersect
  sources = rglobs('*Test.java'),
  cwd = 'child1',
  dependencies = [
    ':lib'
  ],
)
junit_tests(name='integration-tests',
  # TODO: Ideally, sources between :test, :integration-tests  and :lib should not intersect
  sources = rglobs('*IT.java'),
  cwd = 'child1',
  tags = [
    'integration'
  ],
  dependencies = [
    ':lib'
  ],
)
java_library(name='lib',
  sources = rglobs('*.java'),
  resources = [
    'child1/src/test/resources:resources'
  ],
  dependencies = [
    'child1/src/main/java:lib',
    'child1/src/main/proto',
    'child1/src/test/proto',
    'child2/src/main/java:lib',
    'testing-support/src/main/java:lib'
  ],
  provides = artifact(org='com.example',
                      name='child1-test',
                      repo=square,),  # see squarepants/plugin/repo/register.py
)
"""
            self.assert_file_contents('child1/src/test/java/BUILD.gen',
                                      triple_quote_string)
            triple_quote_string = """
java_protobuf_library(name='proto',
  sources = rglobs('*.proto'),
  imports = [],
  dependencies = [
    ':proto-sources',
    'child1/src/main/proto',
    'child2/src/main/java:lib'
  ],
  provides = artifact(org='com.example',
                      name='child1-proto',
                      repo=square,),  # see squarepants/plugin/repo/register.py
)

wire_proto_path(name='path',
  sources=rglobs('*.proto'),
  dependencies=[
    'child1/src/main/proto:path'
  ],
)

resources(name='proto-sources',
  sources = rglobs('*.proto'),
)
"""
            self.assert_file_contents('child1/src/test/proto/BUILD.gen',
                                      triple_quote_string)
            triple_quote_string = """
resources(name='resources',
  sources = rglobs('*', exclude=[globs('BUILD*')]),
  dependencies = [],
)
"""
            self.assert_file_contents('child1/src/test/resources/BUILD.gen',
                                      triple_quote_string)
    def test_ignore_empty_dirs_in_dep(self):
        with self.setup_two_child_poms() as tmpdir:
            # Create some sources in child1 to create more BUILD.gen files
            self.make_file('child1/src/main/java/Foo.java', 'class Foo { }')
            self.make_file('child1/src/main/proto/foo.proto',
                           'message Foo_Message {}')
            self.make_file('child1/src/main/resources/foo.txt', "Foo bar baz.")
            self.make_file('child1/src/test/java/FooTest.java',
                           'class FooTest { }')
            self.make_file('child1/src/test/proto/foo_test.proto',
                           'message FooTest_Message {}')
            self.make_file('child1/src/test/java/FooIT.java', 'class FooIT {}')
            self.make_file('child1/src/test/resources/foo_test.txt',
                           "Testing: Foo bar baz.")
            PomToBuild().convert_pom(
                'child1/pom.xml',
                rootdir=tmpdir,
                generation_context=GenerationContext(print_headers=False))

            triple_quote_string = """
target(name='proto',
  dependencies = [
    'child1/src/main/proto:proto'
  ],
)

target(name='lib',
  dependencies = [
    'child1/src/main/java:lib'
  ],
)

target(name='test',
  dependencies = [
    'child1/src/test/java:test'
  ],
)
"""
            self.assert_file_contents('child1/BUILD.gen', triple_quote_string)

            # There should be no references to child2 in the BUILD files under src/main
            # because the directories under child2 are empty
            triple_quote_string = """
java_library(name='lib',
  sources = rglobs('*.java'),
  resources = [
    'child1/src/main/resources:resources'
  ],
  dependencies = [
    'child1/src/main/proto'
  ],
  provides = artifact(org='com.example',
                      name='child1',
                      repo=square,),  # see squarepants/plugin/repo/register.py
)
"""
            self.assert_file_contents('child1/src/main/java/BUILD.gen',
                                      triple_quote_string)
            triple_quote_string = """
java_protobuf_library(name='proto',
  sources = rglobs('*.proto'),
  imports = [],
  dependencies = [
    ':proto-sources'
  ],
  provides = artifact(org='com.example',
                      name='child1-proto',
                      repo=square,),  # see squarepants/plugin/repo/register.py
)
wire_proto_path(name='path',
  sources=rglobs('*.proto'),
  dependencies=[],
)
resources(name='proto-sources',
  sources = rglobs('*.proto'),
)
"""
            self.assert_file_contents('child1/src/main/proto/BUILD.gen',
                                      triple_quote_string)
            triple_quote_string = """
resources(name='resources',
  sources = rglobs('*', exclude=[globs('BUILD*')]),
  dependencies = [],
)
"""
            self.assert_file_contents('child1/src/main/resources/BUILD.gen',
                                      triple_quote_string)

            # TODO(Eric Ayers) The provides statement in src/test/java is the same as in lib.  This probably
            # shouldn't be duplicated like this!
            triple_quote_string = """
junit_tests(name='test',
  # TODO: Ideally, sources between :test, :integration-tests  and :lib should not intersect
  sources = rglobs('*Test.java'),
  cwd = 'child1',
  dependencies = [
    ':lib'
  ],
)
junit_tests(name='integration-tests',
  # TODO: Ideally, sources between :test, :integration-tests  and :lib should not intersect
  sources = rglobs('*IT.java'),
  cwd = 'child1',
  tags = [
    'integration'
  ],
  dependencies = [
    ':lib'
  ],
)
java_library(name='lib',
  sources = rglobs('*.java'),
  resources = [
    'child1/src/test/resources:resources'
  ],
  dependencies = [
    'child1/src/main/java:lib',
    'child1/src/main/proto',
    'child1/src/test/proto',
    'testing-support/src/main/java:lib'
  ],
  provides = artifact(org='com.example',
                      name='child1-test',
                      repo=square,),  # see squarepants/plugin/repo/register.py
)
"""
            self.assert_file_contents('child1/src/test/java/BUILD.gen',
                                      triple_quote_string)
            triple_quote_string = """
java_protobuf_library(name='proto',
  sources = rglobs('*.proto'),
  imports = [],
  dependencies = [
    ':proto-sources',
    'child1/src/main/proto'
  ],
  provides = artifact(org='com.example',
                      name='child1-proto',
                      repo=square,),  # see squarepants/plugin/repo/register.py
)

wire_proto_path(name='path',
  sources=rglobs('*.proto'),
  dependencies=[
    'child1/src/main/proto:path'
  ],
)

resources(name='proto-sources',
  sources = rglobs('*.proto'),
)
"""
            self.assert_file_contents('child1/src/test/proto/BUILD.gen',
                                      triple_quote_string)
            triple_quote_string = """
resources(name='resources',
  sources = rglobs('*', exclude=[globs('BUILD*')]),
  dependencies = [],
)
"""
            self.assert_file_contents('child1/src/test/resources/BUILD.gen',
                                      triple_quote_string)