diff --git a/src/builtin_test.cpp b/src/builtin_test.cpp index 2701389d4..05313353d 100644 --- a/src/builtin_test.cpp +++ b/src/builtin_test.cpp @@ -820,7 +820,7 @@ int builtin_test(parser_t &parser, io_streams_t &streams, wchar_t **argv) { wcstring_list_t eval_errors; bool result = expr->evaluate(eval_errors); - if (!eval_errors.empty()) { + if (!eval_errors.empty() && ! should_suppress_stderr_for_tests()) { printf("test returned eval errors:\n"); for (size_t i = 0; i < eval_errors.size(); i++) { printf("\t%ls\n", eval_errors.at(i).c_str()); diff --git a/src/common.cpp b/src/common.cpp index 76d2b752a..c51e0ccdf 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -533,11 +533,15 @@ ssize_t read_loop(int fd, void *buff, size_t count) { return result; } +bool should_suppress_stderr_for_tests() { + // Hack to not print error messages in the tests. + return program_name && !wcscmp(program_name, TESTS_PROGRAM_NAME); +} + static bool should_debug(int level) { if (level > debug_level) return false; - // Hack to not print error messages in the tests. - if (program_name && !wcscmp(program_name, L"(ignore)")) return false; + if (should_suppress_stderr_for_tests()) return false; return true; } diff --git a/src/common.h b/src/common.h index 61af1e9cc..303605fb6 100644 --- a/src/common.h +++ b/src/common.h @@ -373,6 +373,10 @@ string_fuzzy_match_t string_fuzzy_match_string(const wcstring &string, /// Test if a list contains a string using a linear search. bool list_contains_string(const wcstring_list_t &list, const wcstring &str); +// Check if we are running in the test mode, where we should suppress error output +#define TESTS_PROGRAM_NAME L"(ignore)" +bool should_suppress_stderr_for_tests(); + void assert_is_main_thread(const char *who); #define ASSERT_IS_MAIN_THREAD_TRAMPOLINE(x) assert_is_main_thread(x) #define ASSERT_IS_MAIN_THREAD() ASSERT_IS_MAIN_THREAD_TRAMPOLINE(__FUNCTION__) diff --git a/src/fish_tests.cpp b/src/fish_tests.cpp index fa1238c0d..63b1a58a0 100644 --- a/src/fish_tests.cpp +++ b/src/fish_tests.cpp @@ -4072,7 +4072,9 @@ int main(int argc, char **argv) { srand((unsigned int)time(NULL)); configure_thread_assertions_for_testing(); - program_name = L"(ignore)"; + // Set the program name to this sentinel value + // This will prevent some misleading stderr output during the tests + program_name = TESTS_PROGRAM_NAME; s_arguments = argv + 1; struct utsname uname_info; diff --git a/src/parse_execution.cpp b/src/parse_execution.cpp index 3b667063e..936b78ab9 100644 --- a/src/parse_execution.cpp +++ b/src/parse_execution.cpp @@ -708,7 +708,8 @@ parse_execution_result_t parse_execution_context_t::report_errors( parser->get_backtrace(src, error_list, &backtrace_and_desc); // Print it. - fprintf(stderr, "%ls", backtrace_and_desc.c_str()); + if (! should_suppress_stderr_for_tests()) + fprintf(stderr, "%ls", backtrace_and_desc.c_str()); } return parse_execution_errored; }