2024-03-10 07:59:50 +00:00
|
|
|
#include <iostream>
|
2024-03-22 23:45:05 +00:00
|
|
|
#include <span>
|
2024-03-10 07:59:50 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "test-session.hh"
|
2024-11-12 23:07:17 +00:00
|
|
|
#include "lix/libutil/escape-char.hh"
|
|
|
|
#include "lix/libutil/processes.hh"
|
|
|
|
#include "lix/libutil/strings.hh"
|
2024-03-10 07:59:50 +00:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
static constexpr const bool DEBUG_REPL_PARSER = false;
|
|
|
|
|
|
|
|
RunningProcess RunningProcess::start(std::string executable, Strings args)
|
|
|
|
{
|
|
|
|
args.push_front(executable);
|
|
|
|
|
|
|
|
Pipe procStdin{};
|
|
|
|
Pipe procStdout{};
|
|
|
|
|
|
|
|
procStdin.create();
|
|
|
|
procStdout.create();
|
|
|
|
|
|
|
|
// This is separate from runProgram2 because we have different IO requirements
|
2024-04-05 19:15:22 +00:00
|
|
|
auto pid = startProcess([&]() {
|
2024-03-22 23:45:05 +00:00
|
|
|
if (dup2(procStdout.writeSide.get(), STDOUT_FILENO) == -1) {
|
2024-03-10 07:59:50 +00:00
|
|
|
throw SysError("dupping stdout");
|
2024-03-22 23:45:05 +00:00
|
|
|
}
|
|
|
|
if (dup2(procStdin.readSide.get(), STDIN_FILENO) == -1) {
|
2024-03-10 07:59:50 +00:00
|
|
|
throw SysError("dupping stdin");
|
2024-03-22 23:45:05 +00:00
|
|
|
}
|
2024-03-10 07:59:50 +00:00
|
|
|
procStdin.writeSide.close();
|
|
|
|
procStdout.readSide.close();
|
2024-03-22 23:45:05 +00:00
|
|
|
if (dup2(STDOUT_FILENO, STDERR_FILENO) == -1) {
|
2024-03-10 07:59:50 +00:00
|
|
|
throw SysError("dupping stderr");
|
2024-03-22 23:45:05 +00:00
|
|
|
}
|
2024-03-10 07:59:50 +00:00
|
|
|
execv(executable.c_str(), stringsToCharPtrs(args).data());
|
|
|
|
throw SysError("exec did not happen");
|
|
|
|
});
|
|
|
|
|
|
|
|
procStdout.writeSide.close();
|
|
|
|
procStdin.readSide.close();
|
|
|
|
|
|
|
|
return RunningProcess{
|
2024-04-05 19:15:22 +00:00
|
|
|
.pid = std::move(pid),
|
2024-03-10 07:59:50 +00:00
|
|
|
.procStdin = std::move(procStdin),
|
|
|
|
.procStdout = std::move(procStdout),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
[[gnu::unused]]
|
2024-03-22 23:45:05 +00:00
|
|
|
std::ostream &
|
|
|
|
operator<<(std::ostream & os, ReplOutputParser::State s)
|
2024-03-10 07:59:50 +00:00
|
|
|
{
|
|
|
|
switch (s) {
|
|
|
|
case ReplOutputParser::State::Prompt:
|
|
|
|
os << "prompt";
|
|
|
|
break;
|
|
|
|
case ReplOutputParser::State::Context:
|
|
|
|
os << "context";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReplOutputParser::transition(State new_state, char responsible_char, bool wasPrompt)
|
|
|
|
{
|
|
|
|
if constexpr (DEBUG_REPL_PARSER) {
|
2024-03-22 23:41:42 +00:00
|
|
|
std::cerr << "transition " << new_state << " for " << MaybeHexEscapedChar{responsible_char}
|
2024-03-10 07:59:50 +00:00
|
|
|
<< (wasPrompt ? " [prompt]" : "") << "\n";
|
|
|
|
}
|
|
|
|
state = new_state;
|
|
|
|
pos_in_prompt = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ReplOutputParser::feed(char c)
|
|
|
|
{
|
|
|
|
if (c == '\n') {
|
|
|
|
transition(State::Prompt, c);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
switch (state) {
|
|
|
|
case State::Context:
|
|
|
|
break;
|
|
|
|
case State::Prompt:
|
|
|
|
if (pos_in_prompt == prompt.length() - 1 && prompt[pos_in_prompt] == c) {
|
|
|
|
transition(State::Context, c, true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (pos_in_prompt >= prompt.length() - 1 || prompt[pos_in_prompt] != c) {
|
|
|
|
transition(State::Context, c);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pos_in_prompt++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-03-22 23:45:05 +00:00
|
|
|
bool TestSession::readOutThen(ReadOutThenCallback cb)
|
2024-03-10 07:59:50 +00:00
|
|
|
{
|
|
|
|
std::vector<char> buf(1024);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
ssize_t res = read(proc.procStdout.readSide.get(), buf.data(), buf.size());
|
|
|
|
|
|
|
|
if (res < 0) {
|
|
|
|
throw SysError("read");
|
|
|
|
}
|
|
|
|
if (res == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-03-22 23:45:05 +00:00
|
|
|
switch (cb(std::span(buf.data(), res))) {
|
|
|
|
case ReadOutThenCallbackResult::Stop:
|
|
|
|
return true;
|
|
|
|
case ReadOutThenCallbackResult::Continue:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TestSession::waitForPrompt()
|
|
|
|
{
|
2024-04-06 21:39:01 +00:00
|
|
|
bool notEof = readOutThen([&](std::span<const char> s) -> ReadOutThenCallbackResult {
|
2024-03-10 07:59:50 +00:00
|
|
|
bool foundPrompt = false;
|
2024-03-22 23:45:05 +00:00
|
|
|
|
|
|
|
for (auto ch : s) {
|
2024-03-10 07:59:50 +00:00
|
|
|
// foundPrompt = foundPrompt || outputParser.feed(buf[i]);
|
|
|
|
bool wasEaten = true;
|
2024-03-22 23:45:05 +00:00
|
|
|
eater.feed(ch, [&](char c) {
|
2024-03-10 07:59:50 +00:00
|
|
|
wasEaten = false;
|
2024-03-22 23:45:05 +00:00
|
|
|
foundPrompt = outputParser.feed(ch) || foundPrompt;
|
2024-03-10 07:59:50 +00:00
|
|
|
|
|
|
|
outLog.push_back(c);
|
|
|
|
});
|
|
|
|
|
|
|
|
if constexpr (DEBUG_REPL_PARSER) {
|
2024-03-22 23:45:05 +00:00
|
|
|
std::cerr << "raw " << MaybeHexEscapedChar{ch} << (wasEaten ? " [eaten]" : "") << "\n";
|
2024-03-10 07:59:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-22 23:45:05 +00:00
|
|
|
return foundPrompt ? ReadOutThenCallbackResult::Stop : ReadOutThenCallbackResult::Continue;
|
|
|
|
});
|
|
|
|
|
|
|
|
return notEof;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestSession::wait()
|
|
|
|
{
|
2024-04-06 21:39:01 +00:00
|
|
|
readOutThen([&](std::span<const char> s) {
|
2024-03-22 23:45:05 +00:00
|
|
|
for (auto ch : s) {
|
|
|
|
eater.feed(ch, [&](char c) {
|
|
|
|
outputParser.feed(c);
|
|
|
|
outLog.push_back(c);
|
|
|
|
});
|
2024-03-10 07:59:50 +00:00
|
|
|
}
|
2024-03-22 23:45:05 +00:00
|
|
|
// just keep reading till we hit eof
|
|
|
|
return ReadOutThenCallbackResult::Continue;
|
|
|
|
});
|
2024-03-10 07:59:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestSession::close()
|
|
|
|
{
|
|
|
|
proc.procStdin.close();
|
2024-03-22 23:45:05 +00:00
|
|
|
wait();
|
2024-03-10 07:59:50 +00:00
|
|
|
proc.procStdout.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestSession::runCommand(std::string command)
|
|
|
|
{
|
2024-03-22 23:45:05 +00:00
|
|
|
if constexpr (DEBUG_REPL_PARSER) {
|
2024-03-10 07:59:50 +00:00
|
|
|
std::cerr << "runCommand " << command << "\n";
|
2024-03-22 23:45:05 +00:00
|
|
|
}
|
2024-03-10 07:59:50 +00:00
|
|
|
command += "\n";
|
2024-05-14 00:05:08 +00:00
|
|
|
// We have to feed a newline into the output parser, since Lix might not
|
2024-03-10 07:59:50 +00:00
|
|
|
// give us a newline before a prompt in all cases (it might clear line
|
|
|
|
// first, e.g.)
|
|
|
|
outputParser.feed('\n');
|
|
|
|
// Echo is disabled, so we have to make our own
|
|
|
|
outLog.append(command);
|
|
|
|
writeFull(proc.procStdin.writeSide.get(), command, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|