def bundle_and_run(self, target, bundle_name, bundle_jar_name=None, bundle_options=None,
                     args=None,
                     expected_bundle_jar_content=None,
                     expected_bundle_content=None,
                     library_jars_are_symlinks=True):
    """Creates the bundle with pants, then does java -jar {bundle_name}.jar to execute the bundle.

    :param target: target name to compile
    :param bundle_name: resulting bundle filename (minus .zip extension)
    :param bundle_jar_name: monolithic jar filename (minus .jar extension), if None will be the
      same as bundle_name
    :param bundle_options: additional options for bundle
    :param args: optional arguments to pass to executable
    :param expected_bundle_content: verify the bundle zip content
    :param expected_bundle_jar_content: verify the bundle jar content
    :param library_jars_are_symlinks: verify library jars are symlinks if True, and actual
      files if False. Default `True` because we always create symlinks for both external and internal
      dependencies, only exception is when shading is used.
    :return: stdout as a string on success, raises an Exception on error
    """
    bundle_jar_name = bundle_jar_name or bundle_name
    bundle_options = bundle_options or []
    bundle_options = ['bundle.jvm'] + bundle_options + ['--archive=zip', target]
    with self.pants_results(bundle_options) as pants_run:
      self.assert_success(pants_run)

      self.assertTrue(check_symlinks('dist/{bundle_name}-bundle/libs'.format(bundle_name=bundle_name),
                                     library_jars_are_symlinks))
      # TODO(John Sirois): We need a zip here to suck in external library classpath elements
      # pointed to by symlinks in the run_pants ephemeral tmpdir.  Switch run_pants to be a
      # contextmanager that yields its results while the tmpdir workdir is still active and change
      # this test back to using an un-archived bundle.
      with temporary_dir() as workdir:
        ZIP.extract('dist/{bundle_name}.zip'.format(bundle_name=bundle_name), workdir)
        if expected_bundle_content:
          self.assertTrue(contains_exact_files(workdir, expected_bundle_content))
        if expected_bundle_jar_content:
          with temporary_dir() as check_bundle_jar_dir:
            bundle_jar = os.path.join(workdir, '{bundle_jar_name}.jar'
                                      .format(bundle_jar_name=bundle_jar_name))
            ZIP.extract(bundle_jar, check_bundle_jar_dir)
            self.assertTrue(contains_exact_files(check_bundle_jar_dir, expected_bundle_jar_content))

        optional_args = []
        if args:
          optional_args = args
        java_run = subprocess.Popen(['java',
                                     '-jar',
                                     '{bundle_jar_name}.jar'.format(bundle_jar_name=bundle_jar_name)]
                                    + optional_args,
                                    stdout=subprocess.PIPE,
                                    cwd=workdir)

        stdout, _ = java_run.communicate()
      java_returncode = java_run.returncode
      self.assertEqual(java_returncode, 0)
      return stdout.decode('utf-8')
