summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2020-05-11 11:28:13 +0100
committerEdward Thomson <ethomson@edwardthomson.com>2020-06-05 07:14:25 +0100
commit98456aaa70306033cd7eb46516f0b8d0f012f062 (patch)
tree81510282d6731d32a499f6d103ca85005cb3c35e
parent13aa30e479a662086b09a90b611fb62426199656 (diff)
downloadlibgit2-98456aaa70306033cd7eb46516f0b8d0f012f062.tar.gz
libgit2client tests: add a test framework for the client library
-rw-r--r--tests/CMakeLists.txt1
-rw-r--r--tests/libgit2client/CMakeLists.txt65
-rw-r--r--tests/libgit2client/core/init.c54
-rw-r--r--tests/libgit2client/git2client_tests.h7
-rw-r--r--tests/libgit2client/global.c17
-rw-r--r--tests/libgit2client/precompiled.c1
-rw-r--r--tests/libgit2client/precompiled.h3
7 files changed, 148 insertions, 0 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 3bff439f9..f83050790 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -7,3 +7,4 @@ function(add_clar_test name)
endfunction(add_clar_test)
add_subdirectory(libgit2)
+add_subdirectory(libgit2client)
diff --git a/tests/libgit2client/CMakeLists.txt b/tests/libgit2client/CMakeLists.txt
new file mode 100644
index 000000000..0eb78892a
--- /dev/null
+++ b/tests/libgit2client/CMakeLists.txt
@@ -0,0 +1,65 @@
+find_package(PythonInterp)
+
+if(NOT PYTHONINTERP_FOUND)
+ message(FATAL_ERROR "Could not find a python interpeter, which is needed to build the tests. "
+ "Make sure python is available, or pass -DBUILD_CLAR=OFF to skip building the tests")
+endif()
+
+get_property(LIBGIT2CLIENT_OBJECTS GLOBAL PROPERTY libgit2client_objects)
+get_property(LIBGIT2CLIENT_INCLUDES GLOBAL PROPERTY libgit2client_includes)
+get_property(LIBGIT2CLIENT_SYSTEM_INCLUDES GLOBAL PROPERTY libgit2client_system_includes)
+get_property(LIBGIT2CLIENT_LIBS GLOBAL PROPERTY libgit2client_libs)
+
+set(CLAR_SOURCE_DIR "${libgit2_SOURCE_DIR}/tests/clar")
+set(CLAR_FIXTURES "${libgit2_SOURCE_DIR}/tests/resources/")
+add_definitions(-DCLAR_FIXTURE_PATH=\"${CLAR_FIXTURES}\")
+add_definitions(-DCLAR_TMPDIR=\"libgit2client_tests\")
+add_definitions(-D_FILE_OFFSET_BITS=64)
+
+# Ensure that we do not use deprecated functions internally
+add_definitions(-DGIT_DEPRECATE_HARD)
+
+include_directories(${CLAR_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR} ${libgit2_BINARY_DIR}/src)
+file(GLOB_RECURSE SRC_CASES */*.c */*.h)
+list(SORT SRC_CASES)
+
+file(GLOB_RECURSE SRC_CLAR "${CLAR_SOURCE_DIR}/*.c" "${CLAR_SOURCE_DIR}/*.h")
+list(SORT SRC_CLAR)
+
+set(SRC_LOCAL global.c)
+
+if(MSVC_IDE)
+ list(APPEND SRC_LOCAL "precompiled.c")
+endif()
+
+add_custom_command(
+ OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/clar.suite
+ COMMAND ${PYTHON_EXECUTABLE} "${CLAR_SOURCE_DIR}/generate.py" -o "${CMAKE_CURRENT_BINARY_DIR}" -f -xonline -xstress -xperf .
+ DEPENDS ${SRC_CASES}
+ WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+)
+include_directories(${CMAKE_CURRENT_BINARY_DIR})
+
+set_source_files_properties(
+ "${CLAR_SOURCE_DIR}/clar.c"
+ PROPERTIES OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/clar.suite)
+
+include_directories(${LIBGIT2CLIENT_INCLUDES})
+include_directories(SYSTEM ${LIBGIT2CLIENT_SYSTEM_INCLUDES})
+
+add_executable(libgit2client_tests ${SRC_LOCAL} ${SRC_CLAR} ${SRC_CASES} ${LIBGIT2CLIENT_OBJECTS})
+
+set_target_properties(libgit2client_tests PROPERTIES C_STANDARD 90)
+set_target_properties(libgit2client_tests PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${libgit2_BINARY_DIR})
+
+target_include_directories(libgit2client_tests PRIVATE "${libgit2_SOURCE_DIR}/src/util" "${libgit2_SOURCE_DIR}/src/libgit2" PUBLIC "${libgit2_SOURCE_DIR}/include")
+target_link_libraries(libgit2client_tests libgit2_meta ${LIBGIT2CLIENT_LIBS})
+ide_split_sources(libgit2client_tests)
+
+if(MSVC_IDE)
+ # Precompiled headers
+ set_target_properties(libgit2client_tests PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h")
+ set_source_files_properties("precompiled.c" COMPILE_FLAGS "/Ycprecompiled.h")
+endif()
+
+add_clar_test(client -v)
diff --git a/tests/libgit2client/core/init.c b/tests/libgit2client/core/init.c
new file mode 100644
index 000000000..7ada100cc
--- /dev/null
+++ b/tests/libgit2client/core/init.c
@@ -0,0 +1,54 @@
+#include "git2client_tests.h"
+
+void test_core_init__returns_count(void)
+{
+ /* libgit2client_clar initializes us first, so we have an
+ * existing initialization.
+ */
+ cl_assert_equal_i(2, git_client_init());
+ cl_assert_equal_i(3, git_client_init());
+
+ cl_assert_equal_i(2, git_client_shutdown());
+ cl_assert_equal_i(1, git_client_shutdown());
+}
+
+void test_core_init__reinit_succeeds(void)
+{
+ cl_assert_equal_i(0, git_client_shutdown());
+ cl_assert_equal_i(1, git_client_init());
+ cl_sandbox_set_search_path_defaults();
+}
+
+#ifdef GIT_THREADS
+static void *reinit(void *unused)
+{
+ unsigned i;
+
+ for (i = 0; i < 20; i++) {
+ cl_assert(git_libgit2_init() > 0);
+ cl_assert(git_libgit2_shutdown() >= 0);
+ }
+
+ return unused;
+}
+#endif
+
+void test_core_init__concurrent_init_succeeds(void)
+{
+#ifdef GIT_THREADS
+ git_thread threads[10];
+ unsigned i;
+
+ cl_assert_equal_i(2, git_client_init());
+
+ for (i = 0; i < ARRAY_SIZE(threads); i++)
+ git_thread_create(&threads[i], reinit, NULL);
+ for (i = 0; i < ARRAY_SIZE(threads); i++)
+ git_thread_join(&threads[i], NULL);
+
+ cl_assert_equal_i(1, git_client_shutdown());
+ cl_sandbox_set_search_path_defaults();
+#else
+ cl_skip();
+#endif
+}
diff --git a/tests/libgit2client/git2client_tests.h b/tests/libgit2client/git2client_tests.h
new file mode 100644
index 000000000..211fc171a
--- /dev/null
+++ b/tests/libgit2client/git2client_tests.h
@@ -0,0 +1,7 @@
+#ifndef GIT2CLIENT_TESTS__
+#define GIT2CLIENT_TESTS__
+
+#include <git2client.h>
+#include "clar_libgit2.h"
+
+#endif
diff --git a/tests/libgit2client/global.c b/tests/libgit2client/global.c
new file mode 100644
index 000000000..80a1271b7
--- /dev/null
+++ b/tests/libgit2client/global.c
@@ -0,0 +1,17 @@
+#include "clar_libgit2.h"
+#include "git2client_tests.h"
+
+int git_global_test_init(void)
+{
+ int res = git_client_init();
+
+ if (res < 0)
+ fprintf(stderr, "failed to init allocator");
+
+ return res;
+}
+
+void git_global_test_shutdown(void)
+{
+ git_client_shutdown();
+}
diff --git a/tests/libgit2client/precompiled.c b/tests/libgit2client/precompiled.c
new file mode 100644
index 000000000..5f656a45d
--- /dev/null
+++ b/tests/libgit2client/precompiled.c
@@ -0,0 +1 @@
+#include "precompiled.h"
diff --git a/tests/libgit2client/precompiled.h b/tests/libgit2client/precompiled.h
new file mode 100644
index 000000000..c4914ff16
--- /dev/null
+++ b/tests/libgit2client/precompiled.h
@@ -0,0 +1,3 @@
+#include "git2client.h"
+#include "clar_libgit2.h"
+#include "git2client_tests.h"