如何使 autotools 测试读取文件?
how to make autotools tests read files?
我的 autotools 项目有几个单元测试。
这些测试之一 (filereader
) 需要读取文件 (data/test1.bin
)
这是我的文件系统布局:
- libfoo/tests/filereader.c
- libfoo/tests/data/test1.bin
和我的 libfoo/tests/Makefile.am:
AUTOMAKE_OPTIONS = foreign
AM_CPPFLAGS = -I$(top_srcdir)/foo
LDADD = $(top_builddir)/src/libfoo.la
EXTRA_DIST = data/file1.bin
TESTS = filereader
check_PROGRAMS= filereader
filereader_SOURCES = filereader.c
这很好用,只要我做树内构建。
但是,当 运行 测试套件超出树范围时(例如 make distcheck
),filereader
测试无法再找到输入文件。
这显然是因为只有源代码树包含输入文件,而没有构建树。
我想知道解决这个问题的规范方法是什么?
- 将测试文件的目录编译到unittest (
AM_CPPFLAGS+=-DSRCDIR=$(srcdir)
)
- 将合格的输入文件作为命令行参数传递给测试? (例如
$(builddir)/filereader $(srcdir)/data/file1.bin
)
- 将输入文件从源代码树复制到构建树? (
cp $(srcdir)/data/file1.bin $(builddir)/data/file1.bin
?正确的 make-rule 应该是什么样子??)
通常,解决方案是将文件的路径定义到单元测试中,因此您设置的第一个选项。第二个也是可能的,但它需要使用中间驱动程序脚本。
我建议避开第三个,但如果你确实想走那条路,请使用 $(LN_S)
而不是 cp
;这样你就可以减少测试的 I/O 负载。
有一种方法可以使用 autoconf 来做到这一点。来自 netcdf -c configure.ac:
##
# Some files need to exist in build directories
# that do not correspond to their source directory, or
# the test program makes an assumption about where files
# live. AC_CONFIG_LINKS provides a mechanism to link/copy files
# if an out-of-source build is happening.
##
AC_CONFIG_LINKS([nc_test4/ref_hdf5_compat1.nc:nc_test4/ref_hdf5_compat1.nc])
AC_CONFIG_LINKS([nc_test4/ref_hdf5_compat2.nc:nc_test4/ref_hdf5_compat2.nc])
AC_CONFIG_LINKS([nc_test4/ref_hdf5_compat3.nc:nc_test4/ref_hdf5_compat3.nc])
AC_CONFIG_LINKS([nc_test4/ref_chunked.hdf4:nc_test4/ref_chunked.hdf4])
AC_CONFIG_LINKS([nc_test4/ref_contiguous.hdf4:nc_test4/ref_contiguous.hdf4])
我的 autotools 项目有几个单元测试。
这些测试之一 (filereader
) 需要读取文件 (data/test1.bin
)
这是我的文件系统布局: - libfoo/tests/filereader.c - libfoo/tests/data/test1.bin
和我的 libfoo/tests/Makefile.am:
AUTOMAKE_OPTIONS = foreign
AM_CPPFLAGS = -I$(top_srcdir)/foo
LDADD = $(top_builddir)/src/libfoo.la
EXTRA_DIST = data/file1.bin
TESTS = filereader
check_PROGRAMS= filereader
filereader_SOURCES = filereader.c
这很好用,只要我做树内构建。
但是,当 运行 测试套件超出树范围时(例如 make distcheck
),filereader
测试无法再找到输入文件。
这显然是因为只有源代码树包含输入文件,而没有构建树。
我想知道解决这个问题的规范方法是什么?
- 将测试文件的目录编译到unittest (
AM_CPPFLAGS+=-DSRCDIR=$(srcdir)
) - 将合格的输入文件作为命令行参数传递给测试? (例如
$(builddir)/filereader $(srcdir)/data/file1.bin
) - 将输入文件从源代码树复制到构建树? (
cp $(srcdir)/data/file1.bin $(builddir)/data/file1.bin
?正确的 make-rule 应该是什么样子??)
通常,解决方案是将文件的路径定义到单元测试中,因此您设置的第一个选项。第二个也是可能的,但它需要使用中间驱动程序脚本。
我建议避开第三个,但如果你确实想走那条路,请使用 $(LN_S)
而不是 cp
;这样你就可以减少测试的 I/O 负载。
有一种方法可以使用 autoconf 来做到这一点。来自 netcdf -c configure.ac:
##
# Some files need to exist in build directories
# that do not correspond to their source directory, or
# the test program makes an assumption about where files
# live. AC_CONFIG_LINKS provides a mechanism to link/copy files
# if an out-of-source build is happening.
##
AC_CONFIG_LINKS([nc_test4/ref_hdf5_compat1.nc:nc_test4/ref_hdf5_compat1.nc])
AC_CONFIG_LINKS([nc_test4/ref_hdf5_compat2.nc:nc_test4/ref_hdf5_compat2.nc])
AC_CONFIG_LINKS([nc_test4/ref_hdf5_compat3.nc:nc_test4/ref_hdf5_compat3.nc])
AC_CONFIG_LINKS([nc_test4/ref_chunked.hdf4:nc_test4/ref_chunked.hdf4])
AC_CONFIG_LINKS([nc_test4/ref_contiguous.hdf4:nc_test4/ref_contiguous.hdf4])