Stebalien / tempfile

Temporary file library for rust
http://stebalien.com/projects/tempfile-rs
Apache License 2.0
1.2k stars 120 forks source link

Add convenient method equivalent to`TempDir:new('foo')` #249

Closed mamcx closed 1 year ago

mamcx commented 1 year ago

I want to remove the dependabot for https://github.com/advisories/GHSA-mc8h-8q98-g5hr and I miss the old TempDir:new('foo') call.

The available constructors have different semantics and the proposed solution is kinda verbose: tempfile::Builder::new().prefix(prefix).tempdir()

Stebalien commented 1 year ago

You need to specify a prefix for the tempdir name? Or do you just need to specify a location. If you need to specify a location, TempDir::new_in(...) should do it.

mamcx commented 1 year ago

TempDir::new_in(...) does not work because that needs that folder created, the old tempdir crate make it.

Stebalien commented 1 year ago

Hm. I don't think I'm on the same page. Could you explain exactly what you're trying to do and the desired effect?

mamcx commented 1 year ago

The desired effect is to recover the behaviour of the old Tempdir::new, I think the equivalent now is tempfile::Builder::new().prefix(prefix).tempdir()?

It add + create a folder with the prefix...

Stebalien commented 1 year ago

I'll consider it, but a stronger motivation beyond "I want to do this" would be helpful. Personally, I usually care about where my temporary files go, but rarely care about their names.

mamcx commented 1 year ago

Understood. We have a battery of test that run on parallel and we use the same temp_folder but distinct names for each, so the idea is that we have:

/tmp/test_a
/tmp/test_b
/tmp/test_c

without causing interferences, and having names help in debugging issues, that is very hard to do if the names are machine generated!

Also if used outside the test, this show up in logs and is nicer to see that this particular temp file was part of certain "context".

Stebalien commented 1 year ago

Yeah, that makes sense. My concern is that I could add TempDir::with_prefix(prefix), but then what about TempDir::with_prefix_in(prefix, directory)? In retrospect, new on both TempDir and NamedTempFile should have always taken a prefix (costs the user nothing to specify one) but that ship has sailed.

Yeah, ok, I should probably just do it. Now to the question of names:

I'm thinking the first one.

mamcx commented 1 year ago

Yes with_ sounds good.