Question about setting up unit tests for a plugin....
# plugins
p
Question about setting up unit tests for a plugin. I’m following these Testing plugins https://www.pantsbuild.org/docs/test-custom-plugin-goal and Testing plugins https://www.pantsbuild.org/docs/rules-api-testing guides but they don’t describe any of the setup needed to get the test code to run i.e. installing
pantsbuild.pants.testutil
. Here’s what I figured out to do so far: In `3rdparty/BUILD`L
Copy code
python_requirements(
    name="pants-reqs",
    description="3rd Party Requirements for testing pants plugins",
    source="requirements-pants-testutil.txt",
    resolve="test_pants_plugins"
)
In
3rdparty/requirements-pants-testutil.txt
Copy code
pantsbuild.pants.testutil
In the plugin dir `pants-plugins/version_check/BUILD`:
Copy code
pants_requirements(
    name="pants",
    resolve="test_pants_plugins",
)

resource(name="version_check", source="version_check.py")

python_tests(
    name="tests",
    sources=["**/test_*.py"],
    dependencies=[
        ":version_check",
        "3rdparty:pants-reqs",
    ],
    resolve="test_pants_plugins",
)
But then just trying install
pantsbuild.pants.testutil
via
./pants generate-lockfiles --resolve=test_pants_plugins
gives me an error abot specifying the version (which I’m not):
ERROR: Cannot install pantsbuild.pants.testutil and pantsbuild.pants.testutil<2.16 and >=2.15.0a0 because these package versions have conflicting dependencies.
Full error:
Copy code
./pants generate-lockfiles --resolve=test_pants_plugins
Bootstrapping Pants using /Users/yegor.ius/.pyenv/shims/python3.9
Creating the virtualenv PEX.
Downloading the Pex PEX.
SHA256 fingerprint of <https://github.com/pantsbuild/pex/releases/download/v2.1.62/pex> verified.
Installing pantsbuild.pants==2.15.0 into a virtual environment at /Users/yegor.ius/.cache/pants/setup/bootstrap-Darwin-arm64/2.15.0_py39
New virtual environment successfully created at /Users/yegor.ius/.cache/pants/setup/bootstrap-Darwin-arm64/2.15.0_py39.
23:02:16.92 [INFO] Completed: Generate lockfile for test_pants_plugins
23:02:16.93 [ERROR] 1 Exception encountered:

Engine traceback:
  in `generate-lockfiles` goal
  in Generate Python lockfile

ProcessExecutionFailure: Process 'Generate lockfile for test_pants_plugins' failed with exit code 1.
stdout:

stderr:
pid 71088 -> /Users/yegor.ius/.cache/pants/named_caches/pex_root/venvs/ecdb14bb96158224592177b93ecc7679cf00f4af/d8428a8ea90f0582092e86ce2d333b0b4540e45f/bin/python -sE /Users/yegor.ius/.cache/pants/named_caches/pex_root/venvs/ecdb14bb96158224592177b93ecc7679cf00f4af/d8428a8ea90f0582092e86ce2d333b0b4540e45f/pex --disable-pip-version-check --no-python-version-warning --exists-action a --no-input --isolated -q --cache-dir /Users/yegor.ius/.cache/pants/named_caches/pex_root/pip_cache --log /private/var/folders/x7/dhdtyk6533118y9892k7fgt80000gn/T/pants-sandbox-8khVeZ/.tmp/pex-pip-log.r3as47pc/pip.log download --dest /private/var/folders/x7/dhdtyk6533118y9892k7fgt80000gn/T/pants-sandbox-8khVeZ/.tmp/tmpzwlni972/Users.yegor.ius..pyenv.versions.3.8.10.bin.python3.8 pantsbuild.pants.testutil pantsbuild.pants.testutil<2.16,>=2.15.0a0 pantsbuild.pants<2.16,>=2.15.0a0 --index-url <https://pypi.org/simple/> --extra-index-url <https://build:5aLHGjm4szT6wPWQDUWv8Pdtta2a5fK7@repo.artifactory.enigma.com/artifactory/api/pypi/pypi/simple> --retries 5 --timeout 15 exited with 1 and STDERR:
ERROR: Cannot install pantsbuild.pants.testutil and pantsbuild.pants.testutil<2.16 and >=2.15.0a0 because these package versions have conflicting dependencies.
ERROR: ResolutionImpossible: for help visit <https://pip.pypa.io/en/latest/user_guide/#fixing-conflicting-dependencies>

 The conflict is caused by:
     The user requested pantsbuild.pants.testutil
     The user requested pantsbuild.pants.testutil<2.16 and >=2.15.0a0

 To fix this you could try to:
 1. loosen the range of package versions you've specified
 2. remove package versions to allow pip attempt to solve the dependency conflict




Use `--keep-sandboxes=on_failure` to preserve the process chroot for inspection.
Where’s this
pantsbuild.pants.testutil<2.16 and >=2.15.0a0
requirement coming from? We’re using pants 2.15.0
pants.toml
Copy code
[GLOBAL]
pants_version = "2.15.0"
h
You'll want to use this special target type to represent your plugin's dependency on Pants: https://www.pantsbuild.org/docs/reference-pants_requirements
👀 1
It ensures that the version of the pants libraries are in sync with the version of Pants you're using in your repo
So you won't need your requirements-pants-testutil.txt etc
e
The IC in fact comes from a
pants_requirements
target. The issue here is revealed in the OP. Pants is running under Python 3.9 Mac ARM (
./pants
script). The rule that is running is failing to find a
pantsbuild.pants
distribution for 2.15.x for Python 3.8 for Mac ARM, which makes sense since we only publish 3.8 wheels for Mac x86_64.
r
In @polite-angle-82480’s 1st code block, he is using
pants_requirements
Copy code
pants_requirements(
    name="pants",
    resolve="test_pants_plugins",
)
I see that
requirements-pants-testutil.txt
is unnecessary for
pantsbuild.pants.testutil
, but what if we have other 3rd party dependencies that we would like to use in a plug-in?
If I remove
pantsbuild.pants.testutil
from the list of requirements and change the interpreter to 3.9
Copy code
[python.resolves_to_interpreter_constraints]
pants-plugins = [">=3.9,<3.10"]
then
./pants generate-lockfiles --resolve=test_pants_plugins
takes forever to run. I waited 1.5h before giving up.
Nevermind, I figured it out, it looks like the pip resolver took forever resolving irreconcilable dependencies. It would be helpful if there were some kind of warning for those cases.
🎉 2
p
To close the loop, I think my issue was I had 2
pants_requirements
in
3rdparty/BUILD
and in the
pants-plugin/BUILD
I was able to install the
pantsbuild
lib
/pants generate-lockfiles --resolve=test_pants_plugins
with the following
pants-plugin/BUILD
file
Copy code
pants_requirements(
    name="pants",
    resolve="pants-plugins",
    testutil=True,
)

python_tests(
    name="tests",
    sources=["**/test_*.py"],
    dependencies=[
        ":pants",
    ],
    resolve="pants-plugins",
    interpreter_constraints=[">=3.9,<3.10"]
)