Skip to content

Commit

Permalink
Parse CMD args in runner
Browse files Browse the repository at this point in the history
  • Loading branch information
cegonse committed Nov 20, 2024
1 parent 0041084 commit 52e8b45
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions runner/cmd-args.cpp
Original file line number Diff line number Diff line change
@@ -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<argc; ++i)
{
std::string arg(argv[i]);

if (!isCmdArg(arg))
this->_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;
}
15 changes: 15 additions & 0 deletions runner/cmd-args.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once
#include <string>

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;
};
18 changes: 18 additions & 0 deletions runner/directory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <fstream>
#include <sstream>
#include <algorithm>
#include <cstdlib>

constexpr bool hasPerms(std::filesystem::perms target, std::filesystem::perms other)
{
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions runner/directory.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ namespace Directory
{
std::vector<std::string> 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);
}
16 changes: 10 additions & 6 deletions runner/runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,18 @@ int Runner::runTests(const std::vector<std::string>& 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));
Expand Down
52 changes: 52 additions & 0 deletions runner/test/cmd-args.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <cest>
#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<char *, 1> 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<char *, 3> 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<char *, 1> 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<char *, 2> argv = { (char *)cmd_path.c_str(), (char *)watch.c_str() };

auto cmd_args = CmdArgs(argv.size(), argv.data());

expect(cmd_args.watch()).toBeTruthy();
});
});
});
4 changes: 4 additions & 0 deletions runner/test/helpers/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 52e8b45

Please sign in to comment.