Exemplo n.º 1
0
def is_missing_jvm(version):
  init_subsystem(DistributionLocator)
  try:
    DistributionLocator.cached(minimum_version=version, maximum_version='{}.9999'.format(version))
    return False
  except DistributionLocator.Error:
    return True
Exemplo n.º 2
0
  def setUp(self):
    super(TestSetupPy, self).setUp()
    distdir = os.path.join(self.build_root, 'dist')
    self.set_options(pants_distdir=distdir)
    init_subsystem(Target.Arguments)

    self.dependency_calculator = SetupPy.DependencyCalculator(self.build_graph)
 def test_java_home_extraction_empty(self):
   init_subsystem(DistributionLocator)
   result = tuple(self._get_zinc_arguments(
     JvmPlatformSettings('1.7', '1.7', [])
   ))
   self.assertEqual(4, len(result),
                     msg='_get_zinc_arguments did not correctly handle empty args.')
Exemplo n.º 4
0
  def test_exported_antlr(self):
    self.create_file(relpath='src/antlr/exported/exported.g', contents=dedent("""
      grammar exported;

      options {
        language = Python;
      }

      WORD: ('a'..'z'|'A'..'Z'|'0'..'9'|'-'|'_')+;

      static: WORD;
    """))
    target = self.make_target(spec='src/antlr/exported',
                              target_type=PythonAntlrLibrary,
                              antlr_version='3.1.3',
                              sources=['exported.g'],
                              module='exported',
                              provides=PythonArtifact(name='test.exported', version='0.0.0'))

    # TODO(John Sirois): This hacks around a direct but undeclared dependency
    # `pants.java.distribution.distribution.Distribution` gained in
    # https://rbcommons.com/s/twitter/r/2657
    # Remove this once proper Subsystem dependency chains are re-established.
    init_subsystem(JVM)
    with self.run_execute(target) as created:
      self.assertEqual([target], created.keys())
Exemplo n.º 5
0
 def setUp(self):
   """
   :API: public
   """
   super(JvmTaskTestBase, self).setUp()
   init_subsystem(JvmResolveSubsystem)
   self.set_options_for_scope('resolver', resolver='ivy')
Exemplo n.º 6
0
  def scala_platform_setup(self):
    options = {
      ScalaPlatform.options_scope: {
        'version': 'custom',
        'suffix_version': '2.10',
      }
    }
    init_subsystem(ScalaPlatform, options)

    self.make_target('//:scalastyle',
                     JarLibrary,
                     jars=[JarDependency('org.scalastyle', 'scalastyle_2.10', '0.3.2')]
    )

    self.make_target('//:scala-repl',
               JarLibrary,
               jars=[
                 JarDependency(org = 'org.scala-lang',
                               name = 'jline',
                               rev = '2.10.5'),
                 JarDependency(org = 'org.scala-lang',
                               name = 'scala-compiler',
                               rev = '2.10.5')])

    self.make_target('//:scalac',
                     JarLibrary,
                     jars=[JarDependency('org.scala-lang', 'scala-compiler', '2.10.5')])
 def setUp(self):
   init_subsystem(Target.TagAssignments)
   self.target_macro_factory = TargetMacro.Factory.wrap(
     lambda ctx: ctx.create_object(self.BlueTarget,
                                   type_alias='jill',
                                   name=os.path.basename(ctx.rel_path)),
     self.BlueTarget)
Exemplo n.º 8
0
  def test_coverage_auto_option_no_explicit_coverage(self):
    init_subsystem(Target.Arguments)
    init_subsystem(SourceRootConfig)

    self.add_to_build_file('src/python/util', 'python_library()')

    self.create_file(
      'src/python/util/math.py',
      dedent("""
          def one():  # line 1
            return 1  # line 2
        """).strip())

    self.add_to_build_file('test/python/util', 'python_tests(dependencies = ["src/python/util"])')

    self.create_file(
      'test/python/util/test_math.py',
      dedent("""
          import unittest

          from util import math

          class MathTest(unittest.TestCase):
            def test_one(self):
              self.assertEqual(1, math.one())
        """))
    test = self.target('test/python/util')
    covered_path = os.path.join(self.build_root, 'src/python/util/math.py')

    all_statements, not_run_statements = self.run_coverage_auto(targets=[test],
                                                                covered_path=covered_path)
    self.assertEqual([1, 2], all_statements)
    self.assertEqual([], not_run_statements)
Exemplo n.º 9
0
  def test_coverage_auto_option_no_explicit_coverage_idiosyncratic_layout(self):
    # The all target has no coverage attribute and the code under test does not follow the
    # auto-discover (parallel packages) pattern so we should get no coverage.
    init_subsystem(Target.Arguments)
    init_subsystem(SourceRootConfig)

    self.add_to_build_file('src/python/util', 'python_library()')

    self.create_file(
      'src/python/util/math.py',
      dedent("""
          def one():  # line 1
            return 1  # line 2
        """).strip())

    self.add_to_build_file('test/python/util_tests', 'python_tests(dependencies = ["src/python/util"])')

    self.create_file(
      'test/python/util_tests/test_math.py',
      dedent("""
          import unittest

          from util import math

          class MathTest(unittest.TestCase):
            def test_one(self):
              self.assertEqual(1, math.one())
        """))
    test = self.target('test/python/util_tests')
    covered_path = os.path.join(self.build_root, 'src/python/util/math.py')
    all_statements, not_run_statements = self.run_coverage_auto(targets=[test],
                                                                covered_path=covered_path)
    self.assertEqual([1, 2], all_statements)
    self.assertEqual([1, 2], not_run_statements)
