Hi! I’m migrating a monorepo from custom tooling (...
# general
a
Hi! I’m migrating a monorepo from custom tooling (
poetry
,
nox
+ custom scripts) to pants, and I’m facing problems in how to configure tests. We mark some tests as
integration
and these are usually not run locally but only in the CI; in the past, our nox configuration took care of this by adding
-m 'not integration
when running the normal tests and we would have a special “target” to run full tests. I am not sure which is the best pattern to use in pants to achieve this. My first approach was to do something like
Copy code
python_tests(
        name="tests",
        sources=["tests/**/*.py"],
        extra_env_vars=["PYTEST_ADDOPTS=-m 'not integration'"],
        **tests_kwargs,
    )
and then create a
tests_full
without the extra env vars, but this doesn’t work when I run
./pants test lib:tests
, because the skipped tests give a return code 5 in
pytests
. What would be another solution to achieve this?
h
Hi! A common pattern is to partition the targets themselves. So you have
Copy code
python_tests(
  name="unit", 
  sources=[...]
)

python_tests(
  name="integration", 
  sources=[...],
  tags=["integration"],
)
In the various directories where that is relevant. And then select targets using the tag. So:
Copy code
./pants test :: --tag=-integration # Only run non-integration tests
./pants test :: --tag=integration # Only run integration tests
./pants test :: # Run all tests
Would this work for your situation?
You'd have to know how to separate the tests by sources. We typically do this by naming the integration tests with an
_integration.py
suffix, so they are easy to include/exclude in the targets'
sources=
globs.
a
oh, I see
we use in some cases folder structures
tests/integration
vs
test/unit
so that would be easy
is there any way of making
./pants test :: --tag=-integration
the default? to minimize dev friction
h
If you're interested, we could help you write a small plugin to add the goals
unit
and
it
maybe? They would behave like
test
but automatically apply the filter for you
a
ahh, interesting! I will first use the tags approach and once my whole repo is migrated I’ll start writing plugins
thanks!
I was wondering something further: because some of the “apps” in the repo contain a library + pex + tests, I use a macro to set everything up consistently. I would also like to code up this folder structure to have the two test tags, but I think this will fail when there are no integration tests (ie, sources are empty). Since I can’t import python modules to check whether the integration test folder exists, is there any good ways of handling this “conditional” target creation?
h
Unfortunately I think macros cannot currently look at the filesystem. But we are looking into making BUILD file macros much more powerful, and able to invoke engine operations (such as filesystem access).
Are you sure it fails on nonexistent sources though?
You should get an
Unmatched glob
warning, but it shouldn't fail the build
a
I’ll reheck, I may have tried with a different type of target
I confirm, I get the
Unmatched glob
Copy code
Do the file(s) exist? If so, check if the file(s) are in your `.gitignore` or the global `pants_ignore` option, which may result in Pants not being able to see the file(s) even though they exist on disk. Refer to <https://www.pantsbuild.org/v2.6/docs/troubleshooting#pants-cannot-find-a-file-in-your-project>.
15:04:32.36 [WARN] Unmatched glob from lib/common:integration's `sources` field: "lib/common/tests/integration/*.py"
So it works! Any way to silence these warnings? I prefer to have a “clean” test suite
h
🙌 1
You can set this in
pants.toml
a
I was trying this, and I don’t see a way of “selectively” ignoring warnings? For example,
Copy code
Do the file(s) exist? If so, check if the file(s) are in your `.gitignore` or the global `pants_ignore` option, which may result in Pants not being able to see the file(s) even though they exist on disk. Refer to <https://www.pantsbuild.org/v2.6/docs/troubleshooting#pants-cannot-find-a-file-in-your-project>.
11:28:14.18 [WARN] Unmatched glob from lib/notifications:unit's `sources` field: "lib/notifications/tests/unit/*.py"

Do the file(s) exist? If so, check if the file(s) are in your `.gitignore` or the global `pants_ignore` option, which may result in Pants not being able to see the file(s) even though they exist on disk. Refer to <https://www.pantsbuild.org/v2.6/docs/troubleshooting#pants-cannot-find-a-file-in-your-project>.
11:28:14.26 [WARN] The target app/csv_importer imports `clickhouse_driver.errors.Error`, but Pants cannot safely infer a dependency because more than one target owns this module, so it is ambiguous which to use: ['app/csv_importer:clickhouse-driver', 'lib/common:clickhouse-driver'].
The first one is a well understood side effect of the discussion we had above, the bottom one is a config mistake (why is it a WARN and not an error?)
h
Hmm, you should be able to ignore only the warnings that match a regex?
And whether that WARN should be an ERROR? I'm not sure. @witty-crayon-22786, thoughts?
An ERROR should fail the build, and I'm not sure if we'd want to in this case?