www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Temporary file creation for unittests

reply Russel Winder <russel winder.org.uk> writes:
Hi,

What's the current official position on how to create temporary files for u=
se
during a unittest. I found=20

https://github.com/dlang/phobos/pull/5788

but it seems to be languishing in the "we have discussed all the issues tha=
t
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?
=20
--=20
Russel.
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
Dr Russel Winder      t: +44 20 7585 2200
41 Buckmaster Road    m: +44 7770 465 077
London SW11 1EN, UK   w: www.russel.org.uk
May 18 2018
next sibling parent reply Uknown <sireeshkodali1 gmail.com> writes:
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
May 18 2018
parent Dr.No <jckj33 gmail.com> writes:
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...
May 18 2018
prev sibling next sibling parent Joakim <dlang joakim.fea.st> writes:
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 std.file.deleteme, which is what the Phobos unittests use: https://github.com/dlang/phobos/blob/master/std/file.d#L117
May 20 2018
prev sibling parent reply Atila Neves <atila.neves gmail.com> writes:
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
Not official, but... import unit_threaded; with(const Sandbox()) { writeFile("myfile.txt", "contents"); shouldExist("myfile.txt"); shouldEqualContent("myfile.txt", "contents"); fileShouldContain("myfile.txt", "cont"); } Atila
May 21 2018
next sibling parent reply Dr.No <jckj33 gmail.com> writes:
On Monday, 21 May 2018 at 15:16:11 UTC, Atila Neves 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
Not official, but... import unit_threaded; with(const Sandbox()) { writeFile("myfile.txt", "contents"); shouldExist("myfile.txt"); shouldEqualContent("myfile.txt", "contents"); fileShouldContain("myfile.txt", "cont"); } Atila
I've never seen "should" being in used in function names before...
May 21 2018
parent Atila Neves <atila.neves gmail.com> writes:
On Monday, 21 May 2018 at 15:20:14 UTC, Dr.No wrote:
 On Monday, 21 May 2018 at 15:16:11 UTC, Atila Neves 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
Not official, but... import unit_threaded; with(const Sandbox()) { writeFile("myfile.txt", "contents"); shouldExist("myfile.txt"); shouldEqualContent("myfile.txt", "contents"); fileShouldContain("myfile.txt", "cont"); } Atila
I've never seen "should" being in used in function names before...
There's a whole lot of them here: https://github.com/atilaneves/unit-threaded/blob/master/source/unit_threaded/should.d
May 21 2018
prev sibling parent reply Russel Winder <russel winder.org.uk> writes:
On Mon, 2018-05-21 at 15:16 +0000, Atila Neves via Digitalmars-d-learn wrot=
e:
 On Friday, 18 May 2018 at 15:16:52 UTC, Russel Winder wrote:
 Hi,
=20
 What's the current official position on how to create temporary=20
 files for use during a unittest. I found
=20 Not official, but... =20 import unit_threaded; =20 with(const Sandbox()) { writeFile("myfile.txt", "contents"); shouldExist("myfile.txt"); shouldEqualContent("myfile.txt", "contents"); fileShouldContain("myfile.txt", "cont"); } =20 Atila
OK, we like this. A lot. Given I use Unit-Threaded, why did I not know this. Ah, OK, RTFM. :-) Did I mention how much I like this RAII approach? --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Dr Russel Winder t: +44 20 7585 2200 41 Buckmaster Road m: +44 7770 465 077 London SW11 1EN, UK w: www.russel.org.uk
May 21 2018
parent Atila Neves <atila.neves gmail.com> writes:
On Monday, 21 May 2018 at 17:03:40 UTC, Russel Winder wrote:
 On Mon, 2018-05-21 at 15:16 +0000, Atila Neves via 
 Digitalmars-d-learn 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
Not official, but... import unit_threaded; with(const Sandbox()) { writeFile("myfile.txt", "contents"); shouldExist("myfile.txt"); shouldEqualContent("myfile.txt", "contents"); fileShouldContain("myfile.txt", "cont"); } Atila
OK, we like this. A lot.
:)
 Given I use Unit-Threaded, why did I not know this. Ah, OK, 
 RTFM. :-)
It's got so many features that I don't know how to document everything and make it accessible at the same time.
 Did I mention how much I like this RAII approach?
Me too - RAII is definitely C++'s gift to the world. I've been abusing `with` lately quite a bit. I think it's underused.
May 21 2018