www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Running unit tests from DUB single file packages

reply Johannes Loher <johannes.loher fg4f.de> writes:
Hello everybody,

when solving today's problem for the adventofcode [1], I decided to try
out DUB's single file package feature, in order to keep things short.

When doing the adventofcode, I always use the given examples as unit
tests in order to verify that my solutions are actually correct.
However, I am having trouble running the unit tests when using the
single file package format:

```
johannesloher saiph:..020/day1/part1-mir(master)> dub test --single
main.d

Package main (configuration "application") defines no import paths, use
{"importPaths": [...]} or the default package directory structure to fix
this.

Generating test runner configuration 'main-test-application' for
'application' (executable).

Source file
'/home/johannesloher/Development/aoc2020/day1/part1-mir/main.d' not
found in any import path.
```

I tried defining `importPaths` as suggested in the error message but
apparently that's not possible for single file packages:

```
Single-file packages are not allowed to specify import paths.
```

WARNING: Spoilers for day 1 of the adventofcode, please stop reading if
you have not solved it yet and want to do so by yourself.

Here is the source for my main.d file:

```

/+ dub.sdl:
    name "main"
    dependency "mir-algorithm" version="~>3.10.12"
+/

import std;
import mir.combinatorics;

void main()
{
    File("input",
"r").byLine.map!(to!int).array.multiplyNEntriesThatSumTo(2, 2020).writeln;
}

alias product = partial!(reverseArgs!(fold!((a, b) => a * b)), 1);

int multiplyNEntriesThatSumTo(int[] input, int n, int requiredSum)
{
    return input.combinations(n).filter!(combination => combination.sum
== requiredSum)
        .map!(combination => combination.product)
        .front;
}

unittest
{
    auto input = [1721, 979, 366, 299, 675, 1456];

    assert(input.multiplyNEntriesThatSumTo(2, 2020) == 514_579);
}
```

Any hints on how to execute unit tests from single file DUB packages? Is
it even possible at the moment? Thanks in advance for any help!


[1] https://adventofcode.com/
Dec 01 2020
next sibling parent reply jmh530 <john.michael.hall gmail.com> writes:
On Tuesday, 1 December 2020 at 11:40:38 UTC, Johannes Loher wrote:
 [snip]

 Any hints on how to execute unit tests from single file DUB 
 packages? Is it even possible at the moment? Thanks in advance 
 for any help!


 [1] https://adventofcode.com/
Have you tried it without the imports?
Dec 01 2020
parent reply jmh530 <john.michael.hall gmail.com> writes:
On Tuesday, 1 December 2020 at 13:52:35 UTC, jmh530 wrote:
 On Tuesday, 1 December 2020 at 11:40:38 UTC, Johannes Loher 
 wrote:
 [snip]

 Any hints on how to execute unit tests from single file DUB 
 packages? Is it even possible at the moment? Thanks in advance 
 for any help!


 [1] https://adventofcode.com/
Have you tried it without the imports?
Or rather, without the dependency.
Dec 01 2020
parent reply Johannes Loher <johannes.loher fg4f.de> writes:
Am 01.12.20 um 14:52 schrieb jmh530:
 On Tuesday, 1 December 2020 at 13:52:35 UTC, jmh530 wrote:
 On Tuesday, 1 December 2020 at 11:40:38 UTC, Johannes Loher wrote:
 [snip]

 Any hints on how to execute unit tests from single file DUB packages?
 Is it even possible at the moment? Thanks in advance for any help!


 [1] https://adventofcode.com/
