Example #1
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')
 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')
Example #3
0
    def register_options(cls, register):
        """Register options for this optionable.

    In this case, there are no special options, but we want to use this opportunity to setup
    goal validation in PrepCommand before the build graph is parsed.
    """
        super(RunPrepCommandBase, cls).register_options(register)
        PrepCommand.add_goal(cls.goal)
Example #4
0
  def register_options(cls, register):
    """Register options for this optionable.

    In this case, there are no special options, but we want to use this opportunity to setup
    goal validation in PrepCommand before the build graph is parsed.
    """
    super().register_options(register)
    PrepCommand.add_allowed_goal(cls.goal)
Example #5
0
    def test_prep_command_case(self):
        PrepCommand.add_allowed_goal("compile")
        PrepCommand.add_allowed_goal("test")
        self.add_to_build_file(
            "build-support/thrift",
            dedent(
                """
                prep_command(
                  name='prepare_binary_compile',
                  goals=['compile'],
                  prep_executable='/bin/true',
                )

                prep_command(
                  name='prepare_binary_test',
                  goals=['test'],
                  prep_executable='/bin/true',
                )

                target(
                  name='prepare_binary',
                  dependencies=[
                    ':prepare_binary_compile',
                    ':prepare_binary_test',
                  ],
                )
                """
            ),
        )
        prepare_binary_compile = self.make_target(
            spec="build-support/thrift:prepare_binary_compile",
            target_type=PrepCommand,
            prep_executable="/bin/true",
            goals=["compile"],
        )
        prepare_binary_test = self.make_target(
            spec="build-support/thrift:prepare_binary_test",
            target_type=PrepCommand,
            prep_executable="/bin/true",
            goals=["test"],
        )
        self.make_target(
            spec="build-support/thrift:prepare_binary",
            dependencies=[prepare_binary_compile, prepare_binary_test],
        )

        pants = self.create_python_library(
            relpath="src/python/pants",
            name="pants",
            provides="setup_py(name='pants', version='0.0.0')",
            dependencies=["build-support/thrift:prepare_binary"],
        )
        with self.run_execute(pants) as created:
            self.assertEqual([pants], list(created.keys()))
Example #6
0
  def execute(self):
    if self.goal not in PrepCommand.allowed_goals():
      raise AssertionError('Got goal "{}". Expected goal to be one of {}'.format(
          self.goal, PrepCommand.goals()))

    targets = self.context.targets(postorder=True, predicate=self.runnable_prep_cmd)
    Cmdline = namedtuple('Cmdline', ['cmdline', 'environ'])

    def make_cmdline(target):
      executable = target.payload.get_field_value('prep_command_executable')
      args = target.payload.get_field_value('prep_command_args', [])
      prep_environ = target.payload.get_field_value('prep_environ')
      cmdline = [executable]
      cmdline.extend(args)
      return Cmdline(cmdline=tuple(cmdline), environ=prep_environ)

    def has_prep(target):
      return target.payload.get_field_value('prep_command_executable')

    cmdlines = [make_cmdline(target) for target in targets if has_prep(target)]

    if not cmdlines:
      return

    with self.context.new_workunit(name='prep_command', labels=[WorkUnitLabel.PREP]) as workunit:
      completed_cmdlines = set()
      for item in cmdlines:
        cmdline = item.cmdline
        environ = item.environ
        if not cmdline in completed_cmdlines:
          completed_cmdlines.add(cmdline)
          stderr = workunit.output('stderr') if workunit else None
          try:
            process = subprocess.Popen(cmdline, stdout=subprocess.PIPE, stderr=stderr)
          except OSError as e:
            workunit.set_outcome(WorkUnit.FAILURE)
            raise TaskError('RunPrepCommand failed to execute {cmdline}: {error}'.format(
              cmdline=cmdline, error=e))
          stdout, _ = process.communicate()

          if environ:
            if not process.returncode:
              environment_vars = stdout.decode().split('\0')
              for kvpair in environment_vars:
                var, value = kvpair.split('=', 1)
                os.environ[var] = value
          else:
            if workunit:
              workunit.output('stdout').write(stdout)

          workunit.set_outcome(WorkUnit.FAILURE if process.returncode else WorkUnit.SUCCESS)
          if process.returncode:
            raise TaskError('RunPrepCommand failed to run {cmdline}'.format(cmdline=cmdline))
Example #7
0
  def execute(self):
    if self.goal not in PrepCommand.allowed_goals():
      raise AssertionError('Got goal "{}". Expected goal to be one of {}'.format(
          self.goal, PrepCommand.goals()))

    targets = self.context.targets(postorder=True, predicate=self.runnable_prep_cmd)
    Cmdline = namedtuple('Cmdline', ['cmdline', 'environ'])

    def make_cmdline(target):
      executable = target.payload.get_field_value('prep_command_executable')
      args = target.payload.get_field_value('prep_command_args', [])
      prep_environ = target.payload.get_field_value('prep_environ')
      cmdline = [executable]
      cmdline.extend(args)
      return Cmdline(cmdline=tuple(cmdline), environ=prep_environ)

    def has_prep(target):
      return target.payload.get_field_value('prep_command_executable')

    cmdlines = [make_cmdline(target) for target in targets if has_prep(target)]

    if not cmdlines:
      return

    with self.context.new_workunit(name='prep_command', labels=[WorkUnitLabel.PREP]) as workunit:
      completed_cmdlines = set()
      for item in cmdlines:
        cmdline = item.cmdline
        environ = item.environ
        if not cmdline in completed_cmdlines:
          completed_cmdlines.add(cmdline)
          stderr = workunit.output('stderr') if workunit else None
          try:
            process = subprocess.Popen(cmdline, stdout=subprocess.PIPE, stderr=stderr)
          except OSError as e:
            workunit.set_outcome(WorkUnit.FAILURE)
            raise TaskError('RunPrepCommand failed to execute {cmdline}: {error}'.format(
              cmdline=cmdline, error=e))
          stdout, _ = process.communicate()

          if environ:
            if not process.returncode:
              environment_vars = stdout.decode('utf-8').split('\0')
              for kvpair in environment_vars:
                var, value = kvpair.split('=', 1)
                os.environ[var] = value
          else:
            if workunit:
              workunit.output('stdout').write(stdout)

          workunit.set_outcome(WorkUnit.FAILURE if process.returncode else WorkUnit.SUCCESS)
          if process.returncode:
            raise TaskError('RunPrepCommand failed to run {cmdline}'.format(cmdline=cmdline))
