Fails to use testFilename in unittest

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 18 02:49:26 PDT 2017


On Thursday, May 18, 2017 09:40:33 biocyberman via Digitalmars-d-learn 
wrote:
> There is a ongoing discussion about temp file over here:
> http://forum.dlang.org/thread/sbehcxusxxibmpkaeopl@forum.dlang.org
>
> I have a question about generating a temporary file to write test
> data. I can create my own file and use it but just want to use
> the existing tool for convenience. testFilename() is used all
> over phobos. So, I don't understand why it does not work on my
> code.
>
> The following code fails to compile.
>
>
>    % cat testFile.d
> #!/usr/bin/env rdmd
> import std.stdio;
>
> unittest{
>
>    static import std.file;
>    auto deleteme = testFilename();
>    scope(failure) printf("Failed test at line %d\n", __LINE__);
>
>    scope(exit) std.file.remove(deleteme);
>
>    // Do some stuffs with open or writing and reading of the temp
> file.
>     assert(true);
>
>
>
> }
> void main(string [] args){
>    writeln("Main");
>
> }

Actually, it's not used all over the place in Phobos. It's only used
std.stdio, where it's a private function in a version(unittest) block. It's
not part of the public API. And other modules that need something similar
have their own solution.

std.stdio has

version(unittest) string testFilename(string file = __FILE__, size_t line = 
__LINE__) @safe
{
    import std.conv : text;
    import std.file : deleteme;
    import std.path : baseName;

    // filename intentionally contains non-ASCII (Russian) characters for 
test Issue 7648
    return text(deleteme, "-детка.", baseName(file), ".", line);
}

and std.file has

@property string deleteme() @safe
{
    import std.conv : to;
    import std.path : buildPath;
    import std.process : thisProcessID;

    static _deleteme = "deleteme.dmd.unittest.pid";
    static _first = true;

    if (_first)
    {
        _deleteme = buildPath(tempDir(), _deleteme) ~ 
to!string(thisProcessID);
        _first = false;
    }

    return _deleteme;
}

If you want to use anything like them, you'll need to declare them in your
own code.

- Jonathan M Davis




More information about the Digitalmars-d-learn mailing list