From 326647011b8167cf43f0e09a74a8e961a2641218 Mon Sep 17 00:00:00 2001 From: Duncan Ogilvie Date: Tue, 6 Apr 2021 14:57:22 +0200 Subject: [PATCH] Initial attempt at adding tests --- .github/workflows/build.yml | 5 +++++ CMakeLists.txt | 10 +++++++++ cmake.toml | 2 +- src/cmkrlib/build.cpp | 5 ++--- src/cmkrlib/cmake.cpp | 6 ++++-- src/cmkrlib/cmake.hpp | 6 ++++-- src/cmkrlib/gen.cpp | 8 +++++--- tests/CMakeLists.txt | 19 +++++++++++++++++ tests/basic/CMakeLists.txt | 41 +++++++++++++++++++++++++++++++++++++ tests/basic/cmake.toml | 9 ++++++++ tests/basic/src/basic.cpp | 5 +++++ tests/cmake.toml | 5 +++++ 12 files changed, 110 insertions(+), 11 deletions(-) create mode 100644 tests/CMakeLists.txt create mode 100644 tests/basic/CMakeLists.txt create mode 100644 tests/basic/cmake.toml create mode 100644 tests/basic/src/basic.cpp create mode 100644 tests/cmake.toml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 96ee7eb..13a9079 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -6,6 +6,7 @@ jobs: build: runs-on: ${{ matrix.os }} strategy: + fail-fast: false matrix: os: [windows-2016, windows-2019, macos-10.15, ubuntu-16.04, ubuntu-18.04, ubuntu-20.04] env: @@ -17,3 +18,7 @@ jobs: run: | cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=${{ env.BUILD_TYPE }} cmake --build build --config ${{ env.BUILD_TYPE }} --parallel + - name: Test + run: | + cd build/tests + ctest -C ${{ env.BUILD_TYPE }} diff --git a/CMakeLists.txt b/CMakeLists.txt index 9073fab..8eb3abc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,6 +46,16 @@ endif() add_subdirectory(third_party) set(CMAKE_FOLDER ${CMKR_CMAKE_FOLDER}) +# tests +set(CMKR_CMAKE_FOLDER ${CMAKE_FOLDER}) +if(CMAKE_FOLDER) + set(CMAKE_FOLDER "${CMAKE_FOLDER}/tests") +else() + set(CMAKE_FOLDER tests) +endif() +add_subdirectory(tests) +set(CMAKE_FOLDER ${CMKR_CMAKE_FOLDER}) + # Target cmkrlib set(cmkrlib_SOURCES diff --git a/cmake.toml b/cmake.toml index 56b4cac..96e94a8 100644 --- a/cmake.toml +++ b/cmake.toml @@ -11,7 +11,7 @@ name = "cmkr" version = "0.1.3" description = "CMakeLists generator from TOML" languages = ["CXX"] -subdirs = ["third_party"] +subdirs = ["third_party", "tests"] [target.cmkrlib] type = "static" diff --git a/src/cmkrlib/build.cpp b/src/cmkrlib/build.cpp index 83b2666..2c4f0e6 100644 --- a/src/cmkrlib/build.cpp +++ b/src/cmkrlib/build.cpp @@ -22,9 +22,8 @@ int run(int argc, char **argv) { } std::stringstream ss; - if (!fs::exists("CMakeLists.txt")) - if (gen::generate_cmake(fs::current_path().string().c_str())) - throw std::runtime_error("CMake generation failure!"); + if (gen::generate_cmake(fs::current_path().string().c_str())) + throw std::runtime_error("CMake generation failure!"); ss << "cmake -S. -B" << cmake.build_dir << " "; diff --git a/src/cmkrlib/cmake.cpp b/src/cmkrlib/cmake.cpp index 639b4f0..a7b18d1 100644 --- a/src/cmkrlib/cmake.cpp +++ b/src/cmkrlib/cmake.cpp @@ -204,8 +204,10 @@ CMake::CMake(const std::string &path, bool build) { for (const auto &t : ts) { Test test; test.name = toml::find(t, "name").as_string(); - test.cmd = toml::find(t, "type").as_string(); - get_optional(t, "arguments", test.args); + get_optional(t, "configurations", test.configurations); + get_optional(t, "working-directory", test.working_directory); + test.command = toml::find(t, "command").as_string(); + get_optional(t, "arguments", test.arguments); tests.push_back(test); } } diff --git a/src/cmkrlib/cmake.hpp b/src/cmkrlib/cmake.hpp index cc3a642..29d480b 100644 --- a/src/cmkrlib/cmake.hpp +++ b/src/cmkrlib/cmake.hpp @@ -72,8 +72,10 @@ struct Target { struct Test { std::string name; - std::string cmd; - std::vector args; + std::vector configurations; + std::string working_directory; + std::string command; + std::vector arguments; }; struct Install { diff --git a/src/cmkrlib/gen.cpp b/src/cmkrlib/gen.cpp index 7e39731..4c5063a 100644 --- a/src/cmkrlib/gen.cpp +++ b/src/cmkrlib/gen.cpp @@ -611,12 +611,14 @@ int generate_cmake(const char *path, bool root) { } if (!cmake.tests.empty()) { - cmd("include")("CTest"); cmd("enable_testing")().endl(); for (const auto &test : cmake.tests) { auto name = std::make_pair("NAME", test.name); - auto command = std::make_pair("COMMAND", test.cmd); - cmd("add_test")(name, command, test.args).endl(); + auto configurations = std::make_pair("CONFIGURATIONS", test.configurations); + auto working_directory = std::make_pair("WORKING_DIRECTORY", test.working_directory); + auto command = std::make_pair("COMMAND", test.command); + auto arguments = std::make_pair("", test.arguments); + cmd("add_test")(name, configurations, working_directory, command, arguments).endl(); } } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..7680178 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,19 @@ +# This file was generated automatically by cmkr. + +# Create a configure-time dependency on cmake.toml to improve IDE support +if(CMKR_ROOT_PROJECT) + configure_file(cmake.toml cmake.toml COPYONLY) +endif() + +enable_testing() + +add_test( + NAME + basic + WORKING_DIRECTORY + "${CMAKE_CURRENT_LIST_DIR}/basic" + COMMAND + $ + build +) + diff --git a/tests/basic/CMakeLists.txt b/tests/basic/CMakeLists.txt new file mode 100644 index 0000000..34dc8d6 --- /dev/null +++ b/tests/basic/CMakeLists.txt @@ -0,0 +1,41 @@ +# This file was generated automatically by cmkr. + +cmake_minimum_required(VERSION 3.5) + +# Regenerate CMakeLists.txt automatically in the root project +set(CMKR_ROOT_PROJECT OFF) +if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) + set(CMKR_ROOT_PROJECT ON) + + # Bootstrap cmkr + include(cmkr.cmake OPTIONAL RESULT_VARIABLE CMKR_INCLUDE_RESULT) + if(CMKR_INCLUDE_RESULT) + cmkr() + endif() + + # Enable folder support + set_property(GLOBAL PROPERTY USE_FOLDERS ON) +endif() + +# Create a configure-time dependency on cmake.toml to improve IDE support +if(CMKR_ROOT_PROJECT) + configure_file(cmake.toml cmake.toml COPYONLY) +endif() + +project(basic) + +# Target basic +set(basic_SOURCES + "src/basic.cpp" + cmake.toml +) + +add_executable(basic ${basic_SOURCES}) + +get_directory_property(CMKR_VS_STARTUP_PROJECT DIRECTORY ${PROJECT_SOURCE_DIR} DEFINITION VS_STARTUP_PROJECT) +if(NOT CMKR_VS_STARTUP_PROJECT) + set_property(DIRECTORY ${PROJECT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT basic) +endif() + +source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${basic_SOURCES}) + diff --git a/tests/basic/cmake.toml b/tests/basic/cmake.toml new file mode 100644 index 0000000..33aab4d --- /dev/null +++ b/tests/basic/cmake.toml @@ -0,0 +1,9 @@ +[cmake] +version = "3.5" + +[project] +name = "basic" + +[target.basic] +type = "executable" +sources = ["src/basic.cpp"] diff --git a/tests/basic/src/basic.cpp b/tests/basic/src/basic.cpp new file mode 100644 index 0000000..84b6c59 --- /dev/null +++ b/tests/basic/src/basic.cpp @@ -0,0 +1,5 @@ +#include + +int main() { + puts("Hello from cmkr!"); +} diff --git a/tests/cmake.toml b/tests/cmake.toml new file mode 100644 index 0000000..d25a362 --- /dev/null +++ b/tests/cmake.toml @@ -0,0 +1,5 @@ +[[test]] +name = "basic" +command = "$" +working-directory = "${CMAKE_CURRENT_LIST_DIR}/basic" +arguments = ["build"] \ No newline at end of file