How can I configure fpm to build the test executable without running it?

Hello!

I would like to build the executable of my unittest programm, but unlike ‘fpm test’ it should not start running them immediately.
Some context: I use Visual Studio Code as IDE and for running and debugging the code I have the following launch and task configurations:

    "version": "0.2.0",
    "configurations": [
        {
            "name": "run project",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}\\build\\gfortran_821022990192F960\\app\\my_project.exe",
            "args": [""],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "miDebuggerPath": "gdb.exe",
            "preLaunchTask": "fpm_build"
        },
        {
            "name": "test project",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}\\build\\gfortran_821022990192F960\\test\\check.exe",
            "args": ["-v"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "miDebuggerPath": "gdb.exe",
            "preLaunchTask": "fpm_test"
        },
    ]
}
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "set_compiler",
            "type": "shell",
            "command": "set FPM_FC=gfortran"
        },{
            "label": "fpm_build",
            "dependsOn":["set_compiler"],
            "type": "shell",
            "command": "fpm build"
        },{
            "label": "fpm_test",
            "dependsOn":["set_compiler"],
            "type": "shell",
            "command": "fpm test"
        }
    ]
}

With this setup I can build the normal program and then debug it. However the ‘test project’ launch task will first build the check programm, then run it (as part for fpm test command) and the run it again (this time with actual debugging enabled). This will result in running the tests twice and if any test throws an exception I won’t be able to debug the test cases (since the actual launch won’t happen due to the failing preLaunchTask).

Ideally I would like to just build the test programm with my preLaunchTask. So do some command flags/fpm configuration exist, that would achieve this? If not, do you know of any other workaround?

Greetings
Tavi007

1 Like
# fpm build --help
NAME
 build(1) - the fpm(1) subcommand to build a project

[...]
 --tests       build all tests (otherwise only if needed)
2 Likes

Ah thank you. Not sure how missed that :sweat_smile:

2 Likes

Easy to miss; as it has changed and is in an inconsistent state that makes it less intuitive.

Initially fpm(1) built everything including app/, test/ and example/ files. But there was a need to not build the tests when being used as an external dependency. Tests can have other packages needed that are not needed to use the package itself. Not wanting to build everything for the
install(1) command can be an issue as well.; and the original concept was for install, run, test, run --example, … to first call the build procedure to make sure everything was built.

So an early solution was to not build the test/ directory unless the test command was called. But then for people that DID want to build everything the build command had the --tests option added. But the same was not done for the example/ directory so it is still built by a build command.

And as an aside, run, run --example, and test can be “tricked” into building and not executing the executable by using the --runner option with no command (it defaults to echo $name(s)) or -list.

To make it consistent build(1) perhaps builds less by default and a -app and -example could be added, but since the main thing being built might be an app, having to specify --app all the time would be bad.

Another way to go would be to drop the --tests option and make the default for build be everything again with a way to not build executables. There could be --noapps, --notest, --noexample or there could be a -noexe which would not build executables, but if given a list would build all but the ones specified.

-noexe  # build no executables
--noexe test example  # do not build tests and examples

and when used as a dependency “build -noexe” would be used.

So instead of each command that calls “build” having a -nobuild switch or having switches for building in each directory; or worrying about whether the names given are generic or actual directory names (if I put my executables in exe/ instead of app/ and give the directory name in fpm.toml manifest file do I enter “–noexe exe” or does “–noexe app” handle all application directories?

while fpm is still in development, I think that needs to be rethought and made consistent for all the executables (app, test, example, …) and take into account being built for installation (which can be libraries, executables or both) ,an option for being built but not run for development, and being used as an external dependency need consistent support. Remember that executables and tests can have package dependencies that the base package often does not need for it to be used as a dependency.

I know what my preference would be but I think the feature is worth revisiting to make sure the most intuitive solution is incorporated.

3 Likes