Exemplo n.º 10
0
  def setUp(self):
    super(ClassmapTaskTest, self).setUp()
    init_subsystem(Target.Arguments)

    self.add_to_build_file(
      'a',
      'java_library(sources=["a1.java", "a2.java"])',
    )

    self.jar_artifact = self.create_artifact(org='org.example', name='foo', rev='1.0.0')
    with open_zip(self.jar_artifact.pants_path, 'w') as jar:
      jar.writestr('foo/Foo.class', '')

    self.add_to_build_file(
      'b',
      'jar_library(jars=[jar(org="org.example", name="foo", rev="1.0.0")])',
    )

    self.add_to_build_file(
      'c',
      'java_library(dependencies=["a", "b"])',
    )

    self.target_a = self.target('a')
    self.target_b = self.target('b')
    self.target_c = self.target('c')
Exemplo n.º 11
0
  def test_force_override(self):
    jars = list(self.a.payload.jars)
    with temporary_file_path() as ivyxml:
      init_subsystem(JarDependencyManagement)
      IvyUtils.generate_ivy([self.a], jars=jars, excludes=[], ivyxml=ivyxml, confs=['default'])

      doc = ET.parse(ivyxml).getroot()

      conf = self.find_single(doc, 'configurations/conf')
      self.assert_attributes(conf, name='default')

      dependencies = list(doc.findall('dependencies/dependency'))
      self.assertEqual(2, len(dependencies))

      dep1 = dependencies[0]
      self.assert_attributes(dep1, org='org1', name='name1', rev='rev1')
      conf = self.find_single(dep1, 'conf')
      self.assert_attributes(conf, name='default', mapped='default')

      dep2 = dependencies[1]
      self.assert_attributes(dep2, org='org2', name='name2', rev='rev2', force='true')
      conf = self.find_single(dep1, 'conf')
      self.assert_attributes(conf, name='default', mapped='default')

      override = self.find_single(doc, 'dependencies/override')
      self.assert_attributes(override, org='org2', module='name2', rev='rev2')
Exemplo n.º 12
0
  def test_setuptools_version(self):
    self.create_file('src/python/foo/__init__.py')
    self.create_python_library(
      relpath='src/python/foo/commands',
      name='commands',
      source_contents_map={
        'print_sys_path.py': dedent("""
          import os
          import sys
          from setuptools import Command


          class PrintSysPath(Command):
            user_options = []

            def initialize_options(self):
              pass

            def finalize_options(self):
              pass

            def run(self):
              with open(os.path.join(os.path.dirname(__file__), 'sys_path.txt'), 'w') as fp:
                fp.write(os.linesep.join(sys.path))
          """)
      },
    )
    foo = self.create_python_library(
      relpath='src/python/foo',
      name='foo',
      dependencies=[
        'src/python/foo/commands',
      ],
      provides=dedent("""
      setup_py(
        name='foo',
        version='0.0.0',
      )
      """)
    )
    self.set_options(run='print_sys_path')

    # Make sure setup.py can see our custom distutils Command 'print_sys_path'.
    sdist_srcdir = os.path.join(self.distdir, 'foo-0.0.0', 'src')
    with environment_as(PYTHONPATH=sdist_srcdir):
      with self.run_execute(foo):
        with open(os.path.join(sdist_srcdir, 'foo', 'commands', 'sys_path.txt')) as fp:
          def assert_extra(name, expected_version):
            package = Package.from_href(fp.readline().strip())
            self.assertEqual(name, package.name)
            self.assertEqual(expected_version, package.raw_version)

          # The 1st two elements of the sys.path should be our custom SetupPyRunner Installer's
          # setuptools and wheel mixins, which should match the setuptools and wheel versions
          # specified by the PythonSetup subsystem.
          init_subsystem(PythonSetup)
          python_setup = PythonSetup.global_instance()
          assert_extra('setuptools', python_setup.setuptools_version)
          assert_extra('wheel', python_setup.wheel_version)
Exemplo n.º 13
0
 def setUp(self):
   super(ScalastyleTest, self).setUp()
   # Default scalastyle config (import grouping rule) and no excludes.
   init_subsystem(ScalaPlatform, {
     ScalaPlatform.options_scope: {
       'version': '2.10'
     }
   })
Exemplo n.º 14
0
 def setUp(self):
   super(RunPrepCommandTest, self).setUp()
   # This is normally taken care of in RunPrepCommandBase.register_options() when running pants,
   # but these don't get called in testing unless you call `self.create_task()`.
   # Some of these unit tests need to create targets before creating the task.
   PrepCommand.add_allowed_goal('test')
   PrepCommand.add_allowed_goal('binary')
   init_subsystem(Target.Arguments)
