forked from lix-project/lix
showTrace flag for ErrorInfo; showTrace test.
This commit is contained in:
parent
9c0e1fd4f1
commit
9ab808c926
|
@ -303,6 +303,7 @@ int handleExceptions(const string & programName, std::function<void()> fun)
|
||||||
ReceiveInterrupts receiveInterrupts; // FIXME: need better place for this
|
ReceiveInterrupts receiveInterrupts; // FIXME: need better place for this
|
||||||
|
|
||||||
ErrorInfo::programName = baseNameOf(programName);
|
ErrorInfo::programName = baseNameOf(programName);
|
||||||
|
ErrorInfo::showTrace = settings.showTrace;
|
||||||
|
|
||||||
string error = ANSI_RED "error:" ANSI_NORMAL " ";
|
string error = ANSI_RED "error:" ANSI_NORMAL " ";
|
||||||
try {
|
try {
|
||||||
|
@ -323,9 +324,6 @@ int handleExceptions(const string & programName, std::function<void()> fun)
|
||||||
printError("Try '%1% --help' for more information.", programName);
|
printError("Try '%1% --help' for more information.", programName);
|
||||||
return 1;
|
return 1;
|
||||||
} catch (BaseError & e) {
|
} catch (BaseError & e) {
|
||||||
// TODO showTrace as argument, or have calcWhat check settings?
|
|
||||||
// if (settings.showTrace && e.prefix() != "")
|
|
||||||
// printError(e.prefix());
|
|
||||||
logError(e.info());
|
logError(e.info());
|
||||||
// TODO fix to detect non-empty trace here.
|
// TODO fix to detect non-empty trace here.
|
||||||
if (e.hasTrace() && !settings.showTrace)
|
if (e.hasTrace() && !settings.showTrace)
|
||||||
|
|
|
@ -34,6 +34,7 @@ const string& BaseError::calcWhat() const
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<string> ErrorInfo::programName = std::nullopt;
|
std::optional<string> ErrorInfo::programName = std::nullopt;
|
||||||
|
bool ErrorInfo::showTrace = false;
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream &os, const hintformat &hf)
|
std::ostream& operator<<(std::ostream &os, const hintformat &hf)
|
||||||
{
|
{
|
||||||
|
@ -325,42 +326,44 @@ std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
// traces
|
// traces
|
||||||
for (auto iter = einfo.traces.rbegin(); iter != einfo.traces.rend(); ++iter)
|
if (ErrorInfo::showTrace) {
|
||||||
{
|
for (auto iter = einfo.traces.rbegin(); iter != einfo.traces.rend(); ++iter)
|
||||||
try {
|
{
|
||||||
if (nl)
|
try {
|
||||||
out << std::endl << prefix;
|
if (nl)
|
||||||
|
out << std::endl << prefix;
|
||||||
|
|
||||||
const string tracetitle(" show-trace output ");
|
const string tracetitle(" show-trace output ");
|
||||||
|
|
||||||
int fill = errwidth - tracetitle.length();
|
int fill = errwidth - tracetitle.length();
|
||||||
int lw = 0;
|
int lw = 0;
|
||||||
int rw = 0;
|
int rw = 0;
|
||||||
const int min_dashes = 3;
|
const int min_dashes = 3;
|
||||||
if (fill > min_dashes * 2) {
|
if (fill > min_dashes * 2) {
|
||||||
if (fill % 2 != 0) {
|
if (fill % 2 != 0) {
|
||||||
lw = fill / 2;
|
lw = fill / 2;
|
||||||
rw = lw + 1;
|
rw = lw + 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
lw = rw = fill / 2;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
lw = rw = min_dashes;
|
||||||
lw = rw = fill / 2;
|
|
||||||
}
|
out << ANSI_BLUE << std::string(lw, '-') << tracetitle << std::string(rw, '-') << std::endl << prefix;
|
||||||
|
out << iter->hint.str() << std::endl;
|
||||||
|
|
||||||
|
auto pos = *iter->pos;
|
||||||
|
printAtPos(prefix, pos, out);
|
||||||
|
nl = true;
|
||||||
|
auto loc = getCodeLines(pos);
|
||||||
|
if (loc.has_value())
|
||||||
|
printCodeLines(out, prefix, pos, *loc);
|
||||||
|
} catch(const std::bad_optional_access& e) {
|
||||||
|
out << iter->hint.str() << std::endl;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
lw = rw = min_dashes;
|
|
||||||
|
|
||||||
out << ANSI_BLUE << std::string(lw, '-') << tracetitle << std::string(rw, '-') << std::endl << prefix;
|
|
||||||
out << iter->hint.str() << std::endl;
|
|
||||||
|
|
||||||
auto pos = *iter->pos;
|
|
||||||
printAtPos(prefix, pos, out);
|
|
||||||
nl = true;
|
|
||||||
auto loc = getCodeLines(pos);
|
|
||||||
if (loc.has_value())
|
|
||||||
printCodeLines(out, prefix, pos, *loc);
|
|
||||||
} catch(const std::bad_optional_access& e) {
|
|
||||||
out << iter->hint.str() << std::endl;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -107,6 +107,7 @@ struct ErrorInfo {
|
||||||
std::list<Trace> traces;
|
std::list<Trace> traces;
|
||||||
|
|
||||||
static std::optional<string> programName;
|
static std::optional<string> programName;
|
||||||
|
static bool showTrace;
|
||||||
};
|
};
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo);
|
std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo);
|
||||||
|
|
|
@ -251,6 +251,7 @@ namespace nix {
|
||||||
|
|
||||||
TEST(addTrace, showTracesWithShowTrace) {
|
TEST(addTrace, showTracesWithShowTrace) {
|
||||||
SymbolTable testTable;
|
SymbolTable testTable;
|
||||||
|
ErrorInfo::showTrace = true;
|
||||||
auto problem_file = testTable.create(test_file);
|
auto problem_file = testTable.create(test_file);
|
||||||
|
|
||||||
auto oneliner_file = testTable.create(one_liner);
|
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");
|
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
|
* hintfmt
|
||||||
* --------------------------------------------------------------------------*/
|
* --------------------------------------------------------------------------*/
|
||||||
|
|
Loading…
Reference in a new issue