www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Unit tests via DUB

reply alexanderzhirov <alexander zhirov.website> writes:
I don't quite understand why compiling unit tests using DUB 
doesn't work.

JSON:
```json
{
   "authors": [
     "alexander"
   ],
   "description": "Array Slicing",
   "license": "proprietary",
   "name": "array_slicing",
   "targetName": "program",
   "targetPath": "bin"
}
```
Projects structure:
```sh
├── bin
├── dub.json
└── source
     └── app.d
```
Errors:
```sh
No source files found in configuration 'library'. Falling back to 
"dub -b unittest".
Performing "unittest" build using /usr/bin/dmd for x86_64.
array_slicing ~master: building configuration "application"...
Linking...
/usr/bin/ld: 
/usr/lib64/gcc/x86_64-solus-linux/11/../../../../lib64/crt1.o: в 
функции «_start»:
/home/build/YPKG/root/glibc/build/glibc.git/csu/../sysdeps/x86_64/start.S:110:
неопределённая ссылка на «main»
collect2: ошибка: выполнение ld завершилось с
кодом возврата 1
Error: linker exited with status 1
/usr/bin/dmd failed with exit code 1.
```
Apr 02 2022
next sibling parent reply Salih Dincer <salihdb hotmail.com> writes:
On Saturday, 2 April 2022 at 11:53:12 UTC, alexanderzhirov wrote:
 I don't quite understand why compiling unit tests using DUB 
 doesn't work.
Source code?
Apr 02 2022
parent reply Alexander Zhirov <alexander zhirov.website> writes:
On Saturday, 2 April 2022 at 13:12:04 UTC, Salih Dincer wrote:
 On Saturday, 2 April 2022 at 11:53:12 UTC, alexanderzhirov 
 wrote:
 I don't quite understand why compiling unit tests using DUB 
 doesn't work.
Source code?
A common example from a textbook ```d import std.array; bool binarySearch(T)(T[] input, T value) { while (!input.empty) { auto i = input.length / 2; auto mid = input[i]; if (mid > value) input = input[0 .. i]; else if (mid < value) input = input[i + 1 .. $]; else return true; } return false; } unittest { assert(binarySearch([1, 3, 6, 7, 9, 15], 7)); assert(!binarySearch([1, 3, 6, 7, 9, 15], 5)); } ```
Apr 02 2022
parent reply Salih Dincer <salihdb hotmail.com> writes:
On Saturday, 2 April 2022 at 13:31:47 UTC, Alexander Zhirov wrote:
 On Saturday, 2 April 2022 at 13:12:04 UTC, Salih Dincer wrote:
 On Saturday, 2 April 2022 at 11:53:12 UTC, alexanderzhirov 
 wrote:
 I don't quite understand why compiling unit tests using DUB 
 doesn't work.
Source code?
A common example from a textbook ```d import std.array; bool binarySearch(T)(T[] input, T value) { while (!input.empty) { auto i = input.length / 2; auto mid = input[i]; if (mid > value) input = input[0 .. i]; else if (mid < value) input = input[i + 1 .. $]; else return true; } return false; } unittest { assert(binarySearch([1, 3, 6, 7, 9, 15], 7)); assert(!binarySearch([1, 3, 6, 7, 9, 15], 5)); } ```
is there main()? if isn't it, please add the following line at the end: ```d void main() {} ```
Apr 02 2022
parent Alexander Zhirov <alexander zhirov.website> writes:
On Saturday, 2 April 2022 at 14:35:49 UTC, Salih Dincer wrote:
 On Saturday, 2 April 2022 at 13:31:47 UTC, Alexander Zhirov 
 wrote:
 On Saturday, 2 April 2022 at 13:12:04 UTC, Salih Dincer wrote:
 On Saturday, 2 April 2022 at 11:53:12 UTC, alexanderzhirov 
 wrote:
 I don't quite understand why compiling unit tests using DUB 
 doesn't work.