Exemplo n.º 15
0
 def execute_tool(self, classpath, main, args=None):
   init_subsystem(DistributionLocator)
   executor = SubprocessExecutor(DistributionLocator.cached())
   process = executor.spawn(classpath, main, args=args,
                            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
   out, err = process.communicate()
   self.assertEqual(0, process.returncode)
   self.assertEqual('', err.strip())
   yield out
Exemplo n.º 16
0
  def marking_tests(self):
    init_subsystem(Target.Arguments)
    init_subsystem(SourceRootConfig)

    with temporary_dir() as marker_dir:
      self.create_file(
        'test/python/passthru/test_passthru.py',
        dedent("""
            import inspect
            import os
            import pytest
            import unittest


            class PassthruTest(unittest.TestCase):
              def touch(self, path):
                with open(path, 'wb') as fp:
                  fp.close()

              def mark_test_run(self):
                caller_frame_record = inspect.stack()[1]

                # For the slot breakdown of a frame record tuple, see:
                #   https://docs.python.org/2/library/inspect.html#the-interpreter-stack
                _, _, _, caller_func_name, _, _ = caller_frame_record

                marker_file = os.path.join({marker_dir!r}, caller_func_name)
                self.touch(marker_file)

              def test_one(self):
                self.mark_test_run()

              @pytest.mark.purple
              def test_two(self):
                self.mark_test_run()

              def test_three(self):
                self.mark_test_run()

              @pytest.mark.red
              def test_four(self):
                self.mark_test_run()

              @pytest.mark.green
              def test_five(self):
                self.mark_test_run()
          """.format(marker_dir=marker_dir)))

      def assert_mark(exists, name):
        message = '{} {!r} to be executed.'.format('Expected' if exists else 'Did not expect', name)
        marker_file = os.path.join(marker_dir, name)
        self.assertEqual(exists, os.path.exists(marker_file), message)

      self.add_to_build_file('test/python/passthru', 'python_tests()')
      test = self.target('test/python/passthru')
      yield test, functools.partial(assert_mark, True), functools.partial(assert_mark, False)
Exemplo n.º 17
0
  def _generate_strict_dependencies(self):
    init_subsystem(Target.Arguments)
    self.lib_aa = self.make_target(
      'com/foo:AA',
      target_type=SourcesTarget,
      sources=['com/foo/AA.scala'],
    )

    self.lib_a = self.make_target(
      'com/foo:A',
      target_type=SourcesTarget,
      sources=['com/foo/A.scala'],
    )

    self.lib_b = self.make_target(
      'com/foo:B',
      target_type=SourcesTarget,
      sources=['com/foo/B.scala'],
      dependencies=[self.lib_a, self.lib_aa],
      exports=[':A'],
    )

    self.lib_c = self.make_target(
      'com/foo:C',
      target_type=SourcesTarget,
      sources=['com/foo/C.scala'],
      dependencies=[self.lib_b],
      exports=[':B'],
    )

    self.lib_c_alias = self.make_target(
      'com/foo:C_alias',
      dependencies=[self.lib_c],
    )

    self.lib_d = self.make_target(
      'com/foo:D',
      target_type=SourcesTarget,
      sources=['com/foo/D.scala'],
      dependencies=[self.lib_c_alias],
      exports=[':C_alias'],
    )

    self.lib_f = self.make_target(
      'com/foo:F',
      target_type=SourcesTarget,
      sources=['com/foo/E.scala'],
      scope=Scopes.RUNTIME
    )

    self.lib_e = self.make_target(
      'com/foo:E',
      target_type=SourcesTarget,
      sources=['com/foo/E.scala'],
      dependencies=[self.lib_d, self.lib_f],
    )
Exemplo n.º 18
0
  def setUp(self):
    super(ScalaFmtTestBase, self).setUp()

    init_subsystem(ScalaPlatform)
    init_subsystem(SourceRootConfig)

    self.configuration = self.create_file(
      relpath='build-support/scalafmt/config',
      contents=dedent("""
      align.arrowEnumeratorGenerator = true
      align.openParenCallSite = false
      align.openParenDefnSite = false
      assumeStandardLibraryStripMargin = false
      binPack.parentConstructors = false
      continuationIndent.callSite = 4
      continuationIndent.defnSite = 4
      maxColumn = 100
      newlines.sometimesBeforeColonInMethodReturnType = true
      spaces.afterTripleEquals = true
      spaces.inImportCurlyBraces = false
      """)
    )

    self.test_file_contents = dedent(
      """
      package org.pantsbuild.badscalastyle

      /**
       * These comments are formatted incorrectly
       * and the parameter list is too long for one line
       */
      case class ScalaStyle(one: String,two: String,three: String,four: String,
          five: String,six: String,seven: String,eight: String,  nine: String)

      class Person(name: String,age: Int,astrologicalSign: String,
          shoeSize: Int,
          favoriteColor: java.awt.Color) {
        def getAge:Int={return age}
        def sum(longvariablename: List[String]): Int = {
          longvariablename.map(_.toInt).foldLeft(0)(_ + _)
        }
      }
      """
    )
    self.test_file = self.create_file(
      relpath='src/scala/org/pantsbuild/badscalastyle/BadScalaStyle.scala',
      contents=self.test_file_contents,
    )
    self.library = self.make_target(spec='src/scala/org/pantsbuild/badscalastyle',
                                    sources=['BadScalaStyle.scala'],
                                    target_type=ScalaLibrary)
    self.as_resources = self.make_target(spec='src/scala/org/pantsbuild/badscalastyle:as_resources',
                                         target_type=Resources,
                                         sources=['BadScalaStyle.scala'],
                                         description='Depends on the same sources as the target '
                                                     'above, but as resources.')
Exemplo n.º 19
0
  def test_scala_exclude(self):
    init_subsystem(ScalaPlatform)

    name = 'foo_lib'
    suffixed_name = ScalaPlatform.global_instance().suffix_version(name)

    self.assertEqual(
        suffixed_name,
        ScalaExclude(org='example.com', name=name).name
      )
Exemplo n.º 20
0
  def setUp(self):
    pants_workdir = tempfile.mkdtemp()
    self.addCleanup(safe_rmtree, pants_workdir)

    init_subsystem(BuildInvalidator.Factory, options={'': {'pants_workdir': pants_workdir}})
    self.root_invalidator = BuildInvalidator.Factory.create()
    self.scoped_invalidator1 = BuildInvalidator.Factory.create(build_task='gen')
    self.scoped_invalidator2 = BuildInvalidator.Factory.create(build_task='resolve')

    self.key = self.cache_key()
Exemplo n.º 21
0
 def test_no_synthetic_resources_in_output(self):
   # `python_library` w/o `sources` requires initializing the needed subsystem.
   init_subsystem(Target.Arguments)
   self.add_to_build_file('BUILD', dedent("""
   python_library(
     name = 'lib',
     resources = ['BUILD'],
   )
   """))
   output = self.execute_console_task(targets=self.targets('::'))
   self.assertIn('//:lib', output)
   self.assertTrue(all('synthetic' not in line for line in output))
Exemplo n.º 22
0
  def setUp(self):
    super(ClassmapTaskTest, self).setUp()
    init_subsystem(Target.Arguments)

    self.target_a = self.make_target('a', target_type=JavaLibrary, sources=['a1.java', 'a2.java'])

    self.jar_artifact = self.create_artifact(org='org.example', name='foo', rev='1.0.0')
    with open_zip(self.jar_artifact.pants_path, 'w') as jar:
      jar.writestr('foo/Foo.class', '')
    self.target_b = self.make_target('b', target_type=JarLibrary,
                                     jars=[JarDependency(org='org.example', name='foo', rev='1.0.0')])

    self.target_c = self.make_target('c', dependencies=[self.target_a, self.target_b],
                                     target_type=JavaLibrary)
Exemplo n.º 23
0
  def test_implicit_junit_dep(self):
    init_subsystem(JUnit)
    # Check that the implicit dep is added, and doesn't replace other deps.
    target = self.make_target('//:target', Target)
    test1 = self.make_target('//:test1', JUnitTests, sources=[], dependencies=[target])
    self.assertEqual(['JarLibrary(//:junit_library)', 'Target(//:target)'],
                      sorted(str(x) for x in test1.dependencies))

    # Check that having an explicit dep doesn't cause problems.
    junit_target = self.build_graph.get_target_from_spec('//:junit_library')
    test2 = self.make_target('//:test2', JUnitTests, sources=[],
                             dependencies=[junit_target, target])
    self.assertEqual(['JarLibrary(//:junit_library)', 'Target(//:target)'],
                      sorted(str(x) for x in test2.dependencies))
  def test_java_home_extraction(self):
    init_subsystem(DistributionLocator)
    _, source, _, target, foo, bar, composite, single = tuple(ZincCompile._get_zinc_arguments(
      JvmPlatformSettings('1.7', '1.7', [
        'foo', 'bar', 'foo:$JAVA_HOME/bar:$JAVA_HOME/foobar', '$JAVA_HOME',
      ])
    ))

    self.assertEquals('-C1.7', source)
    self.assertEquals('-C1.7', target)
    self.assertEquals('foo', foo)
    self.assertEquals('bar', bar)
    self.assertNotEqual('$JAVA_HOME', single)
    self.assertNotIn('$JAVA_HOME', composite)
    self.assertEquals('foo:{0}/bar:{0}/foobar'.format(single), composite)
Exemplo n.º 25
0
    def test_implicit_junit_dep(self):
        init_subsystem(JUnit)
        # Check that the implicit dep is added, and doesn't replace other deps.
        target = self.make_target("//:target", Target)
        test1 = self.make_target("//:test1", JavaTests, dependencies=[target])
        self.assertEquals(
            ["JarLibrary(//:junit_library)", "Target(//:target)"], sorted(str(x) for x in test1.dependencies)
        )

        # Check that having an explicit dep doesn't cause problems.
        junit_target = self.build_graph.get_target_from_spec("//:junit_library")
        test2 = self.make_target("//:test2", JavaTests, dependencies=[junit_target, target])
        self.assertEquals(
            ["JarLibrary(//:junit_library)", "Target(//:target)"], sorted(str(x) for x in test2.dependencies)
        )
Exemplo n.º 26
0
  def custom_scala_platform_setup(self):
    # We don't need to specify :scalac or :scala-repl since they are never being fetched.
    self.make_target('//:scala-library',
                     JarLibrary,
                     jars=[JarDependency('org.scala-lang', 'scala-library', '2.10')],
    )
    self.make_target('//:scalastyle',
                     JarLibrary,
                     jars=[JarDependency('org.scalastyle', 'scalastyle_2.10', '0.3.2')],
    )

    init_subsystem(ScalaPlatform, {
      ScalaPlatform.options_scope: {
        'version': 'custom'
      }
    })
  def assert_options(self, subsystem_type, **input_opts):
    expected = {'skip': True}
    expected.update(**input_opts)

    unexpected = {('random', 'global', 'option', 'non', 'string', 'key'): 42,
                  'another_unneeded': 137}

    opts = expected.copy()
    opts.update(unexpected)

    subsystem_util.init_subsystem(subsystem_type,
                                  options={subsystem_type.options_scope: opts})

    subsystem_instance = subsystem_type.global_instance()
    self.assertTrue(subsystem_instance.get_options().skip)
    self.assertEqual(expected, json.loads(subsystem_instance.options_blob()))
    def fake_distribution_locator(*versions):
      """Sets up a fake distribution locator with fake distributions.

      Creates one distribution for each java version passed as an argument, and yields a list of
      paths to the java homes for each distribution.
      """
      with fake_distributions(versions) as paths:
        path_options = {
          DistributionLocator.options_scope: {
            'paths': {
              os_name: paths,
            }
          }
        }
        Subsystem.reset()
        init_subsystem(DistributionLocator, options=path_options)
        yield paths
Exemplo n.º 29
0
  def test_shader_project(self):
    """Test that the binary target at the ``shading_project`` can be built and run.

    Explicitly checks that the classes end up with the correct shaded fully qualified classnames.
    """
    shading_project = 'testprojects/src/java/org/pantsbuild/testproject/shading'
    self.assert_success(self.run_pants(['clean-all']))
    self.assert_success(self.run_pants(['binary', shading_project]))

    expected_classes = {
      # Explicitly excluded by a shading_exclude() rule.
      'org/pantsbuild/testproject/shadingdep/PleaseDoNotShadeMe.class',
      # Not matched by any rule, so stays the same.
      'org/pantsbuild/testproject/shading/Main.class',
      # Shaded with the target_id prefix, along with the default pants prefix.
      ('__shaded_by_pants__/org/pantsbuild/testproject/shadingdep/otherpackage/'
       'ShadeWithTargetId.class'),
      # Also shaded with the target_id prefix and default pants prefix, but for a different target
      # (so the target_id is different).
      ('__shaded_by_pants__/org/pantsbuild/testproject/shading/ShadeSelf.class'),
      # All these are shaded by the same shading_relocate_package(), which is recursive by default.
      '__shaded_by_pants__/org/pantsbuild/testproject/shadingdep/subpackage/Subpackaged.class',
      '__shaded_by_pants__/org/pantsbuild/testproject/shadingdep/SomeClass.class',
      '__shaded_by_pants__/org/pantsbuild/testproject/shadingdep/Dependency.class',
      # Shaded by a shading_relocate() that completely renames the package and class name.
      'org/pantsbuild/testproject/foo/bar/MyNameIsDifferentNow.class',
    }

    path = os.path.join('dist', 'shading.jar')
    init_subsystem(DistributionLocator)
    execute_java = DistributionLocator.cached(minimum_version='1.6').execute_java
    self.assertEqual(0, execute_java(classpath=[path],
                                      main='org.pantsbuild.testproject.shading.Main'))
    self.assertEqual(0, execute_java(classpath=[path],
                                      main='org.pantsbuild.testproject.foo.bar.MyNameIsDifferentNow'))

    received_classes = set()
    with temporary_dir() as tempdir:
      ZIP.extract(path, tempdir, filter_func=lambda f: f.endswith('.class'))
      for root, dirs, files in os.walk(tempdir):
        for name in files:
          received_classes.add(os.path.relpath(os.path.join(root, name), tempdir))

    self.assertEqual(expected_classes, received_classes)
Exemplo n.º 30
0
  def test_validation(self):
    init_subsystem(JUnit)
    target = self.make_target('//:mybird', Target)
    # A plain invocation with no frills
    test1 = self.make_target('//:test1', JUnitTests, sources=['Test.java'], dependencies=[target])
    self.assertIsNone(test1.cwd)
    self.assertIsNone(test1.concurrency)
    self.assertIsNone(test1.threads)
    self.assertIsNone(test1.timeout)

    # cwd parameter
    testcwd = self.make_target('//:testcwd1', JUnitTests, sources=['Test.java'],
                               concurrency='SERIAL', cwd='/foo/bar')
    self.assertEqual('/foo/bar', testcwd.cwd)

    # concurrency parameter
    tc1 = self.make_target('//:testconcurrency1', JUnitTests, sources=['Test.java'],
                           concurrency='SERIAL')
    self.assertEqual(JUnitTests.CONCURRENCY_SERIAL, tc1.concurrency)
    tc2 = self.make_target('//:testconcurrency2', JUnitTests, sources=['Test.java'],
                           concurrency='PARALLEL_CLASSES')
    self.assertEqual(JUnitTests.CONCURRENCY_PARALLEL_CLASSES, tc2.concurrency)
    tc3 = self.make_target('//:testconcurrency3', JUnitTests, sources=['Test.java'],
                           concurrency='PARALLEL_METHODS')
    self.assertEqual(JUnitTests.CONCURRENCY_PARALLEL_METHODS, tc3.concurrency)
    tc4 = self.make_target('//:testconcurrency4', JUnitTests, sources=['Test.java'],
                           concurrency='PARALLEL_CLASSES_AND_METHODS')
    self.assertEqual(JUnitTests.CONCURRENCY_PARALLEL_CLASSES_AND_METHODS, tc4.concurrency)
    with self.assertRaisesRegexp(TargetDefinitionException, r'concurrency'):
      self.make_target('//:testconcurrency5', JUnitTests, sources=['Test.java'],
                       concurrency='nonsense')

    # threads parameter
    tt1 = self.make_target('//:testthreads1', JUnitTests, sources=['Test.java'], threads=99)
    self.assertEqual(99, tt1.threads)
    tt2 = self.make_target('//:testthreads2', JUnitTests, sources=['Test.java'], threads="123")
    self.assertEqual(123, tt2.threads)
    with self.assertRaisesRegexp(TargetDefinitionException, r'threads'):
      self.make_target('//:testthreads3', JUnitTests, sources=['Test.java'], threads="abc")

    # timeout parameter
    timeout = self.make_target('//:testtimeout1', JUnitTests, sources=['Test.java'], timeout=999)
    self.assertEqual(999, timeout.timeout)
Exemplo n.º 31
0
 def setUp(self):
     super(JarCreateTestBase, self).setUp()
     self.set_options(compressed=False,
                      pants_bootstrapdir='~/.cache/pants',
                      max_subprocess_args=100)
     init_subsystem(Target.Arguments)
Exemplo n.º 32
0
    def test_setuptools_version(self):
        self.create_file('src/python/foo/__init__.py')
        self.create_python_library(
            relpath='src/python/foo/commands',
            name='commands',
            source_contents_map={
                'print_sys_path.py':
                dedent("""
          import os
          import sys
          from setuptools import Command


          class PrintSysPath(Command):
            user_options = []

            def initialize_options(self):
              pass

            def finalize_options(self):
              pass

            def run(self):
              with open(os.path.join(os.path.dirname(__file__), 'sys_path.txt'), 'w') as fp:
                fp.write(os.linesep.join(sys.path))
          """)
            },
        )
        foo = self.create_python_library(relpath='src/python/foo',
                                         name='foo',
                                         dependencies=[
                                             'src/python/foo/commands',
                                         ],
                                         provides=dedent("""
      setup_py(
        name='foo',
        version='0.0.0',
      )
      """))
        self.set_options(run='print_sys_path')

        # Make sure setup.py can see our custom distutils Command 'print_sys_path'.
        sdist_srcdir = os.path.join(self.distdir, 'foo-0.0.0', 'src')
        with environment_as(PYTHONPATH=sdist_srcdir):
            with self.run_execute(foo):
                with open(
                        os.path.join(sdist_srcdir, 'foo', 'commands',
                                     'sys_path.txt'), 'r') as fp:
                    load_package = lambda: Package.from_href(fp.readline().
                                                             strip())
                    # We don't care about the ordering of `wheel` and `setuptools` on the `sys.path`, just
                    # that they are 1st as a group.
                    extras = {
                        p.name: p
                        for p in (load_package(), load_package())
                    }

                    def assert_extra(name, expected_version):
                        package = extras.get(name)
                        self.assertIsNotNone(package)
                        self.assertEqual(expected_version, package.raw_version)

                    # The 1st two elements of the sys.path should be our custom SetupPyRunner Installer's
                    # setuptools and wheel mixins, which should match the setuptools and wheel versions
                    # specified by the PythonSetup subsystem.
                    init_subsystem(PythonSetup)
                    python_setup = PythonSetup.global_instance()
                    assert_extra('setuptools', python_setup.setuptools_version)
                    assert_extra('wheel', python_setup.wheel_version)
Exemplo n.º 33
0
 def test_unknown_kwargs(self):
   options = {Target.Arguments.options_scope: {'ignored': {'Target': ['foobar']}}}
   init_subsystem(Target.Arguments, options)
   target = self.make_target('foo:bar', Target, foobar='barfoo')
   self.assertFalse(hasattr(target, 'foobar'))
Exemplo n.º 34
0
 def create_native_scheduler(self, intrinsic_entries, root_subject_types, rules):
   init_subsystem(Native.Factory)
   rule_index = RuleIndex.create(rules, intrinsic_entries)
   native = Native.Factory.global_instance().create()
   scheduler = WrappedNativeScheduler(native, '.', [], rule_index, root_subject_types)
   return scheduler
Exemplo n.º 35
0
 def setUp(self):
     super(ClasspathProductsTest, self).setUp()
     init_subsystem(Target.Arguments)
Exemplo n.º 36
0
def init_native():
    """Initialize and return the `Native` subsystem."""
    init_subsystem(Native.Factory)
    return Native.Factory.global_instance().create()
Exemplo n.º 37
0
 def setUp(self):
     super(SetupPyTestBase, self).setUp()
     self.distdir = os.path.join(self.build_root, 'dist')
     self.set_options(pants_distdir=self.distdir)
     init_subsystem(Target.Arguments)
Exemplo n.º 38
0
 def pex_builder_wrapper(**kwargs):
     subsystem_util.init_subsystem(PexBuilderWrapper.Factory)
     return PexBuilderWrapper.Factory.create(PEXBuilder(**kwargs))
Exemplo n.º 39
0
 def setUp(self):
     super(PythonTaskTestBase, self).setUp()
     subsystem_util.init_subsystem(PythonBinary.Defaults)
Exemplo n.º 40
0
 def setUp(self):
     super().setUp()
     init_subsystem(Target.Arguments)
Exemplo n.º 41
0
 def setUp(self):
     super(CoursierResolveFingerprintStrategyTest, self).setUp()
     init_subsystem(JarDependencyManagement)
Exemplo n.º 42
0
    def test_metacp_job_scheduled_for_jar_library(self):
        # Init dependencies for scala library targets.
        init_subsystem(
            ScalaPlatform, {
                ScalaPlatform.options_scope: {
                    'version': 'custom',
                    'suffix_version': '2.12',
                }
            })
        self.make_target(
            '//:scala-library',
            target_type=JarLibrary,
            jars=[JarDependency(org='com.example', name='scala', rev='0.0.0')])

        jar_target = self.make_target('java/classpath:jar_lib',
                                      target_type=JarLibrary,
                                      jars=[
                                          JarDependency(org='com.example',
                                                        name='example',
                                                        rev='0.0.0')
                                      ])

        java_target = self.make_target('java/classpath:java_lib',
                                       target_type=JavaLibrary,
                                       sources=['com/example/Foo.java'],
                                       dependencies=[jar_target])

        scala_target = self.make_target('java/classpath:scala_lib',
                                        target_type=ScalaLibrary,
                                        sources=['com/example/Foo.scala'],
                                        dependencies=[jar_target])

        context = self.context(target_roots=[jar_target])

        context.products.get_data(
            'compile_classpath',
            ClasspathProducts.init_func(self.pants_workdir))
        context.products.get_data(
            'runtime_classpath',
            ClasspathProducts.init_func(self.pants_workdir))

        task = self.create_task(context)
        # tried for options, but couldn't get it to reconfig
        task._size_estimator = lambda srcs: 0
        with temporary_dir() as tmp_dir:
            compile_contexts = {
                target:
                task.create_compile_context(target,
                                            os.path.join(tmp_dir, target.id))
                for target in [jar_target, java_target, scala_target]
            }

            invalid_targets = [java_target, scala_target, jar_target]

            jobs = task._create_compile_jobs(
                compile_contexts,
                invalid_targets,
                invalid_vts=[LightWeightVTS(t) for t in invalid_targets],
                classpath_product=None)

            exec_graph = ExecutionGraph(
                jobs,
                task.get_options().print_exception_stacktrace)
            dependee_graph = exec_graph.format_dependee_graph()

            self.assertEqual(
                dedent("""
                     metacp(jdk) -> {
                       rsc(java/classpath:scala_lib),
                       compile_against_rsc(java/classpath:scala_lib),
                       metacp(java/classpath:jar_lib)
                     }
                     compile_against_rsc(java/classpath:java_lib) -> {}
                     rsc(java/classpath:scala_lib) -> {
                       compile_against_rsc(java/classpath:scala_lib)
                     }
                     compile_against_rsc(java/classpath:scala_lib) -> {}
                     metacp(java/classpath:jar_lib) -> {
                       rsc(java/classpath:scala_lib)
                     }""").strip(), dependee_graph)
Exemplo n.º 43
0
    def setUp(self):
        super(DepmapTest, self).setUp()
        subsystem_util.init_subsystem(PythonBinary.Defaults)

        def add_to_build_file(path, name, type, deps=(), **kwargs):
            self.add_to_build_file(
                path,
                dedent("""
          {type}(name='{name}',
            dependencies=[{deps}],
            {extra}
          )
          """.format(type=type,
                     name=name,
                     deps=','.join("'{0}'".format(dep) for dep in list(deps)),
                     extra=('' if not kwargs else ', '.join(
                         '{0}={1}'.format(k, v) for k, v in kwargs.items())))))

        def create_python_binary_target(path, name, entry_point, type,
                                        deps=()):
            self.add_to_build_file(
                path,
                dedent("""
          {type}(name='{name}',
            entry_point='{entry_point}',
            dependencies=[{deps}]
          )
          """.format(type=type,
                     entry_point=entry_point,
                     name=name,
                     deps=','.join("'{0}'".format(dep)
                                   for dep in list(deps)))))

        def create_jvm_app(path, name, type, binary, deps=()):
            self.add_to_build_file(
                path,
                dedent("""
          {type}(name='{name}',
            dependencies=['{binary}'],
            bundles={deps}
          )
          """.format(type=type, name=name, binary=binary, deps=deps)))

        add_to_build_file('common/a', 'a', 'target')
        add_to_build_file('common/b', 'b', 'target')
        self.add_to_build_file(
            'common/c',
            dedent("""
      java_library(name='c',
        sources=[],
      )
    """))
        add_to_build_file('common/d', 'd', 'python_library', sources=[])
        create_python_binary_target('common/e', 'e', 'common.e.entry',
                                    'python_binary')
        add_to_build_file('common/f', 'f', 'jvm_binary')
        add_to_build_file('common/g', 'g', 'jvm_binary', deps=['common/f:f'])
        self.create_dir('common/h')
        self.create_file('common/h/common.f')
        create_jvm_app('common/h', 'h', 'jvm_app', 'common/f:f',
                       "[bundle(fileset='common.f')]")
        self.create_dir('common/i')
        self.create_file('common/i/common.g')
        create_jvm_app('common/i', 'i', 'jvm_app', 'common/g:g',
                       "[bundle(fileset='common.g')]")
        add_to_build_file('overlaps',
                          'one',
                          'jvm_binary',
                          deps=['common/h', 'common/i'])
        self.add_to_build_file(
            'overlaps',
            dedent("""
      java_library(name='two',
        dependencies=['overlaps:one'],
        sources=[],
      )
    """))
        self.add_to_build_file(
            'resources/a',
            dedent("""
      resources(
        name='a_resources',
        sources=['a.resource']
      )
    """))
        self.add_to_build_file(
            'src/java/a',
            dedent("""
      java_library(
        name='a_java',
        sources=[],
        dependencies=['resources/a:a_resources']
      )
    """))
        self.add_to_build_file(
            'src/java/a',
            dedent("""
      target(
        name='a_dep',
        dependencies=[':a_java']
      )
    """))

        self.add_to_build_file(
            'src/java/b',
            dedent("""
      java_library(
        name='b_java',
        dependencies=[':b_dep'],
        sources=[],
      )
      target(
        name='b_dep',
        dependencies=[':b_lib']
      )
      java_library(
        name='b_lib',
        sources=[],
      )
    """))

        self.add_to_build_file(
            'src/java/c',
            dedent('''
      jar_library(
        name='c_jar_lib',
        jars=[
          jar(org='org.pantsbuild.test', name='c_test', rev='1.0'),
          jar(org='org.pantsbuild.test', name='d_test', rev=''),
        ]
      )
    '''))

        # It makes no sense whatsoever to have a java_library that depends
        # on a Python library, but we want to ensure that depmap handles
        # cases like this anyway because there might be other cases which
        # do make sense (e.g. things that generate generic resources)
        self.add_to_build_file(
            'src/java/java_depends_on_python',
            dedent("""
      java_library(
        name='java_depends_on_python',
        dependencies=['common/d:d'],
        sources=[],
      )
    """))
Exemplo n.º 44
0
 def setUp(self):
     self.jarjar = '/not/really/jarjar.jar'
     init_subsystem(DistributionLocator)
     executor = SubprocessExecutor(DistributionLocator.cached())
     self.shader = Shader(jarjar_classpath=[self.jarjar], executor=executor)
     self.output_jar = '/not/really/shaded.jar'
Exemplo n.º 45
0
 def setUp(self):
   super().setUp()
   init_subsystem(JUnit)
Exemplo n.º 46
0
    def test_validation(self):
        init_subsystem(JUnit)
        target = self.make_target('//:mybird', Target)
        # A plain invocation with no frills
        test1 = self.make_target('//:test1',
                                 JUnitTests,
                                 sources=['Test.java'],
                                 dependencies=[target])
        self.assertIsNone(test1.cwd)
        self.assertIsNone(test1.concurrency)
        self.assertIsNone(test1.threads)
        self.assertIsNone(test1.timeout)

        # cwd parameter
        testcwd = self.make_target('//:testcwd1',
                                   JUnitTests,
                                   sources=['Test.java'],
                                   concurrency='SERIAL',
                                   cwd='/foo/bar')
        self.assertEquals('/foo/bar', testcwd.cwd)

        # concurrency parameter
        tc1 = self.make_target('//:testconcurrency1',
                               JUnitTests,
                               sources=['Test.java'],
                               concurrency='SERIAL')
        self.assertEquals(JUnitTests.CONCURRENCY_SERIAL, tc1.concurrency)
        tc2 = self.make_target('//:testconcurrency2',
                               JUnitTests,
                               sources=['Test.java'],
                               concurrency='PARALLEL_CLASSES')
        self.assertEquals(JUnitTests.CONCURRENCY_PARALLEL_CLASSES,
                          tc2.concurrency)
        tc3 = self.make_target('//:testconcurrency3',
                               JUnitTests,
                               sources=['Test.java'],
                               concurrency='PARALLEL_METHODS')
        self.assertEquals(JUnitTests.CONCURRENCY_PARALLEL_METHODS,
                          tc3.concurrency)
        tc4 = self.make_target('//:testconcurrency4',
                               JUnitTests,
                               sources=['Test.java'],
                               concurrency='PARALLEL_CLASSES_AND_METHODS')
        self.assertEquals(JUnitTests.CONCURRENCY_PARALLEL_CLASSES_AND_METHODS,
                          tc4.concurrency)
        with self.assertRaisesRegexp(TargetDefinitionException,
                                     r'concurrency'):
            self.make_target('//:testconcurrency5',
                             JUnitTests,
                             sources=['Test.java'],
                             concurrency='nonsense')

        # threads parameter
        tt1 = self.make_target('//:testthreads1',
                               JUnitTests,
                               sources=['Test.java'],
                               threads=99)
        self.assertEquals(99, tt1.threads)
        tt2 = self.make_target('//:testthreads2',
                               JUnitTests,
                               sources=['Test.java'],
                               threads="123")
        self.assertEquals(123, tt2.threads)
        with self.assertRaisesRegexp(TargetDefinitionException, r'threads'):
            self.make_target('//:testthreads3',
                             JUnitTests,
                             sources=['Test.java'],
                             threads="abc")

        # timeout parameter
        timeout = self.make_target('//:testtimeout1',
                                   JUnitTests,
                                   sources=['Test.java'],
                                   timeout=999)
        self.assertEquals(999, timeout.timeout)
Exemplo n.º 47
0
 def setUp(self):
     super().setUp()
     init_subsystem(JarDependencyManagement)
Exemplo n.º 48
0
 def setUp(self):
     super(BootstrapperTest, self).setUp()
     init_subsystem(IvySubsystem)
Exemplo n.º 49
0
def init_native():
    """Initialize and return a `Native` instance."""
    init_subsystem(BinaryUtil.Factory)
    opts = create_options_for_optionables([])
    return Native.create(opts.for_global_scope())
Exemplo n.º 50
0
 def test_illegal_kwargs(self):
     init_subsystem(Target.Arguments)
     with self.assertRaises(Target.Arguments.UnknownArgumentError) as cm:
         self.make_target('foo:bar', Target, foobar='barfoo')
     self.assertTrue('foobar = barfoo' in str(cm.exception))
     self.assertTrue('foo:bar' in str(cm.exception))
Exemplo n.º 51
0
 def setUp(self):
     super(JUnitRunnerTest, self).setUp()
     init_subsystem(JUnit)
Exemplo n.º 52
0
  def test_coverage_omit_test_sources(self):
    init_subsystem(Target.Arguments)
    init_subsystem(SourceRootConfig)

    self.add_to_build_file('src/python/util', 'python_library()\n')

    self.create_file(
      'src/python/util/math.py',
      dedent("""
          from util import THE_LONELIEST_NUMBER  # line 1
          def one():                             # line 2
            return THE_LONELIEST_NUMBER          # line 3
        """).strip())

    self.create_file(
      'src/python/util/__init__.py',
      dedent("""
          THE_LONELIEST_NUMBER = 1  # line 1
        """).strip())

    self.add_to_build_file('src/python/util',
                           'python_tests(name="tests", dependencies = [":util"])\n')

    self.create_file(
      'src/python/util/math_test.py',
      dedent("""
          import unittest                                            # line 1

          from util import math                                      # line 3

          class MathTestInSameDirectoryAsSource(unittest.TestCase):  # line 5
            def test_one(self):                                      # line 6
              self.assertEqual(1, math.one())                        # line 7
        """).strip())

    test = self.target('src/python/util:tests')

    src_path = os.path.join(self.build_root, 'src/python/util/math.py')
    init_path = os.path.join(self.build_root, 'src/python/util/__init__.py')
    test_path = os.path.join(self.build_root, 'src/python/util/math_test.py')

    # First run omitting the test file.
    self.assertFalse(os.path.isfile(self.coverage_data_file()))
    coverage_kwargs = {'coverage': 'auto'}
    context = self.run_tests(targets=[test], **coverage_kwargs)
    all_statements, not_run_statements = self.load_coverage_data_for(context, src_path)
    self.assertEqual([1, 2, 3], all_statements)
    self.assertEqual([], not_run_statements)
    all_statements, not_run_statements = self.load_coverage_data_for(context, init_path)
    self.assertEqual([1], all_statements)
    self.assertEqual([], not_run_statements)
    all_statements, not_run_statements = self.load_coverage_data_for(context, test_path)
    self.assertEqual([1, 3, 5, 6, 7], all_statements)
    # "not run" means "not traced".
    self.assertEqual([1, 3, 5, 6, 7], not_run_statements)
    # TODO: Switch this test to read coverage data from an XML report instead of
    # directly from the analysis. That way we see what the users sees, instead of the
    # slightly confusing raw analysis.

    os.unlink(self.coverage_data_file())

    # Now run again, including the test file.
    self.assertFalse(os.path.isfile(self.coverage_data_file()))
    coverage_kwargs = {'coverage': 'auto', 'coverage_include_test_sources': True}
    context = self.run_tests(targets=[test], **coverage_kwargs)
    all_statements, not_run_statements = self.load_coverage_data_for(context, src_path)
    self.assertEqual([1, 2, 3], all_statements)
    self.assertEqual([], not_run_statements)
    all_statements, not_run_statements = self.load_coverage_data_for(context, init_path)
    self.assertEqual([1], all_statements)
    self.assertEqual([], not_run_statements)
    all_statements, not_run_statements = self.load_coverage_data_for(context, test_path)
    self.assertEqual([1, 3, 5, 6, 7], all_statements)
    self.assertEqual([], not_run_statements)