Issue 4669 - Unit tests do not work in libraries compiled by dmd with -lib
Summary: Unit tests do not work in libraries compiled by dmd with -lib
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P3 normal
Assignee: No Owner
URL:
Keywords: bootcamp, wrong-code
: 6464 (view as issue list)
Depends on:
Blocks:
 
Reported: 2010-08-17 08:55 UTC by Mike Linford
Modified: 2022-12-17 10:45 UTC (History)
7 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Mike Linford 2010-08-17 08:55:13 UTC
The following unit test in mylib.d is not run:

mylib.d:

  1 module mylib;
  2
  3 void blah()
  4 {
  5 }
  6 unittest
  7 {
  8    assert(false);
  9 }
 10

test.d:

  1 module test;
  2
  3 import mylib;
  4
  5 void main()
  6 {
  7    blah();
  8 }
  9


Makefile:
  1 test : mylib.a test.d
  2    dmd -unittest test.d mylib.a
  3
  4 mylib.a : mylib.d
  5    dmd -unittest -lib mylib.d
  6
  7 clean :
  8    rm -f test mylib.a *.o
  9

However, it WILL be run if the project is compiled as follows:

dmd -unittest -c mylib.d
ar -rc mylib.a mylib.o
dmd -unittest test.d mylib.a
Comment 1 Stephan Dilly 2010-08-17 10:13:09 UTC
i can confirm that unittests in static librarys build by dmd does not get triggered on windows too.
Comment 2 Mike Parker 2011-08-10 03:49:56 UTC
*** Issue 6464 has been marked as a duplicate of this issue. ***
Comment 3 Andrej Mitrovic 2011-09-15 07:10:13 UTC
This needs more attention imho. If unittests are first-class citizens in D then we shouldn't have to use workarounds just to trigger them in a library.
Comment 4 Phil Lavoie 2012-10-14 14:57:54 UTC
Yeah, I also confirm this issue on Windows. Since unit testing plays an important part in software construction, I hope that it gets the attention it deserves and gets fixed soon :). Love D 4 Life.
Comment 5 Andrej Mitrovic 2013-01-13 09:02:59 UTC
The cause is this code in 'FuncDeclaration::toObjFile':

if (multiobj && !isStaticDtorDeclaration() && !isStaticCtorDeclaration())
{   obj_append(this);
    return;
}

'multiobj' is true when -lib is passed, this unittest is then deferred to be generated later (and it *is* generated later), however it never seems to be run.

Anyway a qujick fix is to add a check for lib generation:

if (multiobj && !global.params.lib && !isStaticDtorDeclaration() && !isStaticCtorDeclaration()) { }

This makes the unittest work, but I don't know if that's a proper fix.

Any backend experts know more?
Comment 6 Elvis Zhou 2013-10-05 22:15:19 UTC
I came across this issue too, it really deserves more attentions,
how about the current status?
Comment 7 Maor 2014-06-08 13:56:28 UTC
I can confirm this issue on both Mac os x and Linux.

It's a real issue when trying to write a non trivial project and using unittests.