Exemplo n.º 1
0
 def test_single_non_test_target(self):
     bfaddr = BuildFileAddress(None, 'bin', 'some/dir')
     target_adaptor = PythonBinaryAdaptor(type_alias='python_binary')
     with self.captured_logging(logging.INFO):
         # Note that this is not the same error message the end user will see, as we're resolving
         # union Get requests in run_rule, not the real engine.  But this test still asserts that
         # we error when we expect to error.
         with self.assertRaisesRegex(
                 AssertionError,
                 r'Rule requested: .* which cannot be satisfied.'):
             run_rule(
                 coordinator_of_tests,
                 rule_args=[
                     HydratedTarget(bfaddr.to_address(), target_adaptor,
                                    ()),
                     UnionMembership(
                         union_rules={TestTarget: [PythonTestsAdaptor]}),
                     AddressProvenanceMap(
                         bfaddr_to_spec={
                             bfaddr:
                             SingleAddress(directory='some/dir',
                                           name='bin'),
                         }),
                 ],
                 mock_gets=[
                     MockGet(
                         product_type=TestResult,
                         subject_type=PythonTestsAdaptor,
                         mock=lambda _: TestResult(status=Status.SUCCESS,
                                                   stdout='foo',
                                                   stderr=''),
                     ),
                 ],
             )
Exemplo n.º 2
0
    def test_globbed_non_test_target(self):
        bfaddr = BuildFileAddress(None, 'bin', 'some/dir')
        target_adaptor = PythonBinaryAdaptor(type_alias='python_binary')
        with self.captured_logging(logging.INFO):
            result = run_rule(
                coordinator_of_tests,
                rule_args=[
                    HydratedTarget(bfaddr.to_address(), target_adaptor, ()),
                    UnionMembership(
                        union_rules={TestTarget: [PythonTestsAdaptor]}),
                    AddressProvenanceMap(
                        bfaddr_to_spec={
                            bfaddr: DescendantAddresses(directory='some/dir')
                        }),
                ],
                mock_gets=[
                    MockGet(
                        product_type=TestResult,
                        subject_type=PythonTestsAdaptor,
                        mock=lambda _: TestResult(
                            status=Status.SUCCESS, stdout='foo', stderr=''),
                    ),
                ],
            )

            self.assertEqual(result,
                             AddressAndTestResult(bfaddr.to_address(), None))
Exemplo n.º 3
0
    def test_coordinator_python_test(self):
        addr = Address.parse("some/target")
        target_adaptor = PythonTestsAdaptor(type_alias='python_tests')
        with self.captured_logging(logging.INFO):
            result = run_rule(
                coordinator_of_tests,
                rule_args=[
                    HydratedTarget(addr, target_adaptor, ()),
                    UnionMembership(
                        union_rules={TestTarget: [PythonTestsAdaptor]}),
                    AddressProvenanceMap(bfaddr_to_spec={}),
                ],
                mock_gets=[
                    MockGet(
                        product_type=TestResult,
                        subject_type=PythonTestsAdaptor,
                        mock=lambda _: TestResult(
                            status=Status.FAILURE, stdout='foo', stderr=''),
                    ),
                ],
            )

        self.assertEqual(
            result,
            AddressAndTestResult(
                addr, TestResult(status=Status.FAILURE,
                                 stdout='foo',
                                 stderr='')))
Exemplo n.º 4
0
async def run_setup_pys(targets: HydratedTargets, options: SetupPyOptions, console: Console,
                        provenance_map: AddressProvenanceMap,
                        distdir: DistDir, workspace: Workspace) -> SetupPy:
  """Run setup.py commands on all exported targets addressed."""
  args = tuple(options.values.args)
  validate_args(args)

  # Get all exported targets, ignoring any non-exported targets that happened to be
  # globbed over, but erroring on any explicitly-requested non-exported targets.

  exported_targets: List[ExportedTarget] = []
  explicit_nonexported_targets: List[HydratedTarget] = []

  for hydrated_target in targets:
    if _is_exported(hydrated_target):
      exported_targets.append(ExportedTarget(hydrated_target))
    elif provenance_map.is_single_address(hydrated_target.address):
      explicit_nonexported_targets.append(hydrated_target)
  if explicit_nonexported_targets:
    raise TargetNotExported(
      'Cannot run setup.py on these targets, because they have no `provides=` clause: '
      f'{", ".join(so.address.reference() for so in explicit_nonexported_targets)}')

  if options.values.transitive:
    # Expand out to all owners of the entire dep closure.
    tht = await Get[TransitiveHydratedTargets](
      BuildFileAddresses([et.hydrated_target.address for et in exported_targets]))
    owners = await MultiGet(
      Get[ExportedTarget](OwnedDependency(ht)) for ht in tht.closure if is_ownable_target(ht)
    )
    exported_targets = list(set(owners))

  chroots = await MultiGet(Get[SetupPyChroot](SetupPyChrootRequest(target))
                           for target in exported_targets)

  if args:
    setup_py_results = await MultiGet(
      Get[RunSetupPyResult](RunSetupPyRequest(exported_target, chroot, tuple(args)))
      for exported_target, chroot in zip(exported_targets, chroots)
    )

    for exported_target, setup_py_result in zip(exported_targets, setup_py_results):
      addr = exported_target.hydrated_target.address.reference()
      console.print_stderr(f'Writing contents of dist dir for {addr} to {distdir.relpath}')
      workspace.materialize_directory(
        DirectoryToMaterialize(setup_py_result.output, path_prefix=str(distdir.relpath))
      )
  else:
    # Just dump the chroot.
    for exported_target, chroot in zip(exported_targets, chroots):
      addr = exported_target.hydrated_target.address.reference()
      provides = exported_target.hydrated_target.adaptor.provides
      setup_py_dir = distdir.relpath / f'{provides.name}-{provides.version}'
      console.print_stderr(f'Writing setup.py chroot for {addr} to {setup_py_dir}')
      workspace.materialize_directory(
        DirectoryToMaterialize(chroot.digest, path_prefix=str(setup_py_dir))
      )

  return SetupPy(0)
