-
Notifications
You must be signed in to change notification settings - Fork 17.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: cmd/go: Need an easy/obvious way to copy testdata from module cache to a test-running directory #50312
Comments
Rather than building the test binary ( Could you elaborate on why you don't do that? My guess would be to exclude time taken to build the test from running the test. If so, then perhaps (just throwing out ideas) an alternate approach would be for |
I don't do that (in bent) because
On the other hand, bent already deals with the testdata problem. I definitely need at least the interleaved run order unless benchmarks are run on a very controlled machine, "stuff" just happens sometimes. (I.e., the purpose of bent is to get rid of all my benchmarking and testing speedbumps and footguns -- and immutable testdata in the module cache is one of those speedbumps). That also doesn't help fuzzing, and some benchmarks/tests assume (incorrectly, and I sometimes submit pull requests to fix them) that they can write to testdata. An alternate approach for fuzzing (because bent has solved the problem in its case) might be to have fuzzing copy testdata out of the module cache into the current directory? |
/cc @golang/fuzzing |
//go:embed seems like a useful tool here. Maybe cmd/go and/or package testing could make that easier. As a straw man, cmd/go could embed testdata by default and package testing could expose a fs.FS with their contents. |
Strong +1 on providing an fs.FS with the contents of testdata. |
This proposal has been added to the active column of the proposals project |
When you run You can do what |
A common workflow for me (to catch flaky tests) is to
Sure, but that doesn't mean we can't make the common case ( |
|
Yes, you can also copy the testdata instead of moving the binary. This is a question of "just works"/"batteries included", vs not. |
Sorry, I thought this issue was about copying the testdata. Oh, now I see that @dr2chase suggested the |
The current way of doing it requires knowing the where's-my-module-cache incantation, which I think is a too-large speedbump. With the benefit of additional hindsight,
The reason for this is that tests already exist that variously
As far as I know, none of our tools will complain at people for doing any of these things, therefore, they have already done it, and will continue to do it. I am open to suggestions about a better name for the option. The benchmark runner (bent) contains (or will contain, once CLs are submitted) workarounds for all these things. For the commands above, where the current directory is treated as containing "build instructions", the go files in the current directory are ignored. Building "." instead of |
Regarding the enumeration in the previous comment: (1) Writing files during tests is disallowed. People may do it, but it makes tests break and we are actively discouraging it by doing things like making the module cache read-only. (2) Reading files from other subdirectories or parent directories within the same module is allowed; tests can assume they run in the directory where the test sources live, since that's what go test does. (3) Same. (4) That was a bug and I believe it is being fixed or is now fixed. It sounds like maybe what you would like better is "extract this module into a directory so I can cd into it and pretend to be a developer of that module". I wonder how we should address that. We could make go mod download have a -o flag, I suppose. The next problem will be that you can't use version control commands in that directory, and we won't fix that though. Maybe doing
which will tell you the Dir where it is unpacked, and then just cp -a the entire dir to go into it? |
This "extract this module into a directory so I can cd into it and pretend to be a developer of that module" is a plausible description of the sort of thing I'm after, but I think "go test" should not just be for developers of a particular module.
|
We have lots of related proposals for that already:
Some combination of those would allow for a tool that accepts a Go module path and version, resolves it to a VCS origin and revision, verifies that the module contents are the same, and materializes a clone of the repo checked out to that revision. |
It sounds like this specific fix "copy testdata out" is probably not the answer to working with other people's modules. Given that, perhaps we should decline this proposal and focus on others that help obtaining the whole module? For the record, 'go test' of dependencies is expected to work. If a test is writing to its own testdata directory during a test, that is a bug in their code, same as the code not compiling on a particular system or any other kind of bug, and it should be fixed. |
Perhaps we should explicitly document this in |
@martin-sucha, it is documented as of Go 1.18, in the same place where we mention where the test binary is run. Per https://pkg.go.dev/cmd/go@master#hdr-Testing_flags:
|
Based on the discussion above, this proposal seems like a likely decline. |
No change in consensus, so declined. |
To test, benchmark, or fuzz-test downloaded software modules, the current recipe (at least, the one that I learned) is
The problem here is that test and benchmarking often depend on files in
testdata
. It is often possible to run the test in the module cache, but this depends on the test/benchmark not writing to the testdata directory, and also depends on knowing the directory in the module cache (see workaround below). Fuzzing that finds a failure does write to the testdata directory, so this just doesn't work for new fuzzing.One workaround is
but this is not something that people find or figure out easily.
I propose adding an option to
go get
or a subcommand togo mod
to copy testdata, if it exists in the downloaded package, into the current directory. For example,go get -d -t -T -v somepackage@someversion
orgo mod testdata
.The text was updated successfully, but these errors were encountered: