Skip to content

Commit ae4ab78

Browse files
authored
Merge pull request #551 from pmembrey/add-meson-generator-for-test-runner
Enhance meson support so that it can automatically generate a test runner
2 parents 5eca8d3 + 63ea077 commit ae4ab78

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

auto/generate_test_runner.rb

100644100755
+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/ruby
2+
13
# ==========================================
24
# Unity Project - A Test Framework for C
35
# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams

docs/MesonGeneratorRunner.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Meson Generator - Test Runner
2+
3+
One of the really nice things about using Unity with Ceedling is that Ceedling takes care of generating all of the test runners automatically. If you're not using Ceedling though, you'll need to do this yourself.
4+
5+
The way this is done in Unity is via a Ruby script called `generate_test_runner.rb`. When given a test file such as `test_example.c`, the script will generate `test_example_Runner.c`, which provides the `main` method and some other useful plumbing.
6+
7+
So that you don't have to run this by hand, a Meson generator is provided to generate the runner automatically for you. Generally with Meson, you would use Unity as a subproject and you'd want to access the generator from the parent.
8+
9+
For example, to get the generator you can use:
10+
11+
unity_proj = subproject('unity')
12+
runner_gen = unity_proj.get_variable('gen_test_runner')
13+
14+
Once you have the generator you need to pass it the absolute path of your test file. This seems to be a bug in how the paths work with subprojects in Meson. You can get the full path with `meson.source_root()`, so you could do:
15+
16+
test_runner = meson.source_root() / 'test/test_example.c'
17+
18+
You can then include `test_runner` as a normal dependency to your builds. Meson will create the test runner in a private directory for each build target. It's only meant to be used as part of the build, so if you need to refer to the runner after the build, you won't be able to use the generator.

meson.build

+12
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,15 @@ project('unity', 'c',
1111

1212
subdir('src')
1313
unity_dep = declare_dependency(link_with: unity_lib, include_directories: unity_dir)
14+
15+
16+
# Get the generate_test_runner script relative to itself or the parent project if it is being used as a subproject
17+
# NOTE: This could be (and probably is) a complete hack - but I haven't yet been able to find a better way....
18+
if meson.is_subproject()
19+
gen_test_runner_path = find_program(meson.source_root() / 'subprojects/unity/auto/generate_test_runner.rb')
20+
else
21+
gen_test_runner_path = find_program('subprojects/unity/auto/generate_test_runner.rb')
22+
endif
23+
24+
# Create a generator that we can access from the parent project
25+
gen_test_runner = generator(gen_test_runner_path, output: '@BASENAME@_Runner.c', arguments: ['@INPUT@', '@OUTPUT@'] )

0 commit comments

Comments
 (0)