Exemplo n.º 5
0
 def is_testable(target: HydratedTarget, *,
                 union_membership: UnionMembership,
                 provenance_map: AddressProvenanceMap) -> bool:
     is_valid_target_type = (
         provenance_map.is_single_address(target.address)
         or union_membership.is_member(TestTarget, target.adaptor))
     has_sources = hasattr(
         target.adaptor,
         "sources") and target.adaptor.sources.snapshot.files
     return is_valid_target_type and has_sources
Exemplo n.º 6
0
 def run_coordinator_of_tests(
     self,
     *,
     address: Address,
     bfaddr_to_address_spec: Optional[Dict[BuildFileAddress,
                                           AddressSpec]] = None,
     test_target_type: bool = True,
     include_sources: bool = True,
 ) -> AddressAndTestResult:
     mocked_fileset = EagerFilesetWithSpec(
         "src",
         {"globs": []},
         snapshot=Snapshot(
             # TODO: this is not robust to set as an empty digest. Add a test util that provides
             #  some premade snapshots and possibly a generalized make_hydrated_target function.
             directory_digest=EMPTY_DIRECTORY_DIGEST,
             files=tuple(["test.py"] if include_sources else []),
             dirs=()))
     target_adaptor = (PythonTestsAdaptor(type_alias='python_tests',
                                          sources=mocked_fileset)
                       if test_target_type else PythonBinaryAdaptor(
                           type_alias='python_binary',
                           sources=mocked_fileset))
     with self.captured_logging(logging.INFO):
         result: AddressAndTestResult = run_rule(
             coordinator_of_tests,
             rule_args=[
                 HydratedTarget(address, target_adaptor, ()),
                 UnionMembership(
                     union_rules={TestTarget: [PythonTestsAdaptor]}),
                 AddressProvenanceMap(
                     bfaddr_to_address_spec=bfaddr_to_address_spec or {}),
             ],
             mock_gets=[
                 MockGet(
                     product_type=TestResult,
                     subject_type=PythonTestsAdaptor,
                     mock=lambda _: TestResult(
                         status=Status.SUCCESS, stdout='foo', stderr=''),
                 ),
             ],
         )
     return result
Exemplo n.º 7
0
def coordinator_of_tests(
        target: HydratedTarget, union_membership: UnionMembership,
        provenance_map: AddressProvenanceMap) -> AddressAndTestResult:
    # TODO(#6004): when streaming to live TTY, rely on V2 UI for this information. When not a
    # live TTY, periodically dump heavy hitters to stderr. See
    # https://github.com/pantsbuild/pants/issues/6004#issuecomment-492699898.
    if (provenance_map.is_single_address(target.address)
            or union_membership.is_member(TestTarget, target.adaptor)):
        logger.info("Starting tests: {}".format(target.address.reference()))
        # NB: This has the effect of "casting" a TargetAdaptor to a member of the TestTarget union.
        # The adaptor will always be a member because of the union membership check above, but if
        # it were not it would fail at runtime with a useful error message.
        result = yield Get(TestResult, TestTarget, target.adaptor)
        logger.info("Tests {}: {}".format(
            "succeeded" if result.status == Status.SUCCESS else "failed",
            target.address.reference(),
        ))
    else:
        result = None  # Not a test target.
    yield AddressAndTestResult(target.address, result)
Exemplo n.º 8
0
  def test_globbed_test_target(self):
    bfaddr = BuildFileAddress(None, 'tests', 'some/dir')
    target_adaptor = PythonTestsAdaptor(type_alias='python_tests')
    with self.captured_logging(logging.INFO):
      result = run_rule(
        coordinator_of_tests,
        HydratedTarget(bfaddr.to_address(), target_adaptor, ()),
        UnionMembership(union_rules={TestTarget: [PythonTestsAdaptor]}),
        AddressProvenanceMap(bfaddr_to_spec={
          bfaddr: DescendantAddresses(directory='some/dir')
        }),
        {
          (TestResult, PythonTestsAdaptor):
            lambda _: TestResult(status=Status.SUCCESS, stdout='foo', stderr=''),
        })

      self.assertEqual(
        result,
        AddressAndTestResult(bfaddr.to_address(),
                             TestResult(status=Status.SUCCESS, stdout='foo', stderr=''))
      )