gentle-florist-96289
08/14/2023, 9:21 AMsrc
and tests
for tests. When I run the tests with pants test ::
I get the ModuleNotFoundError
as the environment does not know where the source code is. I tried to add the python_sources to the dependency but it doesn't work. Is there any document to setup testing in this scenario?late-advantage-75311
08/14/2023, 10:05 AMpython_sources
tells pants where all the different kinds of targets are (usually files), but an additional configuration of source roots
can be needed so that pants can properly relate import package/module names to the files when inferring dependencies between targets. You can try tweaking according to https://www.pantsbuild.org/docs/source-roots#configuring-source-roots-using-patternslate-advantage-75311
08/14/2023, 10:08 AMgentle-florist-96289
08/14/2023, 10:14 AM├── my-app
│ ├── BUILD
│ ├── pyproject.toml
│ ├── src
│ │ ├── app.py
│ │ └── __init__.py
│ └── tests
│ ├── conftest.py
│ ├── __init__.py
│ └── test_app.py
├── my-lib
│ ├── BUILD
│ ├── pyproject.toml
│ ├── src
│ │ └── lib
│ │ ├── __init__.py
│ │ └── utils
│ │ └── main.py
│ └── tests
│ ├── conftest.py
│ ├── __init__.py
│ └── test_lib.py
└── pants.toml
I have setup the source root as
[source]
root_patterns = [
'/my-*',
]
This is because it worked with this to package the lib and app.late-advantage-75311
08/14/2023, 10:33 AMroot_patterns = ['src/', 'test/']
. if I understand correctly this should cause all four dirs [my-app/src, my-app/test, my-lib/src, my-lib/test] to be included as source roots.
Also, would you mind sharing an example import statement from one of the tests and the ModuleNotFoundError you are getting when pants tries to run the test?late-advantage-75311
08/14/2023, 10:39 AMgentle-florist-96289
08/14/2023, 10:45 AMfrom lib.utils.main import foo
late-advantage-75311
08/14/2023, 10:58 AMBUILD
files here? Also, for the individual test target can you print its dependencies? pants dependencies <the_test_target>
(https://www.pantsbuild.org/docs/project-introspection)gentle-florist-96289
08/14/2023, 11:02 AMresource(name="pyproject", source="pyproject.toml")
python_sources(name="lib", sources=["lib/**/*.py"])
python_distribution(
name="my-lib",
dependencies=[
":pyproject",
":lib"
# Dependencies on code to be packaged into the distribution.
],
provides=python_artifact(
name="my-lib",
version="0.0.1"
),
# Example of setuptools config, other build backends may have other config.
wheel_config_settings={"--global-option": ["--python-tag", "py39"]},
generate_setup=True,
)
and the dependencies for the tests is like
pants dependencies my-lib/tests
16:30:44.49 [WARN] Pants cannot infer owners for the following imports in the target my-lib/tests/test_lib.py:
* lib.utils.main.foo (line: 1)
late-advantage-75311
08/14/2023, 11:08 AMsrc/lib/utils
has an __init__.py
?late-advantage-75311
08/14/2023, 11:08 AMlate-advantage-75311
08/14/2023, 11:09 AMpants list --filter-target-type=python_source ::
late-advantage-75311
08/14/2023, 11:11 AMroot_patterns = ['src/', 'test/']
conf, correct?late-advantage-75311
08/14/2023, 11:11 AMlate-advantage-75311
08/14/2023, 2:02 PM.
├── my-lib
│ ├── src
│ │ └── lib
│ │ ├── BUILD
│ │ ├── __init__.py
│ │ └── liba.py
│ └── tests
│ ├── BUILD
│ ├── __init__.py
│ ├── conftest.py
│ └── test_lib.py
└── pants.toml
where src/lib/BUILD:
python_sources()
liba.py
def add10(x):
return x + 10
my-lib/tests/BUILD
python_test_utils(name="test_utils")
python_tests()
and test_lib.py
from lib import liba
def test_add10():
assert 11 == liba.add10(1)
with pants.toml
[GLOBAL]
pants_version = "2.16.0"
backend_packages.add = ["pants.backend.python"]
[source]
root_patterns = ["src", "tests"]
then pants test ::
runs fine and passes, but if we switch to
root_patterns = ['/my-*',]
we get:
E ImportError: cannot import name 'liba' from 'lib' (unknown location)
when running the same test, which is very similar to what you are experiencing.
Here the BUILD files are the simple ones generated by pants tailor ::
. I tried at first to do a single BUILD in my-lib
but struggled to get it going.happy-kitchen-89482
08/14/2023, 2:42 PMhappy-kitchen-89482
08/14/2023, 2:43 PMfrom lib.utils.main import foo
implies that the parent of lib
should be a source root. Is it?gentle-florist-96289
08/16/2023, 5:19 AMlate-advantage-75311
08/16/2023, 11:02 AMgentle-florist-96289
08/16/2023, 11:02 AMlate-advantage-75311
08/16/2023, 1:35 PMlate-advantage-75311
08/16/2023, 1:46 PMlate-advantage-75311
08/16/2023, 1:47 PMlate-advantage-75311
08/16/2023, 2:02 PMpants dependencies my-app/tests/test_app.py
and noticing that the main file was not showing up as a dep. It was because it was not included as a target via BUILD file.gentle-florist-96289
08/17/2023, 9:33 AMBUILD
files in each directory. This works fine for running the tests. However, the packaging doesn't work anymore.gentle-florist-96289
08/17/2023, 9:34 AMgentle-florist-96289
08/17/2023, 9:36 AM