Esempio n. 2
0
  def bundle_and_run(self, target, bundle_name, bundle_jar_name=None, bundle_options=None,
                     args=None,
                     expected_bundle_jar_content=None,
                     expected_bundle_content=None,
                     library_jars_are_symlinks=True):
    """Creates the bundle with pants, then does java -jar {bundle_name}.jar to execute the bundle.

    :param target: target name to compile
    :param bundle_name: resulting bundle filename (minus .zip extension)
    :param bundle_jar_name: monolithic jar filename (minus .jar extension), if None will be the
      same as bundle_name
    :param bundle_options: additional options for bundle
    :param args: optional arguments to pass to executable
    :param expected_bundle_content: verify the bundle zip content
    :param expected_bundle_jar_content: verify the bundle jar content
    :param library_jars_are_symlinks: verify library jars are symlinks if True, and actual
      files if False. Default `True` because we always create symlinks for both external and internal
      dependencies, only exception is when shading is used.
    :return: stdout as a string on success, raises an Exception on error
    """
    bundle_jar_name = bundle_jar_name or bundle_name
    bundle_options = bundle_options or []
    bundle_options = ['bundle.jvm'] + bundle_options + ['--archive=zip', target]
    with self.pants_results(bundle_options) as pants_run:
      self.assert_success(pants_run)

      self.assertTrue(check_symlinks('dist/{bundle_name}-bundle/libs'.format(bundle_name=bundle_name),
                                     library_jars_are_symlinks))
      # TODO(John Sirois): We need a zip here to suck in external library classpath elements
      # pointed to by symlinks in the run_pants ephemeral tmpdir.  Switch run_pants to be a
      # contextmanager that yields its results while the tmpdir workdir is still active and change
      # this test back to using an un-archived bundle.
      with temporary_dir() as workdir:
        ZIP.extract('dist/{bundle_name}.zip'.format(bundle_name=bundle_name), workdir)
        if expected_bundle_content:
          self.assertTrue(contains_exact_files(workdir, expected_bundle_content))
        if expected_bundle_jar_content:
          with temporary_dir() as check_bundle_jar_dir:
            bundle_jar = os.path.join(workdir, '{bundle_jar_name}.jar'
                                      .format(bundle_jar_name=bundle_jar_name))
            ZIP.extract(bundle_jar, check_bundle_jar_dir)
            self.assertTrue(contains_exact_files(check_bundle_jar_dir, expected_bundle_jar_content))

        optional_args = []
        if args:
          optional_args = args
        java_run = subprocess.Popen(['java',
                                     '-jar',
                                     '{bundle_jar_name}.jar'.format(bundle_jar_name=bundle_jar_name)]
                                    + optional_args,
                                    stdout=subprocess.PIPE,
                                    cwd=workdir)

        stdout, _ = java_run.communicate()
      java_returncode = java_run.returncode
      self.assertEquals(java_returncode, 0)
      return stdout
Esempio n. 3
0
  def _test_canonical_classpath_helper(self,
                                       classpath_products,
                                       targets,
                                       libs_dir,
                                       expected_canonical_classpath,
                                       expected_classspath_files,
                                       excludes=None):
    """
    Helper method to call `create_canonical_classpath` and verify generated canonical classpath.

    :param ClasspathProducts classpath_products: Classpath products.
    :param list targets: List of targets to generate canonical classpath from.
    :param string libs_dir: Directory where canonical classpath are to be generated.
    :param list expected_canonical_classpath: List of canonical classpath relative to a base directory.
    :param dict expected_classspath_files: A dict of classpath.txt path to its expected content.
    """
    canonical_classpath = ClasspathProducts.create_canonical_classpath(
      classpath_products, targets, libs_dir, save_classpath_file=True,
      internal_classpath_only=False, excludes=excludes)
    # check canonical path returned
    self.assertEquals(expected_canonical_classpath,
                      relativize_paths(canonical_classpath, libs_dir))

    # check canonical path created contain the exact set of files, no more, no less
    self.assertTrue(contains_exact_files(libs_dir,
                                         expected_canonical_classpath +
                                         expected_classspath_files.keys()))

    # check the content of classpath.txt
    for classpath_file in expected_classspath_files:
      self.assertTrue(check_file_content(os.path.join(libs_dir, classpath_file),
                                         expected_classspath_files[classpath_file]))
