I recently converted my build system to automake/autoconf. In my project I have a few unit tests that need some input data files in the direcory from where they are run. When I run make distcheck and it tries the VPATH build, these tests fail because they are apparently not run from the directory where the input files are. I was wondering if there is some quick fix for this. For example, can I somehow tell the system not to run these tests on make distcheck (but still run them on make check)? Or to cd to the directory where the files are before running the tests?
2 Answers
I had the same problem and used solution similar to William's. My Makefile.am looks something like this:
EXTRA_DIST = testdata/test1.dat
AM_CPPFLAGS = -DDATADIR=\"$(srcdir)/\"
Then, in my unittest, I use the DATADIR define:
string path = DATADIR "/testdata/test1.dat"
This works with make check
and make distcheck
.

- 548
- 2
- 9
The typical solution is to write the tests so that they look in the source directory for the data files. For example, you can reference $srcdir
in the test, or convert test
to test.in
and refer to @srcdir@
.
If your tests are all in the source directory, you can run all the tests in that directory by setting TESTS_ENVIRONMENT in Makefile.am:
TESTS_ENVIRONMENT = cd $(srcdir) &&
This will fail if some of your tests are created by configure and therefore live only in the build directory, in which case you can selectively cd with something like:
TESTS_ENVIRONMENT = { test $${tst} = mytest && cd $(srcdir); true; } &&
Trying to use TESTS_ENVIRONMENT like this is fragile at best, and it would be best to write the tests so that they look in the source directory for the data files.

- 204,365
- 48
- 270
- 300
-
My tests are in src/some/path and the data files are in the same directory. I have tried the TESTS_ENVIRONMENT = cd $(srcdir) && trick but it didn't work because now it doesn't find the test executable any more. Copying the required data files to the current directory also doesn't work because, in a VPATH build, you apparently don't have write permissions to the directory where the tests are run. – Martin Wiebusch Jul 08 '12 at 20:56
-
@Martin, you don't have write access to the source directory, but you do have write access to the build directory. How are you copying the file? Simply adding `EXTRA_DIST = datafile` to Makefile.am and `cp ${srcdir}/datafile .` in test.in should work. However, if the test is running from the source directory then attempting to copy will fail due to write permissions. I suspect you have some tests that are run from the build dir and some that are run from the source dir. (Tests named `*.in` are (probably) run from the build dir, while other tests are run from the source dir.) – William Pursell Jul 09 '12 at 13:31
-
This looked exactly like what I was looking for, but this doesn't seem to work with Automake 1.15, unfortunately: with this, it creates `*.log` and `*.trs` files in the source directory (which is already bad), but still looks for them in the build directory and, of course, doesn't find them there (which is fatal). – VZ. Jan 12 '17 at 17:10