Example #8
0
    def test_prep_command_case(self):
        PrepCommand.add_allowed_goal("compile")
        PrepCommand.add_allowed_goal("test")
        self.add_to_build_file(
            "build-support/thrift",
            dedent(
                """
                           prep_command(
                             name='prepare_binary_compile',
                             goal='compile',
                             prep_executable='/bin/true',
                           )

                           prep_command(
                             name='prepare_binary_test',
                             goal='test',
                             prep_executable='/bin/true',
                           )

                           target(
                             name='prepare_binary',
                             dependencies=[
                               ':prepare_binary_compile',
                               ':prepare_binary_test',
                             ],
                           )
                           """
            ),
        )
        prepare_binary_compile = self.make_target(
            spec="build-support/thrift:prepare_binary_compile",
            target_type=PrepCommand,
            prep_executable="/bin/true",
            goal="compile",
        )
        prepare_binary_test = self.make_target(
            spec="build-support/thrift:prepare_binary_test",
            target_type=PrepCommand,
            prep_executable="/bin/true",
            goal="test",
        )
        self.make_target(
            spec="build-support/thrift:prepare_binary", dependencies=[prepare_binary_compile, prepare_binary_test]
        )

        pants = self.create_python_library(
            relpath="src/python/pants",
            name="pants",
            provides="setup_py(name='pants', version='0.0.0')",
            dependencies=["build-support/thrift:prepare_binary"],
        )
        with self.run_execute(pants) as created:
            self.assertEqual([pants], created.keys())
Example #9
0
  def test_prep_command_case(self):
    PrepCommand.add_allowed_goal('compile')
    PrepCommand.add_allowed_goal('test')
    self.add_to_build_file('build-support/thrift',
                           dedent("""
                           prep_command(
                             name='prepare_binary_compile',
                             goals=['compile'],
                             prep_executable='/bin/true',
                           )

                           prep_command(
                             name='prepare_binary_test',
                             goals=['test'],
                             prep_executable='/bin/true',
                           )

                           target(
                             name='prepare_binary',
                             dependencies=[
                               ':prepare_binary_compile',
                               ':prepare_binary_test',
                             ],
                           )
                           """))
    prepare_binary_compile = self.make_target(spec='build-support/thrift:prepare_binary_compile',
                                              target_type=PrepCommand,
                                              prep_executable='/bin/true',
                                              goals=['compile'])
    prepare_binary_test = self.make_target(spec='build-support/thrift:prepare_binary_test',
                                           target_type=PrepCommand,
                                           prep_executable='/bin/true',
                                           goals=['test'])
    self.make_target(spec='build-support/thrift:prepare_binary',
                     dependencies=[
                       prepare_binary_compile,
                       prepare_binary_test
                     ])

    pants = self.create_python_library(
      relpath='src/python/pants',
      name='pants',
      provides="setup_py(name='pants', version='0.0.0')",
      dependencies=[
        'build-support/thrift:prepare_binary'
      ]
    )
    with self.run_execute(pants) as created:
      self.assertEqual([pants], list(created.keys()))
Example #10
0
  def test_prep_command_case(self):
    PrepCommand.add_allowed_goal('compile')
    PrepCommand.add_allowed_goal('test')
    self.add_to_build_file('build-support/thrift',
                           dedent("""
                           prep_command(
                             name='prepare_binary_compile',
                             goal='compile',
                             prep_executable='/bin/true',
                           )

                           prep_command(
                             name='prepare_binary_test',
                             goal='test',
                             prep_executable='/bin/true',
                           )

                           target(
                             name='prepare_binary',
                             dependencies=[
                               ':prepare_binary_compile',
                               ':prepare_binary_test',
                             ],
                           )
                           """))
    prepare_binary_compile = self.make_target(spec='build-support/thrift:prepare_binary_compile',
                                              target_type=PrepCommand,
                                              prep_executable='/bin/true',
                                              goal='compile')
    prepare_binary_test = self.make_target(spec='build-support/thrift:prepare_binary_test',
                                           target_type=PrepCommand,
                                           prep_executable='/bin/true',
                                           goal='test')
    self.make_target(spec='build-support/thrift:prepare_binary',
                     dependencies=[
                       prepare_binary_compile,
                       prepare_binary_test
                     ])

    pants = self.create_python_library(
      relpath='src/python/pants',
      name='pants',
      provides="setup_py(name='pants', version='0.0.0')",
      dependencies=[
        'build-support/thrift:prepare_binary'
      ]
    )
    with self.run_execute(pants) as created:
      self.assertEqual([pants], created.keys())
Example #11
0
 def tearDown(self):
     PrepCommand.reset()
Example #12
0
 def tearDown(self):
   PrepCommand.reset()