Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added simple unit test sample for demonstration #4154

Merged
merged 7 commits into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions .ci/ci-script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,31 @@

set -ex

PARALLEL="-j2"
CMAKE_BUILD_TYPE="RelWithDebInfo"
GENERATOR="Ninja"
VERBOSE="-v"
KEEPGOING="-k0"

if [ "$GENERATOR" = "Unix Makefiles" ];
then
VERBOSE="VERBOSE=1";
KEEPGOING="-k"
fi;

if [ -z "${MAKEFLAGS+x}" ];
then
MAKEFLAGS="-j2 $VERBOSE"
fi

target_build()
{
# to get as much of the issues into the log as possible
cmake --build "$BUILD_DIR" -- $PARALLEL -v || cmake --build "$BUILD_DIR" -- -j1 -v -k0
cmake --build "$BUILD_DIR" -- $MAKEFLAGS || cmake --build "$BUILD_DIR" -- -j1 $VERBOSE $KEEPGOING

ctest --output-on-failure || ctest --rerun-failed -V -VV

# and now check that it installs where told and only there.
cmake --build "$BUILD_DIR" --target install -- $PARALLEL -v || cmake --build "$BUILD_DIR" --target install -- -j1 -v -k0
cmake --build "$BUILD_DIR" --target install -- $MAKEFLAGS || cmake --build "$BUILD_DIR" --target install -- -j1 $VERBOSE $KEEPGOING
}

target_usermanual()
Expand All @@ -48,25 +64,29 @@ target_usermanual()
# ls -lah doc/usermanual/darktable-usermanual.pdf
}

du -hcs "$SRC_DIR"
du -hcs "$BUILD_DIR"
du -hcs "$INSTALL_PREFIX"
diskspace()
{
df
du -hcs "$SRC_DIR"
du -hcs "$BUILD_DIR"
du -hcs "$INSTALL_PREFIX"
}

diskspace

cd "$BUILD_DIR"
cmake -DCMAKE_INSTALL_PREFIX="$INSTALL_PREFIX" -GNinja -DCMAKE_BUILD_TYPE=RelWithDebInfo $ECO -DVALIDATE_APPDATA_FILE=On "$SRC_DIR" || (cat "$BUILD_DIR"/CMakeFiles/CMakeOutput.log; cat "$BUILD_DIR"/CMakeFiles/CMakeError.log)
cmake -DCMAKE_INSTALL_PREFIX="$INSTALL_PREFIX" -G"$GENERATOR" -DCMAKE_BUILD_TYPE="$CMAKE_BUILD_TYPE" $ECO -DVALIDATE_APPDATA_FILE=ON -DBUILD_TESTING=ON "$SRC_DIR" || (cat "$BUILD_DIR"/CMakeFiles/CMakeOutput.log; cat "$BUILD_DIR"/CMakeFiles/CMakeError.log)

case "$TARGET" in
"usermanual")
target_usermanual
;;
"build")
target_build
;;
"usermanual")
target_usermanual
;;
*)
exit 1
;;
esac

