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(f'dist/{bundle_name}-bundle/libs', 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(f'dist/{bundle_name}.zip', 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, f'{bundle_jar_name}.jar')
            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', f'{bundle_jar_name}.jar'] + optional_args,
          stdout=subprocess.PIPE,
          cwd=workdir
        )

        stdout, _ = java_run.communicate()
      java_returncode = java_run.returncode
      self.assertEqual(java_returncode, 0)
      return stdout.decode()
 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))
Пример #3
0
    def _test_canonical_classpath_helper(
        self,
        classpath_products,
        targets,
        libs_dir,
        expected_canonical_classpath,
        expected_classpath_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_classpath_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.assertEqual(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 +
                list(expected_classpath_files.keys())))

        # check the content of classpath.txt
        for classpath_file in expected_classpath_files:
            self.assertTrue(
                check_file_content(
                    os.path.join(libs_dir, classpath_file),
                    expected_classpath_files[classpath_file],
                ))
Пример #4
0
 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))