Re: Temporary file creation for unittests

2018-05-21 Thread Atila Neves via Digitalmars-d-learn

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


Re: Temporary file creation for unittests

2018-05-21 Thread Atila Neves via Digitalmars-d-learn

On Monday, 21 May 2018 at 17:03:40 UTC, Russel Winder wrote:
On Mon, 2018-05-21 at 15:16 +, 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.





Re: Temporary file creation for unittests

2018-05-21 Thread Russel Winder via Digitalmars-d-learn
On Mon, 2018-05-21 at 15:16 +, 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. :-)

Did I mention how much I like this RAII approach?

-- 
Russel.
==
Dr Russel Winder  t: +44 20 7585 2200
41 Buckmaster Roadm: +44 7770 465 077
London SW11 1EN, UK   w: www.russel.org.uk


signature.asc
Description: This is a digitally signed message part


Re: Temporary file creation for unittests

2018-05-21 Thread Dr.No via Digitalmars-d-learn

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...


Re: Temporary file creation for unittests

2018-05-21 Thread Atila Neves via Digitalmars-d-learn

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


Re: Temporary file creation for unittests

2018-05-20 Thread Joakim via Digitalmars-d-learn

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


Re: Temporary file creation for unittests

2018-05-18 Thread Dr.No via Digitalmars-d-learn

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...




Re: Temporary file creation for unittests

2018-05-18 Thread Uknown via Digitalmars-d-learn

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


Temporary file creation for unittests

2018-05-18 Thread Russel Winder via Digitalmars-d-learn
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?
 
-- 
Russel.
==
Dr Russel Winder  t: +44 20 7585 2200
41 Buckmaster Roadm: +44 7770 465 077
London SW11 1EN, UK   w: www.russel.org.uk


signature.asc
Description: This is a digitally signed message part