Merge pull request #33 from linuxdeploy/tests

Add Unit Tests
This commit is contained in:
TheAssassin
2018-10-25 01:26:46 +02:00
committed by GitHub
19 changed files with 259 additions and 5 deletions

1
.gitignore vendored
View File

@@ -2,5 +2,4 @@ cmake-build-*/
*build*/
.idea/
squashfs-root/
tests/
*.AppImage

3
.gitmodules vendored
View File

@@ -76,3 +76,6 @@
[submodule "lib/CImg"]
path = lib/CImg
url = https://github.com/dtschump/CImg.git
[submodule "lib/googletest"]
path = lib/googletest
url = https://github.com/google/googletest

View File

@@ -44,7 +44,7 @@ script:
after_success:
- ls -lh
# make sure only pushes to rewrite create a new release, otherwise pretend PR and upload to transfer.sh
- if [ "$TRAVIS_BRANCH" != "master" ]; then export TRAVIS_EVENT_TYPE=pull_request; fi
- if [ "$TRAVIS_TAG" != "$TRAVIS_BRANCH" ] && [ "$TRAVIS_BRANCH" == "master" ]; then export TRAVIS_EVENT_TYPE=pull_request; fi
- wget -c https://github.com/probonopd/uploadtool/raw/master/upload.sh
- bash upload.sh linuxdeploy-"$ARCH".AppImage*

View File

@@ -13,3 +13,8 @@ set(USE_SYSTEM_CIMG ON CACHE BOOL "Set to OFF to use CImg library bundled in lib
add_subdirectory(lib)
add_subdirectory(src)
include(CTest)
if(BUILD_TESTING)
add_subdirectory(tests)
endif()

View File

@@ -3,3 +3,6 @@ set(CMAKE_SYSTEM_PROCESSOR i386 CACHE STRING "" FORCE)
set(CMAKE_C_FLAGS "-m32" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS "-m32" CACHE STRING "" FORCE)
# https://gitlab.kitware.com/cmake/cmake/issues/16920#note_299077
set(THREADS_PTHREAD_ARG "2" CACHE STRING "Forcibly set by CMakeLists.txt" FORCE)

View File

@@ -1,3 +1,5 @@
include(CTest)
add_library(subprocess INTERFACE)
target_sources(subprocess INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/cpp-subprocess/subprocess.hpp)
target_include_directories(subprocess INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/cpp-subprocess)
@@ -11,9 +13,11 @@ add_library(cpp-feather-ini-parser INTERFACE)
target_sources(cpp-feather-ini-parser INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/cpp-feather-ini-parser/INI.h)
target_include_directories(cpp-feather-ini-parser INTERFACE cpp-feather-ini-parser)
add_executable(test_cpp_feather_ini_parser EXCLUDE_FROM_ALL ${CMAKE_CURRENT_SOURCE_DIR}/cpp-feather-ini-parser/example/example.cpp)
target_link_libraries(test_cpp_feather_ini_parser PRIVATE cpp-feather-ini-parser)
add_test(test_cpp_feather_ini_parser test_cpp_feather_ini_parser)
if(BUILD_TESTING)
add_executable(test_cpp_feather_ini_parser ${CMAKE_CURRENT_SOURCE_DIR}/cpp-feather-ini-parser/example/example.cpp)
target_link_libraries(test_cpp_feather_ini_parser PRIVATE cpp-feather-ini-parser)
add_test(test_cpp_feather_ini_parser test_cpp_feather_ini_parser)
endif()
if(NOT USE_SYSTEM_BOOST)
add_library(boost_config INTERFACE)
@@ -108,3 +112,7 @@ if(NOT USE_SYSTEM_BOOST)
boost_type_traits boost_static_assert boost_integer boost_preprocessor boost_functional boost_detail
)
endif()
if(BUILD_TESTING)
add_subdirectory(googletest)
endif()

1
lib/googletest Submodule

Submodule lib/googletest added at 3bb00b7ead

6
tests/CMakeLists.txt Normal file
View File

@@ -0,0 +1,6 @@
# first build dependencies for tests
add_subdirectory(simple_library)
add_subdirectory(simple_executable)
# now include actual tests
add_subdirectory(core)

18
tests/core/CMakeLists.txt Normal file
View File

