diff --git a/src/linuxdeploy.cpp b/src/linuxdeploy.cpp index 39ae85f..cc8e74d 100644 --- a/src/linuxdeploy.cpp +++ b/src/linuxdeploy.cpp @@ -13,68 +13,69 @@ using namespace linuxdeploy::core::log; using namespace linuxdeploy::util; namespace bf = boost::filesystem; +namespace linuxdeploy { + int deployAppDirRootFiles(args::ValueFlagList& desktopFilePaths, + args::ValueFlag& customAppRunPath, appdir::AppDir& appDir) { + // search for desktop file and deploy it to AppDir root + ldLog() << std::endl << "-- Deploying files into AppDir root directory --" << std::endl; -int deployAppDirRootFiles(args::ValueFlagList& desktopFilePaths, - args::ValueFlag& customAppRunPath, appdir::AppDir& appDir) { - // search for desktop file and deploy it to AppDir root - ldLog() << std::endl << "-- Deploying files into AppDir root directory --" << std::endl; - - if (is_regular_file(appDir.path() / "AppRun")) { - if (customAppRunPath) - ldLog() << LD_WARNING << "AppRun exists but custom AppRun specified, overwriting existing AppRun" - << std::endl; - else - ldLog() << LD_WARNING << "AppRun exists, skipping deployment" << std::endl; - } else { - auto deployedDesktopFiles = appDir.deployedDesktopFiles(); - - desktopfile::DesktopFile desktopFile; - - if (deployedDesktopFiles.empty()) { - ldLog() << LD_WARNING - << "Could not find desktop file in AppDir, cannot create links for AppRun, desktop file and icon in AppDir root" - << std::endl; + if (is_regular_file(appDir.path() / "AppRun")) { + if (customAppRunPath) + ldLog() << LD_WARNING << "AppRun exists but custom AppRun specified, overwriting existing AppRun" + << std::endl; + else + ldLog() << LD_WARNING << "AppRun exists, skipping deployment" << std::endl; } else { - if (!desktopFilePaths.Get().empty()) { - auto firstDeployedDesktopFileName = boost::filesystem::path( - desktopFilePaths.Get().front()).filename().string(); + auto deployedDesktopFiles = appDir.deployedDesktopFiles(); - auto desktopFileMatchingName = find_if( - deployedDesktopFiles.begin(), - deployedDesktopFiles.end(), - [&firstDeployedDesktopFileName](const desktopfile::DesktopFile& desktopFile) { - auto fileName = desktopFile.path().filename().string(); - return fileName == firstDeployedDesktopFileName; + desktopfile::DesktopFile desktopFile; + + if (deployedDesktopFiles.empty()) { + ldLog() << LD_WARNING + << "Could not find desktop file in AppDir, cannot create links for AppRun, desktop file and icon in AppDir root" + << std::endl; + } else { + if (!desktopFilePaths.Get().empty()) { + auto firstDeployedDesktopFileName = boost::filesystem::path( + desktopFilePaths.Get().front()).filename().string(); + + auto desktopFileMatchingName = find_if( + deployedDesktopFiles.begin(), + deployedDesktopFiles.end(), + [&firstDeployedDesktopFileName](const desktopfile::DesktopFile& desktopFile) { + auto fileName = desktopFile.path().filename().string(); + return fileName == firstDeployedDesktopFileName; + } + ); + + if (desktopFileMatchingName != deployedDesktopFiles.end()) { + desktopFile = *desktopFileMatchingName; + } else { + ldLog() << LD_ERROR << "Could not find desktop file deployed earlier any more:" + << firstDeployedDesktopFileName << std::endl; + return 1; } - ); - - if (desktopFileMatchingName != deployedDesktopFiles.end()) { - desktopFile = *desktopFileMatchingName; } else { - ldLog() << LD_ERROR << "Could not find desktop file deployed earlier any more:" - << firstDeployedDesktopFileName << std::endl; + desktopFile = deployedDesktopFiles[0]; + ldLog() << LD_WARNING << "No desktop file specified, using first desktop file found:" + << desktopFile.path() << std::endl; + } + + ldLog() << "Deploying desktop file:" << desktopFile.path() << std::endl; + + bool rv; + + if (customAppRunPath) { + rv = appDir.createLinksInAppDirRoot(desktopFile, customAppRunPath.Get()); + } else { + rv = appDir.createLinksInAppDirRoot(desktopFile); + } + + if (!rv) { return 1; } - } else { - desktopFile = deployedDesktopFiles[0]; - ldLog() << LD_WARNING << "No desktop file specified, using first desktop file found:" - << desktopFile.path() << std::endl; - } - - ldLog() << "Deploying desktop file:" << desktopFile.path() << std::endl; - - bool rv; - - if (customAppRunPath) { - rv = appDir.createLinksInAppDirRoot(desktopFile, customAppRunPath.Get()); - } else { - rv = appDir.createLinksInAppDirRoot(desktopFile); - } - - if (!rv) { - return 1; } } + return true; } - return true; } diff --git a/src/linuxdeploy.h b/src/linuxdeploy.h index 6cdf4be..0434b4b 100644 --- a/src/linuxdeploy.h +++ b/src/linuxdeploy.h @@ -7,7 +7,8 @@ #include #include #include - -int deployAppDirRootFiles(args::ValueFlagList& desktopFilePaths, - args::ValueFlag& customAppRunPath, - linuxdeploy::core::appdir::AppDir& appDir); +namespace linuxdeploy { + int deployAppDirRootFiles(args::ValueFlagList& desktopFilePaths, + args::ValueFlag& customAppRunPath, + linuxdeploy::core::appdir::AppDir& appDir); +} diff --git a/src/main.cpp b/src/main.cpp index 6fd93fe..0ccaa88 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -254,7 +254,7 @@ int main(int argc, char** argv) { return 1; } } - if (!deployAppDirRootFiles(desktopFilePaths, customAppRunPath, appDir)) + if (!linuxdeploy::deployAppDirRootFiles(desktopFilePaths, customAppRunPath, appDir)) return 1; if (outputPlugins) { diff --git a/tests/core/CMakeLists.txt b/tests/core/CMakeLists.txt index 58c3fc4..3c71c67 100644 --- a/tests/core/CMakeLists.txt +++ b/tests/core/CMakeLists.txt @@ -16,3 +16,24 @@ add_test(test_appdir test_appdir) # make sure library and executable are built before test_appdir add_dependencies(test_appdir simple_library simple_executable) + + + +add_executable(test_linuxdeploy test_linuxdeploy.cpp ../../src/linuxdeploy.cpp) +target_link_libraries(test_linuxdeploy PRIVATE linuxdeploy_core args gtest gtest_main) +target_include_directories(test_linuxdeploy PRIVATE ${PROJECT_SOURCE_DIR}/include ${PROJECT_SOURCE_DIR}/src) + +# calculate paths to resources using CMake and hardcode them in the test binary +target_compile_definitions(test_linuxdeploy PRIVATE + -DSIMPLE_LIBRARY_PATH="$" + -DSIMPLE_EXECUTABLE_PATH="$" + -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_linuxdeploy test_linuxdeploy) + +# make sure library and executable are built before test_appdir +add_dependencies(test_linuxdeploy simple_library simple_executable) diff --git a/tests/core/test_linuxdeploy.cpp b/tests/core/test_linuxdeploy.cpp new file mode 100644 index 0000000..d5544e6 --- /dev/null +++ b/tests/core/test_linuxdeploy.cpp @@ -0,0 +1,74 @@ +#include "gtest/gtest.h" + +#include "linuxdeploy.h" + +using namespace boost::filesystem; + +namespace LinuxDeployTest { + class LinuxDeployTestsFixture : public ::testing::Test { + public: + path tmpAppDir; + + public: + LinuxDeployTestsFixture() = default; + + void SetUp() override { + tmpAppDir = temp_directory_path() / unique_path("linuxdeploy-tests-%%%%-%%%%-%%%%"); + create_directories(tmpAppDir); + } + + void TearDown() override { + remove_all(tmpAppDir); + } + + ~LinuxDeployTestsFixture() override = default; + + void listDeployedFiles() { + std::cout << "Files deployed in AppDir:" << std::endl; + 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; + } + } + + void fillRegularAppDir() { + add_executable(); + add_desktop(); + add_icon(); + } + + void add_executable() const { + path source_executable_path = SIMPLE_EXECUTABLE_PATH; + path target_executable_path = tmpAppDir / "usr/bin" / source_executable_path.filename(); + create_directories(target_executable_path.parent_path()); + copy_file(source_executable_path, target_executable_path); + } + + void add_desktop() const { + path source_desktop_path = SIMPLE_DESKTOP_ENTRY_PATH; + path target_desktop_path = tmpAppDir / "usr/share/applications" / source_desktop_path.filename(); + create_directories(target_desktop_path.parent_path()); + copy_file(source_desktop_path, target_desktop_path); + } + + void add_icon() const { + path source_icon_path = SIMPLE_ICON_PATH; + path target_icon_path = tmpAppDir / "usr/share/icons/hicolor/scalable/apps" / source_icon_path.filename(); + create_directories(target_icon_path.parent_path()); + copy_file(source_icon_path, target_icon_path); + } + + void add_apprun() const { + path source_apprun_path = SIMPLE_FILE_PATH; + path target_apprun_path = tmpAppDir / "AppRun"; + copy_file(source_apprun_path, target_apprun_path); + } + }; + + TEST_F(LinuxDeployTestsFixture, deployAppDirRootFilesWithExistentAppRun) { + fillRegularAppDir(); + add_apprun(); + + listDeployedFiles(); + } +}