Have you tried it without the imports?
Or rather, without the dependency.
The point of using DUB (and the single file package format) is easy access to libraries from the DUB registry. If I didn't want to use a dependency, I would not be using DUB at all. That said, leaving out the dependency does not solve the issue, it also occurs with the following source file: ``` /+ dub.sdl: name "main" +/ void main() {} unittest { assert(true); } ``` ``` johannesloher saiph:~> dub test --single main.d Package main (configuration "application") defines no import paths, use {"importPaths": [...]} or the default package directory structure to fix this. Generating test runner configuration 'main-test-application' for 'application' (executable). Source file '/home/johannesloher/main.d' not found in any import path. ```
Dec 01 2020
parent jmh530 <john.michael.hall gmail.com> writes:
On Tuesday, 1 December 2020 at 14:15:22 UTC, Johannes Loher wrote:
 [snip]

 The point of using DUB (and the single file package format) is 
 easy access to libraries from the DUB registry. If I didn't 
 want to use a dependency, I would not be using DUB at all. That 
 said, leaving out the dependency does not solve the issue, it 
 also occurs with the following source file:
Thanks. The reason I was asking was because if you've ever tried run.dlang.org with dependencies and unit tests, then you'll notice that the unittests are skipped, which is basically the same issue you are having. If you remove the dependencies, then it works. So I was thinking that whatever they used to get run.dlang.org working without dependencies might help you. I had hoped to try to get run.dlang.org working with dependencies and unittests, but haven't found the time to get a solution. Maybe this PR might improve matters...
Dec 01 2020
prev sibling parent reply drug <drug2004 bk.ru> writes:
On 12/1/20 2:40 PM, Johannes Loher wrote:
 ...
 However, I am having trouble running the unit tests when using the
 ...
This can be one of solution https://github.com/dlang/dub/pull/2050
Dec 01 2020
parent reply Johannes Loher <johannes.loher fg4f.de> writes:
Am 01.12.20 um 14:55 schrieb drug:
 On 12/1/20 2:40 PM, Johannes Loher wrote:
 ...
 However, I am having trouble running the unit tests when using the
 ...
This can be one of solution https://github.com/dlang/dub/pull/2050
Thanks! Let's see if it gets merged or if a slightly more involved solution is needed.
Dec 01 2020
parent reply drug <drug2004 bk.ru> writes:
On 12/1/20 5:18 PM, Johannes Loher wrote:
 Am 01.12.20 um 14:55 schrieb drug:
 On 12/1/20 2:40 PM, Johannes Loher wrote:
 ...
 However, I am having trouble running the unit tests when using the
 ...
This can be one of solution https://github.com/dlang/dub/pull/2050
Thanks! Let's see if it gets merged or if a slightly more involved solution is needed.
Remake it - https://github.com/dlang/dub/pull/2052 This has more chances to be merged
Dec 02 2020
parent reply jmh530 <john.michael.hall gmail.com> writes:
On Wednesday, 2 December 2020 at 12:51:11 UTC, drug wrote:
 [snip]
 
 Thanks! Let's see if it gets merged or if a slightly more 
 involved
 solution is needed.
 
Remake it - https://github.com/dlang/dub/pull/2052 This has more chances to be merged
Looks like this got merged and will be part of the newest version, which is great news. Have you checked that it works with dependencies?
Dec 20 2020
parent reply drug <drug2004 bk.ru> writes:
On 12/20/20 9:31 PM, jmh530 wrote:
 On Wednesday, 2 December 2020 at 12:51:11 UTC, drug wrote:
 [snip]
 Thanks! Let's see if it gets merged or if a slightly more involved
 solution is needed.