@@ -0,0 +1,18 @@
add_executable(test_appdir test_appdir.cpp)
target_link_libraries(test_appdir PRIVATE linuxdeploy_core gtest)
target_include_directories(test_appdir PRIVATE ${PROJECT_SOURCE_DIR}/include)
# calculate paths to resources using CMake and hardcode them in the test binary
target_compile_definitions(test_appdir PRIVATE
-DSIMPLE_LIBRARY_PATH="$<TARGET_FILE:simple_library>"
-DSIMPLE_EXECUTABLE_PATH="$<TARGET_FILE:simple_executable>"
-DSIMPLE_DESKTOP_ENTRY_PATH="${CMAKE_CURRENT_SOURCE_DIR}/../data/simple_app.desktop"
-DSIMPLE_ICON_PATH="${CMAKE_CURRENT_SOURCE_DIR}/../data/simple_icon.svg"
-DSIMPLE_FILE_PATH="${CMAKE_CURRENT_SOURCE_DIR}/../data/simple_file.txt"
)
# register in CTest
add_test(test_appdir test_appdir)
# make sure library and executable are built before test_appdir
add_dependencies(test_appdir simple_library simple_executable)

129
tests/core/test_appdir.cpp Normal file
View File

@@ -0,0 +1,129 @@
#include "gtest/gtest.h"
#include "linuxdeploy/core/appdir.h"
using namespace linuxdeploy::core::appdir;
using namespace linuxdeploy::core::desktopfile;
using namespace boost::filesystem;
namespace AppDirTest {
class AppDirUnitTestsFixture : public ::testing::Test {
public:
path tmpAppDir;
AppDir appDir;
public:
AppDirUnitTestsFixture() :
tmpAppDir(temp_directory_path() / unique_path("linuxdeploy-tests-%%%%-%%%%-%%%%")),
appDir(tmpAppDir) {
}
void SetUp() override {
}
void TearDown() override {
remove_all(tmpAppDir);
}
~AppDirUnitTestsFixture() override = default;
void listDeployedFiles() {
std::cout << "Files deployed in AppDir:";
recursive_directory_iterator end_itr; // default construction yields past-the-end
for (recursive_directory_iterator itr(tmpAppDir); itr != end_itr; itr++) {
std::cout << relative(itr->path(), tmpAppDir).string() << std::endl;
}
}
};
TEST_F(AppDirUnitTestsFixture, createBasicStructure) {
// in this test case we expect the following exact directory set to be created in the AppDir
std::set<std::string> expected = {
"usr",
"usr/bin",
"usr/share",
"usr/share/icons",
"usr/share/icons/hicolor",
"usr/share/icons/hicolor/scalable",
"usr/share/icons/hicolor/scalable/apps",
"usr/share/icons/hicolor/32x32",
"usr/share/icons/hicolor/32x32/apps",
"usr/share/icons/hicolor/256x256",
"usr/share/icons/hicolor/256x256/apps",
"usr/share/icons/hicolor/16x16",
"usr/share/icons/hicolor/16x16/apps",
"usr/share/icons/hicolor/128x128",
"usr/share/icons/hicolor/128x128/apps",
"usr/share/icons/hicolor/64x64",
"usr/share/icons/hicolor/64x64/apps",
"usr/share/applications",
"usr/lib",
};
appDir.createBasicStructure();
recursive_directory_iterator end_itr; // default construction yields past-the-end
for (recursive_directory_iterator itr(tmpAppDir); itr != end_itr; itr++) {
std::string path = relative(itr->path(), tmpAppDir).string();
ASSERT_NE(expected.find(path), expected.end());
expected.erase(path);
}
ASSERT_TRUE(expected.empty());
}
TEST_F(AppDirUnitTestsFixture, depoloyLibraryWrongPath) {
ASSERT_THROW(appDir.deployLibrary("/lib/fakelib.so"), std::exception);
}
TEST_F(AppDirUnitTestsFixture, depoloyLibrary) {
appDir.deployLibrary(SIMPLE_LIBRARY_PATH);
appDir.executeDeferredOperations();
ASSERT_TRUE(is_regular_file(tmpAppDir / "usr/lib" / path(SIMPLE_LIBRARY_PATH).filename()));
}
TEST_F(AppDirUnitTestsFixture, deployExecutable) {
appDir.deployExecutable(SIMPLE_EXECUTABLE_PATH);
appDir.executeDeferredOperations();
ASSERT_TRUE(is_regular_file(tmpAppDir / "usr/bin" / path(SIMPLE_EXECUTABLE_PATH).filename()));
ASSERT_TRUE(is_regular_file(tmpAppDir / "usr/lib" / path(SIMPLE_LIBRARY_PATH).filename()));
}
TEST_F(AppDirUnitTestsFixture, deployDesktopFile) {
DesktopFile desktopFile{SIMPLE_DESKTOP_ENTRY_PATH};
appDir.deployDesktopFile(desktopFile);
appDir.executeDeferredOperations();
ASSERT_TRUE(is_regular_file(tmpAppDir / "usr/share/applications" / path(SIMPLE_DESKTOP_ENTRY_PATH).filename()));
}
TEST_F(AppDirUnitTestsFixture, deployIcon) {
appDir.deployIcon(SIMPLE_ICON_PATH);
appDir.executeDeferredOperations();
ASSERT_TRUE(is_regular_file(tmpAppDir / "usr/share/icons/hicolor/scalable/apps" / path(SIMPLE_ICON_PATH).filename()));
}
TEST_F(AppDirUnitTestsFixture, deployFileToDirectory) {
auto destination = tmpAppDir / "usr/share/doc/simple_application/";
appDir.deployFile(SIMPLE_FILE_PATH, destination);
appDir.executeDeferredOperations();
ASSERT_TRUE(is_regular_file(destination / path(SIMPLE_FILE_PATH).filename()));
}
TEST_F(AppDirUnitTestsFixture, deployFileToAbsoluteFilePath) {
auto destination = tmpAppDir / "usr/share/doc/simple_application/test123";
appDir.deployFile(SIMPLE_FILE_PATH, destination);
appDir.executeDeferredOperations();
ASSERT_TRUE(is_regular_file(destination));
}
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@@ -0,0 +1,19 @@
[Desktop Entry]
Version=1.0
Type=Application
Name=Simple Application
Comment=The most simple application available!
TryExec=simple_executable
Exec=simple_executable %F
Icon=simple_icon
MimeType=image/x-foo;
Actions=SimpleAction;AnotherSimpleAction;
[Desktop Action SimpleAction]
Exec=simple_executable --do-it
Name=Do something simple!
[Desktop Action AnotherSimpleAction]
Exec=simple_executable --do-it-again
Name=Do another simple thing!
Icon=simple_icon

View File

@@ -0,0 +1 @@
Hello World!

View File

@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
id="svg8"
version="1.1"
viewBox="0 0 4.2333332 4.2333335"
height="16"
width="16">
<defs
id="defs2" />
<metadata
id="metadata5">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
transform="translate(0,-292.76665)"
id="layer1">
<rect
width="4.2333322"
height="4.2333322"
y="292.76666"
x="6.0100706e-07"
id="rect815"
style="opacity:1;fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:#000000;stroke-width:0;stroke-linecap:square;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;paint-order:markers fill stroke" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@@ -0,0 +1,2 @@
add_executable(simple_executable simple_executable.cpp)
target_link_libraries(simple_executable simple_library pthread)

View File

@@ -0,0 +1,9 @@
#include <cstdio>
#include <simple_library.h>
int main() {
printf("Hello World");
hello_world();
return 0;
}

View File

@@ -0,0 +1,3 @@
add_library(simple_library SHARED simple_library.cpp simple_library.h)
target_link_libraries(simple_library PUBLIC CImg)
target_include_directories(simple_library PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

View File

@@ -0,0 +1,6 @@
#include "CImg.h"
using namespace cimg_library;
void hello_world() {
cimg::info();
}

View File

@@ -0,0 +1 @@
void hello_world();

View File

@@ -39,6 +39,9 @@ cmake "$REPO_ROOT" -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=RelWithDebInfo
make -j$(nproc)
## Run Unit Tests
ctest -V
# args are used more than once
LINUXDEPLOY_ARGS=("--appdir" "AppDir" "-e" "bin/linuxdeploy" "-i" "$REPO_ROOT/resources/linuxdeploy.png" "--create-desktop-file" "-e" "/usr/bin/patchelf" "-e" "/usr/bin/strip")