Tempfiles in unittests

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Mar 9 11:34:07 PST 2012


On Fri, Mar 09, 2012 at 01:42:01PM -0500, Jonathan M Davis wrote:
> On Friday, March 09, 2012 16:48:33 Magnus Lie Hetland wrote:
> > On 2012-03-09 15:08:42 +0000, bearophile said:
> > > Magnus Lie Hetland:
> > >> It seems that File has no method for reading the entire file
> > >> contents into a string, so I'd have to read and concatenate lines
> > >> or chunks or something?
> > > 
> > > There are std.file.read() and std.file.readText().
> > 
> > Yeah, I found those. I guess my post was a bit verbose, but the main
> > point was that I have a File object, but no file name (because I'm
> > using std.stdio.tmpfile() in a unit test), so std.file.read() and
> > std.file.readText() are useless to me... :)
> 
> File has a name property. You can do something like
> 
> auto file = File.tmpfile();
> auto filename = file.name;
> auto str = readText(filename);
> 
> I do grant you though that it's a bit odd to need to get the file name
> and operate on that rather than operating on the File object. It works
> though, and is a lot cleaner than reading in the file line-by-line, if
> you don't actually need to do that.
[...]

This seems like a strange limitation. Shouldn't it be possible to slurp
the entire contents of an already-open file into a string? Perhaps a new
method in File?

Also, using file.name is prone to race conditions in certain situations.
(Not that it'd be a problem usually, but just sayin'.)


T

-- 
Designer clothes: how to cover less by paying more.


More information about the Digitalmars-d-learn mailing list