navid/veins / b2c412f69b54c19975059840cf196fc8e7c4e022fdc4dd9fa5e2bb0472ff7c52 / xveins/tests/integration/support.h
support.h
← xveins/tests/integration · 8420 bytes · raw · download
#pragma once
#include "test_support.h"
#include "core/common.h"
#include <algorithm>
#include <array>
#include <chrono>
#include <cstdint>
#include <map>
#include <optional>
#include <set>
#include <source_location>
#include <sstream>
#include <string>
#include <string_view>
#include <vector>
namespace vn::integration {
struct CommandResult {
int code = 0;
std::string out;
std::string err;
std::string text() const {
return out + err;
}
};
std::string trim_copy(std::string_view text);
std::vector<std::string> split_lines(std::string_view text);
std::vector<std::string> split_columns(std::string_view line);
std::string join_args(const std::vector<std::string>& args);
CommandResult run_cli(const std::vector<std::string>& command, const fs::path& cwd);
void set_environment(const char* key, const std::string& value);
CommandResult run_shell(const std::vector<std::string>& command, const fs::path& cwd);
struct LogRow {
std::string commit;
std::string message;
std::string raw;
bool working_copy = false;
};
struct VnFixture {
fs::path root;
fs::path home;
fs::path repo;
fs::path original_cwd;
VnFixture() : original_cwd(fs::current_path()) {
root = fs::temp_directory_path() /
("vn-test-" + std::to_string(random_u64()) + "-" +
std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));
home = root / "home";
repo = root / "repo";
fs::create_directories(home / ".config");
fs::create_directories(repo);
set_environment("HOME", home.string());
set_environment("XDG_CONFIG_HOME", (home / ".config").string());
set_environment("USER", "tester");
set_environment("GIT_AUTHOR_NAME", "Tester");
set_environment("GIT_AUTHOR_EMAIL", "tester@example.com");
set_environment("GIT_COMMITTER_NAME", "Tester");
set_environment("GIT_COMMITTER_EMAIL", "tester@example.com");
set_environment("NO_COLOR", "1");
set_environment("TZ", "UTC");
set_environment("VN_EDITOR", "/bin/true");
}
~VnFixture() {
std::error_code ec;
fs::current_path(original_cwd, ec);
fs::remove_all(root, ec);
}
CommandResult vn(const std::vector<std::string>& command, const fs::path& cwd = {},
bool check = true,
const std::source_location& location = std::source_location::current()) const {
const fs::path actual_cwd = cwd.empty() ? repo : cwd;
CommandResult result = run_cli(command, actual_cwd);
if (check && result.code != 0) {
throw test::Failure(test::location_text(location) +
": command failed: " + join_args(command) + "\nstdout:\n" +
result.out + "\nstderr:\n" + result.err);
}
return result;
}
CommandResult
failed(const std::vector<std::string>& command, std::string_view expected = {},
const fs::path& cwd = {},
const std::source_location& location = std::source_location::current()) const {
CommandResult result = vn(command, cwd, false, location);
if (result.code == 0) {
throw test::Failure(test::location_text(location) + ": command unexpectedly passed: " +
join_args(command) + "\n" + result.text());
}
if (!expected.empty() && result.text().find(expected) == std::string::npos) {
throw test::Failure(test::location_text(location) + ": expected output to contain \"" +
std::string(expected) + "\"\n" + result.text());
}
return result;
}
void init(const fs::path& cwd = {}) {
vn({"init", "."}, cwd);
}
void write(const fs::path& relative, std::string_view content, const fs::path& cwd = {}) const {
const fs::path path = (cwd.empty() ? repo : cwd) / relative;
fs::create_directories(path.parent_path());
const Bytes bytes(reinterpret_cast<const std::uint8_t*>(content.data()), content.size());
if (!write_file(path, bytes))
throw test::Failure("cannot write test file: " + path.string());
}
std::string read(const fs::path& relative, const fs::path& cwd = {}) const {
const fs::path path = (cwd.empty() ? repo : cwd) / relative;
const auto data = read_file_opt(path);
if (!data)
throw test::Failure("cannot read test file: " + path.string());
return std::string(reinterpret_cast<const char*>(data->data()), data->size());
}
bool exists(const fs::path& relative, const fs::path& cwd = {}) const {
return path_exists((cwd.empty() ? repo : cwd) / relative);
}
void commit(std::string_view message, const fs::path& cwd = {}) const {
vn({"go", "-m", std::string(message)}, cwd);
}
std::string hash(std::string_view revision = "@", const fs::path& cwd = {}) const {
return trim_copy(vn({"hash", std::string(revision)}, cwd).out);
}
std::string short_hash(std::string_view revision = "@", const fs::path& cwd = {}) const {
return trim_copy(vn({"hash", "-short", std::string(revision)}, cwd).out);
}
std::vector<LogRow> log_rows(const fs::path& cwd = {}) const {
const std::string output = vn({"log", "-no-col-titles"}, cwd).out;
std::vector<LogRow> rows;
for (const std::string& line : split_lines(output)) {
const std::string stripped = trim_copy(line);
if (stripped.empty())
continue;
std::vector<std::string> parts = split_columns(stripped);
if (parts.empty())
continue;
LogRow row;
row.raw = line;
row.working_copy = parts[0].starts_with("@");
std::string first = row.working_copy ? trim_copy(parts[0].substr(1)) : parts[0];
row.commit = first.substr(0, std::min<std::size_t>(12, first.size()));
row.message = parts.size() > 1 ? parts.back() : std::string();
rows.push_back(std::move(row));
}
return rows;
}
std::vector<std::string> log_commits(const fs::path& cwd = {}) const {
std::vector<std::string> commits;
for (const LogRow& row : log_rows(cwd))
commits.push_back(row.commit);
return commits;
}
std::string commit_for_message(std::string_view message, const fs::path& cwd = {}) const {
for (const LogRow& row : log_rows(cwd)) {
if (row.message.find(message) != std::string::npos)
return row.commit;
}
throw test::Failure("no commit with message \"" + std::string(message) + "\"");
}
std::map<std::string, std::string> metadata(std::string_view revision = "@",
const fs::path& cwd = {}) const {
const std::string output = vn({"show", std::string(revision)}, cwd).out;
std::map<std::string, std::string> values;
for (const std::string& line : split_lines(output)) {
if (line.empty())
break;
const std::size_t space = line.find(' ');
if (space == std::string::npos)
continue;
const std::string key = line.substr(0, space);
if (key == "commit" || key == "change" || key == "author" || key == "date" ||
key == "parents") {
values[key] = trim_copy(line.substr(space + 1));
}
}
return values;
}
std::set<std::string> parents(std::string_view revision = "@", const fs::path& cwd = {}) const {
std::set<std::string> result;
const auto values = metadata(revision, cwd);
const auto found = values.find("parents");
if (found == values.end())
return result;
std::istringstream stream(found->second);
std::string parent;
while (stream >> parent)
result.insert(parent);
return result;
}
};
bool git_available();
void require_shell(test::Context& t, const CommandResult& result, std::string_view label);
fs::path make_git_repository(test::Context& t, VnFixture& f);
void make_source_repository(VnFixture& f);
void linear_history(VnFixture& f, int count);
} // namespace vn::integration