Temporary file creation for unittests

Dr.No jckj33 at gmail.com
Fri May 18 20:08:00 UTC 2018


On Friday, 18 May 2018 at 15:30:05 UTC, Uknown wrote:
> On Friday, 18 May 2018 at 15:16:52 UTC, Russel Winder wrote:
>> Hi,
>>
>> What's the current official position on how to create 
>> temporary files for use during a unittest. I found
>>
>> https://github.com/dlang/phobos/pull/5788
>>
>> but it seems to be languishing in the "we have discussed all 
>> the issues that no-one will ever have a problem with" phase.
>>
>> What to do between now and when there is an LDC release that 
>> has the result of
>> the merge?
>
> You could use libc's tmpfile with std.stdio.File until a D 
> alternative pops up.
>
> http://en.cppreference.com/w/c/io/tmpfile

I've had no idea C++'s got this in the standard library lol a 
while ago I ended up doing this (on Windows):

/// Creates a uniquely named, zero-byte temporary file on disk 
and returns the full path of that file.
/// Returns: the full path of the temp file.
string tempFilename()
{
	import  core.sys.windows.windows : GetTempFileNameW, MAX_PATH;
	import std.file : tempDir;
	import core.stdc.wchar_ : wcslen;
	import std.windows.syserror : wenforce;
	import std.conv : text, wtext;
	wchar[] path = new wchar[MAX_PATH+1];
	string dir = tempDir;
	wenforce(GetTempFileNameW(dir.wtext.ptr, // temp path
							  ("tmp"w).ptr, // dir prefix
							  0, // id generated internally
							  path.ptr // path buffer
			),
			"GetTempFileName()");
	return path[0 .. wcslen(path.ptr)].text;
}

It's windows-only and call GetTempFileNameW actually so just a 
function wrapper to work with D. There's a way to MAX_PATH but I 
didn't care to implement at time...



More information about the Digitalmars-d-learn mailing list