From d61f7b6fdec6ffbd8649da779fb5a29b24669386 Mon Sep 17 00:00:00 2001 From: youmetme <103353084+WangZhongDian@users.noreply.github.com> Date: Sat, 2 Nov 2024 12:38:36 +0800 Subject: [PATCH] Dev test (#6) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * #feat 增强Fatal级别的底色,修改logging类的方法 * 更新版本号 * 加入test脚本 * fix:conanfile * test action --- .github/workflows/linux_test.yml | 17 +++++++++++++++++ CMakeLists.txt | 7 +++++-- conanfile.py | 27 ++++++++++----------------- src/CMakeLists.txt | 2 +- src/logging.c | 19 +++++++++---------- test.sh | 2 ++ tests/CMakeLists.txt | 10 ++++++++-- 7 files changed, 52 insertions(+), 32 deletions(-) create mode 100644 .github/workflows/linux_test.yml create mode 100755 test.sh diff --git a/.github/workflows/linux_test.yml b/.github/workflows/linux_test.yml new file mode 100644 index 0000000..5ea5b3a --- /dev/null +++ b/.github/workflows/linux_test.yml @@ -0,0 +1,17 @@ + +name: test on Linux + +on: + push: + branches: ["mian"] + pull_request: + branches: ["mian"] + +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: checkout code + uses: actions/checkout@v4 + - name: test + run: bash ./test.sh \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 4c98e89..dc18cd3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,8 @@ cmake_minimum_required( VERSION 3.28) +project(logging) +option(TEST "是否启动单元测试" ON) +option(SHARED "是否编译为动态库" OFF) set(LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib) set(ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib) @@ -11,7 +14,7 @@ include_directories(${CMAKE_SOURCE_DIR}/include) add_subdirectory(src) #测试单元 -if (SKIPTEST) -else() +if (TEST) + enable_testing() add_subdirectory(tests) endif() diff --git a/conanfile.py b/conanfile.py index 66ed435..ff498df 100644 --- a/conanfile.py +++ b/conanfile.py @@ -1,11 +1,8 @@ from conan import ConanFile from conan.tools.cmake import CMake, CMakeToolchain, CMakeDeps, cmake_layout -from conan.tools.files import copy, get -from conan.tools.build import check_min_cppstd -from conan import tools +from conan.tools.files import copy import os - class loggingRecipe(ConanFile): name = "logging" version = "0.2.3" @@ -16,11 +13,11 @@ class loggingRecipe(ConanFile): topics = ("logging", "C", "simple", "easy-to-use", "log","Logging") settings = "os", "compiler", "build_type", "arch" - options = {"shared": [True, False], "fPIC": [True, False]} - default_options = {"shared": False, "fPIC": True} + options = {"shared": [True, False], "fPIC": [True, False],"test":[True,False]} + default_options = {"shared": False, "fPIC": True,"test":True} - exports_sources = "include/*", "CMakeLists.txt", "src/*" + exports_sources = "include/*", "CMakeLists.txt", "src/*", "tests/*" def config_options(self): if self.settings.os == "Windows": @@ -30,8 +27,6 @@ class loggingRecipe(ConanFile): if self.options.shared: self.options.rm_safe("fPIC") - - def layout(self): cmake_layout(self) @@ -39,18 +34,16 @@ class loggingRecipe(ConanFile): deps = CMakeDeps(self) deps.generate() tc = CMakeToolchain(self) - tc.variables["SKIPTEST"]=True - if self.options.shared: - tc.variables["SHARED"] = True - else: - tc.variables["SHARED"] = False + tc.variables["TEST"] = True if self.options.test else False + tc.variables["SHARED"] = True if self.options.shared else False tc.generate() def build(self): cmake = CMake(self) cmake.configure() - cmake.build(target="Logging") - + cmake.build() + if self.options.test: + cmake.test() def package(self): copy(self, "LICENSE", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses")) @@ -62,4 +55,4 @@ class loggingRecipe(ConanFile): copy(self, pattern="*.dylib", src=self.build_folder, dst=os.path.join(self.package_folder, "lib"), keep_path=False) def package_info(self): - self.cpp_info.libs = ["Logging"] \ No newline at end of file + self.cpp_info.libs = ["logging"] \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 24b1142..93e3dd2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,4 +1,4 @@ -project(Logging) +project(logging) aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} SRC) aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/handler SRC) diff --git a/src/logging.c b/src/logging.c index c343aa1..471c8a2 100644 --- a/src/logging.c +++ b/src/logging.c @@ -9,16 +9,15 @@ #include #include -#define RED "\033[0;31m" -#define RED_B "\033[0;41m" -#define GREEN "\033[0;32m" -#define YELLOW "\033[0;33m" -#define BLUE "\033[0;34m" -#define RESET "\033[0m" -#define CYAN "\033[0;36m" -#define MAGENTA "\033[0;35m" -#define WHITE "\033[0;37m" -#define BLACK "\033[0;30m" + +#define RED "\033[0;31m" +#define RED_B "\033[0;41m" +#define GREEN "\033[0;32m" +#define YELLOW "\033[0;33m" +#define BLUE "\033[0;34m" +#define RESET "\033[0m" +#define CYAN "\033[0;36m" + #define LOG_BUFFER_SIZE 1024 diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..e372a43 --- /dev/null +++ b/test.sh @@ -0,0 +1,2 @@ +#!/bin/bash +cmake build -B build . && cd build && make && ctest \ No newline at end of file diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3519e83..74e1515 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,8 +1,14 @@ project(test) +enable_testing() + +#测试简单基本应用 add_executable(${PROJECT_NAME}simple test_simple.c) -target_link_libraries(${PROJECT_NAME}simple Logging) +target_link_libraries(${PROJECT_NAME}simple logging) +add_test(test_simple ${CMAKE_SOURCE_DIR}/bin/${PROJECT_NAME}simple) +#测试拦截器 add_executable(${PROJECT_NAME}interceptor test_interceptor.c) -target_link_libraries(${PROJECT_NAME}interceptor Logging) +target_link_libraries(${PROJECT_NAME}interceptor logging) +add_test(test_interceptor ${CMAKE_SOURCE_DIR}/bin/${PROJECT_NAME}interceptor) \ No newline at end of file