Skip to content

Commit

Permalink
tests/ctests: Build ctests separately
Browse files Browse the repository at this point in the history
At present the ctests are not properly linked with the built code,
this causes issues when there are interdependencies between the
modules.

The meson build system does not appear to have support for
internal build order or dependncies, so the only way I have found
to make this work is to break out the build of the ctests as a
separate project.

This is required to avoid undefined symbols in the test code for
the next patch in this series.

Signed-off-by: Frode Nordahl <[email protected]>
  • Loading branch information
fnordahl committed Dec 2, 2023
1 parent c97c476 commit 9d23719
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
5 changes: 4 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ test_env = [
]

if get_option('unit_testing')
subdir('tests/ctests')
test('ctests',
find_program('tests/ctests/run.sh'),
workdir: join_paths(meson.current_source_dir(), 'tests/ctests'),
args: [meson.current_source_dir(), meson.current_build_dir()])
endif

#FIXME: exclude doc/env/
Expand Down
23 changes: 20 additions & 3 deletions tests/ctests/meson.build
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
project('ctests', 'c')

add_project_arguments(
'-DSBINDIR="' + join_paths(get_option('prefix'), get_option('sbindir')) + '"',
'-D_GNU_SOURCE',
language: 'c')

tests = {
'test_netplan_parser': false,
'test_netplan_state': false,
Expand All @@ -10,15 +17,25 @@ tests = {
'test_netplan_openvswitch': false,
}

glib = dependency('glib-2.0')
gio = dependency('gio-2.0')
yaml = dependency('yaml-0.1')
uuid = dependency('uuid')
cmocka = dependency('cmocka', required: true)
netplan = meson.get_compiler('c').find_library(
'netplan',
dirs: [join_paths(get_option('parent_current_build_dir'), 'src')])

foreach name, should_fail: tests
exe = executable(name,
'@[email protected]'.format(name),
include_directories: [inc, inc_internal],
dependencies: [cmocka, glib, gio, yaml, uuid],
include_directories: [
join_paths(get_option('parent_current_source_dir'), 'include'),
join_paths(get_option('parent_current_source_dir'), 'src'),
],
dependencies: [cmocka, glib, gio, yaml, uuid, netplan],
c_args: [
'-DFIXTURESDIR="' + meson.project_source_root() + '/tests/ctests/fixtures"',
'-DFIXTURESDIR="' + meson.project_source_root() + '/fixtures"',
'-Wno-deprecated-declarations',
'-D_GNU_SOURCE',
],
Expand Down
17 changes: 17 additions & 0 deletions tests/ctests/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh

PARENT_CURRENT_SOURCE_DIR="$1"
PARENT_CURRENT_BUILD_DIR="$2"
printf "%s%s%s\n" \
"option('parent_current_source_dir', " \
"type: 'string', " \
"value: '${PARENT_CURRENT_SOURCE_DIR=}')" > meson_options.txt
printf "%s%s%s\n" \
"option('parent_current_build_dir', " \
"type: 'string', " \
"value: '${PARENT_CURRENT_BUILD_DIR}')" >> meson_options.txt

rm -rf build
meson setup build && cd build
meson compile
meson test

0 comments on commit 9d23719

Please sign in to comment.