Esempio n. 4
0
    def _test_canonical_classpath_helper(self, classpath_products, targets,
                                         expected_canonical_classpath,
                                         expected_classspath_files,
                                         use_target_id):
        """
    Helper method to call `create_canonical_classpath` and verify generated canonical classpath.

    :param ClasspathProducts classpath_products: Classpath products.
    :param list targets: List of targets to generate canonical classpath from.
    :param list expected_canonical_classpath: List of canonical classpath relative to a base directory.
    :param dict expected_classspath_files: A dict of classpath.txt path to its expected content.
    """
        with temporary_dir() as base_dir:
            canonical_classpath = ClasspathUtil.create_canonical_classpath(
                classpath_products,
                targets,
                base_dir,
                save_classpath_file=True,
                use_target_id=use_target_id)
            # check canonical path returned
            self.assertEquals(expected_canonical_classpath,
                              relativize_paths(canonical_classpath, base_dir))

            # check canonical path created contain the exact set of files, no more, no less
            self.assertTrue(
                contains_exact_files(
                    base_dir, expected_canonical_classpath +
                    expected_classspath_files.keys()))

            # check the content of classpath.txt
            for classpath_file in expected_classspath_files:
                self.assertTrue(
                    check_file_content(
                        os.path.join(base_dir, classpath_file),
                        expected_classspath_files[classpath_file]))
  def _test_canonical_classpath_helper(self,
                                       classpath_products,
                                       targets,
                                       libs_dir,
                                       expected_canonical_classpath,
                                       expected_classspath_files,
                                       excludes=None):
    """
    Helper method to call `create_canonical_classpath` and verify generated canonical classpath.

    :param ClasspathProducts classpath_products: Classpath products.
    :param list targets: List of targets to generate canonical classpath from.
    :param string libs_dir: Directory where canonical classpath are to be generated.
    :param list expected_canonical_classpath: List of canonical classpath relative to a base directory.
    :param dict expected_classspath_files: A dict of classpath.txt path to its expected content.
    """
    canonical_classpath = ClasspathProducts.create_canonical_classpath(
      classpath_products, targets, libs_dir, save_classpath_file=True,
      internal_classpath_only=False, excludes=excludes)
    # check canonical path returned
    self.assertEquals(expected_canonical_classpath,
                      relativize_paths(canonical_classpath, libs_dir))

    # check canonical path created contain the exact set of files, no more, no less
    self.assertTrue(contains_exact_files(libs_dir,
                                         expected_canonical_classpath +
                                         expected_classspath_files.keys()))

    # check the content of classpath.txt
    for classpath_file in expected_classspath_files:
      self.assertTrue(check_file_content(os.path.join(libs_dir, classpath_file),
                                         expected_classspath_files[classpath_file]))
