[tesitng custom plugins thread]
# general
w
[tesitng custom plugins thread]
Hey folks trying to test some custom targets and rules and running into an issue that t maybe you can help me with
Test class looks like this currently
Copy code
from pants.engine.target import Target
from pants.testutil.test_base import TestBase
from targets.cloud_formation import CloudFormationStack
from .deploy_cloud_formation import get_digest_path


class CloudFormationTests(TestBase):
    def test_get_digest_path(self) -> None:
        """Tests that given a Target we can get the path of a file in its digest"""
        target = self.make_target(
            spec="targets:mytarget",
            target_type=CloudFormationStack,
            stack_name="mystack",
            template_file="temp.yaml",
        )
running this returns;
Copy code
/Users/jerkelens/.pex/installed_wheels/3b9417b721fe8046939c4c27f886514cfc662352/pantsbuild.pants.testutil-1.30.0-py36.py37.py38-none-any.whl/pants/test
util/test_base.py:241: in make_target
    name=address.target_name, address=address, build_graph=self.build_graph, **kwargs
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <[AttributeError("'CloudFormationStack' object has no attribute 'field_values'") raised in repr()] CloudFormationStack object at 0x10ce23c90>
args = ()
kwargs = {'address': Address(targets, mytarget), 'build_graph': <pants.engine.legacy.graph.LegacyBuildGraph object at 0x10ce23050>, 'name': 'mytarget',
 'stack_name': 'mystack', ...}

    @wraps(prev_init)
    def new_init(self, *args: Any, **kwargs: Any) -> None:
>       prev_init(self, *args, **kwargs)
E       TypeError: __init__() got an unexpected keyword argument 'name'

/Users/jerkelens/.pex/installed_wheels/8f29d6fb12e6a07fd5f39bcdb51280c3d64ca46a/pantsbuild.pants-1.30.0-cp37-cp37m-macosx_10_11_x86_64.whl/pants/util/m
eta.py:182: TypeError
cannot realllyl make heads or tails of this
note. im using v1.30.0
h
self.make_target
uses the v1 engine and returns a v1 Target, rather than a v2 target. We deleted that in 2.0 Instead, use
self.add_to_build_file("project/util", "python_library(sources=['f1.py'])
Then, “request” the target by using
self.request_single_product(WrappedTarget, Address("project/util").target
, where that
.target
at the end “unwraps” the
WrappedTarget
to get you the underlying
Target
class. (In 2.0, you would instead use
self.get_target(Address("project/util"))
. Sorry that 1.30 testing is not at all cleaned up..)
You will also need to register your target type, as
TestBase
is totally hermetic.
Copy code
@classmethod
def target_types(cls):
  return [PythonLibrary]
w
great! i'll try this out
would you consider it more trouble than its worth to do 1.30 testingn
h
I think it’s still valuable. The actual resulting tests will be the same. You’ll only need to do some renames, e.g.: *
TestBase
is deprecated in favor of the Pytest style
RuleRunner
. Not yet deleted tho. * You can use that nice new
get_target()
method * It’s now
request_product()
, rather than
request_single_product()
Ditto with the Rules API in general, as you’ve seen. Most of the functionality is identical between 1.30 and 2.0; it’s almost all polish like renames
👍🏼 1
I finished documenting Approach #2:
run_rule_with_mocks
. You likely want to use Approach #3 instead, given the nature of your plugins, but FYI https://www.pantsbuild.org/v2.0/docs/rules-api-testing#approach-2-run_rule_with_mocks-unit-tests-for-rules Working on fleshing out the Approach #3
RuleRunner
docs now