purple-hydrogen-4740
07/31/2023, 11:23 AMfresh-cat-90827
07/31/2023, 11:25 AMpex_binary
with uvicorn
and make it accessible to your test? https://www.pantsbuild.org/docs/reference-python_test#coderuntime_package_dependenciescodefresh-cat-90827
07/31/2023, 11:27 AMdef test_case():
result = subprocess.run("uvicorn --run foo")
assert result...
and you want to make sure that the uvicorn
comes from a PEX file in your test sandbox, not from some random place on the machine, right?fresh-cat-90827
07/31/2023, 11:35 AMpython_requirement(
name="tabulate",
requirements=["tabulate"],
entry_point="tabulate:_main",
)
pex_binary(
name="runner",
dependencies=[":tabulate"],
entry_point="tabulate:_main",
)
and now you can do
⯠dist/runner.pex --help
Usage: tabulate [options] [FILE ...]
Pretty-print tabular data.
...
purple-hydrogen-4740
07/31/2023, 11:36 AMpurple-hydrogen-4740
07/31/2023, 11:50 AMfresh-cat-90827
07/31/2023, 11:52 AMpython_tests
target you'd need to tell Pants that you want to make a package accessiblefresh-cat-90827
07/31/2023, 11:53 AMrunner.pex
with pants package //:runner
with //:runner
being the target addressfresh-cat-90827
07/31/2023, 11:54 AMdist
directoryfresh-cat-90827
07/31/2023, 11:54 AMpython_tests(
name="test-name",
...
runtime_package_dependencies=[
"//:runner", # path to your pex_binary target from the root of the repo
],
)
fresh-cat-90827
07/31/2023, 11:57 AMdist
after pants package //:runner
will help to confirm you can run the uvicorn
when it's going to be accessible to a testfresh-cat-90827
07/31/2023, 11:58 AMpurple-hydrogen-4740
07/31/2023, 12:11 PMfresh-cat-90827
07/31/2023, 12:29 PMfresh-cat-90827
07/31/2023, 1:01 PMdef test_tabulate():
#import pdb; pdb.set_trace()
ls_result = subprocess.Popen(["ls"], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
ls_output = ls_result.stdout.readlines()
tabulate_result = subprocess.Popen(["python3", "tabulate-for-tests.pex", "--help"], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
tabulate_output = tabulate_result.stdout.read().decode()
assert 'Pretty-print tabular data' in tabulate_output
and
python_requirement(
name="tabulate",
requirements=["tabulate"],
entry_point="tabulate:_main",
)
pex_binary(
name="tabulate-for-tests",
dependencies=[":tabulate"],
entry_point="tabulate:_main",
)
and
python_tests(
name="tests",
runtime_package_dependencies=[
"//:tabulate-for-tests", # path to your pex_binary target from the root of the repo
],
)
fresh-cat-90827
07/31/2023, 1:02 PMpdb
line, you can run pants test --debug ::
and step through to see what's inside your sandboxfresh-cat-90827
07/31/2023, 1:03 PMpants --keep-sandboxes=on_failure test ::
and you can inspect your sandboxfresh-cat-90827
07/31/2023, 1:04 PMpex_binary
), once we know where it's stored, we can provide the path in ["python3", "tabulate-for-tests.pex"...
fresh-cat-90827
07/31/2023, 1:07 PMpex_binary
is declared in helloworld/greet/BUILD
, then when packaged:
⯠tree dist
dist
āāā helloworld.greet
ā āāā tabulate-for-tests.pex
fresh-cat-90827
07/31/2023, 1:08 PMpurple-hydrogen-4740
07/31/2023, 3:22 PMpython3 path/to/runner.pex
or just path/to/runner.pex
it has problem to locate a working python interpreter now, despite having python interpreter constraints setup in pants.toml with search_path to PYENV and other tests running normally, Idkfresh-cat-90827
07/31/2023, 3:25 PMso the pex binary is generated in tmp folderI assume you mean not
/tmp
but rather something like /tmp/pants-sandbox-vwutM9
? Just checking š
it has problem to locate a working python interpreter nowOh that's great, so you are able to locate the pex now from your Python test, right?
despite having python interpreter constraints setup in pants.toml with search_path to PYENVwould you be able to share the command you run from your test? I can try to repro locally
fresh-cat-90827
07/31/2023, 3:26 PMOh that's great, so you are able to locate the pex now from your Python test, right?I mean you can confirm you can run from you test some shell command on that file, e.g.
head -1 foo/bar.pex
#!/usr/bin/env python3.11
purple-hydrogen-4740
07/31/2023, 3:39 PM[
"python",
"{os.getenv('PWD')}/services.runner.tests/runner.pex",
]
purple-hydrogen-4740
07/31/2023, 3:39 PM[
"{os.getenv('PWD')}/services.runner.tests/runner.pex",
]
fresh-cat-90827
07/31/2023, 3:39 PMrunner.pex
, right? Just checking špurple-hydrogen-4740
07/31/2023, 3:40 PMfresh-cat-90827
07/31/2023, 3:40 PMpurple-hydrogen-4740
07/31/2023, 3:40 PMfresh-cat-90827
07/31/2023, 3:40 PM[
"python",
"{os.getenv('PWD')}/services.runner.tests/runner.pex",
]
in a subprocess or call or whatever, what's the error you get?purple-hydrogen-4740
07/31/2023, 3:42 PMfresh-cat-90827
07/31/2023, 3:45 PMpants.toml
file?
[python]
interpreter_constraints = ["==3.9.*"]
fresh-cat-90827
07/31/2023, 3:46 PMpex_binary
should default to the IC set in pants.toml
, see https://www.pantsbuild.org/docs/reference-pex_binary#codeinterpreter_constraintscodefresh-cat-90827
07/31/2023, 3:46 PMVersion matches CPython==3.8
whether it should instead be ==3.8.*
špurple-hydrogen-4740
07/31/2023, 3:53 PM[python]
enable_resolves = true
interpreter_constraints = ["CPython==3.8"]
[python-bootstrap]
search_path = ["<PYENV>"]
purple-hydrogen-4740
07/31/2023, 3:54 PMpurple-hydrogen-4740
07/31/2023, 3:55 PMfresh-cat-90827
07/31/2023, 3:56 PMpants test path/to/test.py
and you get
Failed to find compatible interpreter on path $PATH
...
error?purple-hydrogen-4740
07/31/2023, 3:58 PMpants test path/to/tests/::
and one fails and the rest passesfresh-cat-90827
07/31/2023, 3:58 PM/usr/bin/python3
to check it can find a specific interpreter https://www.pantsbuild.org/docs/python-interpreter-compatibility#changing-the-interpreter-search-path and https://www.pantsbuild.org/docs/reference-pex_binary#codeshebangcodepurple-hydrogen-4740
07/31/2023, 4:05 PMpex_binary(
...
shebang="/usr/bin/python3"
)
[python-bootstrap]
search_path = ["<PYENV>", "/usr/bin/python3"]
also didnt helpfresh-cat-90827
07/31/2023, 4:06 PMpurple-hydrogen-4740
07/31/2023, 4:08 PMfresh-cat-90827
07/31/2023, 4:09 PM[
"/usr/bin/python3",
"{os.getenv('PWD')}/services.runner.tests/runner.pex",
]
purple-hydrogen-4740
07/31/2023, 4:13 PMfresh-cat-90827
07/31/2023, 4:13 PMfresh-cat-90827
07/31/2023, 4:14 PMpurple-hydrogen-4740
07/31/2023, 4:16 PMpurple-hydrogen-4740
07/31/2023, 4:16 PMfresh-cat-90827
07/31/2023, 4:18 PMpants --no-pantsd --no-local-cache test path/to/tests/::
and then I'll go šfresh-cat-90827
07/31/2023, 4:18 PMpurple-hydrogen-4740
07/31/2023, 4:21 PMpurple-hydrogen-4740
07/31/2023, 4:21 PMenough-analyst-54434
07/31/2023, 9:30 PM