navid/veins / 83508482911f348af86cb892949a35bdce0d6b2f949b4a0faab7cba9a499fb82 / xveins/tests/integration/support.cpp
support.cpp
← xveins/tests/integration · 6302 bytes · raw · download
#include "integration/support.h"
#include "test_support.h"
#include "cli/cli.h"
#include "core/common.h"
#include "store/store.h"
#include "version/version.h"
#include <algorithm>
#include <array>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <string_view>
#include <vector>
#if !defined(_WIN32)
#include <sys/wait.h>
#endif
namespace vn::integration {
std::string trim_copy(std::string_view text) {
std::size_t start = 0;
std::size_t end = text.size();
while (start < end && (text[start] == ' ' || text[start] == '\t' || text[start] == '\r' ||
text[start] == '\n'))
++start;
while (end > start && (text[end - 1] == ' ' || text[end - 1] == '\t' || text[end - 1] == '\r' ||
text[end - 1] == '\n'))
--end;
return std::string(text.substr(start, end - start));
}
std::vector<std::string> split_lines(std::string_view text) {
std::vector<std::string> lines;
std::size_t start = 0;
while (start < text.size()) {
const std::size_t end = text.find('\n', start);
if (end == std::string_view::npos) {
lines.emplace_back(text.substr(start));
break;
}
lines.emplace_back(text.substr(start, end - start));
start = end + 1;
}
return lines;
}
std::vector<std::string> split_columns(std::string_view line) {
std::vector<std::string> parts;
std::size_t start = 0;
while (start < line.size()) {
while (start < line.size() && line[start] == ' ')
++start;
if (start >= line.size())
break;
std::size_t end = start;
while (end < line.size()) {
if (line[end] == ' ' && end + 1 < line.size() && line[end + 1] == ' ')
break;
++end;
}
parts.emplace_back(line.substr(start, end - start));
start = end;
}
return parts;
}
std::string join_args(const std::vector<std::string>& args) {
std::string out;
for (std::size_t i = 0; i < args.size(); ++i) {
if (i != 0)
out.push_back(' ');
out += args[i];
}
return out;
}
CommandResult run_cli(const std::vector<std::string>& command, const fs::path& cwd) {
std::vector<std::string> args;
args.reserve(command.size() + 1);
args.emplace_back("vn");
args.insert(args.end(), command.begin(), command.end());
const fs::path previous = fs::current_path();
fs::current_path(cwd);
std::ostringstream out;
std::ostringstream err;
auto* old_out = std::cout.rdbuf(out.rdbuf());
auto* old_err = std::cerr.rdbuf(err.rdbuf());
int code = 0;
try {
code = cli_run(args);
} catch (const std::exception& error) {
code = 1;
err << "exception: " << error.what() << "\n";
} catch (...) {
code = 1;
err << "exception: unknown failure\n";
}
std::cout.flush();
std::cerr.flush();
std::cout.rdbuf(old_out);
std::cerr.rdbuf(old_err);
fs::current_path(previous);
return CommandResult{code, out.str(), err.str()};
}
void set_environment(const char* key, const std::string& value) {
#if defined(_WIN32)
_putenv_s(key, value.c_str());
#else
setenv(key, value.c_str(), 1);
#endif
}
std::string shell_quote(std::string_view value) {
std::string out = "'";
for (char c : value) {
if (c == '\'')
out += "'\\''";
else
out.push_back(c);
}
out.push_back('\'');
return out;
}
CommandResult run_shell(const std::vector<std::string>& command, const fs::path& cwd) {
#if defined(_WIN32)
(void)command;
(void)cwd;
return CommandResult{1, {}, "shell commands are not supported on Windows\n"};
#else
std::string line = "cd " + shell_quote(cwd.string()) + " && ";
for (std::size_t i = 0; i < command.size(); ++i) {
if (i != 0)
line.push_back(' ');
line += shell_quote(command[i]);
}
line += " 2>&1";
FILE* pipe = popen(line.c_str(), "r");
if (!pipe)
return CommandResult{1, {}, "popen failed\n"};
std::string out;
std::array<char, 4096> buffer{};
while (std::fgets(buffer.data(), static_cast<int>(buffer.size()), pipe) != nullptr)
out += buffer.data();
const int status = pclose(pipe);
const int code = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
return CommandResult{code, out, {}};
#endif
}
bool git_available() {
return run_shell({"git", "--version"}, fs::current_path()).code == 0;
}
void require_shell(test::Context& t, const CommandResult& result, std::string_view label) {
if (result.code != 0)
t.fail(std::string(label) + " failed:\n" + result.text());
}
fs::path make_git_repository(test::Context& t, VnFixture& f) {
const fs::path git_repo = f.root / "gitrepo";
fs::create_directories(git_repo);
require_shell(t, run_shell({"git", "init", "-q"}, git_repo), "git init");
require_shell(t, run_shell({"git", "config", "user.name", "Git Tester"}, git_repo),
"git config user.name");
require_shell(t, run_shell({"git", "config", "user.email", "git@example.com"}, git_repo),
"git config user.email");
f.write("a.txt", "one\n", git_repo);
require_shell(t, run_shell({"git", "add", "a.txt"}, git_repo), "git add");
require_shell(t, run_shell({"git", "commit", "-qm", "git one"}, git_repo), "git commit one");
f.write("a.txt", "one\ntwo\n", git_repo);
require_shell(t, run_shell({"git", "commit", "-qam", "git two"}, git_repo), "git commit two");
return git_repo;
}
void linear_history(VnFixture& f, int count) {
for (int index = 0; index < count; ++index) {
f.write("file.txt", "line " + std::to_string(index) + "\n");
f.commit("commit " + std::to_string(index));
}
}
void make_source_repository(VnFixture& f) {
const fs::path source = f.root / "source";
fs::create_directories(source);
f.init(source);
f.write("a.txt", "hello\n", source);
f.vn({"bmk", "set", "master"}, source);
f.commit("initial", source);
f.vn({"access", "anonymous", "on"}, source);
}
} // namespace vn::integration