CMakeLists.txt revision b18c2d1e
1#
2# Automated tests
3#
4
5set(CTWMBIN "ctwm")
6set(CTWMPATH "${CMAKE_BINARY_DIR}/ctwm")
7add_custom_target(test_bins)
8add_dependencies(test_bins ctwm)
9
10
11# Add some infrastructure for building executables for unit tests
12#
13# BIN - name of the binary to run.
14# BINPATH - path of the binary to run.
15# ARGS - Add'l arguments to the binary for the test
16# WORKING_DIRECTORY - self-explanatory
17#
18# Currently, ${BINPATH} means a thing is already built, so we don't need
19# to worry about building, that's just the path to run.  Else, ${BIN} is
20# built from ${BIN}.c (though multiple calls with the same ${BIN} won't
21# try to re-do the builds).  e.g., commonly, $BINPATH is used when we're
22# running the main ctwm binary, instead of s test-specific bin.
23#
24# This is not un-janky, but it works, and we'll worry about cleaning it
25# up if it gets worse.
26function(ctwm_simple_unit_test TNAME)
27	set(_opts ALLOW_DISPLAY)
28	set(_arg_one BIN BINPATH WORKING_DIRECTORY)
29	set(_arg_multi ARGS)
30	cmake_parse_arguments(_ARGS "${_opts}" "${_arg_one}" "${_arg_multi}"
31		${ARGN})
32
33	# Maybe we're passed a specific path to run
34	if(_ARGS_BINPATH)
35		set(BIN_RUN "${_ARGS_BINPATH}")
36	endif()
37
38	# What's the binary name?
39	if(_ARGS_BIN)
40		set(BIN "${_ARGS_BIN}")
41	else()
42		set(BIN "${TNAME}")
43	endif()
44
45	# Derive path if we only got a name
46	if(NOT BIN_RUN)
47		set(BIN_RUN $<TARGET_FILE:${BIN}>)
48	endif()
49
50	# Building and linking it
51	if(NOT TARGET ${BIN})
52		add_executable(${BIN} EXCLUDE_FROM_ALL "${BIN}.c")
53		target_sources(${BIN} PUBLIC $<TARGET_OBJECTS:ctwmlib>)
54
55		# Few of our tests really need any of the X or other libs we pull in.
56		# However, most of the .o's for ctwm itself are going to have some
57		# ref out to them somewhere, so any tests that wind up pulling
58		# functions from those are going to make the linker want to resolve
59		# them when we link ${TNAME}.  So just unconditionally add them and
60		# don't worry about which tests may not actually need 'em.
61		target_link_libraries(${BIN} ${CTWMLIBS})
62
63		# Add to pre-test target
64		add_dependencies(test_bins ${BIN})
65	endif()
66
67	# And add the test itself
68	add_test(NAME ${TNAME}
69		COMMAND ${BIN_RUN} ${_ARGS_ARGS}
70		WORKING_DIRECTORY ${_ARGS_WORKING_DIRECTORY}
71		)
72
73	# Don't allow $DISPLAY unless specifically requested.
74	if(NOT _ARGS_ALLOW_DISPLAY)
75		set_tests_properties(${TNAME} PROPERTIES
76			ENVIRONMENT DISPLAY=
77			)
78	endif()
79
80endfunction()
81
82
83# Simple wrapper for when we want to explicitly skip a test (as opposed
84# to just silently not doing it; better UX to say we're skipping one and
85# why...)
86#
87# XXX This seems like the simplest way of actually "skip"'ing a test
88# based on configure options.  That's nuts.  REQUIRED_FILES sounds like
89# it would, but actually causes the test to "not run" and be considered
90# failed   :(
91function(ctwm_skip_unit_test TNAME REASON)
92	add_test(NAME ${TNAME} COMMAND sh -c "echo Skipped: ${REASON} ; exit 99")
93	set_tests_properties(${TNAME} PROPERTIES SKIP_RETURN_CODE 99)
94endfunction()
95
96
97
98# First a simple smoke test of the built binary
99ctwm_simple_unit_test(run-info
100	BIN ${CTWMBIN}
101	BINPATH ${CTWMPATH}
102	ARGS --info
103	)
104set_tests_properties(run-info PROPERTIES
105	PASS_REGULAR_EXPRESSION "Twm version: "
106	)
107
108
109# Try parsing the system.ctwmrc
110ctwm_simple_unit_test(cfgchk
111	BIN ${CTWMBIN}
112	BINPATH ${CTWMPATH}
113	ARGS --cfgchk -f ${CMAKE_SOURCE_DIR}/system.ctwmrc
114	)
115
116
117# Simple test of m4 preprocessing, but we skip if built without m4.
118if(USE_M4)
119	add_subdirectory(test_m4)
120else()
121	ctwm_skip_unit_test(test_m4 "Built without USE_M4")
122endif()
123
124
125# A first run at a unit test
126add_subdirectory(util_expand)
127
128# RLayout stuff
129add_subdirectory(layout)
130
131# TwmKeys menu bits
132add_subdirectory(menu_twmkeys)
133