How do/Can I read config (i.e. pants.toml) while r...
# plugins
g
How do/Can I read config (i.e. pants.toml) while running a plugin test? Context: For my OCI plugin I need to the user namespace mappings, and they depend on what machine I'm running. While automating these is the end goal, I've opted to shortcut it by putting it in pants.toml. But it's still different between different hosts so I need to override it depending on where tests run, ie from pants.ci.toml... so packaging etc works but not tests.
f
Everything is
pants.toml
is part of the configuration, which should each be accessible via the applicable
Subsystem
subclass.
In a test, you could call `rule_runner.request`to ask the test instance of the engine what config it is using, but that is isolated from the actual Pants invocation.
Could you just use some fixture mechanism more appropriate to pytest?
(This seems more like a case of using a pytest feature, than putting something in pants.toml which I submit is for Pants configuration, not for tests.)
g
The problem is that this is host config, so it needs to change for tests based on where the tests run
f
so why not have a pytest fixture which reads the config from the appropriate place on the current system?
and if it is needed by a plugin rule being tested, pass it into RuleRunner as a config option via
.set_options
g
Well, the appropriate place is
pants.ci.toml
or
pants.toml
😛 For now.
f
Maybe use https://www.pantsbuild.org/docs/reference-test#extra_env_vars to pass the config into tests as an environment variable? Then read it from
os.environ
in the test.
If you want the config to be the same config used by your plugin rules, the challenge I see is that there is no way currently to give your pytest tests access to the configuration of the "outer"
./pants test
invocation (except with methods like the env vars)
You could consider writing a "internal plugin" for use only in your repository to somehow modify the test environment when
./pants test
is run.
Assuming that there was a
UnionRule
you could hook to do that.
for example, the
pants
repo itself uses the internal plugin pattern for maintaining JVM lockfiles for tests. https://github.com/pantsbuild/pants/tree/main/pants-plugins/internal_plugins/test_lockfile_fixtures
although in this case you might need to add a
UnionRule
to the
test
goal rules to allow plugins to modify / extend the test environment
and such a
UnionRule
hook would be a nice feature in and of itself putting aside the config issue here
g
Gotcha. Yeah. Maybe this is a hint I'm doing it wrong. I should probably do the automatic lookup. I'm already 4k lines into this feature, may as well make it a bit bigger...