So I'm on Pants 2.16.0rc1, testing out the `shell_...
# general
b
So I'm on Pants 2.16.0rc1, testing out the
shell_command
. I have a python project, with python tests, and inside the BUILD file for said tests I've added
Copy code
python_tests(...)

shell_command(
    command="./just_run_touch_foo.sh",
    tools=["bash", "touch"],
    execution_dependencies=[":scripts"],
    output_directories=["."],
    workdir=".",
    description="this just runs `touch foo"
)

shell_sources(
    name="scripts"
)
What should I expect to happen here? Hoping to see a file
foo
sitting in the directory after the tests run. (Or, at least, inside the sandbox while tests run. So far I have seen neither.) At the current time, my attempt at running the shell command above results in
Copy code
19:40:00.77 [WARN] No applicable files or targets matched. The `test` goal works with these target types:

  * experimental_test_shell_command
  * python_test
  * shunit2_test

However, you only specified target arguments with these target types:

  * shell_command
I'm pretty sure I don't want to run
experimental_test_shell_command
, as this actually runs a test inside bash script. What I want eventually is to download some canonical dataset that will be used in my unit tests...
b
shell_command
is designed as a target for generating files for other targets to use: you will need a dependency from the Python test target to the shell command one, so that pants knows the files are needed.
Specifically, it is a “codegen” target, and you can interact with it directly for debugging with
pants export-codegen path/to:target
. Dependencies are the right way to integrate it with other targets. (That said, for downloading a public file specifically, another option might be: `file(name=“`…`”, source=http_source(
))` https://www.pantsbuild.org/docs/reference-file)
👌 1
b
About that
file
target... Is there any way I can send an authentication section in a header, like what you'd do with curl?
b
Not that I know of. Being able to pick up auth details from a environment variable (or something along those lines?) seems like a sensible feature request for http_source, if you have a moment to file an issue: https://github.com/pantsbuild/pants/issues/new?assignees=&labels=enhancement&template=feature_request.md&title=
c
b
@broad-processor-92400 added an issue let me know what you think.
👍 1
b
Thanks!
b
No problem!
What do people do to pull in external data for running tests? We have a module that runs some numerical code, and the CI server runs a regression test: We run the algorithm on a known dataset and compare the current result against a known good output. Is there an example out there on how to do this in pants? Trying to get our repo bootstrapped so that we can evaluate Pants (which by all accounts looks like a huge improvement over the current state of Python builds!)
w
until your issue to support additional headers is resolved, a shell command that uses
curl
is a decent option: you’d only want to ensure that you included some version information and/or `shasum`ing of the downloaded file in your script.
b
Okay. Do you have an example
shell_command
that I can refer to? As noted above, I'm seeing an error "No applicable files or targets matched." I think it's because I'm doing something wrong with the BUILD file for this directory. I'm on Pants 2.16.0rc1
b
That sounds like you still might be using
pants run ...
? You'll need to use
pants export-codegen ...
to validate/debug it, and set dependencies in appropriate targets to use it for tests, e.g.
Copy code
shell_command(
  name="data",
  command="curl ... && shasum ...",
  tools=["curl", "shasum"],
  output_file=["some_file.txt"]
)

python_tests(
  ...,
  dependencies=[":data"]
)
This will mean that running those tests knows that it needs to run the data command and materialize the files within the testing sandbox
b
So, what I have now is an actual shell script with a shebang at the top. What you're saying is to not do that, but put the actual shell command under the
command=
option?
And this is because Pants has some fancy introspection that runs behind each operation?
That sounds like you still might be using
btw, I'm using
pants test ::
👍 1
b
ah, a shell script should be fine too. The key is having the dependencies set up: a
shell_command
target describes how to generate some files for other targets to use.
Hm, what's the full error message for
pants test ::
?
b
Okay, coming back to this from Friday, and I'm no longer getting an error.
But it also appears like the shell script is not being run.
w
It will be cached after the first run
Which is the other reason why you should include a shasum check: if you'd like it to re-run, you need to bump a version or sha
👍 1
b
Here is the BUILD file:
Copy code
python_tests(
    name="tests",
    dependencies=[":data"]
)

files(
    name="data",
    sources=["test-data/*.h5"]
)

shell_command(
    command="./just_run_touch_file.sh",
    tools=["bash", "touch"],
    execution_dependencies=[":scripts"],
    output_directories=["."],
    workdir=".",
    description="run a shell command",
    dependencies=[":scripts"],
)

shell_sources(
    name="scripts"
)
and here is the script:
Copy code
#!/usr/bin/bash

echo "hello"

touch foo
I don't see either "hello" nor the file
foo
So in theory this should work, but I just need a way to invalidate the cache?
b
Ah, `shell_command`s output is normally hidden (if they succeed). You can add
shell_command(..., log_output=True)
to help with debugging. You might also need to change the script slightly (e.g. add a comment
# 1
that you can just increment to bust the cache for now)
👍 1
b
Also, I really appreciate your time... is there an example project that I could learn from? Somehow the docs are not clicking for me.
b
The file
foo
will, theoretically, be placed into the same directory as the
BUILD
file, in the test sandbox. You can peek into those sandboxes with https://www.pantsbuild.org/docs/reference-global#keep_sandboxes. For instance, if the tests are failing
pants --keep-sandboxes=on_failure test ::
and the cd/exploring the directory that is logged.
https://github.com/pantsbuild/example-adhoc covers
adhoc_tool
, which is similar to
shell_command
but not exactly what you're doing... so might not be super easy to generalise from for this initial learning curve.
👌 1
(There's a chance that
output_directory=["."]
might not be doing the expected thing. One way to eliminate that variable would be using
output_files=["foo"]
instead, for now.)
b
Tried that, but no luck. I'll continue debugging later on. Thanks for your help @cold-soccer-63228 @witty-crayon-22786
👍 1