From 5ffce72fa5256f7524508dd7d528a85cb348d55d Mon Sep 17 00:00:00 2001 From: "David J. Allen" Date: Sat, 7 Jan 2023 14:50:09 -0600 Subject: [PATCH] Updated unit tests -Added tests for functions -Added error class and return from package manager functions -Updated `compile.sh` script --- bin/compile.sh | 14 ++++-- bin/test.sh | 1 + include/error.hpp | 6 +++ include/log.hpp | 7 ++- include/package_manager.hpp | 8 ++-- src/package_manager.cpp | 85 +++++++++++++++++++++++++------------ tests/basic.cpp | 30 ++++++++++--- 7 files changed, 109 insertions(+), 42 deletions(-) diff --git a/bin/compile.sh b/bin/compile.sh index f457268..c6b081c 100755 --- a/bin/compile.sh +++ b/bin/compile.sh @@ -9,9 +9,15 @@ # CMake/ninja build system mkdir -p build cmake -B build -S . -DCMAKE_EXPORT_COMPILE_COMMANDS=1 -G Ninja -ninja -C build +ninja -C build -j $(nproc) # Create symlinks to executables in build folder if necessary -rm bin/gdpm bin/gdpm-tests -ln -s ../build/gdpm bin/gdpm -ln -s ../build/gdpm-tests bin/gdpm-tests \ No newline at end of file +if test -f "../build/gdpm"; then + rm bin/gdpm + ln -s ../build/gdpm bin/gdpm +fi + +if test -f "../build/gdpm-tests"; then + rm bin/gdpm-tests + ln -s ../build/gdpm-tests bin/gdpm-tests +fi diff --git a/bin/test.sh b/bin/test.sh index c2b957b..cb08344 100755 --- a/bin/test.sh +++ b/bin/test.sh @@ -1,4 +1,5 @@ #!/bin/bash + command=build/gdpm # Install packages using install command and specifying each package name or file diff --git a/include/error.hpp b/include/error.hpp index 88faca7..93a7825 100644 --- a/include/error.hpp +++ b/include/error.hpp @@ -8,6 +8,12 @@ namespace gdpm{ error(int code = 0, const std::string& message = ""): m_code(code), m_message(message) {} + + void set_code(int code) { m_code = code; } + void set_message(const std::string& message) { m_message = message; } + + int get_code() const { return m_code; } + std::string get_message() const { return m_message; } private: int m_code; diff --git a/include/log.hpp b/include/log.hpp index 1de8c94..ecdfa12 100644 --- a/include/log.hpp +++ b/include/log.hpp @@ -75,7 +75,7 @@ namespace gdpm::log ); } - template + template static constexpr void println(const S& format, Args&&...args){ vlog( fmt::format("{}\n", format), @@ -84,4 +84,9 @@ namespace gdpm::log ); } + template + static constexpr void println(const std::string& format = ""){ + println(format); + } + } \ No newline at end of file diff --git a/include/package_manager.hpp b/include/package_manager.hpp index 66275c3..3025c58 100644 --- a/include/package_manager.hpp +++ b/include/package_manager.hpp @@ -61,10 +61,10 @@ namespace gdpm::package_manager{ GDPM_DLL_EXPORT int initialize(int argc, char **argv); GDPM_DLL_EXPORT int execute(); GDPM_DLL_EXPORT void finalize(); - GDPM_DLL_EXPORT void install_packages(const std::vector& package_titles); - GDPM_DLL_EXPORT void remove_packages(const std::vector& package_titles); - GDPM_DLL_EXPORT void update_packages(const std::vector& package_titles); - GDPM_DLL_EXPORT void search_for_packages(const std::vector& package_titles); + GDPM_DLL_EXPORT error install_packages(const std::vector& package_titles, bool skip_prompt = false); + GDPM_DLL_EXPORT error remove_packages(const std::vector& package_titles, bool skip_prompt = false); + GDPM_DLL_EXPORT error update_packages(const std::vector& package_titles, bool skip_prompt = false); + GDPM_DLL_EXPORT error search_for_packages(const std::vector& package_titles, bool skip_prompt = false); GDPM_DLL_EXPORT void list_information(const std::vector& opts); GDPM_DLL_EXPORT void clean_temporary(const std::vector& package_titles); GDPM_DLL_EXPORT void link_packages(const std::vector& package_titles, const std::vector& paths); diff --git a/src/package_manager.cpp b/src/package_manager.cpp index 8419bba..f694049 100644 --- a/src/package_manager.cpp +++ b/src/package_manager.cpp @@ -83,9 +83,10 @@ namespace gdpm::package_manager{ } - void install_packages(const std::vector& package_titles){ - using namespace rapidjson; + error install_packages(const std::vector& package_titles, bool skip_prompt){ + using namespace rapidjson; params.verbose = config.verbose; + error error; /* TODO: Need a way to use remote sources from config until none left */ @@ -115,7 +116,9 @@ namespace gdpm::package_manager{ /* Found nothing to install so there's nothing to do at this point. */ if(p_found.empty()){ log::error("No packages found to install."); - return; + error.set_code(-1); + error.set_message("No packages found to install."); + return error; } log::println("Packages to install: "); @@ -127,7 +130,7 @@ namespace gdpm::package_manager{ if(!skip_prompt){ if(!utils::prompt_user_yn("Do you want to install these packages? (y/n)")) - return; + return error; } using ss_pair = std::pair; @@ -150,7 +153,8 @@ namespace gdpm::package_manager{ if(doc.HasParseError() || doc.IsNull()){ log::println(""); log::error("Error parsing HTTP response. (error code: {})", doc.GetParseError()); - return; + error.set_code(doc.GetParseError()); + return error; } p.category = doc["category"].GetString(); p.description = doc["description"].GetString(); @@ -211,7 +215,9 @@ namespace gdpm::package_manager{ log::println("Done."); }else{ log::error("Something went wrong...(code {})", response.code); - return; + error.set_code(response.code); + error.set_message("Error in HTTP response."); + return error; } } @@ -230,25 +236,34 @@ namespace gdpm::package_manager{ log::info_n("Updating local asset data..."); cache::update_package_info(p_found); log::println("done."); + + return error; } - void remove_packages(const std::vector& package_titles){ + error remove_packages(const std::vector& package_titles, bool skip_prompt){ using namespace rapidjson; using namespace std::filesystem; + error error; if(package_titles.empty()){ + std::string message("No packages to remove."); log::println(""); - log::error("No packages to remove."); - return; + log::error(message); + error.set_code(-1); + error.set_message(message); + return error; } /* Find the packages to remove if they're is_installed and show them to the user */ std::vector p_cache = cache::get_package_info_by_title(package_titles); if(p_cache.empty()){ + std::string message("Could not find any packages to remove."); log::println(""); - log::error("Could not find any packages to remove."); - return; + log::error(message); + error.set_code(-1); + error.set_message(message); + return error; } /* Count number packages in cache flagged as is_installed. If there are none, then there's nothing to do. */ @@ -258,9 +273,12 @@ namespace gdpm::package_manager{ }); if(p_count == 0){ + std::string message("No packages to remove."); log::println(""); - log::error("No packages to remove."); - return; + log::error(message); + error.set_code(-1); + error.set_message(message); + return error; } log::println("Packages to remove:"); @@ -271,7 +289,7 @@ namespace gdpm::package_manager{ if(!skip_prompt){ if(!utils::prompt_user_yn("Do you want to remove these packages? (y/n)")) - return; + return error; } log::info_n("Removing packages..."); @@ -313,21 +331,28 @@ namespace gdpm::package_manager{ log::info_n("Updating local asset data..."); cache::update_package_info(p_cache); log::println("done."); + + return error; } - void update_packages(const std::vector& package_titles){ + error update_packages(const std::vector& package_titles, bool skip_prompt){ using namespace rapidjson; + error error; + /* If no package titles provided, update everything and then exit */ if(package_titles.empty()){ std::string url{constants::HostUrl}; url += rest_api::endpoints::GET_AssetId; Document doc = rest_api::get_assets_list(url, params); if(doc.IsNull()){ - log::error("Could not get response from server. Aborting."); - return; + std::string message("Could not get response from server. Aborting."); + log::error(message); + error.set_code(-1); + error.set_message(message); + return error; } - return; + return error; } /* Fetch remote asset data and compare to see if there are package updates */ @@ -352,20 +377,22 @@ namespace gdpm::package_manager{ if(!skip_prompt){ if(!utils::prompt_user_yn("Do you want to update the following packages? (y/n)")) - return; + return error; } remove_packages(p_updates); install_packages(p_updates); + return error; } - void search_for_packages(const std::vector &package_titles){ + error search_for_packages(const std::vector &package_titles, bool skip_prompt){ std::vector p_cache = cache::get_package_info_by_title(package_titles); + error error; if(!p_cache.empty() && !config.enable_sync){ print_package_list(p_cache); - return; + return error; } for(const auto& p_title : package_titles){ using namespace rapidjson; @@ -379,13 +406,17 @@ namespace gdpm::package_manager{ request_url += rest_api::endpoints::GET_Asset; Document doc = rest_api::get_assets_list(request_url, params); if(doc.IsNull()){ - log::error("Could not search for packages."); - return; + std::string message("Could not search for packages."); + log::error(message); + error.set_code(-1); + error.set_message(message); + return error; } log::info("{} package(s) found...", doc["total_items"].GetInt()); print_package_list(doc); } + return error; } @@ -751,10 +782,10 @@ namespace gdpm::package_manager{ /* Used to run the command AFTER parsing and setting all command line args. */ void run_command(command_e c, const std::vector& package_titles, const std::vector& opts){ switch(c){ - case install: install_packages(package_titles); break; - case remove: remove_packages(package_titles); break; - case update: update_packages(package_titles); break; - case search: search_for_packages(package_titles); break; + case install: install_packages(package_titles, skip_prompt); break; + case remove: remove_packages(package_titles, skip_prompt); break; + case update: update_packages(package_titles, skip_prompt); break; + case search: search_for_packages(package_titles, skip_prompt); break; case list: list_information(package_titles); break; /* ...opts are the paths here */ case link: link_packages(package_titles, opts); break; diff --git a/tests/basic.cpp b/tests/basic.cpp index 8fcf196..0d97e92 100644 --- a/tests/basic.cpp +++ b/tests/basic.cpp @@ -1,3 +1,4 @@ +#include "package_manager.hpp" #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN #include "log.hpp" @@ -7,20 +8,37 @@ #include +TEST_SUITE("Test database functions"){ -TEST_CASE("Confirm doctest unit testing"){ - CHECK(true); - gdpm::log::print("Testing doctest"); + TEST_CASE("Test cache database functions"){ + gdpm::cache::create_package_database(); + } } -TEST_CASE("Test cache database functions"){ - gdpm::cache::create_package_database(); +TEST_SUITE("Package manager function"){ + std::vector packages{"ResolutionManagerPlugin","godot-hmac", "Godot"}; + gdpm::config::context config = gdpm::config::make_context(); + + TEST_CASE("Test install packages"){ + gdpm::package_manager::install_packages(packages, true); + } + + + TEST_CASE("Test searching packages"){ + gdpm::package_manager::search_for_packages(packages, true); + } + + + TEST_CASE("Test remove packages"){ + gdpm::package_manager::remove_packages(packages, true); + } + } TEST_CASE("Test configuration functions"){ - gdpm::config::context config; + gdpm::config::context config = gdpm::config::make_context(); config.path = gdpm::constants::TestPath + "/"; std::string json = gdpm::config::to_json(config);