Jump to page: 1 2
Thread overview
Running unit tests from DUB single file packages
Dec 01, 2020
Johannes Loher
Dec 01, 2020
jmh530
Dec 01, 2020
jmh530
Dec 01, 2020
Johannes Loher
Dec 01, 2020
jmh530
Dec 01, 2020
drug
Dec 01, 2020
Johannes Loher
Dec 02, 2020
drug
Dec 20, 2020
jmh530
Dec 21, 2020
drug
Dec 21, 2020
jmh530
Dec 22, 2020
drug
Dec 22, 2020
drug
Dec 22, 2020
jmh530
Dec 23, 2020
drug
December 01, 2020
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:

```
#!/usr/bin/env dub
/+ 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/
December 01, 2020
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?
December 01, 2020
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.
December 01, 2020
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

December 01, 2020
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:

```
#!/usr/bin/env dub
/+ 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. ```
December 01, 2020
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.
December 01, 2020
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...


December 02, 2020
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
December 20, 2020
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?
December 21, 2020
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.
« First   ‹ Prev
1 2