www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Get module file path from ModuleInfo

reply Andre Pany <andre s-e-a-p.de> writes:
Hi,

I want to enhance a unittest framework to also report the results 
in SonarQube Generic Execution format. This format lists the file 
paths.

The unittest framework loops through the modules and collects the 
module name and the unittests:

foreach (moduleInfo; ModuleInfo)
     {
         if (moduleInfo)
         {
             auto unitTest = moduleInfo.unitTest;

             if (unitTest)
             {
                 testClass.name = moduleInfo.name;
                 testClass.test = (o, test) { unitTest(); };
                 testClasses ~= testClass;
             }
         }
     }

Is there any way to get the module file path (the value __FILE__ 
would contain in the modules)?


Kind regards
André
Jun 05 2019
parent reply Jacob Carlborg <doob me.com> writes:
On Thursday, 6 June 2019 at 04:20:52 UTC, Andre Pany wrote:
 Hi,

 I want to enhance a unittest framework to also report the 
 results in SonarQube Generic Execution format. This format 
 lists the file paths.

 The unittest framework loops through the modules and collects 
 the module name and the unittests:

 foreach (moduleInfo; ModuleInfo)
     {
         if (moduleInfo)
         {
             auto unitTest = moduleInfo.unitTest;

             if (unitTest)
             {
                 testClass.name = moduleInfo.name;
                 testClass.test = (o, test) { unitTest(); };
                 testClasses ~= testClass;
             }
         }
     }

 Is there any way to get the module file path (the value 
 __FILE__ would contain in the modules)?


 Kind regards
 André
No, I think you have to use `__traits(getUnitTests)`. — /Jacob Carlborg
Jun 06 2019
parent reply Andre Pany <andre s-e-a-p.de> writes:
On Thursday, 6 June 2019 at 10:16:17 UTC, Jacob Carlborg wrote:
 On Thursday, 6 June 2019 at 04:20:52 UTC, Andre Pany wrote:
 Hi,

 I want to enhance a unittest framework to also report the 
 results in SonarQube Generic Execution format. This format 
 lists the file paths.

 The unittest framework loops through the modules and collects 
 the module name and the unittests:

 foreach (moduleInfo; ModuleInfo)
     {
         if (moduleInfo)
         {
             auto unitTest = moduleInfo.unitTest;

             if (unitTest)
             {
                 testClass.name = moduleInfo.name;
                 testClass.test = (o, test) { unitTest(); };
                 testClasses ~= testClass;
             }
         }
     }

 Is there any way to get the module file path (the value 
 __FILE__ would contain in the modules)?


 Kind regards
 André
No, I think you have to use `__traits(getUnitTests)`. — /Jacob Carlborg
Also __traits(getUnitTests) does not return the module file name. The approach above is working fine in the d-unit library (https://code.dlang.org/packages/d-unit) What do you think does make more sense to make it possible: - Enhance ModuleInfo (https://dlang.org/library/object/module_info.html) with a new attribute "string file" - add a new trait __traits(file, "module name") Kind regards André
Jun 06 2019
parent Jacob Carlborg <doob me.com> writes:
On 2019-06-06 12:43, Andre Pany wrote:

 Also __traits(getUnitTests) does not return the module file name.
 The approach above is working fine in the d-unit library
 (https://code.dlang.org/packages/d-unit)
Well, to use __traits(getUnitTests) you need to collect all the files and generate a new file which imports all the files. Therefore you need to know the module name anyway. You can also at the mangled name of a unit test (returned by __traits(getUnitTests)), which will start with the module name. That's a bit of a hack. -- /Jacob Carlborg
Jun 07 2019