www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Fails to use testFilename in unittest

reply biocyberman <biocyberman gmail.com> writes:
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

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");

}
May 18 2017
next sibling parent biocyberman <biocyberman gmail.com> writes:
This is the compile error message by the way:


dmd -unittest ./testFile.d                                        
                                                                   
                                                                   
                                                                   
         !6009

testFile.o: In function `_D8testFile14__unittestL4_1FZv':
./testFile.d:(.text._D8testFile14__unittestL4_1FZv+0x1a): 
undefined reference to `_D3std5stdio12testFilenameFNfAyamZAya'
collect2: error: ld returned 1 exit status
Error: linker exited with status 1
May 18 2017
prev sibling parent reply Jonathan M Davis via Digitalmars-d-learn writes:
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

 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
May 18 2017
parent reply biocyberman <biocyberman gmail.com> writes:
On Thursday, 18 May 2017 at 09:49:26 UTC, Jonathan M Davis wrote:
 On Thursday, May 18, 2017 09:40:33 biocyberman via 
 Digitalmars-d-learn wrote:
 [...]
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. [...]
That's exactly the code I looked at. And yes, I checked std.stdio to see many occurrences of testFilename.
May 18 2017
parent reply Jonathan M Davis via Digitalmars-d-learn writes:
On Thursday, May 18, 2017 09:56:36 biocyberman via Digitalmars-d-learn 
wrote:
 On Thursday, 18 May 2017 at 09:49:26 UTC, Jonathan M Davis wrote:
 On Thursday, May 18, 2017 09:40:33 biocyberman via

 Digitalmars-d-learn wrote:
 [...]
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. [...]
That's exactly the code I looked at. And yes, I checked std.stdio to see many occurrences of testFilename.
My point is that it's a private function for testing std.stdio and not intended to be part of the public API or be used by anyone else (it's not even used anywhere else in Phobos). None of the functions in Phobos that do that sort of thing are in the public API. You can copy-paste testFilename (and std.file.deleteme, since it uses that) into your own code and use them if you like, but the ones in Phobos are just there for Phobos. The only unit testing-specific functionality that Phobos provides beyond what the language itself has is in std.exception with functions such as assertThrown. - Jonathan M Davis
May 18 2017
parent biocyberman <biocyberman gmail.com> writes:
On Thursday, 18 May 2017 at 10:05:41 UTC, Jonathan M Davis wrote:
 On Thursday, May 18, 2017 09:56:36 biocyberman via 
 Digitalmars-d-learn wrote:
 [...]
My point is that it's a private function for testing std.stdio and not intended to be part of the public API or be used by anyone else (it's not even used anywhere else in Phobos). None of the functions in Phobos that do that sort of thing are in the public API. You can copy-paste testFilename (and std.file.deleteme, since it uses that) into your own code and use them if you like, but the ones in Phobos are just there for Phobos. The only unit testing-specific functionality that Phobos provides beyond what the language itself has is in std.exception with functions such as assertThrown. - Jonathan M Davis
Understood. I copied the code. Thanks
May 18 2017