diff options
author | Jade Lovelace <lix@jade.fyi> | 2024-03-09 23:59:50 -0800 |
---|---|---|
committer | Jade Lovelace <lix@jade.fyi> | 2024-03-15 12:31:16 -0700 |
commit | 18ed6c3bdf2c03c763c0d128083f507295bcf864 (patch) | |
tree | 7b3a03bad074f50ecdff4d331ca5ce18d463ec62 /tests/functional/repl_characterization/test-session.hh | |
parent | 38571c50e6dc0ee910e9e7619e482fdbbfd644e1 (diff) |
Implement a repl characterization test system
This allows for automating using the repl without needing a PTY, with
very easy to write test files.
Change-Id: Ia8d7854edd91f93477638942cb6fc261354e6035
Diffstat (limited to 'tests/functional/repl_characterization/test-session.hh')
-rw-r--r-- | tests/functional/repl_characterization/test-session.hh | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/tests/functional/repl_characterization/test-session.hh b/tests/functional/repl_characterization/test-session.hh new file mode 100644 index 000000000..19636640b --- /dev/null +++ b/tests/functional/repl_characterization/test-session.hh @@ -0,0 +1,69 @@ +#pragma once +///@file + +#include <sched.h> +#include <string> + +#include "util.hh" +#include "tests/terminal-code-eater.hh" + +namespace nix { + +struct RunningProcess +{ + pid_t pid; + Pipe procStdin; + Pipe procStdout; + + static RunningProcess start(std::string executable, Strings args); +}; + +/** DFA that catches repl prompts */ +class ReplOutputParser +{ +public: + ReplOutputParser(std::string prompt) + : prompt(prompt) + { + assert(!prompt.empty()); + } + /** Feeds in a character and returns whether this is an open prompt */ + bool feed(char c); + + enum class State { + Prompt, + Context, + }; + +private: + State state = State::Prompt; + size_t pos_in_prompt = 0; + std::string const prompt; + + void transition(State state, char responsible_char, bool wasPrompt = false); +}; + +struct TestSession +{ + RunningProcess proc; + ReplOutputParser outputParser; + TerminalCodeEater eater; + std::string outLog; + std::string prompt; + + TestSession(std::string prompt, RunningProcess && proc) + : proc(std::move(proc)) + , outputParser(prompt) + , eater{} + , outLog{} + , prompt(prompt) + { + } + + bool waitForPrompt(); + + void runCommand(std::string command); + + void close(); +}; +}; |