showTrace flag for ErrorInfo; showTrace test.

This commit is contained in:
Ben Burdette 2020-06-25 09:23:12 -06:00
parent 9c0e1fd4f1
commit 9ab808c926
4 changed files with 61 additions and 34 deletions

View file

@ -303,6 +303,7 @@ int handleExceptions(const string & programName, std::function<void()> fun)
ReceiveInterrupts receiveInterrupts; // FIXME: need better place for this
ErrorInfo::programName = baseNameOf(programName);
ErrorInfo::showTrace = settings.showTrace;
string error = ANSI_RED "error:" ANSI_NORMAL " ";
try {
@ -323,9 +324,6 @@ int handleExceptions(const string & programName, std::function<void()> fun)
printError("Try '%1% --help' for more information.", programName);
return 1;
} catch (BaseError & e) {
// TODO showTrace as argument, or have calcWhat check settings?
// if (settings.showTrace && e.prefix() != "")
// printError(e.prefix());
logError(e.info());
// TODO fix to detect non-empty trace here.
if (e.hasTrace() && !settings.showTrace)

View file

@ -34,6 +34,7 @@ const string& BaseError::calcWhat() const
}
std::optional<string> ErrorInfo::programName = std::nullopt;
bool ErrorInfo::showTrace = false;
std::ostream& operator<<(std::ostream &os, const hintformat &hf)
{
@ -325,6 +326,7 @@ std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo)
}
// traces
if (ErrorInfo::showTrace) {
for (auto iter = einfo.traces.rbegin(); iter != einfo.traces.rend(); ++iter)
{
try {
@ -363,6 +365,7 @@ std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo)
out << iter->hint.str() << std::endl;
}
}
}
return out;
}

View file

@ -107,6 +107,7 @@ struct ErrorInfo {
std::list<Trace> traces;
static std::optional<string> programName;
static bool showTrace;
};
std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo);

View file

@ -251,6 +251,7 @@ namespace nix {
TEST(addTrace, showTracesWithShowTrace) {
SymbolTable testTable;
ErrorInfo::showTrace = true;
auto problem_file = testTable.create(test_file);
auto oneliner_file = testTable.create(one_liner);
@ -272,6 +273,30 @@ namespace nix {
ASSERT_STREQ(str.c_str(), "\x1B[31;1merror:\x1B[0m\x1B[34;1m --- AssertionError --- error-unit-test\x1B[0m\n\x1B[34;1mat: \x1B[33;1m(2:13)\x1B[34;1m from command line argument\x1B[0m\n\na well-known problem occurred\n\n 1| previous line of code\n 2| this is the problem line of code\n | \x1B[31;1m^\x1B[0m\n 3| next line of code\n\nit has been \x1B[33;1mzero\x1B[0m days since our last error\n\x1B[34;1m--- show-trace output ---\nwhile trying to compute \x1B[33;1m42\x1B[0m\n\x1B[34;1mat: \x1B[33;1m(1:19)\x1B[34;1m from stdin\x1B[0m\n 1| this is the other problem line of code\n | \x1B[31;1m^\x1B[0m\n");
}
TEST(addTrace, hideTracesWithoutShowTrace) {
SymbolTable testTable;
ErrorInfo::showTrace = false;
auto problem_file = testTable.create(test_file);
auto oneliner_file = testTable.create(one_liner);
auto e = AssertionError(ErrorInfo {
.name = "wat",
.description = "a well-known problem occurred",
.hint = hintfmt("it has been %1% days since our last error", "zero"),
.errPos = Pos(foString, problem_file, 2, 13),
});
e.addTrace(Pos(foStdin, oneliner_file, 1, 19), "while trying to compute %1%", 42);
testing::internal::CaptureStderr();
logError(e.info());
auto str = testing::internal::GetCapturedStderr();
ASSERT_STREQ(str.c_str(), "\x1B[31;1merror:\x1B[0m\x1B[34;1m --- AssertionError --- error-unit-test\x1B[0m\n\x1B[34;1mat: \x1B[33;1m(2:13)\x1B[34;1m from command line argument\x1B[0m\n\na well-known problem occurred\n\n 1| previous line of code\n 2| this is the problem line of code\n | \x1B[31;1m^\x1B[0m\n 3| next line of code\n\nit has been \x1B[33;1mzero\x1B[0m days since our last error\n");
}
/* ----------------------------------------------------------------------------
* hintfmt
* --------------------------------------------------------------------------*/