How does one write a unit test for a custom subsys...
# general
p
How does one write a unit test for a custom subsystem (i.e. a class that inherits from
from pants.option.subsystem.Subsystem
specifically, I need to have my
CustomSubsystem.global_instance()
method to not blow up. using
pants.testutil.option_util.create_subsystem
doesn't cause global_instance() to work. couldn't find any info in the docs: https://www.pantsbuild.org/docs/rules-api-testing (also note that the page title:
Testing plugins
doesn't match the url, ideally those should match)
h
You can use
rule_runner.request(MySubsystem, [])
. Use
rule_runner.set_options()
beforehand if you want to use non-default values
p
Is there an example somewhere? What do I pass to set_options ?
h
p
I don't see a set_options method on RuleRunner
(this the latest pants beta).. is there a workaround until https://github.com/pantsbuild/pants/pull/10859 gets released ?
h
Yes. For now, use
rule_runner.request(MySubsystem, [create_options_bootstrapper(args=['--my-arg'])
, which is exported from
pants.testutil.option_util
👍 1
p
doesn't work
h
Okay add
QueryRule(AuthStore, [OptionsBootstrapper])
to the
RuleRunner(rules=[])
argument
you should be able to import
QueryRule
from
pants.testutil.rule_runner
(otherwise
pants.engine.rules
), and then
pants.option.options_bootstrapper
p
Screen Shot 2020-09-30 at 12.14.23 PM.png
Screen Shot 2020-09-30 at 12.14.48 PM.png
h
Oh, I thought Toolchain was already using 2.0.0b2. I can do that upgrade. It will allow you to do some of what I’m suggesting
p
I wasn't aware of a new release. upgrading now.
👍 1
ok. on b2 now, the code is:
Copy code
rule_runner = RuleRunner(rules=[QueryRule(AuthStore, [OptionsBootstrapper])])
rule_runner.set_options(args=["--toolchain-setup-repo=puddy", "--buildsense-base-url=<http://little.jerry.com/chicken>", "--auth-from-env-var=CAGE_FREE"])
rule_runner.request(AuthStore, [])
FWIW
AuthStore
inherits from
Subsystem
not GoalSubsystem
h
I gtg for volunteering, but try the variant where you pass options bootstrapper to the request method, but you do not register a QueryRule Sorry, you’re caught in a really awkward in-between
I’m back. Did that work?
p
no. I will try again later.
doesn't work.
h
Ah, okay, register
QueryRule(AuthStore, [])
p
Screen Shot 2020-10-01 at 2.18.17 PM.png,Screen Shot 2020-10-01 at 2.18.26 PM.png
h
Ohhhh, sorry this was so painful. I think you need to also register
SubsystemRule(AuthStore)
in the
RuleRunner
, in addition to the
QueryRule
. this is exported from
pants.engine.rules
It’s really rare to need to register a
SubsystemRule
now because these get included when you use
collect_rules()
in a source file. Normally, in your test setup, you would register something like
my_plugin.rules()
, which will include that
SubsystemRule
. But here, you’re not registering anything but the
QueryRule
, so Pants doesn’t know your subsystem exists. Sorry that took so long to figure out.
p
Screen Shot 2020-10-01 at 2.29.59 PM.png