Updated unit tests

-Added tests for functions
-Added error class and return from package manager functions
-Updated `compile.sh` script
This commit is contained in:
David Allen 2023-01-07 14:50:09 -06:00
parent d4b1ea90c6
commit 5ffce72fa5
7 changed files with 109 additions and 42 deletions

View file

@ -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
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

View file

@ -1,4 +1,5 @@
#!/bin/bash
command=build/gdpm
# Install packages using install command and specifying each package name or file

View file

@ -9,6 +9,12 @@ namespace gdpm{
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;
std::string m_message;

View file

@ -75,7 +75,7 @@ namespace gdpm::log
);
}
template <typename S, typename...Args>
template <typename S = std::string, typename...Args>
static constexpr void println(const S& format, Args&&...args){
vlog(
fmt::format("{}\n", format),
@ -84,4 +84,9 @@ namespace gdpm::log
);
}
template <typename>
static constexpr void println(const std::string& format = ""){
println(format);
}
}

View file

@ -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<std::string>& package_titles);
GDPM_DLL_EXPORT void remove_packages(const std::vector<std::string>& package_titles);
GDPM_DLL_EXPORT void update_packages(const std::vector<std::string>& package_titles);
GDPM_DLL_EXPORT void search_for_packages(const std::vector<std::string>& package_titles);
GDPM_DLL_EXPORT error install_packages(const std::vector<std::string>& package_titles, bool skip_prompt = false);
GDPM_DLL_EXPORT error remove_packages(const std::vector<std::string>& package_titles, bool skip_prompt = false);
GDPM_DLL_EXPORT error update_packages(const std::vector<std::string>& package_titles, bool skip_prompt = false);
GDPM_DLL_EXPORT error search_for_packages(const std::vector<std::string>& package_titles, bool skip_prompt = false);
GDPM_DLL_EXPORT void list_information(const std::vector<std::string>& opts);
GDPM_DLL_EXPORT void clean_temporary(const std::vector<std::string>& package_titles);
GDPM_DLL_EXPORT void link_packages(const std::vector<std::string>& package_titles, const std::vector<std::string>& paths);

View file

@ -83,9 +83,10 @@ namespace gdpm::package_manager{
}
void install_packages(const std::vector<std::string>& package_titles){
error install_packages(const std::vector<std::string>& 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<std::string, std::string>;
@ -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<std::string>& package_titles){
error remove_packages(const std::vector<std::string>& 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<package_info> 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<std::string>& package_titles){
error update_packages(const std::vector<std::string>& 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<std::string> &package_titles){
error search_for_packages(const std::vector<std::string> &package_titles, bool skip_prompt){
std::vector<package_info> 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<std::string>& package_titles, const std::vector<std::string>& 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;

View file

@ -1,3 +1,4 @@
#include "package_manager.hpp"
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "log.hpp"
@ -7,20 +8,37 @@
#include <doctest.h>
TEST_CASE("Confirm doctest unit testing"){
CHECK(true);
gdpm::log::print("Testing doctest");
}
TEST_SUITE("Test database functions"){
TEST_CASE("Test cache database functions"){
gdpm::cache::create_package_database();
}
}
TEST_SUITE("Package manager function"){
std::vector<std::string> 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);