Esempio n. 6
0
  def _test_canonical_classpath_helper(self, classpath_products, targets,
                                       expected_canonical_classpath,
                                       expected_classspath_files,
                                       use_target_id):
    """
    Helper method to call `create_canonical_classpath` and verify generated canonical classpath.

    :param ClasspathProducts classpath_products: Classpath products.
    :param list targets: List of targets to generate canonical classpath from.
    :param list expected_canonical_classpath: List of canonical classpath relative to a base directory.
    :param dict expected_classspath_files: A dict of classpath.txt path to its expected content.
    """
    with temporary_dir() as base_dir:
      canonical_classpath = ClasspathUtil.create_canonical_classpath(classpath_products,
                                                                     targets,
                                                                     base_dir,
                                                                     save_classpath_file=True,
                                                                     use_target_id=use_target_id)
      # check canonical path returned
      self.assertEquals(expected_canonical_classpath,
                        relativize_paths(canonical_classpath, base_dir))

      # check canonical path created contain the exact set of files, no more, no less
      self.assertTrue(contains_exact_files(base_dir,
                                           expected_canonical_classpath +
                                           expected_classspath_files.keys()))

      # check the content of classpath.txt
      for classpath_file in expected_classspath_files:
        self.assertTrue(check_file_content(os.path.join(base_dir, classpath_file),
                                           expected_classspath_files[classpath_file]))
 def test_go_compile_simple(self):
   with self.temporary_workdir() as workdir:
     args = ['compile',
             'contrib/go/examples/src/go/libA']
     pants_run = self.run_pants_with_workdir(args, workdir)
     self.assert_success(pants_run)
     go_dist = global_subsystem_instance(GoDistribution)
     goos = go_dist.create_go_cmd('env', args=['GOOS']).check_output().strip()
     goarch = go_dist.create_go_cmd('env', args=['GOARCH']).check_output().strip()
     expected_files = set('contrib.go.examples.src.go.{libname}.{libname}/'
                          'pkg/{goos}_{goarch}/{libname}.a'
                          .format(libname=libname, goos=goos, goarch=goarch)
                          for libname in ('libA', 'libB', 'libC', 'libD', 'libE'))
     self.assertTrue(contains_exact_files(os.path.join(workdir, 'compile', 'go'),
                                          expected_files, ignore_links=True))
 def test_go_compile_simple(self):
   with self.temporary_workdir() as workdir:
     args = ['compile',
             'contrib/go/examples/src/go/libA']
     pants_run = self.run_pants_with_workdir(args, workdir)
     self.assert_success(pants_run)
     go_dist = global_subsystem_instance(GoDistribution)
     goos = go_dist.create_go_cmd('env', args=['GOOS']).check_output().decode().strip()
     goarch = go_dist.create_go_cmd('env', args=['GOARCH']).check_output().decode().strip()
     expected_files = set('contrib.go.examples.src.go.{libname}.{libname}/'
                          'pkg/{goos}_{goarch}/{libname}.a'
                          .format(libname=libname, goos=goos, goarch=goarch)
                          for libname in ('libA', 'libB', 'libC', 'libD', 'libE'))
     self.assertTrue(contains_exact_files(os.path.join(workdir, 'compile', 'go'),
                                          expected_files, ignore_links=True))
  def test_go_thrift_gen_simple(self):
    with self.temporary_workdir() as workdir:
      args = ['gen',
              'contrib/go/testprojects/src/thrift/thrifttest:fleem']
      pants_run = self.run_pants_with_workdir(args, workdir)
      self.assert_success(pants_run)
      with subsystem_instance(GoDistribution.Factory) as factory:
        go_dist = factory.create()
        goos = go_dist.create_go_cmd('env', args=['GOOS']).check_output().strip()
        goarch = go_dist.create_go_cmd('env', args=['GOARCH']).check_output().strip()
        expected_files = set([
          'contrib.go.testprojects.src.thrift.thrifttest.fleem/039c5ecb4977/src/go/thrifttest/duck/constants.go',
          'contrib.go.testprojects.src.thrift.thrifttest.fleem/039c5ecb4977/src/go/thrifttest/duck/ttypes.go',
        ])

        #Fetch the hash for task impl version.
        go_thrift_contents = os.listdir(os.path.join(workdir, 'gen', 'go-thrift'))
        self.assertEqual(len(go_thrift_contents), 1)

        root = os.path.join(workdir, 'gen', 'go-thrift', go_thrift_contents[0])
        self.assertTrue(contains_exact_files(root, expected_files, ignore_links=True))
  def test_go_thrift_gen_simple(self):
    with self.temporary_workdir() as workdir:
      args = ['gen',
              'contrib/go/testprojects/src/thrift/thrifttest:fleem']
      pants_run = self.run_pants_with_workdir(args, workdir)
      self.assert_success(pants_run)
      with subsystem_instance(GoDistribution.Factory) as factory:
        go_dist = factory.create()
        goos = go_dist.create_go_cmd('env', args=['GOOS']).check_output().strip()
        goarch = go_dist.create_go_cmd('env', args=['GOARCH']).check_output().strip()
        expected_files = set([
          'contrib.go.testprojects.src.thrift.thrifttest.fleem/c4e43b3a84a8/src/go/thrifttest/duck/constants.go',
          'contrib.go.testprojects.src.thrift.thrifttest.fleem/c4e43b3a84a8/src/go/thrifttest/duck/ttypes.go',
        ])

        #Fetch the hash for task impl version.
        go_thrift_contents = os.listdir(os.path.join(workdir, 'gen', 'go-thrift'))
        self.assertEqual(len(go_thrift_contents), 1)

        root = os.path.join(workdir, 'gen', 'go-thrift', go_thrift_contents[0])
        self.assertTrue(contains_exact_files(root, expected_files, ignore_links=True))