du -hcs "$SRC_DIR"
du -hcs "$BUILD_DIR"
du -hcs "$INSTALL_PREFIX"
diskspace
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ matrix:
env: CC=gcc-9 CXX=g++-9 TARGET=usermanual ECO="-DTESTBUILD_OPENCL_PROGRAMS=OFF" # EXTRA_TMPFS="--tmpfs /tmp"
script:
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then docker run --read-only --volume $TRAVIS_BUILD_DIR:$SRC_DIR:ro --tmpfs $BUILD_DIR --workdir $BUILD_DIR --tmpfs $INSTALL_PREFIX $EXTRA_TMPFS --env CC --env CXX --env CFLAGS --env CXXFLAGS --env SRC_DIR --env BUILD_DIR --env INSTALL_PREFIX --env TARGET --env ECO darktable/darktable sh -c "$SRC_DIR/.ci/ci-script.sh"; fi
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then docker run --read-only --volume $TRAVIS_BUILD_DIR:$SRC_DIR:ro --tmpfs $BUILD_DIR:exec --workdir $BUILD_DIR --tmpfs $INSTALL_PREFIX $EXTRA_TMPFS --env CC --env CXX --env CFLAGS --env CXXFLAGS --env SRC_DIR --env BUILD_DIR --env INSTALL_PREFIX --env TARGET --env ECO darktable/darktable sh -c "$SRC_DIR/.ci/ci-script.sh"; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then
export SRC_DIR="$TRAVIS_BUILD_DIR";
export BUILD_DIR="$SRC_DIR/build";
Expand Down
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,12 @@ if(NOT SOURCE_PACKAGE AND NOT (CMAKE_VERSION VERSION_LESS 3.3) AND DEFINED ENV{_
endif()
endif()

if(BUILD_TESTING)
find_package(cmocka 1.1.0 CONFIG REQUIRED)
include(AddCMockaTest)
include(AddCMockaMockTest)
endif(BUILD_TESTING)

#
# Test SSE level
#
Expand Down
1 change: 0 additions & 1 deletion DefineOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ option(BUILD_PRINT "Build the print module" ON)
option(BUILD_RS_IDENTIFY "Build the darktable-rs-identify debug aid" ON)
option(BUILD_SSE2_CODEPATHS "(EXPERIMENTAL OPTION, DO NOT DISABLE) Building SSE2-optimized codepaths" ON)
option(VALIDATE_APPDATA_FILE "Use appstream-util (if found) to validate the .appdata file" OFF)
option(BUILD_TESTS "Build tests in src/tests/, runnable from the build/ directory" OFF)
houz marked this conversation as resolved.
Show resolved Hide resolved
option(BUILD_BATTERY_INDICATOR "Add an icon to the top toolbar showing the state of a laptop battery" OFF)
option(BUILD_MSYS2_INSTALL "Build an MSYS2 version of the install, aka for Windows platform, but without dependency installs" OFF)
option(BUILD_NOISE_TOOLS "Build tools for generating noise proifles" OFF)
Expand Down
56 changes: 56 additions & 0 deletions cmake/modules/AddCMockaMockTest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# MIT License
#
# Copyright (c) 2018 Kamil Lorenc
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

## Add unit test with mocking support
# \param name unit test name (excluding extension and 'test_' prefix)
# \param SOURCES optional list of source files to include in test executable
# (beside test_${name}.c)
# \param MOCKS optional list of functions to be mocked in executable
# \param COMPILE_OPTIONS optional list of options for the compiler
# \param LINK_LIBRARIES optional list of libraries to link (used as
# -l${LINK_LIBRARIES})
# \param LINK_OPTIONS optional list of options to be passed to linker
function(add_cmocka_mock_test name)
# parse arguments passed to the function
set(options )
set(oneValueArgs )
set(multiValueArgs SOURCES MOCKS COMPILE_OPTIONS LINK_LIBRARIES LINK_OPTIONS)
cmake_parse_arguments(ADD_MOCKED_TEST "${options}" "${oneValueArgs}"
"${multiValueArgs}" ${ARGN} )

# create link flags for mocks
set(link_flags "")
foreach (mock ${ADD_MOCKED_TEST_MOCKS})
set(link_flags "${link_flags} -Wl,--wrap=${mock}")
endforeach(mock)

# define test
add_cmocka_test(${name}
SOURCES ${ADD_MOCKED_TEST_SOURCES}
COMPILE_OPTIONS ${DEFAULT_C_COMPILE_FLAGS}
${ADD_MOCKED_TEST_COMPILE_OPTIONS}
LINK_LIBRARIES ${ADD_MOCKED_TEST_LINK_LIBRARIES}
LINK_OPTIONS ${link_flags} ${ADD_MOCKED_TEST_LINK_OPTIONS})

# allow using includes from src/ directory
target_include_directories(${name} PRIVATE ${CMAKE_SOURCE_DIR}/src)
endfunction(add_cmocka_mock_test)
142 changes: 142 additions & 0 deletions cmake/modules/AddCMockaTest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#
# Copyright (c) 2007 Daniel Gollub <[email protected]>
# Copyright (c) 2007-2018 Andreas Schneider <[email protected]>
# Copyright (c) 2018 Anderson Toshiyuki Sasaki <[email protected]>
#
# Redistribution and use is allowed according to the terms of the BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.

#.rst:
# AddCMockaTest
# -------------
#
# This file provides a function to add a test
#
# Functions provided
# ------------------
#
# ::
#
# add_cmocka_test(target_name
# SOURCES src1 src2 ... srcN
# [COMPILE_OPTIONS opt1 opt2 ... optN]
# [LINK_LIBRARIES lib1 lib2 ... libN]
# [LINK_OPTIONS lopt1 lop2 .. loptN]
# )
#
# ``target_name``:
# Required, expects the name of the test which will be used to define a target
#
# ``SOURCES``:
# Required, expects one or more source files names
#
# ``COMPILE_OPTIONS``:
# Optional, expects one or more options to be passed to the compiler
#
# ``LINK_LIBRARIES``:
# Optional, expects one or more libraries to be linked with the test
# executable.
#
# ``LINK_OPTIONS``:
# Optional, expects one or more options to be passed to the linker
#
#
# Example:
#
# .. code-block:: cmake
#
# add_cmocka_test(my_test
# SOURCES my_test.c other_source.c
# COMPILE_OPTIONS -g -Wall
# LINK_LIBRARIES mylib
# LINK_OPTIONS -Wl,--enable-syscall-fixup
# )
#
# Where ``my_test`` is the name of the test, ``my_test.c`` and
# ``other_source.c`` are sources for the binary, ``-g -Wall`` are compiler
# options to be used, ``mylib`` is a target of a library to be linked, and
# ``-Wl,--enable-syscall-fixup`` is an option passed to the linker.
#

enable_testing()
include(CTest)

if (CMAKE_CROSSCOMPILING)
if (WIN32)
find_program(WINE_EXECUTABLE
NAMES wine)
set(TARGET_SYSTEM_EMULATOR ${WINE_EXECUTABLE} CACHE INTERNAL "")
endif()
endif()

function(ADD_CMOCKA_TEST _TARGET_NAME)

set(one_value_arguments
)

set(multi_value_arguments
SOURCES
COMPILE_OPTIONS
LINK_LIBRARIES
LINK_OPTIONS
)

cmake_parse_arguments(_add_cmocka_test
""
"${one_value_arguments}"
"${multi_value_arguments}"
${ARGN}
)

if (NOT DEFINED _add_cmocka_test_SOURCES)
message(FATAL_ERROR "No sources provided for target ${_TARGET_NAME}")
endif()

add_executable(${_TARGET_NAME} ${_add_cmocka_test_SOURCES})

if (DEFINED _add_cmocka_test_COMPILE_OPTIONS)
target_compile_options(${_TARGET_NAME}
PRIVATE ${_add_cmocka_test_COMPILE_OPTIONS}
)
endif()

if (DEFINED _add_cmocka_test_LINK_LIBRARIES)
target_link_libraries(${_TARGET_NAME}
PRIVATE ${_add_cmocka_test_LINK_LIBRARIES}
)
endif()

if (DEFINED _add_cmocka_test_LINK_OPTIONS)
set_target_properties(${_TARGET_NAME}
PROPERTIES LINK_FLAGS
${_add_cmocka_test_LINK_OPTIONS}
)
endif()

add_test(NAME ${_TARGET_NAME}
COMMAND ${TARGET_SYSTEM_EMULATOR} ${_TARGET_NAME})

endfunction (ADD_CMOCKA_TEST)

function(ADD_CMOCKA_TEST_ENVIRONMENT _TARGET_NAME)
if (WIN32 OR CYGWIN OR MINGW OR MSVC)
file(TO_NATIVE_PATH "${cmocka-library_BINARY_DIR}" CMOCKA_DLL_PATH)

if (TARGET_SYSTEM_EMULATOR)
set(DLL_PATH_ENV "WINEPATH=${CMOCKA_DLL_PATH};$ENV{WINEPATH}")
else()
set(DLL_PATH_ENV "PATH=${CMOCKA_DLL_PATH};$ENV{PATH}")
endif()
#
# IMPORTANT NOTE: The set_tests_properties(), below, internally
# stores its name/value pairs with a semicolon delimiter.
# because of this we must protect the semicolons in the path
#
string(REPLACE ";" "\\;" DLL_PATH_ENV "${DLL_PATH_ENV}")

set_tests_properties(${_TARGET_NAME}
PROPERTIES
ENVIRONMENT
"${DLL_PATH_ENV}")
endif()
endfunction()
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -876,9 +876,9 @@ endif(BUILD_CMSTEST)
add_subdirectory(chart)

# have some test programs for unit testing, ...
if(BUILD_TESTS)
if(BUILD_TESTING)
add_subdirectory(tests)
endif(BUILD_TESTS)
endif(BUILD_TESTING)

#
# build darktable executable
Expand Down
7 changes: 1 addition & 6 deletions src/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/..)
include_directories(${CMAKE_CURRENT_BINARY_DIR}/..)

add_executable(darktable-test-variables variables.c)

set_target_properties(darktable-test-variables PROPERTIES INSTALL_RPATH "$ORIGIN/../")
set_target_properties(darktable-test-variables PROPERTIES LINKER_LANGUAGE C)
target_link_libraries(darktable-test-variables lib_darktable)

add_subdirectory(unittests)
8 changes: 8 additions & 0 deletions src/tests/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
add_cmocka_test(test_sample
SOURCES test_sample.c
LINK_LIBRARIES cmocka)

add_cmocka_mock_test(test_filmicrgb
SOURCES test_filmicrgb.c
LINK_LIBRARIES lib_darktable cmocka
MOCKS dt_iop_color_picker_reset)
Loading