Remake it - https://github.com/dlang/dub/pull/2052 This has more chances to be merged
Looks like this got merged and will be part of the newest version, which is great news. Have you checked that it works with dependencies?
Unfortunately I'm very busy. But I check it again and it turns out that the fix does not resolve the problem completely. This PR just remove the single file from testing so currently dub does not run unit tests in the single file package at all. The first variant (https://github.com/dlang/dub/pull/2050) fixes the issue indeed. I need to reevaluate these PRs and close the issue. I'll do it later.
Dec 21 2020
parent reply jmh530 <john.michael.hall gmail.com> writes:
On Monday, 21 December 2020 at 11:31:49 UTC, drug wrote:
 [snip]
 Unfortunately I'm very busy. But I check it again and it turns 
 out that the fix does not resolve the problem completely. This 
 PR just remove the single file from testing so currently dub 
 does not run unit tests in the single file package at all. The 
 first variant (https://github.com/dlang/dub/pull/2050) fixes 
 the issue indeed. I need to reevaluate these PRs and close the 
 issue. I'll do it later.
Thanks for taking a look.
Dec 21 2020
parent reply drug <drug2004 bk.ru> writes:
On 12/21/20 7:31 PM, jmh530 wrote:
 On Monday, 21 December 2020 at 11:31:49 UTC, drug wrote:
 [snip]
 Unfortunately I'm very busy. But I check it again and it turns out 
 that the fix does not resolve the problem completely. This PR just 
 remove the single file from testing so currently dub does not run unit 
 tests in the single file package at all. The first variant 
 (https://github.com/dlang/dub/pull/2050) fixes the issue indeed. I 
 need to reevaluate these PRs and close the issue. I'll do it later.
Thanks for taking a look.
Not at all. But what do you mean exactly by "work with dependency"? As I understand, `dub test` does not run unit tests in dependencies and single file packages work with dependencies in general. Do you mean something else? there is something else I should include in it.
Dec 21 2020
parent reply drug <drug2004 bk.ru> writes:
On 12/22/20 10:52 AM, drug wrote:
 On 12/21/20 7:31 PM, jmh530 wrote:
 On Monday, 21 December 2020 at 11:31:49 UTC, drug wrote:
 [snip]
 Unfortunately I'm very busy. But I check it again and it turns out 
 that the fix does not resolve the problem completely. This PR just 
 remove the single file from testing so currently dub does not run 
 unit tests in the single file package at all. The first variant 
 (https://github.com/dlang/dub/pull/2050) fixes the issue indeed. I 
 need to reevaluate these PRs and close the issue. I'll do it later.
Thanks for taking a look.
Not at all. But what do you mean exactly by "work with dependency"? As I understand, `dub test` does not run unit tests in dependencies and single file packages work with dependencies in general. Do you mean something else? there is something else I should include in it.
https://github.com/dlang/dub/pull/2064
Dec 22 2020
parent reply jmh530 <john.michael.hall gmail.com> writes:
On Tuesday, 22 December 2020 at 15:06:09 UTC, drug wrote:
 [snip]
 
 But what do you mean exactly by "work with dependency"? As I 
 understand, `dub test` does not run unit tests in dependencies 
 and single file packages work with dependencies in general. Do 

 finally and I'd like to know if there is something else I 
 should include in it.
https://github.com/dlang/dub/pull/2064
Thanks. It looks like your UT with taggedalgebraic does exactly what I was looking for. My problem is that run.dlang.org will skip unittests when you have dependencies. I had made some progress on fixing this a few months ago [1], but put it on the back burner when I ran into similar issues that the OP was dealing with. The problem ultimately came down to dub test not working with --single, which it looks like this latest PR will fix for good. [1] https://github.com/dlang-tour/core-exec/pull/56
Dec 22 2020
parent drug <drug2004 bk.ru> writes:
On 12/22/20 8:32 PM, jmh530 wrote:
 On Tuesday, 22 December 2020 at 15:06:09 UTC, drug wrote:
 [snip]
 But what do you mean exactly by "work with dependency"? As I 
 understand, `dub test` does not run unit tests in dependencies and 
 single file packages work with dependencies in general. Do you mean 

 like to know if there is something else I should include in it.
https://github.com/dlang/dub/pull/2064
Thanks. It looks like your UT with taggedalgebraic does exactly what I was looking for. My problem is that run.dlang.org will skip unittests when you have dependencies. I had made some progress on fixing this a few months ago [1], but put it on the back burner when I ran into similar issues that the OP was dealing with. The problem ultimately came down to dub test not working with --single, which it looks like this latest PR will fix for good. [1] https://github.com/dlang-tour/core-exec/pull/56
Ah, I see. Then I would say that `dub test` doesn't run unit tests from the main source file (where the main function is placed). And this is the reason, I guess. Now `dub test` runs unit tests from the main source file if the package is a single file one. I think that dependencies is irrelevant here, at least I didn't do anything special for them. Let's see what happens.
Dec 22 2020