Skip to content

Commit

Permalink
test: split tests based on context (#156)
Browse files Browse the repository at this point in the history
* test: split `test_download_package.cmake` into several files

Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>

* test: split `test_build_package.cmake` into several files

Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>

* test: simplify `test_install_package.cmake` file

Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>

* test: rename `test_project_integration.cmake`

Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>

---------

Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>
  • Loading branch information
threeal authored Oct 9, 2024
1 parent 31344a9 commit 4f6bed2
Show file tree
Hide file tree
Showing 13 changed files with 564 additions and 516 deletions.
12 changes: 8 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ if(CDEPS_ENABLE_TESTS)
enable_testing()

find_package(Assertion 1.0.0 REQUIRED)
assertion_add_test(test/test_build_package.cmake)
assertion_add_test(test/test_download_package.cmake)
assertion_add_test(test/test_install_package.cmake)
assertion_add_test(test/test_project_integration.cmake)
assertion_add_test(test/test_build_generator.cmake)
assertion_add_test(test/test_build_options.cmake)
assertion_add_test(test/test_build.cmake)
assertion_add_test(test/test_download_submodule.cmake)
assertion_add_test(test/test_download_version.cmake)
assertion_add_test(test/test_download.cmake)
assertion_add_test(test/test_install.cmake)
assertion_add_test(test/test_integration.cmake)
assertion_add_test(test/test_utilities.cmake)
endif()

Expand Down
83 changes: 0 additions & 83 deletions test/assert_helper.cmake

This file was deleted.

138 changes: 138 additions & 0 deletions test/test_build.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
include(${CMAKE_CURRENT_LIST_DIR}/../cmake/CDeps.cmake)

set(CMAKE_GENERATOR "Unix Makefiles")

set(CDEPS_ROOT .cdeps)
file(REMOVE_RECURSE .cdeps)

section("it should fail to build a package "
"because it has not been downloaded")
assert_fatal_error(
CALL cdeps_build_package pkg
MESSAGE "CDeps: pkg must be downloaded before building")
endsection()

file(WRITE .cdeps/pkg/src/CMakeLists.txt
"cmake_minimum_required(VERSION 3.5)\n"
"project(pkg LANGUAGES CXX)\n"
"add_executable(main main.cpp)\n")

file(WRITE .cdeps/pkg/src/main.cpp
"#include <iostream>\n"
"\n"
"int main() {\n"
" std::cout << \"Hello World!\\n\";\n"
"}\n")

file(WRITE .cdeps/pkg/src.lock "pkg github.com/user/pkg main")

section("it should build a package")
cdeps_build_package(pkg)

section("it should output the build directory variable")
assert(DEFINED pkg_BUILD_DIR)
assert(pkg_BUILD_DIR STREQUAL .cdeps/pkg/build)
endsection()

section("it should generate the lock file")
file(READ .cdeps/pkg/build.lock CONTENT)
assert(CONTENT MATCHES "^pkg github.com.user.pkg main")
endsection()

section("it should build to the build directory")
assert(EXISTS .cdeps/pkg/build/CMakeCache.txt)
endsection()

section("it should build the correct targets")
assert(EXISTS .cdeps/pkg/build/main)
assert_execute_process(.cdeps/pkg/build/main OUTPUT "Hello World!")
endsection()
endsection()

section("it should not rebuild the package")
block()
set(CMAKE_COMMAND invalid)
cdeps_build_package(pkg)
endblock()

section("it should maintain the lock file")
file(READ .cdeps/pkg/src.lock CONTENT)
assert(CONTENT MATCHES "^pkg github.com.user.pkg main")
endsection()

section("it should maintain the build directory")
assert(EXISTS .cdeps/pkg/build/CMakeCache.txt)
endsection()
endsection()

section("it should rebuild the package because of an invalidated lock file")
file(REMOVE_RECURSE .cdeps/pkg/build)
file(APPEND .cdeps/pkg/build.lock " invalidated")

cdeps_build_package(pkg)

section("it should regenerate the lock file")
file(READ .cdeps/pkg/build.lock CONTENT)
assert(CONTENT MATCHES "^pkg github.com.user.pkg main")
endsection()

section("it should rebuild to the build directory")
assert(EXISTS .cdeps/pkg/build/CMakeCache.txt)
endsection()
endsection()

section("it should fail to rebuild the package "
"because of a corrupted source file")
file(READ .cdeps/pkg/src/main.cpp ORIGINAL_MAIN_CPP)
file(WRITE .cdeps/pkg/src/main.cpp corrupted)
file(APPEND .cdeps/pkg/build.lock " invalidated")

assert_fatal_error(
CALL cdeps_build_package pkg
MESSAGE "CDeps: Failed to build pkg:")

section("it should remove the lock file")
assert(NOT EXISTS .cdeps/pkg/build.lock)
endsection()

section("it should remove the build directory")
assert(NOT EXISTS .cdeps/pkg/build)
endsection()

file(WRITE .cdeps/pkg/src/main.cpp "${ORIGINAL_MAIN_CPP}")
endsection()

section("it should fail to reconfigure the package "
"because of a corrupted CMakelists file")
file(READ .cdeps/pkg/src/CMakeLists.txt ORIGINAL_CMAKELISTS_TXT)
file(WRITE .cdeps/pkg/src/CMakeLists.txt corrupted)

assert_fatal_error(
CALL cdeps_build_package pkg
MESSAGE "CDeps: Failed to configure pkg:")

section("it should remove the lock file")
assert(NOT EXISTS .cdeps/pkg/build.lock)
endsection()

section("it should remove the build directory")
assert(NOT EXISTS .cdeps/pkg/build)
endsection()

file(WRITE .cdeps/pkg/src/CMakeLists.txt "${ORIGINAL_CMAKELISTS_TXT}")
endsection()

section("it should rebuild the package after a failure")
cdeps_build_package(pkg)

section("it should regenerate the lock file")
file(READ .cdeps/pkg/build.lock CONTENT)
assert(CONTENT MATCHES "^pkg github.com.user.pkg main")
endsection()

section("it should rebuild to the build directory")
assert(EXISTS .cdeps/pkg/build/CMakeCache.txt)
endsection()
endsection()

file(REMOVE_RECURSE .cdeps)
82 changes: 82 additions & 0 deletions test/test_build_generator.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
include(${CMAKE_CURRENT_LIST_DIR}/../cmake/CDeps.cmake)

set(CDEPS_ROOT .cdeps)
file(REMOVE_RECURSE .cdeps)

file(WRITE .cdeps/pkg/src/CMakeLists.txt
"cmake_minimum_required(VERSION 3.5)\n"
"project(pkg LANGUAGES NONE)\n"
"set(GENERATOR \"\${CMAKE_GENERATOR}\" CACHE STRING \"\" FORCE)\n")

file(WRITE .cdeps/pkg/src.lock "pkg github.com/user/pkg main")

section("it should build a package with the default generator")
cdeps_build_package(pkg)

section("it should generate the lock file")
file(READ .cdeps/pkg/build.lock CONTENT)
assert(CONTENT MATCHES [^GENERATOR])
endsection()
endsection()

section("it should fail to rebuild the package "
"because of an invalid parent generator")
block()
set(CMAKE_GENERATOR invalid)
assert_fatal_error(
CALL cdeps_build_package pkg
MESSAGE "CDeps: Failed to configure pkg:")
endblock()

section("it should remove the lock file")
assert(NOT EXISTS .cdeps/pkg/build.lock)
endsection()
endsection()

section("it should rebuild the package with the parent generator")
block()
set(CMAKE_GENERATOR "Unix Makefiles")
cdeps_build_package(pkg)
endblock()

section("it should regenerate the lock file")
file(READ .cdeps/pkg/build.lock CONTENT)
assert(CONTENT MATCHES "GENERATOR Unix Makefiles$")
endsection()

section("it should rebuild with the correct generator")
assert_execute_process(
COMMAND ${CMAKE_COMMAND} -L -N .cdeps/pkg/build
OUTPUT "GENERATOR:STRING=Unix Makefiles")
endsection()
endsection()

section("it should fail to rebuild the package "
"because of an invalid specified generator")
block()
assert_fatal_error(
CALL cdeps_build_package pkg GENERATOR invalid
MESSAGE "CDeps: Failed to configure pkg:")
endblock()

section("it should remove the lock file")
assert(NOT EXISTS .cdeps/pkg/build.lock)
endsection()
endsection()

section("it should rebuild the package with the specified generator")
cdeps_build_package(pkg GENERATOR "Unix Makefiles")

section("it should regenerate the lock file")
file(READ .cdeps/pkg/build.lock CONTENT)
assert(CONTENT MATCHES "GENERATOR Unix Makefiles$")
endsection()

section("it should rebuild with the correct generator")
assert_execute_process(
COMMAND ${CMAKE_COMMAND} -L -N .cdeps/pkg/build
OUTPUT "GENERATOR:STRING=Unix Makefiles")
endsection()
endsection()

file(REMOVE_RECURSE .cdeps)
44 changes: 44 additions & 0 deletions test/test_build_options.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
include(${CMAKE_CURRENT_LIST_DIR}/../cmake/CDeps.cmake)

set(CDEPS_ROOT .cdeps)
file(REMOVE_RECURSE .cdeps)

file(WRITE .cdeps/pkg/src/CMakeLists.txt
"cmake_minimum_required(VERSION 3.5)\n"
"project(pkg LANGUAGES NONE)\n"
"set(FIRST_OPTION \"first\" CACHE STRING \"\")\n"
"set(SECOND_OPTION \"second\" CACHE STRING \"\")\n")

file(WRITE .cdeps/pkg/src.lock "pkg github.com/user/pkg main")

section("it should build a package without options")
cdeps_build_package(pkg)

section("it should generate the lock file")
file(READ .cdeps/pkg/build.lock CONTENT)
assert(CONTENT MATCHES [^OPTIONS])
endsection()

section("it should build with the correct options")
assert_execute_process(
COMMAND ${CMAKE_COMMAND} -L -N .cdeps/pkg/build
OUTPUT "FIRST_OPTION:STRING=first.+SECOND_OPTION:STRING=second")
endsection()
endsection()

section("it should rebuild the package with the specified options")
cdeps_build_package(pkg OPTIONS FIRST_OPTION=pertama SECOND_OPTION=kedua)

section("it should regenerate the lock file")
file(READ .cdeps/pkg/build.lock CONTENT)
assert(CONTENT MATCHES "OPTIONS FIRST_OPTION=pertama;SECOND_OPTION=kedua$")
endsection()

section("it should rebuild with the correct options")
assert_execute_process(
COMMAND ${CMAKE_COMMAND} -L -N .cdeps/pkg/build
OUTPUT "FIRST_OPTION:STRING=pertama.+SECOND_OPTION:STRING=kedua")
endsection()
endsection()

file(REMOVE_RECURSE .cdeps)
Loading

0 comments on commit 4f6bed2

Please sign in to comment.