-
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
testing: T.TempDir doesn't work well with testing libraries #38850
Comments
CC @bradfitz |
Maybe? But each subtest has its own |
Does it? They seem complementary to me: For package-independent test helpers, I would recommend passing in the output directory as an explicit argument anyway. For example, the helper might be called from a subtest, but generate an output for a parent test (perhaps guarded by a |
Preserving the temporary directory with ISTM that using Let's put this another way: what's the advantage of always returning the same name from FWIW I've been using this functionality for a long time now, and I've very often found it to be useful to create several independent temporary directories within a test. Having a handy function that returns a new scratch directory is great! (and makes it easy to avoid unintentional bleedthrough and extra bookkeeping). |
Change https://golang.org/cl/231958 mentions this issue: |
Indeed, but I don't think this precludes a subsequent call returning a different directory that is similarly tied to the lifetime of the test. The similar naming of For a given |
@bcmills Any further input on this, please? The release approaches fast! |
I'm skeptical about this. I think of it like os.TempDir, that it returns the "/tmp" for the test. How are libraries and tests colliding exactly? Don't they have different file names they'd use? |
I talked to @bcmills about this. The argument is that if t.TempDir is like os.TempDir, it's hard to get multiple directories without calls to Mkdir or ioutil.TempDir. But if t.TempDir is like ioutil.TempDir, then it's easy to get a single directory - just call it once and remember the result. That suggests we should make the change that @rogpeppe has proposed. I'll review the CL. Thanks. |
FWIW that's why I liked the name "Mkdir" which makes it clear that it's making a new directory each time with no confusion between ioutil.TempDir and os.TempDir. |
Change https://golang.org/cl/234582 mentions this issue: |
In CL 231958, TempDir was changed to create a new temp directory on each allocation, on the theory that it is easy to save in a variable for callers that want the same directory repeatedly. Apply that transformation here. Updates #38850 Change-Id: Ibb014095426c33038e0a2c95303579cf95d5c3ba Reviewed-on: https://go-review.googlesource.com/c/go/+/234582 Run-TryBot: Bryan C. Mills <bcmills@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Change https://golang.org/cl/234583 mentions this issue: |
Updates #38850 Change-Id: I33f48762f5520eb0c0a841d8ca1ccdd65ecc20c8 Reviewed-on: https://go-review.googlesource.com/c/go/+/234583 Run-TryBot: Bryan C. Mills <bcmills@google.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
FYI, we split @rogpeppe's #38850 (comment) into #39169, since this issue is now closed. |
The new
T.TempDir
method is documented to return the same path on every call within the same test:This means that it's easy for tests that use
TempDir
to shoot themselves in the foot by overwriting or reading content owned by testing libraries. To useTempDir
correctly in the presence of multiple packages you must use a unique subdirectory name, which is hard to choose, so you'd need to useioutil.TempDir
which loses the whole point of havingTempDir
in the first place.I propose that every call to
TempDir
return a new temporary directory; tests can save the result in a local variable if need be.If it seems like the implied semantics of the name
TempDir
no longer match the actual behaviour, another possible name would beMkdir
.The text was updated successfully, but these errors were encountered: