add integration tests part 1

This commit is contained in:
Gabriel Ionita 2025-10-27 16:00:00 +01:00
parent 70898edacc
commit 4104bea698
Signed by: gabi
SSH Key Fingerprint: SHA256:mrbYmB/SGtDvT3etRoS6pDrMYWxR0/B5GSF6rR3qhhc

View File

@ -0,0 +1,91 @@
#include <catch2/catch_test_macros.hpp>
#include <array>
#include <cstdio>
#include <filesystem>
#include <memory>
#include <string>
#include <vector>
// Helper function to execute a command and capture output
std::string exec_command(const std::string& cmd) {
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
if (!pipe) {
return "";
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}
TEST_CASE("gwatch binary exists and is executable", "[integration]") {
std::filesystem::path gwatch_path = "./build/gwatch";
if (!std::filesystem::exists(gwatch_path)) {
WARN("gwatch binary not found at ./build/gwatch");
return;
}
REQUIRE(std::filesystem::exists(gwatch_path));
// Check if it's executable
auto perms = std::filesystem::status(gwatch_path).permissions();
bool is_executable = (perms & std::filesystem::perms::owner_exec) != std::filesystem::perms::none;
REQUIRE(is_executable);
}
TEST_CASE("gwatch shows help message", "[integration]") {
std::filesystem::path gwatch_path = "./build/gwatch";
if (!std::filesystem::exists(gwatch_path)) {
WARN("gwatch binary not found, skipping test");
return;
}
std::string output = exec_command("./build/gwatch --help");
// Help should mention the main options
REQUIRE(output.find("--exec") != std::string::npos);
REQUIRE(output.find("--var") != std::string::npos);
}
TEST_CASE("gwatch requires both --exec and --var arguments", "[integration]") {
std::filesystem::path gwatch_path = "./build/gwatch";
if (!std::filesystem::exists(gwatch_path)) {
WARN("gwatch binary not found, skipping test");
return;
}
// Test missing both arguments
std::string output = exec_command("./build/gwatch 2>&1");
bool has_error = (output.find("required") != std::string::npos ||
output.find("Error") != std::string::npos);
REQUIRE(has_error);
}
TEST_CASE("gwatch detects non-existent variable", "[integration]") {
std::filesystem::path gwatch_path = "./build/gwatch";
std::filesystem::path test_binary = "./test_access";
if (!std::filesystem::exists(gwatch_path)) {
WARN("gwatch binary not found, skipping test");
return;
}
if (!std::filesystem::exists(test_binary)) {
WARN("test_access binary not found, skipping test");
return;
}
std::string output = exec_command(
"./build/gwatch --var nonexistent_var_xyz --exec ./test_access 2>&1");
bool found_error = (output.find("not found") != std::string::npos);
REQUIRE(found_error);
}