2020-06-06 08:22:32 +00:00
# include "logging.hh"
# include "nixexpr.hh"
# include "util.hh"
2020-06-19 20:54:41 +00:00
# include <fstream>
2020-06-06 08:22:32 +00:00
# include <gtest/gtest.h>
namespace nix {
/* ----------------------------------------------------------------------------
* logEI
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
TEST ( logEI , catpuresBasicProperties ) {
MakeError ( TestError , Error ) ;
ErrorInfo : : programName = std : : optional ( " error-unit-test " ) ;
try {
throw TestError ( " an error for testing purposes " ) ;
} catch ( Error & e ) {
testing : : internal : : CaptureStderr ( ) ;
logger - > logEI ( e . info ( ) ) ;
auto str = testing : : internal : : GetCapturedStderr ( ) ;
2020-06-16 07:44:19 +00:00
ASSERT_STREQ ( str . c_str ( ) , " \x1B [31;1merror: \x1B [0m \x1B [34;1m --- TestError --- error-unit-test \x1B [0m \n an error for testing purposes \n " ) ;
2020-06-06 08:22:32 +00:00
}
}
TEST ( logEI , appendingHintsToPreviousError ) {
MakeError ( TestError , Error ) ;
ErrorInfo : : programName = std : : optional ( " error-unit-test " ) ;
try {
auto e = Error ( " initial error " ) ;
throw TestError ( e . info ( ) ) ;
} catch ( Error & e ) {
ErrorInfo ei = e . info ( ) ;
ei . hint = hintfmt ( " %s; subsequent error message. " , normaltxt ( e . info ( ) . hint ? e . info ( ) . hint - > str ( ) : " " ) ) ;
testing : : internal : : CaptureStderr ( ) ;
logger - > logEI ( ei ) ;
auto str = testing : : internal : : GetCapturedStderr ( ) ;
2020-06-16 07:44:19 +00:00
ASSERT_STREQ ( str . c_str ( ) , " \x1B [31;1merror: \x1B [0m \x1B [34;1m --- TestError --- error-unit-test \x1B [0m \n \x1B [33;1m \x1B [0minitial error \x1B [0m; subsequent error message. \n " ) ;
2020-06-06 08:22:32 +00:00
}
}
TEST ( logEI , picksUpSysErrorExitCode ) {
MakeError ( TestError , Error ) ;
ErrorInfo : : programName = std : : optional ( " error-unit-test " ) ;
try {
auto x = readFile ( - 1 ) ;
}
catch ( SysError & e ) {
testing : : internal : : CaptureStderr ( ) ;
logError ( e . info ( ) ) ;
auto str = testing : : internal : : GetCapturedStderr ( ) ;
2020-06-16 07:44:19 +00:00
ASSERT_STREQ ( str . c_str ( ) , " \x1B [31;1merror: \x1B [0m \x1B [34;1m --- SysError --- error-unit-test \x1B [0m \n \x1B [33;1m \x1B [0mstatting file \x1B [0m: \x1B [33;1mBad file descriptor \x1B [0m \n " ) ;
2020-06-06 08:22:32 +00:00
}
}
TEST ( logEI , loggingErrorOnInfoLevel ) {
testing : : internal : : CaptureStderr ( ) ;
logger - > logEI ( { . level = lvlInfo ,
2020-06-19 21:28:13 +00:00
. name = " Info name " ,
. description = " Info description " ,
} ) ;
2020-06-06 08:22:32 +00:00
auto str = testing : : internal : : GetCapturedStderr ( ) ;
2020-06-16 07:44:19 +00:00
ASSERT_STREQ ( str . c_str ( ) , " \x1B [32;1minfo: \x1B [0m \x1B [34;1m --- Info name --- error-unit-test \x1B [0m \n Info description \n " ) ;
2020-06-06 08:22:32 +00:00
}
TEST ( logEI , loggingErrorOnTalkativeLevel ) {
2020-06-07 13:24:49 +00:00
verbosity = lvlTalkative ;
2020-06-06 08:22:32 +00:00
testing : : internal : : CaptureStderr ( ) ;
logger - > logEI ( { . level = lvlTalkative ,
. name = " Talkative name " ,
. description = " Talkative description " ,
2020-06-19 21:28:13 +00:00
} ) ;
2020-06-06 08:22:32 +00:00
auto str = testing : : internal : : GetCapturedStderr ( ) ;
2020-06-16 07:44:19 +00:00
ASSERT_STREQ ( str . c_str ( ) , " \x1B [32;1mtalk: \x1B [0m \x1B [34;1m --- Talkative name --- error-unit-test \x1B [0m \n Talkative description \n " ) ;
2020-06-06 08:22:32 +00:00
}
TEST ( logEI , loggingErrorOnChattyLevel ) {
2020-06-07 13:24:49 +00:00
verbosity = lvlChatty ;
2020-06-06 08:22:32 +00:00
testing : : internal : : CaptureStderr ( ) ;
logger - > logEI ( { . level = lvlChatty ,
. name = " Chatty name " ,
. description = " Talkative description " ,
2020-06-19 21:28:13 +00:00
} ) ;
2020-06-06 08:22:32 +00:00
auto str = testing : : internal : : GetCapturedStderr ( ) ;
2020-06-16 07:44:19 +00:00
ASSERT_STREQ ( str . c_str ( ) , " \x1B [32;1mchat: \x1B [0m \x1B [34;1m --- Chatty name --- error-unit-test \x1B [0m \n Talkative description \n " ) ;
2020-06-06 08:22:32 +00:00
}
TEST ( logEI , loggingErrorOnDebugLevel ) {
2020-06-07 13:24:49 +00:00
verbosity = lvlDebug ;
2020-06-06 08:22:32 +00:00
testing : : internal : : CaptureStderr ( ) ;
logger - > logEI ( { . level = lvlDebug ,
. name = " Debug name " ,
. description = " Debug description " ,
2020-06-19 21:28:13 +00:00
} ) ;
2020-06-06 08:22:32 +00:00
auto str = testing : : internal : : GetCapturedStderr ( ) ;
2020-06-16 07:44:19 +00:00
ASSERT_STREQ ( str . c_str ( ) , " \x1B [33;1mdebug: \x1B [0m \x1B [34;1m --- Debug name --- error-unit-test \x1B [0m \n Debug description \n " ) ;
2020-06-06 08:22:32 +00:00
}
TEST ( logEI , loggingErrorOnVomitLevel ) {
2020-06-07 13:24:49 +00:00
verbosity = lvlVomit ;
2020-06-06 08:22:32 +00:00
testing : : internal : : CaptureStderr ( ) ;
logger - > logEI ( { . level = lvlVomit ,
. name = " Vomit name " ,
. description = " Vomit description " ,
2020-06-19 21:28:13 +00:00
} ) ;
2020-06-06 08:22:32 +00:00
auto str = testing : : internal : : GetCapturedStderr ( ) ;
2020-06-16 07:44:19 +00:00
ASSERT_STREQ ( str . c_str ( ) , " \x1B [32;1mvomit: \x1B [0m \x1B [34;1m --- Vomit name --- error-unit-test \x1B [0m \n Vomit description \n " ) ;
2020-06-06 08:22:32 +00:00
}
/* ----------------------------------------------------------------------------
* logError
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
TEST ( logError , logErrorWithoutHintOrCode ) {
testing : : internal : : CaptureStderr ( ) ;
logError ( {
. name = " name " ,
. description = " error description " ,
2020-06-19 21:28:13 +00:00
} ) ;
2020-06-06 08:22:32 +00:00
auto str = testing : : internal : : GetCapturedStderr ( ) ;
2020-06-16 07:44:19 +00:00
ASSERT_STREQ ( str . c_str ( ) , " \x1B [31;1merror: \x1B [0m \x1B [34;1m --- name --- error-unit-test \x1B [0m \n error description \n " ) ;
2020-06-06 08:22:32 +00:00
}
TEST ( logError , logErrorWithPreviousAndNextLinesOfCode ) {
SymbolTable testTable ;
auto problem_file = testTable . create ( " myfile.nix " ) ;
testing : : internal : : CaptureStderr ( ) ;
logError ( {
. name = " error name " ,
. description = " error with code lines " ,
. hint = hintfmt ( " this hint has %1% templated %2%!! " ,
2020-06-19 21:28:13 +00:00
" yellow " ,
" values " ) ,
2020-06-06 08:22:32 +00:00
. nixCode = NixCode {
2020-06-19 21:28:13 +00:00
. errPos = Pos ( problem_file , 40 , 13 ) ,
. prevLineOfCode = " previous line of code " ,
. errLineOfCode = " this is the problem line of code " ,
. nextLineOfCode = " next line of code " ,
2020-06-06 08:22:32 +00:00
} } ) ;
auto str = testing : : internal : : GetCapturedStderr ( ) ;
2020-06-16 07:44:19 +00:00
ASSERT_STREQ ( str . c_str ( ) , " \x1B [31;1merror: \x1B [0m \x1B [34;1m --- error name --- error-unit-test \x1B [0m \n in file: \x1B [34;1mmyfile.nix (40:13) \x1B [0m \n \n error with code lines \n \n 39| previous line of code \n 40| this is the problem line of code \n | \x1B [31;1m^ \x1B [0m \n 41| next line of code \n \n this hint has \x1B [33;1myellow \x1B [0m templated \x1B [33;1mvalues \x1B [0m!! \n " ) ;
2020-06-06 08:22:32 +00:00
}
TEST ( logError , logErrorWithoutLinesOfCode ) {
SymbolTable testTable ;
auto problem_file = testTable . create ( " myfile.nix " ) ;
testing : : internal : : CaptureStderr ( ) ;
logError ( {
. name = " error name " ,
. description = " error without any code lines. " ,
. hint = hintfmt ( " this hint has %1% templated %2%!! " ,
2020-06-19 21:28:13 +00:00
" yellow " ,
" values " ) ,
2020-06-06 08:22:32 +00:00
. nixCode = NixCode {
2020-06-19 21:28:13 +00:00
. errPos = Pos ( problem_file , 40 , 13 )
2020-06-06 08:22:32 +00:00
} } ) ;
auto str = testing : : internal : : GetCapturedStderr ( ) ;
2020-06-16 07:44:19 +00:00
ASSERT_STREQ ( str . c_str ( ) , " \x1B [31;1merror: \x1B [0m \x1B [34;1m --- error name --- error-unit-test \x1B [0m \n in file: \x1B [34;1mmyfile.nix (40:13) \x1B [0m \n \n error without any code lines. \n \n this hint has \x1B [33;1myellow \x1B [0m templated \x1B [33;1mvalues \x1B [0m!! \n " ) ;
2020-06-06 08:22:32 +00:00
}
TEST ( logError , logErrorWithOnlyHintAndName ) {
SymbolTable testTable ;
auto problem_file = testTable . create ( " myfile.nix " ) ;
testing : : internal : : CaptureStderr ( ) ;
logError ( {
. name = " error name " ,
. hint = hintfmt ( " hint %1% " , " only " ) ,
. nixCode = NixCode {
2020-06-19 21:28:13 +00:00
. errPos = Pos ( problem_file , 40 , 13 )
2020-06-06 08:22:32 +00:00
} } ) ;
auto str = testing : : internal : : GetCapturedStderr ( ) ;
2020-06-16 07:44:19 +00:00
ASSERT_STREQ ( str . c_str ( ) , " \x1B [31;1merror: \x1B [0m \x1B [34;1m --- error name --- error-unit-test \x1B [0m \n in file: \x1B [34;1mmyfile.nix (40:13) \x1B [0m \n \n hint \x1B [33;1monly \x1B [0m \n " ) ;
2020-06-06 08:22:32 +00:00
}
/* ----------------------------------------------------------------------------
* logWarning
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
TEST ( logWarning , logWarningWithNameDescriptionAndHint ) {
testing : : internal : : CaptureStderr ( ) ;
logWarning ( {
2020-06-19 21:28:13 +00:00
. name = " name " ,
. description = " error description " ,
. hint = hintfmt ( " there was a %1% " , " warning " ) ,
} ) ;
2020-06-06 08:22:32 +00:00
auto str = testing : : internal : : GetCapturedStderr ( ) ;
2020-06-16 07:44:19 +00:00
ASSERT_STREQ ( str . c_str ( ) , " \x1B [33;1mwarning: \x1B [0m \x1B [34;1m --- name --- error-unit-test \x1B [0m \n error description \n \n there was a \x1B [33;1mwarning \x1B [0m \n " ) ;
2020-06-06 08:22:32 +00:00
}
TEST ( logWarning , logWarningWithFileLineNumAndCode ) {
SymbolTable testTable ;
auto problem_file = testTable . create ( " myfile.nix " ) ;
testing : : internal : : CaptureStderr ( ) ;
logWarning ( {
. name = " warning name " ,
. description = " warning description " ,
. hint = hintfmt ( " this hint has %1% templated %2%!! " ,
2020-06-19 21:28:13 +00:00
" yellow " ,
" values " ) ,
2020-06-06 08:22:32 +00:00
. nixCode = NixCode {
2020-06-19 21:28:13 +00:00
. errPos = Pos ( problem_file , 40 , 13 ) ,
. prevLineOfCode = std : : nullopt ,
. errLineOfCode = " this is the problem line of code " ,
. nextLineOfCode = std : : nullopt
2020-06-06 08:22:32 +00:00
} } ) ;
auto str = testing : : internal : : GetCapturedStderr ( ) ;
2020-06-16 07:44:19 +00:00
ASSERT_STREQ ( str . c_str ( ) , " \x1B [33;1mwarning: \x1B [0m \x1B [34;1m --- warning name --- error-unit-test \x1B [0m \n in file: \x1B [34;1mmyfile.nix (40:13) \x1B [0m \n \n warning description \n \n 40| this is the problem line of code \n | \x1B [31;1m^ \x1B [0m \n \n this hint has \x1B [33;1myellow \x1B [0m templated \x1B [33;1mvalues \x1B [0m!! \n " ) ;
2020-06-06 08:22:32 +00:00
}
2020-06-19 20:54:41 +00:00
/* ----------------------------------------------------------------------------
* hintfmt
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2020-06-19 21:28:13 +00:00
TEST ( hintfmt , percentStringWithoutArgs ) {
2020-06-19 20:54:41 +00:00
2020-06-19 21:28:13 +00:00
const char * teststr = " this is 100%s correct! " ;
auto hint = hintfmt ( teststr ) ;
2020-06-19 20:54:41 +00:00
2020-06-19 21:28:13 +00:00
ASSERT_STREQ ( hint . str ( ) . c_str ( ) , teststr ) ;
2020-06-19 20:54:41 +00:00
2020-06-19 21:28:13 +00:00
}
2020-06-19 20:54:41 +00:00
2020-06-19 22:46:49 +00:00
TEST ( hintfmt , fmtToHintfmt ) {
auto hint = hintfmt ( fmt ( " the color of this this text is %1% " , " not yellow " ) ) ;
ASSERT_STREQ ( hint . str ( ) . c_str ( ) , " the color of this this text is not yellow " ) ;
}
2020-06-19 21:28:13 +00:00
TEST ( hintfmt , tooFewArguments ) {
2020-06-19 20:54:41 +00:00
2020-06-19 21:28:13 +00:00
ASSERT_STREQ (
hintfmt ( " only one arg %1% %2% " , " fulfilled " ) . str ( ) . c_str ( ) ,
" only one arg " ANSI_YELLOW " fulfilled " ANSI_NORMAL " " ) ;
}
TEST ( hintfmt , tooManyArguments ) {
ASSERT_STREQ (
hintfmt ( " what about this %1% %2% " , " %3% " , " one " , " two " ) . str ( ) . c_str ( ) ,
" what about this " ANSI_YELLOW " %3% " ANSI_NORMAL " " ANSI_YELLOW " one " ANSI_NORMAL ) ;
}
2020-06-06 08:22:32 +00:00
}