]> git.saurik.com Git - apt.git/commitdiff
CMake: Add unit tests
authorJulian Andres Klode <jak@debian.org>
Sun, 7 Aug 2016 16:22:14 +0000 (18:22 +0200)
committerJulian Andres Klode <jak@debian.org>
Wed, 10 Aug 2016 14:11:43 +0000 (16:11 +0200)
Add support for our GTest based unit tests. By default, CMake will
look in /usr/src/gtest for the external GTest project, but this can
be overriden by defining GTEST_ROOT when invoking cmake.

Gbp-Dch: ignore

.travis.yml
CMakeLists.txt
README.cmake
test/CMakeLists.txt
test/libapt/CMakeLists.txt [new file with mode: 0644]
test/libapt/fileutl_test.cc
test/libapt/hashsums_test.cc

index 755640831f9ad7b90e3a8d0c0af226bb0ab45cf2..3c4b5931908c55c460901a89ba21a223d29bf146 100644 (file)
@@ -16,6 +16,7 @@ before_script:
  - ( mkdir build && cd build && cmake .. )
  - make -C build -j4
 script:
+ - make -C build test
  - ./test/integration/run-tests -q
  - sudo adduser --force-badname --system --home /nonexistent --no-create-home --quiet _apt || true
  - sudo ./test/integration/run-tests -q
index 6baa8f14f3e8507368ab69c8868008a957dbaaf0..9e85e7b8401c19124ac6e020f5fb24422662f5c2 100644 (file)
@@ -5,6 +5,8 @@
 project(apt)
 cmake_minimum_required(VERSION 3.3.0)
 
+enable_testing()
+
 option(WITH_DOC "Build documentation." OFF)
 option(USE_NLS "Localisation support." ON)
 
index 0ffd08efc06d24da5c9cb217415ddc95ae1c38a0..5afe0449fa822492a20632450bbcfdca320f213d 100644 (file)
@@ -32,4 +32,3 @@ TODO
 The following features have not been implemented yet:
 
  - Translated docbook guides
- - unit tests
index 599f62aff541d078996f7694338a3cd1529e3f84..2f47b5c32ab43b5c8f6fa1ef5ea145dd07629ebe 100644 (file)
@@ -1 +1,2 @@
+add_subdirectory(libapt)
 add_subdirectory(interactive-helper)
diff --git a/test/libapt/CMakeLists.txt b/test/libapt/CMakeLists.txt
new file mode 100644 (file)
index 0000000..7f99905
--- /dev/null
@@ -0,0 +1,22 @@
+include(ExternalProject)
+
+set(GTEST_ROOT "/usr/src/gtest" CACHE FILEPATH "Path to GTest CMake project")
+
+message(STATUS "Found GTest at ${GTEST_ROOT}")
+
+if (EXISTS ${GTEST_ROOT})
+
+ExternalProject_Add(gtest PREFIX ./gtest
+                          SOURCE_DIR ${GTEST_ROOT}
+                          INSTALL_COMMAND true)
+
+link_directories(${CMAKE_CURRENT_BINARY_DIR}/gtest/src/gtest-build)
+FILE(GLOB files gtest_runner.cc *-helpers.cc *_test.cc)
+add_executable(libapt_test ${files})
+target_link_libraries(libapt_test -lgtest ${CMAKE_THREAD_LIBS_INIT} apt-private apt-inst)
+add_dependencies(libapt_test gtest)
+add_test(NAME AptTests
+         COMMAND libapt_test
+         WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
+
+endif()
index 0a299b51af045f1c577b3bfe14eed7759f5ef5c6..67bd850a123811149d72ab7c5b1c919fac1ffeea 100644 (file)
@@ -216,7 +216,7 @@ TEST(FileUtlTest, Glob)
 {
    std::vector<std::string> files;
    // normal match
-   files = Glob("*akefile");
+   files = Glob("*MakeLists.txt");
    EXPECT_EQ(1, files.size());
 
    // not there
index 705c0297d3c0cab8ebf6e0871df7926abe891db1..fb9a25643aefd55c86c120510b89c30fae801cda 100644 (file)
@@ -119,7 +119,7 @@ static void getSummationString(char const * const type, std::string &sum)
    FileFd fd;
    ASSERT_TRUE(fd.Open(tempfile, FileFd::WriteOnly | FileFd::Empty, compress));
    ASSERT_TRUE(fd.IsOpen());
-   FileFd input(__FILE__, FileFd::ReadOnly);
+   FileFd input("/etc/os-release", FileFd::ReadOnly);
    ASSERT_TRUE(input.IsOpen());
    ASSERT_NE(0, input.FileSize());
    ASSERT_TRUE(CopyFile(input, fd));
@@ -161,7 +161,7 @@ TEST(HashSumsTest, FileBased)
    SHA512SumValue sha512(summation);
    EXPECT_EQ(sha512.Value(), summation);
 
-   FileFd fd(__FILE__, FileFd::ReadOnly);
+   FileFd fd("/etc/os-release", FileFd::ReadOnly);
    EXPECT_TRUE(fd.IsOpen());
    std::string FileSize;
    strprintf(FileSize, "%llu", fd.FileSize());
@@ -243,18 +243,18 @@ TEST(HashSumsTest, FileBased)
    fd.Close();
 
    HashString sha2file("SHA512", sha512.Value());
-   EXPECT_TRUE(sha2file.VerifyFile(__FILE__));
+   EXPECT_TRUE(sha2file.VerifyFile("/etc/os-release"));
    HashString sha2wrong("SHA512", "00000000000");
-   EXPECT_FALSE(sha2wrong.VerifyFile(__FILE__));
+   EXPECT_FALSE(sha2wrong.VerifyFile("/etc/os-release"));
    EXPECT_EQ(sha2file, sha2file);
    EXPECT_TRUE(sha2file == sha2file);
    EXPECT_NE(sha2file, sha2wrong);
    EXPECT_TRUE(sha2file != sha2wrong);
 
    HashString sha2big("SHA256", sha256.Value());
-   EXPECT_TRUE(sha2big.VerifyFile(__FILE__));
+   EXPECT_TRUE(sha2big.VerifyFile("/etc/os-release"));
    HashString sha2small("sha256:" + sha256.Value());
-   EXPECT_TRUE(sha2small.VerifyFile(__FILE__));
+   EXPECT_TRUE(sha2small.VerifyFile("/etc/os-release"));
    EXPECT_EQ(sha2big, sha2small);
    EXPECT_TRUE(sha2big == sha2small);
    EXPECT_FALSE(sha2big != sha2small);
@@ -283,7 +283,7 @@ TEST(HashSumsTest, FileBased)
    EXPECT_EQ(2, hashes.size());
    EXPECT_FALSE(hashes.push_back(sha2wrong));
    EXPECT_EQ(2, hashes.size());
-   EXPECT_TRUE(hashes.VerifyFile(__FILE__));
+   EXPECT_TRUE(hashes.VerifyFile("/etc/os-release"));
 
    EXPECT_EQ(similar, hashes);
    EXPECT_TRUE(similar == hashes);