2024-03-10 07:59:50 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2024-03-22 23:45:05 +00:00
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
|
|
|
#include <optional>
|
2024-03-10 07:59:50 +00:00
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2024-03-22 23:45:05 +00:00
|
|
|
#include "escape-string.hh"
|
2024-03-10 07:59:50 +00:00
|
|
|
#include "test-session.hh"
|
2024-03-10 07:59:50 +00:00
|
|
|
#include "tests/characterization.hh"
|
|
|
|
#include "tests/cli-literate-parser.hh"
|
2024-03-10 07:59:50 +00:00
|
|
|
#include "tests/terminal-code-eater.hh"
|
2024-03-22 23:45:05 +00:00
|
|
|
#include "util.hh"
|
2024-03-10 07:59:50 +00:00
|
|
|
|
|
|
|
using namespace std::string_literals;
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2024-03-28 23:26:42 +00:00
|
|
|
static constexpr const std::string_view REPL_PROMPT = "nix-repl> ";
|
2024-03-10 07:59:50 +00:00
|
|
|
|
2024-03-10 07:59:50 +00:00
|
|
|
// ASCII ENQ character
|
2024-03-28 23:26:42 +00:00
|
|
|
static constexpr const std::string_view AUTOMATION_PROMPT = "\x05";
|
2024-03-10 07:59:50 +00:00
|
|
|
|
|
|
|
static std::string_view trimOutLog(std::string_view outLog)
|
|
|
|
{
|
|
|
|
const std::string trailer = "\n"s + AUTOMATION_PROMPT;
|
|
|
|
if (outLog.ends_with(trailer)) {
|
|
|
|
outLog.remove_suffix(trailer.length());
|
|
|
|
}
|
|
|
|
return outLog;
|
|
|
|
}
|
|
|
|
|
2024-03-10 07:59:50 +00:00
|
|
|
class ReplSessionTest : public CharacterizationTest
|
|
|
|
{
|
|
|
|
Path unitTestData = getUnitTestData();
|
|
|
|
|
|
|
|
public:
|
|
|
|
Path goldenMaster(std::string_view testStem) const override
|
|
|
|
{
|
|
|
|
return unitTestData + "/" + testStem;
|
|
|
|
}
|
2024-03-10 07:59:50 +00:00
|
|
|
|
2024-03-22 23:45:05 +00:00
|
|
|
void runReplTest(const std::string content, std::vector<std::string> extraArgs = {}) const
|
2024-03-10 07:59:50 +00:00
|
|
|
{
|
2024-03-22 23:45:05 +00:00
|
|
|
auto parsed = cli_literate_parser::parse(
|
|
|
|
content, cli_literate_parser::Config{.prompt = std::string(REPL_PROMPT), .indent = 2}
|
|
|
|
);
|
|
|
|
parsed.interpolatePwd(unitTestData);
|
2024-03-10 07:59:50 +00:00
|
|
|
|
|
|
|
// FIXME: why does this need two --quiets
|
2024-03-22 23:45:05 +00:00
|
|
|
// show-trace is on by default due to test configuration, but is not a
|
|
|
|
// standard
|
|
|
|
Strings args{
|
|
|
|
"--quiet",
|
|
|
|
"repl",
|
|
|
|
"--quiet",
|
|
|
|
"--option",
|
|
|
|
"show-trace",
|
|
|
|
"false",
|
|
|
|
"--offline",
|
|
|
|
"--extra-experimental-features",
|
|
|
|
"repl-automation",
|
|
|
|
};
|
2024-03-10 07:59:50 +00:00
|
|
|
args.insert(args.end(), extraArgs.begin(), extraArgs.end());
|
2024-03-22 23:45:05 +00:00
|
|
|
args.insert(args.end(), parsed.args.begin(), parsed.args.end());
|
2024-03-10 07:59:50 +00:00
|
|
|
|
|
|
|
auto nixBin = canonPath(getEnvNonEmpty("NIX_BIN_DIR").value_or(NIX_BIN_DIR));
|
|
|
|
|
|
|
|
auto process = RunningProcess::start(nixBin + "/nix", args);
|
2024-03-22 23:45:05 +00:00
|
|
|
auto session = TestSession(std::string(AUTOMATION_PROMPT), std::move(process));
|
|
|
|
|
|
|
|
for (auto & event : parsed.syntax) {
|
|
|
|
std::visit(
|
|
|
|
overloaded{
|
|
|
|
[&](const cli_literate_parser::Command & e) {
|
|
|
|
ASSERT_TRUE(session.waitForPrompt());
|
|
|
|
if (e.text == ":quit") {
|
|
|
|
// If we quit the repl explicitly, we won't have a
|
|
|
|
// prompt when we're done.
|
|
|
|
parsed.shouldStart = false;
|
|
|
|
}
|
|
|
|
session.runCommand(e.text);
|
|
|
|
},
|
|
|
|
[&](const auto & e) {},
|
|
|
|
},
|
|
|
|
event
|
|
|
|
);
|
2024-03-10 07:59:50 +00:00
|
|
|
}
|
2024-03-22 23:45:05 +00:00
|
|
|
if (parsed.shouldStart) {
|
|
|
|
ASSERT_TRUE(session.waitForPrompt());
|
2024-03-10 07:59:50 +00:00
|
|
|
}
|
|
|
|
session.close();
|
|
|
|
|
2024-03-22 23:45:05 +00:00
|
|
|
auto replacedOutLog =
|
|
|
|
boost::algorithm::replace_all_copy(session.outLog, unitTestData, "$TEST_DATA");
|
2024-03-10 07:59:50 +00:00
|
|
|
auto cleanedOutLog = trimOutLog(replacedOutLog);
|
|
|
|
|
2024-03-22 23:45:05 +00:00
|
|
|
auto parsedOutLog = cli_literate_parser::parse(
|
|
|
|
std::string(cleanedOutLog),
|
|
|
|
cli_literate_parser::Config{.prompt = std::string(AUTOMATION_PROMPT), .indent = 0}
|
|
|
|
);
|
2024-03-10 07:59:50 +00:00
|
|
|
|
2024-03-22 23:45:05 +00:00
|
|
|
auto expected = parsed.tidyOutputForComparison();
|
|
|
|
auto actual = parsedOutLog.tidyOutputForComparison();
|
2024-03-10 07:59:50 +00:00
|
|
|
|
2024-03-22 23:45:05 +00:00
|
|
|
ASSERT_EQ(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
void runReplTestPath(const std::string_view & nameBase, std::vector<std::string> extraArgs)
|
|
|
|
{
|
|
|
|
auto nixPath = goldenMaster(nameBase + ".nix");
|
|
|
|
if (pathExists(nixPath)) {
|
|
|
|
extraArgs.push_back("-f");
|
|
|
|
extraArgs.push_back(nixPath);
|
|
|
|
}
|
|
|
|
readTest(nameBase + ".test", [this, extraArgs](std::string input) {
|
|
|
|
runReplTest(input, extraArgs);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void runReplTestPath(const std::string_view & nameBase)
|
|
|
|
{
|
|
|
|
runReplTestPath(nameBase, {});
|
|
|
|
}
|
2024-03-10 07:59:50 +00:00
|
|
|
};
|
|
|
|
|
2024-03-22 23:45:05 +00:00
|
|
|
TEST_F(ReplSessionTest, round_trip)
|
2024-03-10 07:59:50 +00:00
|
|
|
{
|
2024-03-22 23:45:05 +00:00
|
|
|
writeTest("basic.test", [this]() {
|
2024-03-10 07:59:50 +00:00
|
|
|
const std::string content = readFile(goldenMaster("basic.test"));
|
2024-03-22 23:45:05 +00:00
|
|
|
auto parsed = cli_literate_parser::parse(
|
|
|
|
content, cli_literate_parser::Config{.prompt = std::string(REPL_PROMPT)}
|
|
|
|
);
|
2024-03-10 07:59:50 +00:00
|
|
|
|
|
|
|
std::ostringstream out{};
|
2024-03-22 23:45:05 +00:00
|
|
|
for (auto & node : parsed.syntax) {
|
|
|
|
cli_literate_parser::unparseNode(out, node, true);
|
2024-03-10 07:59:50 +00:00
|
|
|
}
|
|
|
|
return out.str();
|
|
|
|
});
|
2024-03-22 23:45:05 +00:00
|
|
|
}
|
2024-03-10 07:59:50 +00:00
|
|
|
|
2024-03-22 23:45:05 +00:00
|
|
|
TEST_F(ReplSessionTest, tidy)
|
|
|
|
{
|
|
|
|
writeTest("basic.ast", [this]() {
|
|
|
|
const std::string content = readFile(goldenMaster("basic.test"));
|
|
|
|
auto parsed = cli_literate_parser::parse(
|
|
|
|
content, cli_literate_parser::Config{.prompt = std::string(REPL_PROMPT)}
|
|
|
|
);
|
|
|
|
std::ostringstream out{};
|
|
|
|
for (auto & node : parsed.syntax) {
|
|
|
|
out << debugNode(node) << "\n";
|
|
|
|
}
|
|
|
|
return out.str();
|
|
|
|
});
|
2024-03-10 07:59:50 +00:00
|
|
|
writeTest("basic_tidied.ast", [this]() {
|
|
|
|
const std::string content = readFile(goldenMaster("basic.test"));
|
2024-03-22 23:45:05 +00:00
|
|
|
auto parsed = cli_literate_parser::parse(
|
|
|
|
content, cli_literate_parser::Config{.prompt = std::string(REPL_PROMPT)}
|
|
|
|
);
|
|
|
|
auto tidied = parsed.tidyOutputForComparison();
|
2024-03-10 07:59:50 +00:00
|
|
|
std::ostringstream out{};
|
2024-03-22 23:45:05 +00:00
|
|
|
for (auto & node : tidied) {
|
|
|
|
out << debugNode(node) << "\n";
|
2024-03-10 07:59:50 +00:00
|
|
|
}
|
|
|
|
return out.str();
|
|
|
|
});
|
2024-03-10 07:59:50 +00:00
|
|
|
}
|
2024-03-10 07:59:50 +00:00
|
|
|
|
2024-03-22 23:45:05 +00:00
|
|
|
#define REPL_TEST(name) \
|
2024-03-10 07:59:50 +00:00
|
|
|
TEST_F(ReplSessionTest, name) \
|
2024-03-22 23:45:05 +00:00
|
|
|
{ \
|
|
|
|
runReplTestPath(#name); \
|
2024-03-10 07:59:50 +00:00
|
|
|
}
|
|
|
|
|
2024-03-22 23:45:05 +00:00
|
|
|
REPL_TEST(basic_repl);
|
|
|
|
REPL_TEST(no_nested_debuggers);
|
|
|
|
REPL_TEST(regression_9917);
|
|
|
|
REPL_TEST(regression_9918);
|
|
|
|
REPL_TEST(regression_l145);
|
|
|
|
REPL_TEST(stack_vars);
|
2024-03-11 09:02:09 +00:00
|
|
|
REPL_TEST(repl_printing);
|
2024-03-10 07:59:50 +00:00
|
|
|
|
2024-03-22 23:45:05 +00:00
|
|
|
}; // namespace nix
|