From 4b4b99634cb12c234088817d772f4f94c6ba439e Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 13 May 2025 21:46:04 +1200 Subject: [PATCH] Super simple assert for now. --- CMakeLists.txt | 11 ----------- src/autocomplete.cpp | 4 ++-- src/commands/edit.cpp | 2 +- src/commands/help.cpp | 2 +- src/commands/install.cpp | 4 ++-- src/commands/list.cpp | 2 +- src/commands/nuke.cpp | 2 +- src/commands/standard_autocomplete.cpp | 2 +- src/commands/uninstall.cpp | 2 +- src/main.cpp | 2 +- src/service_runner.cpp | 4 ++-- src/templates.cpp | 20 ++++++++++---------- src/utils/assert.hpp | 11 +++++++++++ src/utils/execute.cpp | 2 +- 14 files changed, 35 insertions(+), 35 deletions(-) create mode 100644 src/utils/assert.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index fdbb6ac..67bb6a1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,21 +49,10 @@ target_include_directories(dropshell PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/commands ) -include(FetchContent) -FetchContent_Declare( - libassert - GIT_REPOSITORY https://github.com/jeremy-rifkin/libassert.git - GIT_TAG v2.1.5 # -) -FetchContent_MakeAvailable(libassert) -target_link_libraries(dropshell libassert::assert) - -# On windows copy libassert.dll to the same directory as the executable for your_target if(WIN32) add_custom_command( TARGET dropshell POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different - $ $ ) endif() diff --git a/src/autocomplete.cpp b/src/autocomplete.cpp index 66e70fa..90a77ac 100644 --- a/src/autocomplete.cpp +++ b/src/autocomplete.cpp @@ -5,7 +5,7 @@ #include "services.hpp" #include "servers.hpp" -#include +#include #include #include @@ -36,7 +36,7 @@ bool autocomplete(const std::vector &args) return true; } - ASSERT(args.size() >= 3); + ASSERT(args.size() >= 3, "Invalid number of arguments"); std::string cmd = args[2]; // std::cout<<" cmd = ["< #include #include -#include +#include "utils/assert.hpp" namespace dropshell { diff --git a/src/commands/help.cpp b/src/commands/help.cpp index 6a054bd..12fc959 100644 --- a/src/commands/help.cpp +++ b/src/commands/help.cpp @@ -10,7 +10,7 @@ #include #include #include -#include +#include "utils/assert.hpp" namespace dropshell { diff --git a/src/commands/install.cpp b/src/commands/install.cpp index d3cdf97..9ac3496 100644 --- a/src/commands/install.cpp +++ b/src/commands/install.cpp @@ -11,7 +11,7 @@ #include #include #include -#include +#include "utils/assert.hpp" namespace dropshell { @@ -59,7 +59,7 @@ namespace dropshell server_env_manager &server_env, bool silent) { - ASSERT(!local_path.empty() && !remote_path.empty()); + ASSERT(!local_path.empty() && !remote_path.empty(), "Local or remote path not specified. Can't rsync."); std::string rsync_cmd = "rsync --delete --mkpath -zrpc -e 'ssh -p " + server_env.get_SSH_PORT() + "' " + quote(local_path + "/") + " " + diff --git a/src/commands/list.cpp b/src/commands/list.cpp index f41a7ea..2160187 100644 --- a/src/commands/list.cpp +++ b/src/commands/list.cpp @@ -12,7 +12,7 @@ #include #include #include -#include +#include "utils/assert.hpp" namespace dropshell { diff --git a/src/commands/nuke.cpp b/src/commands/nuke.cpp index 348845b..1cab57a 100644 --- a/src/commands/nuke.cpp +++ b/src/commands/nuke.cpp @@ -7,7 +7,7 @@ #include "servers.hpp" #include "templates.hpp" -#include +#include "utils/assert.hpp" #pragma TODO("Fix issues with Nuke below.") diff --git a/src/commands/standard_autocomplete.cpp b/src/commands/standard_autocomplete.cpp index 9ecb037..86350a6 100644 --- a/src/commands/standard_autocomplete.cpp +++ b/src/commands/standard_autocomplete.cpp @@ -4,7 +4,7 @@ #include "servers.hpp" #include "services.hpp" -#include +#include "utils/assert.hpp" namespace dropshell { diff --git a/src/commands/uninstall.cpp b/src/commands/uninstall.cpp index 49324ae..0cd29a7 100644 --- a/src/commands/uninstall.cpp +++ b/src/commands/uninstall.cpp @@ -3,7 +3,7 @@ #include "shared_commands.hpp" #include "templates.hpp" -#include +#include "utils/assert.hpp" namespace dropshell { diff --git a/src/main.cpp b/src/main.cpp index 7292245..c8364b9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,7 +16,7 @@ #include #include #include -#include +#include #include #include namespace dropshell { diff --git a/src/service_runner.cpp b/src/service_runner.cpp index e0f7cae..973b183 100644 --- a/src/service_runner.cpp +++ b/src/service_runner.cpp @@ -7,7 +7,7 @@ #include #include #include -#include +#include "utils/assert.hpp" #include "config.hpp" #include "service_runner.hpp" @@ -457,7 +457,7 @@ bool service_runner::backup(bool silent) { std::string local_backup_file_path = (std::filesystem::path(local_backups_dir) / backup_filename).string(); // assert that the backup filename is valid - -_- appears exactly 3 times in local_backup_file_path. - ASSERT(3 == count_substring(magic_string, local_backup_file_path)); + ASSERT(3 == count_substring(magic_string, local_backup_file_path), "Invalid backup filename"); { // Run backup script cRemoteTempFolder remote_temp_folder(mServerEnv); diff --git a/src/templates.cpp b/src/templates.cpp index bcdffd4..2bd65de 100644 --- a/src/templates.cpp +++ b/src/templates.cpp @@ -6,7 +6,7 @@ #include #include #include - #include + #include "utils/assert.hpp" #include "utils/envmanager.hpp" #include "utils/directories.hpp" @@ -105,7 +105,7 @@ // ------------------------------------------------------------------------------------------------ void template_manager::list_templates() const { - ASSERT(mLoaded && mSources.size() > 0); + ASSERT(mLoaded && mSources.size() > 0, "Template manager not loaded, or no template sources found."); auto templates = get_template_list(); if (templates.empty()) { @@ -128,7 +128,7 @@ std::set template_manager::get_template_list() const { - ASSERT(mLoaded && mSources.size() > 0); + ASSERT(mLoaded && mSources.size() > 0, "Template manager not loaded, or no template sources found."); std::set templates; for (const auto& source : mSources) { auto source_templates = source->get_template_list(); @@ -139,7 +139,7 @@ bool template_manager::has_template(const std::string &template_name) const { - ASSERT(mLoaded && mSources.size() > 0); + ASSERT(mLoaded && mSources.size() > 0, "Template manager not loaded, or no template sources found."); template_source_interface* source = get_source(template_name); if (!source) return false; @@ -148,7 +148,7 @@ template_info template_manager::get_template_info(const std::string &template_name) const { - ASSERT(mLoaded && mSources.size() > 0); + ASSERT(mLoaded && mSources.size() > 0, "Template manager not loaded, or no template sources found."); template_source_interface* source = get_source(template_name); if (source) return source->get_template_info(template_name); @@ -159,7 +159,7 @@ bool template_manager::template_command_exists(const std::string &template_name, const std::string &command) const { - ASSERT(mLoaded && mSources.size() > 0); + ASSERT(mLoaded && mSources.size() > 0, "Template manager not loaded, or no template sources found."); template_source_interface* source = get_source(template_name); if (!source) { std::cerr << "Error: Template '" << template_name << "' not found" << std::endl; @@ -249,9 +249,9 @@ void template_manager::load_sources() { - ASSERT(mSources.empty()); - ASSERT(gConfig().is_config_set()); - ASSERT(!mLoaded); + ASSERT(mSources.empty(), "Template manager already loaded (sources are not empty)."); + ASSERT(gConfig().is_config_set(), "Config not set."); + ASSERT(!mLoaded, "Template manager already loaded."); auto local_template_paths = gConfig().get_template_local_paths(); if (local_template_paths.empty()) return; @@ -285,7 +285,7 @@ template_source_interface *template_manager::get_source(const std::string &template_name) const { - ASSERT(mLoaded && mSources.size() > 0); + ASSERT(mLoaded && mSources.size() > 0, "Template manager not loaded, or no template sources found."); for (const auto& source : mSources) { if (source->has_template(template_name)) { return source.get(); diff --git a/src/utils/assert.hpp b/src/utils/assert.hpp new file mode 100644 index 0000000..385362a --- /dev/null +++ b/src/utils/assert.hpp @@ -0,0 +1,11 @@ +#ifndef ASSERT_HPP +#define ASSERT_HPP + + +#define ASSERT(condition, message) \ + if (!(condition)) { \ + std::cerr << "Assertion failed: " << message << std::endl; \ + std::exit(1); \ + } + +#endif // ASSERT_HPP diff --git a/src/utils/execute.cpp b/src/utils/execute.cpp index a0734d9..4bd48f2 100644 --- a/src/utils/execute.cpp +++ b/src/utils/execute.cpp @@ -6,7 +6,7 @@ #include #include #include -#include +#include "utils/assert.hpp" #include "execute.hpp" #include "utils/utils.hpp"