From 52e8b45b5ac65631ab1e2d9aa48658d4e735a245 Mon Sep 17 00:00:00 2001 From: Cesar Gonzalez Date: Wed, 20 Nov 2024 16:42:39 +0100 Subject: [PATCH] Parse CMD args in runner --- .gitignore | 1 + Makefile | 1 + runner/cmd-args.cpp | 36 +++++++++++++++++++++++ runner/cmd-args.h | 15 ++++++++++ runner/directory.cpp | 18 ++++++++++++ runner/directory.h | 1 + runner/runner.cpp | 16 ++++++---- runner/test/cmd-args.test.cpp | 52 +++++++++++++++++++++++++++++++++ runner/test/helpers/helpers.cpp | 4 +++ 9 files changed, 138 insertions(+), 6 deletions(-) create mode 100644 runner/cmd-args.cpp create mode 100644 runner/cmd-args.h create mode 100644 runner/test/cmd-args.test.cpp diff --git a/.gitignore b/.gitignore index 2d5565d..c9dc9af 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,7 @@ build/ log.xml test_summary.jsonl runner/test/test_runner +runner/test/test_cmd_args test/examples/test_assertions test/examples/test_chatbot test/examples/test_description diff --git a/Makefile b/Makefile index 107b903..6ae6fc9 100644 --- a/Makefile +++ b/Makefile @@ -28,6 +28,7 @@ all: build runner $(TESTS) run runner: build g++ $(RUNNER_SRCS) -std=c++17 -g -O0 -o build/cest-runner g++ runner/test/runner.test.cpp runner/runner.cpp runner/helpers.cpp runner/test/helpers/helpers.cpp -Ibuild -std=c++17 -g -O0 -o runner/test/test_runner + g++ runner/test/cmd-args.test.cpp runner/cmd-args.cpp runner/test/helpers/helpers.cpp -Ibuild -std=c++17 -g -O0 -o runner/test/test_cmd_args build: mkdir -p build diff --git a/runner/cmd-args.cpp b/runner/cmd-args.cpp new file mode 100644 index 0000000..388aee8 --- /dev/null +++ b/runner/cmd-args.cpp @@ -0,0 +1,36 @@ +#include "cmd-args.h" +#include "directory.h" + +static bool isCmdArg(const std::string& arg) +{ + return arg.substr(0, 2) == "--"; +} + +CmdArgs::CmdArgs(int argc, char* argv[]) : _path(Directory::cwd()), _watch(false) +{ + for (int i=1; i_path = Directory::absolute(arg); + else + parseArg(arg); + } +} + +void CmdArgs::parseArg(const std::string& arg) +{ + if (arg == "--watchAll") + this->_watch = true; +} + +const std::string& CmdArgs::path() +{ + return this->_path; +} + +bool CmdArgs::watch() +{ + return this->_watch; +} diff --git a/runner/cmd-args.h b/runner/cmd-args.h new file mode 100644 index 0000000..0ce0b6e --- /dev/null +++ b/runner/cmd-args.h @@ -0,0 +1,15 @@ +#pragma once +#include + +class CmdArgs +{ + public: + CmdArgs(int argc, char* argv[]); + const std::string& path(); + bool watch(); + + private: + void parseArg(const std::string& arg); + std::string _path; + bool _watch; +}; diff --git a/runner/directory.cpp b/runner/directory.cpp index d0ff300..4bd2ba7 100644 --- a/runner/directory.cpp +++ b/runner/directory.cpp @@ -3,6 +3,7 @@ #include #include #include +#include constexpr bool hasPerms(std::filesystem::perms target, std::filesystem::perms other) { @@ -63,6 +64,23 @@ std::string Directory::cwd() return std::filesystem::current_path(); } +static bool isHomeRelative(const std::string& path) +{ + return !path.empty() && path[0] == '~'; +} + +static std::string transformHome(const std::string& path) +{ + auto home = std::getenv("HOME"); + return std::string(home) + path.substr(1); +} + +std::string Directory::absolute(const std::string& path) +{ + auto expanded_path = isHomeRelative(path) ? transformHome(path) : path; + return std::filesystem::absolute(path); +} + std::string Directory::readTextFile(const std::string& path) { std::stringstream buffer; diff --git a/runner/directory.h b/runner/directory.h index b5e7f61..1ec0595 100644 --- a/runner/directory.h +++ b/runner/directory.h @@ -6,5 +6,6 @@ namespace Directory { std::vector findExecutableFiles(const std::string& path, const std::string& filter); std::string cwd(); + std::string absolute(const std::string& path); std::string readTextFile(const std::string& path); } diff --git a/runner/runner.cpp b/runner/runner.cpp index e7ba41a..ac2f5d5 100644 --- a/runner/runner.cpp +++ b/runner/runner.cpp @@ -35,14 +35,18 @@ int Runner::runTests(const std::vector& executables) for (const auto& test_file : executables) { int64_t test_time = 0; - const auto test_status = Process::runExecutable(test_file, [](const auto& output) { - Output::print(output); - }, cest_args, test_time); - - status_code |= test_status; + const auto test_status = Process::runExecutable( + test_file, + [](const auto& output) { Output::print(output); }, + cest_args, + test_time + ); total_time_us += test_time; - if (!Process::killedBySignal(status_code)) + if (test_status != 0) + status_code = -1; + + if (!Process::killedBySignal(test_status)) { const auto results_path = "/tmp/cest_" + test_file.substr(test_file.rfind('/') + 1); const auto test_result = TestResults(Directory::readTextFile(results_path)); diff --git a/runner/test/cmd-args.test.cpp b/runner/test/cmd-args.test.cpp new file mode 100644 index 0000000..2db0261 --- /dev/null +++ b/runner/test/cmd-args.test.cpp @@ -0,0 +1,52 @@ +#include +#include "../directory.h" +#include "../cmd-args.h" + +describe("CmdArgs", []() { + describe("path()", []() { + it("defaults to CWD when path is not present in args", []() { + std::string cmd_path = "/bin/test"; + std::array argv = { (char *)cmd_path.c_str() }; + + auto cmd_args = CmdArgs(argv.size(), argv.data()); + + expect(cmd_args.path()).toEqual(Directory::cwd()); + }); + + it("assumes non-first argument not starting with -- to be the path", []() { + std::string cmd_path = "/bin/test"; + std::string watch_mode = "--watchAll"; + std::string path = "/home/tests"; + std::array argv = { + (char *)cmd_path.c_str(), + (char *)watch_mode.c_str(), + (char *)path.c_str() + }; + + auto cmd_args = CmdArgs(argv.size(), argv.data()); + + expect(cmd_args.path()).toEqual("/home/tests"); + }); + }); + + describe("watch()", []() { + it("defaults to false", []() { + std::string cmd_path = "/bin/test"; + std::array argv = { (char *)cmd_path.c_str() }; + + auto cmd_args = CmdArgs(argv.size(), argv.data()); + + expect(cmd_args.watch()).toBeFalsy(); + }); + + it("sets to true when --watchAll is passed", []() { + std::string cmd_path = "/bin/test"; + std::string watch = "--watchAll"; + std::array argv = { (char *)cmd_path.c_str(), (char *)watch.c_str() }; + + auto cmd_args = CmdArgs(argv.size(), argv.data()); + + expect(cmd_args.watch()).toBeTruthy(); + }); + }); +}); diff --git a/runner/test/helpers/helpers.cpp b/runner/test/helpers/helpers.cpp index 6f75f75..4ba71ba 100644 --- a/runner/test/helpers/helpers.cpp +++ b/runner/test/helpers/helpers.cpp @@ -84,6 +84,10 @@ std::string Directory::cwd() { return "/cwd"; } +std::string Directory::absolute(const std::string& path) +{ + return path; +} void Directory::readTextFile_mockOutput(const std::string& output) { __read_text_file_mock_output.push_back(output);