diff --git a/README.md b/README.md index 2a09ffe..6d5c6a5 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ The `myvas::cmdout` library provides facilities for executing shell commands and ``` include(FetchContent) FetchContent_Declare(cmdout - URL https://github.com/myvas/cmdout/archive/refs/tags/0.1.12.tar.gz + URL https://github.com/myvas/cmdout/archive/refs/tags/0.1.13.tar.gz DOWNLOAD_EXTRACT_TIMESTAMP ON ) FetchContent_MakeAvailable(cmdout) diff --git a/benchmarks/cmake/benchmark.cmake b/benchmarks/cmake/benchmark.cmake index d6903f2..3cec19a 100644 --- a/benchmarks/cmake/benchmark.cmake +++ b/benchmarks/cmake/benchmark.cmake @@ -8,11 +8,15 @@ # benchmark::benchmark ################################### -find_package(benchmark QUIET) +set(benchmark_VERSION_REQUIRED 1.8.3) + +find_package(benchmark ${benchmark_VERSION_REQUIRED} QUIET) if(benchmark_FOUND) message(STATUS "Found benchmark: ${benchmark_VERSION}") else() - message(STATUS "CMake package benchmark >=1.8.3 not found! Please wait for fetching from github.com...") + message(STATUS "CMake package benchmark ${benchmark_VERSION_REQUIRED} or later not found!" + " Please wait for fetching from https://github.com/google/benchmark" + ) include(FetchContent) ############################################### @@ -20,8 +24,18 @@ else() ############################################### set(BENCHMARK_ENABLE_TESTING OFF CACHE INTERNAL "") FetchContent_Declare(benchmark - URL https://github.com/google/benchmark/archive/refs/tags/v1.8.3.tar.gz + URL https://github.com/google/benchmark/archive/refs/tags/v${benchmark_VERSION_REQUIRED}.tar.gz DOWNLOAD_EXTRACT_TIMESTAMP ON ) FetchContent_MakeAvailable(benchmark) + + if(benchmark_SOURCE_DIR) + message(STATUS "Built benchmark: ${benchmark_VERSION_REQUIRED}") + else() + if(benchmark_FIND_REQUIRED) + message(FATAL_ERROR "Failed to build benchmark: ${benchmark_VERSION_REQUIRED}") + else() + message(WARNING "Failed to build benchmark: ${benchmark_VERSION_REQUIRED}") + endif() + endif() endif() \ No newline at end of file diff --git a/examples/example_myvas_cmdout.cpp b/examples/example_myvas_cmdout.cpp index 5648ff8..bc1243a 100644 --- a/examples/example_myvas_cmdout.cpp +++ b/examples/example_myvas_cmdout.cpp @@ -20,36 +20,43 @@ void example_myvas_cmdout() { assert(result.status() == EXIT_SUCCESS); } { - auto result = myvas::cmdout("ls ~/").exec(); + std::cout << std::endl; + auto result = myvas::cmdout("ls ~/ -C").exec(); std::cout << result << std::endl; assert(result.status() == EXIT_SUCCESS); } { + std::cout << std::endl; auto result = myvas::cmdout("ls not-exist 2>&1").exec(); std::cout << result << std::endl; assert(result.status() == ENOENT); } { - auto result = myvas::cmdout("ls / -l").exec(); + std::cout << std::endl; + auto result = myvas::cmdout("ls / -C").exec(); std::cout << result << std::endl; assert(result.status() == EXIT_SUCCESS); } { - auto result = myvas::cmdout("ls / -l", std::chrono::milliseconds(0)).exec(); + std::cout << std::endl; + auto result = myvas::cmdout("ls / -C", std::chrono::milliseconds(0)).exec(); std::cout << result << std::endl; assert(result.status() == EXIT_SUCCESS); } { - auto result = myvas::cmdout("ls / -l", std::chrono::milliseconds(1)).exec(); + std::cout << std::endl; + auto result = myvas::cmdout("ls / -C", std::chrono::milliseconds(1)).exec(); std::cout << result << std::endl; assert(result.status() == ETIME); } { - auto result = myvas::cmdout("ls / -l", std::chrono::milliseconds(9)).exec(); + std::cout << std::endl; + auto result = myvas::cmdout("ls / -C", std::chrono::milliseconds(9)).exec(); std::cout << result << std::endl; assert(result.status() == EXIT_SUCCESS); } { + std::cout << std::endl; auto result = myvas::cmdout("ls not-exist 2>&1", std::chrono::milliseconds(9)).exec(); std::cout << result << std::endl; assert(result.status() == ENOENT); diff --git a/examples/example_myvas_system.cpp b/examples/example_myvas_system.cpp index afc0a25..5e1375c 100644 --- a/examples/example_myvas_system.cpp +++ b/examples/example_myvas_system.cpp @@ -15,47 +15,56 @@ void example_myvas_system() { std::cout << "* function myvas::system() *" << std::endl; std::cout << "***********************************" << std::endl; { + std::cout << std::endl; auto result = myvas::system("exit 0"); std::cout << result << std::endl; assert(result.status() == EXIT_SUCCESS); } { + std::cout << std::endl; auto result = myvas::system("exit 1"); std::cout << result << std::endl; assert(result.status() == EXIT_FAILURE); } { + std::cout << std::endl; auto result = myvas::system("ls not-exist 2>&1"); std::cout << result << std::endl; assert(result.status() == ENOENT); } { - auto result = myvas::system("ls ~/"); + std::cout << std::endl; + auto result = myvas::system("ls ~/ -C"); std::cout << result << std::endl; assert(result.status() == EXIT_SUCCESS); } { + std::cout << std::endl; auto result = myvas::system("ls not-exist 2>&1"); std::cout << result << std::endl; assert(result.status() == ENOENT); } { - auto result = myvas::system_timeout_ms("ls / -l", 0); + std::cout << std::endl; + auto result = myvas::system_timeout_ms("ls / -C", 0); std::cout << result << std::endl; assert(result.status() == EXIT_SUCCESS); } { - auto result = myvas::system_timeout_ms("ls / -l", 1); + std::cout << std::endl; + auto result = myvas::system_timeout_ms("ls / -C", 1); std::cout << result << std::endl; assert(result.status() == ETIME); } { - auto result = myvas::system_timeout("ls / -l", std::chrono::milliseconds(0)).exec(); + std::cout << std::endl; + auto result = myvas::system_timeout("ls / -C", std::chrono::milliseconds(0)).exec(); std::cout << result << std::endl; assert(result.status() == EXIT_SUCCESS); } { - auto result = myvas::system_timeout("ls / -l", std::chrono::milliseconds(1)).exec(); + std::cout << std::endl; + auto result = myvas::system_timeout("ls / -C", std::chrono::milliseconds(1)).exec(); std::cout << result << std::endl; assert(result.status() == ETIME); } diff --git a/examples/example_std_system.cpp b/examples/example_std_system.cpp index 39528ea..940d50f 100644 --- a/examples/example_std_system.cpp +++ b/examples/example_std_system.cpp @@ -13,31 +13,42 @@ void example_std_system() { std::cout << "* function std::system() *" << std::endl; std::cout << "*********************************" << std::endl; { + std::cout << std::endl; auto result = std::system(nullptr); - std::cout << result << std::endl; + std::cout << "std::system(nullptr) returns " << result << std::endl; // https://en.cppreference.com/w/cpp/utility/program/system // If command is a null pointer, returns a nonzero value // if and only if the command processor exists. assert(result != 0); + assert(result == 1); + assert(WEXITSTATUS(result) == EXIT_SUCCESS); } { - auto result = std::system("exit 0"); - std::cout << result << std::endl; - assert(result == EXIT_SUCCESS); + std::cout << std::endl; + auto cmd = "exit 0"; + auto result = std::system(cmd); + std::cout << "std::system(\"" << cmd << "\") returns " << result << std::endl; + assert(WEXITSTATUS(result) == EXIT_SUCCESS); } { + std::cout << std::endl; + auto cmd = "exit 1"; auto result = std::system("exit 1"); - std::cout << result << std::endl; - assert(result == EXIT_FAILURE); + std::cout << "std::system(\"" << cmd << "\") returns " << result << std::endl; + assert(WEXITSTATUS(result) == EXIT_FAILURE); } { - auto result = std::system("ls ~/"); - std::cout << result << std::endl; - assert(result == EXIT_SUCCESS); + std::cout << std::endl; + auto cmd = "ls ~/ -C"; + auto result = std::system(cmd); + std::cout << "std::system(\"" << cmd << "\") returns " << result << std::endl; + assert(WEXITSTATUS(result) == EXIT_SUCCESS); } { - auto result = std::system("ls not-exist 2>&1"); - std::cout << result << std::endl; + std::cout << std::endl; + auto cmd = "ls not-exist 2>&1"; + auto result = std::system(cmd); + std::cout << "std::system(\"" << cmd << "\") returns " << result << std::endl; assert(WEXITSTATUS(result) == ENOENT); } } diff --git a/include/cmdout/cmdout_ext.hpp b/include/cmdout/cmdout_ext.hpp index d1b6917..19a21ec 100644 --- a/include/cmdout/cmdout_ext.hpp +++ b/include/cmdout/cmdout_ext.hpp @@ -11,12 +11,13 @@ #include -/** - * @brief Helper for printing a cmdout object. - */ + /** + * @brief Helper for printing a cmdout object. + */ std::ostream& operator<<(std::ostream& os, const myvas::cmdout& value) { - os << "cmdout {" << value.cmd() << "} return " << value.status() - << " length " << value.out().length() << "\n" << value.out(); + os << "cmdout: " << value.cmd() << "\n(return " << value.status() + << " length " << value.out().length() << ")\n" + << std::string(80, '-') << "\n" << value.out(); return os; } \ No newline at end of file diff --git a/include/cmdout/version.h b/include/cmdout/version.h index 9ad92af..d823331 100644 --- a/include/cmdout/version.h +++ b/include/cmdout/version.h @@ -25,7 +25,7 @@ /** * @brief When you make backward compatible bug fixes, You'd increment the PATCH version. */ -#define CMDOUT_VERSION_PATCH 12 +#define CMDOUT_VERSION_PATCH 13 #define CMDOUT_TO_VERSION_NUMBER(major, minor, patch) (major * 10000 + minor * 100 + patch) #define CMDOUT_VERSION_NUMBER CMDOUT_TO_VERSION_NUMBER(CMDOUT_VERSION_MAJOR, CMDOUT_VERSION_MINOR, CMDOUT_VERSION_PATCH) \ No newline at end of file diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 0079baf..e9e14b4 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -10,7 +10,8 @@ if(NOT TARGET cmdout) endif() include(../cmake/utils.cmake) -include(cmake/catch2.cmake) +include(cmake/googletest.cmake) +include(cmake/Catch2.cmake) ######################### # tests sources diff --git a/tests/cmake/Catch2.cmake b/tests/cmake/Catch2.cmake index 325e486..b38b80a 100644 --- a/tests/cmake/Catch2.cmake +++ b/tests/cmake/Catch2.cmake @@ -9,16 +9,27 @@ # apt install catch2 ################################### -find_package(Catch2 3.3.2 QUIET) +set(Catch2_VERSION_REQUIRED 3.3.2) + +find_package(Catch2 ${Catch2_VERSION_REQUIRED} QUIET) if(Catch2_FOUND) message(STATUS "Found Catch2: ${Catch2_VERSION}") else() - message(WARNING "CMake package Catch2 >=3.3.2 not found! Please wait for fetching from github.com...") + message(STATUS "CMake package Catch2 ${Catch2_VERSION_REQUIRED} or later not found!" + " Please wait for fetching from https://github.com/catchorg/Catch2" + ) include(FetchContent) FetchContent_Declare(Catch2 - URL https://github.com/catchorg/Catch2/archive/refs/tags/v3.3.2.tar.gz + URL https://github.com/catchorg/Catch2/archive/refs/tags/v${Catch2_VERSION_REQUIRED}.tar.gz DOWNLOAD_EXTRACT_TIMESTAMP ON + OVERRIDE_FIND_PACKAGE ) - FetchContent_MakeAvailable(Catch2) + find_package(Catch2) + if(Catch2_FOUND) + # TODO: We should extract `REAL` version number by hand, since there is no Catch2_VERSION here! + message(STATUS "Built Catch2: ${Catch2_VERSION}") + else() + message(FATAL_ERROR "Failed to build Catch2: ${Catch2_VERSION_REQUIRED}") + endif() endif() \ No newline at end of file diff --git a/tests/cmake/googletest.cmake b/tests/cmake/googletest.cmake new file mode 100644 index 0000000..2a09b99 --- /dev/null +++ b/tests/cmake/googletest.cmake @@ -0,0 +1,34 @@ +# Copyright (c) 2023 Myvas Foundation +# SPDX-License-Identifier: MIT + +################################### +# Find or fetch a CMake package +# +# googletest >=1.10.0 +# googletest +################################### + +set(googletest_VERSION_REQUIRED 1.14.0) + +find_package(googletest ${googletest_VERSION_REQUIRED} QUIET) +if(googletest_FOUND) + message(STATUS "Found googletest: ${googletest_VERSION}") +else() + message(STATUS "CMake package googletest ${googletest_VERSION_REQUIRED} or later not found!" + " Please wait for fetching from https://github.com/google/googletest" + ) + + include(FetchContent) + FetchContent_Declare(googletest + URL https://github.com/google/googletest/archive/refs/tags/v${googletest_VERSION_REQUIRED}.tar.gz + DOWNLOAD_EXTRACT_TIMESTAMP ON + OVERRIDE_FIND_PACKAGE + ) + find_package(googletest) + if(googletest_FOUND) + # TODO: We should extract `REAL` version number by hand, since there is no googletest_VERSION here! + message(STATUS "Built googletest: ${googletest_VERSION}") + else() + message(FATAL_ERROR "Failed to build googletest: ${googletest_VERSION_REQUIRED}") + endif() +endif() \ No newline at end of file