Source code?
A common example from a textbook ```d import std.array; bool binarySearch(T)(T[] input, T value) { while (!input.empty) { auto i = input.length / 2; auto mid = input[i]; if (mid > value) input = input[0 .. i]; else if (mid < value) input = input[i + 1 .. $]; else return true; } return false; } unittest { assert(binarySearch([1, 3, 6, 7, 9, 15], 7)); assert(!binarySearch([1, 3, 6, 7, 9, 15], 5)); } ```
is there main()? if isn't it, please add the following line at the end: ```d void main() {} ```
I managed to compile the file itself with this command: ```sh dmd -main -unittest app.d ``` It was also possible to compile with the addition of the *main()* function to the project. In the case of dub, I added entry to the project *"buildOptions": ["unittests"]* and the file now looks like this: ```json { "authors": [ "alexander" ], "description": "Array Slicing", "license": "proprietary", "name": "array_slicing", "targetName": "program", "targetPath": "bin", "buildOptions": ["unittests"] } ``` And when I call dub, I get this message ```sh The following compiler flags have been specified in the package description file. They are handled by DUB and direct use in packages is discouraged. Alternatively, you can set the DFLAGS environment variable to pass custom flags to the compiler, or use one of the suggestions below: unittests: Call DUB with --build=unittest Performing "debug" build using dmd for x86_64. array_slicing ~master: building configuration "application"... Linking... Running bin/program 1 modules passed unittests ``` But as you can see, the unit test works. But how do I do all this right?
Apr 02 2022
prev sibling next sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 4/2/22 7:53 AM, alexanderzhirov wrote:
 I don't quite understand why compiling unit tests using DUB doesn't work.
 
 JSON:
 ```json
 {
    "authors": [
      "alexander"
    ],
    "description": "Array Slicing",
    "license": "proprietary",
    "name": "array_slicing",
    "targetName": "program",
    "targetPath": "bin"
 }
 ```
 Projects structure:
 ```sh
 ├── bin
 ├── dub.json
 └── source
      └── app.d
 ```
 Errors:
 ```sh
 No source files found in configuration 'library'. Falling back to "dub 
 -b unittest".
 Performing "unittest" build using /usr/bin/dmd for x86_64.
 array_slicing ~master: building configuration "application"...
 Linking...
 /usr/bin/ld: 
 /usr/lib64/gcc/x86_64-solus-linux/11/../../../../lib64/crt1.o: в
функции 
 «_start»:
 /home/build/YPKG/root/glibc/build/glibc.git/csu/../sysdeps/x86_64/start.S:110: 
 неопределённая ссылка на «main»
 collect2: ошибка: выполнение ld завершилось с
кодом возврата 1
 Error: linker exited with status 1
 /usr/bin/dmd failed with exit code 1.
 ```
 
So to explain a couple things here: 1. you have a source file called `app.d`, which means to dub that this is an executable target, not a library. 2. `dub test` is made for *libraries*. What it does is add its own test runner and main function. Because it thinks you are trying to test an executable, it defaults to running `dub -b unittest` which is the same as building an application, but just adds the `-unittest` flag to the compiler. 3. Your build has no `main` function, so the linker fails. Solutions: 1. Add a `main()` function to your program. 2. Change the name of your source file so it doesn't think this is an application 3. Declare the project is a library via `targetType`. Without knowing what you are doing with the build, I'd say likely you should add the main function as suggested by Salih Dincer. -Steve
Apr 02 2022
prev sibling parent Andre Pany <andre s-e-a-p.de> writes:
On Saturday, 2 April 2022 at 11:53:12 UTC, alexanderzhirov wrote:
 I don't quite understand why compiling unit tests using DUB 
 doesn't work.

 [...]
Have a look here https://andre2007.github.io/d-tips/dub/application_template/ Kind regards Andre
Apr 03 2022