From d4b1ea90c6ce3cbecd393ae13a16447bf8a4ba0f Mon Sep 17 00:00:00 2001 From: "David J. Allen" Date: Thu, 5 Jan 2023 20:55:29 -0600 Subject: [PATCH] Fixed bugs and issues with linking tests --- .gitignore | 3 ++- CMakeLists.txt | 2 +- bin/compile.sh | 1 + bin/lines.sh | 2 ++ bin/test.sh | 1 + include/config.hpp | 3 +-- include/constants.hpp | 2 +- include/error.hpp | 4 ++-- src/config.cpp | 16 +++++++++------- src/package_manager.cpp | 8 ++++---- tests/basic.cpp | 6 +++--- 11 files changed, 27 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 5cbeffb..4117154 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,8 @@ builds/** bin/gdpm bin/gdpm-tests cache/** -tests +tests/* +!tests/*.cpp */tmp/** vgcore.* .cache diff --git a/CMakeLists.txt b/CMakeLists.txt index 5fd8a97..8c9828d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,7 +61,7 @@ target_include_directories(${PROJECT_NAME}-tests PRIVATE ${INCLUDE_DIRS}) target_link_libraries(${PROJECT_NAME} PRIVATE ${PROJECT_NAME}-shared ${LINK_LIBS}) target_link_libraries(${PROJECT_NAME}-shared PRIVATE ${LINK_LIBS}) target_link_libraries(${PROJECT_NAME}-static PRIVATE ${LINK_LIBS}) -target_link_libraries(${PROJECT_NAME}-tests PRIVATE ${LINK_LIBS}) +target_link_libraries(${PROJECT_NAME}-tests PRIVATE ${PROJECT_NAME}-shared ${LINK_LIBS}) # Add project unit tests # add_custom_target("${PROJECT_NAME}-tests" SOURCE ${TESTS}) diff --git a/bin/compile.sh b/bin/compile.sh index 8499ed0..f457268 100755 --- a/bin/compile.sh +++ b/bin/compile.sh @@ -12,5 +12,6 @@ cmake -B build -S . -DCMAKE_EXPORT_COMPILE_COMMANDS=1 -G Ninja ninja -C build # 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 diff --git a/bin/lines.sh b/bin/lines.sh index 55b4368..5e23d4e 100755 --- a/bin/lines.sh +++ b/bin/lines.sh @@ -1,2 +1,4 @@ +#!/usr/bin/env bash + # Call this from project root wc -l include/*.hpp src/*.cpp \ No newline at end of file diff --git a/bin/test.sh b/bin/test.sh index 7e9cbf8..c2b957b 100755 --- a/bin/test.sh +++ b/bin/test.sh @@ -1,3 +1,4 @@ +#!/bin/bash command=build/gdpm # Install packages using install command and specifying each package name or file diff --git a/include/config.hpp b/include/config.hpp index 479865c..4143ebb 100644 --- a/include/config.hpp +++ b/include/config.hpp @@ -26,8 +26,7 @@ namespace gdpm::config{ int verbose; }; std::string to_json(const context& params); - context load(std::filesystem::path path, int verbose = 0); - gdpm::error load(std::filesystem:: path, context& config, int verbose = 0); + gdpm::error load(std::filesystem::path path, context& config, int verbose = 0); gdpm::error save(std::filesystem::path path, const context& config, int verbose = 0); context make_context(const std::string& username = GDPM_CONFIG_USERNAME, const std::string& password = GDPM_CONFIG_PASSWORD, const std::string& path = GDPM_CONFIG_PATH, const std::string& token = GDPM_CONFIG_TOKEN, const std::string& godot_version = GDPM_CONFIG_GODOT_VERSION, const std::string& packages_dir = GDPM_CONFIG_LOCAL_PACKAGES_DIR, const std::string& tmp_dir = GDPM_CONFIG_LOCAL_TMP_DIR, const std::set& remote_sources = {GDPM_CONFIG_REMOTE_SOURCES}, size_t threads = GDPM_CONFIG_THREADS, size_t timeout = 0, bool enable_sync = GDPM_CONFIG_ENABLE_SYNC, bool enable_file_logging = GDPM_CONFIG_ENABLE_FILE_LOGGING, int verbose = GDPM_CONFIG_VERBOSE); diff --git a/include/constants.hpp b/include/constants.hpp index 014f7b4..49c7f80 100644 --- a/include/constants.hpp +++ b/include/constants.hpp @@ -6,7 +6,7 @@ namespace gdpm::constants{ const std::string HomePath(std::string(std::getenv("HOME")) + "/"); - const std::string TestPath(HomePath + ".config/gdpm/tests") + const std::string TestPath(HomePath + ".config/gdpm/tests"); const std::string ConfigPath(HomePath + ".config/gdpm/config.json"); const std::string LockfilePath(HomePath + ".config/gdpm/gdpm.lck"); const std::string LocalPackagesDir(HomePath + ".config/gdpm/packages"); diff --git a/include/error.hpp b/include/error.hpp index de017f3..88faca7 100644 --- a/include/error.hpp +++ b/include/error.hpp @@ -5,12 +5,12 @@ namespace gdpm{ class error{ public: - error(int code, const std::string& message): + error(int code = 0, const std::string& message = ""): m_code(code), m_message(message) {} private: int m_code; std::string m_message; - } + }; } \ No newline at end of file diff --git a/src/config.cpp b/src/config.cpp index fb49926..683fce4 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -58,15 +58,17 @@ namespace gdpm::config{ } - context load(std::filesystem::path path, int verbose){ + gdpm::error load(std::filesystem::path path, context& config, int verbose){ std::fstream file; + gdpm::error error; file.open(path, std::ios::in); if(!file){ if(verbose){ log::info("No configuration file found. Creating a new one."); - save(make_context(), verbose); + config = make_context(); + save(config.path, config, verbose); } - return config; + return error; } else if(file.is_open()){ /* @@ -89,7 +91,7 @@ namespace gdpm::config{ if(!doc.IsObject()){ log::error("Could not load config file."); - return config; + return error; } assert(doc.IsObject()); @@ -139,11 +141,11 @@ namespace gdpm::config{ config.enable_sync = _get_value_int(doc, "enable_sync"); config.enable_file_logging = _get_value_int(doc, "enable_file_logging"); } - return config; + return error; } - int save(std::filesystem::path path, const context& config, int verbose){ + gdpm::error save(std::filesystem::path path, const context& config, int verbose){ using namespace rapidjson; /* Build a JSON string to pass to document */ @@ -160,7 +162,7 @@ namespace gdpm::config{ PrettyWriter writer(osw); doc.Accept(writer); - return 0; + return gdpm::error(); } diff --git a/src/package_manager.cpp b/src/package_manager.cpp index 770c38a..8419bba 100644 --- a/src/package_manager.cpp +++ b/src/package_manager.cpp @@ -52,9 +52,9 @@ namespace gdpm::package_manager{ /* Check for config and create if not exists */ if(!std::filesystem::exists(config.path)){ - config::save(config); + config::save(config.path, config); } - config = config::load(config.path); + config::load(config.path, config); config.enable_sync = true; std::string json = to_json(config); @@ -78,7 +78,7 @@ namespace gdpm::package_manager{ void finalize(){ curl_easy_cleanup(curl); - config::save(config); + config::save(config.path, config); // curl_global_cleanup(); } @@ -622,7 +622,7 @@ namespace gdpm::package_manager{ /* Set option variables first to be used in functions below. */ if(result.count("config")){ config.path = result["config"].as(); - config = config::load(config.path); + config::load(config.path, config); log::info("Config: {}", config.path); } if(result.count("add-remote")){ diff --git a/tests/basic.cpp b/tests/basic.cpp index fd7fdb2..8fcf196 100644 --- a/tests/basic.cpp +++ b/tests/basic.cpp @@ -15,7 +15,7 @@ TEST_CASE("Confirm doctest unit testing"){ TEST_CASE("Test cache database functions"){ - gdpm::cache::create_package_database() + gdpm::cache::create_package_database(); } @@ -24,6 +24,6 @@ TEST_CASE("Test configuration functions"){ config.path = gdpm::constants::TestPath + "/"; std::string json = gdpm::config::to_json(config); - int status = gdpm::config::save(config); - gdpm::config::context new_config = gdpm::config::load(config.path, config) + gdpm::error error_save = gdpm::config::save(config.path, config); + gdpm::error error_load = gdpm::config::load(config.path, config); } \ No newline at end of file