From fea18347f1f7a1d4284edd37f709451dc0e59bb9 Mon Sep 17 00:00:00 2001 From: Timothy Stack Date: Sun, 2 Mar 2014 16:52:18 -0800 Subject: [PATCH] [headless] report output from commands/queries that were executed Also, add some negative test cases. --- TESTS_ENVIRONMENT.in | 15 +++++- src/lnav.cc | 55 ++++++++++++++++------ test/Makefile.am | 4 ++ test/Makefile.in | 12 ++++- test/logfile_multiline.0 | 3 ++ test/test_cmds.sh | 98 ++++++++++++++++++++++++++++++++++++++++ test/test_grep_proc.sh | 5 +- test/test_sql.sh | 24 ++++++++++ 8 files changed, 199 insertions(+), 17 deletions(-) create mode 100644 test/logfile_multiline.0 create mode 100644 test/test_cmds.sh diff --git a/TESTS_ENVIRONMENT.in b/TESTS_ENVIRONMENT.in index ec288d65..4d95c6f0 100644 --- a/TESTS_ENVIRONMENT.in +++ b/TESTS_ENVIRONMENT.in @@ -34,7 +34,7 @@ LAST_TEST="" # run_test() { LAST_TEST="test: $@" - "$@" > ${test_file_base}_${test_num}.tmp 2>&1 + "$@" > ${test_file_base}_${test_num}.tmp 2> ${test_file_base}_${test_num}.err } # @@ -58,11 +58,22 @@ check_output() { echo $LAST_TEST echo $1 cat ${test_file_base}_${test_num}.diff - exit 1 + exit 1 fi test_num=`expr ${test_num} \+ 1` } +check_error_output() { + diff -u ${test_file_base}_${test_num}.err - \ + > ${test_file_base}_${test_num}.err.diff + if test $? -ne 0; then + echo $LAST_TEST + echo $1 + cat ${test_file_base}_${test_num}.err.diff + exit 1 + fi +} + # # Grep for a string in the output generated by a run_test() call. # diff --git a/src/lnav.cc b/src/lnav.cc index 58af0698..d391bb2b 100644 --- a/src/lnav.cc +++ b/src/lnav.cc @@ -846,7 +846,9 @@ public: this->tds_lines.push_back(text.substr(start, end - start)); start = end + 1; } - this->tds_lines.push_back(text.substr(start)); + if (start < text.length()) { + this->tds_lines.push_back(text.substr(start)); + } }; size_t text_line_count() @@ -2070,7 +2072,7 @@ string execute_sql(string sql, string &alt_msg) if (retcode != SQLITE_OK) { const char *errmsg = sqlite3_errmsg(lnav_data.ld_db); - retval = errmsg; + retval = "error: " + string(errmsg); alt_msg = ""; } else if (stmt == NULL) { @@ -2112,9 +2114,9 @@ string execute_sql(string sql, string &alt_msg) default: { const char *errmsg; - log_error("code %d", retcode); + log_error("sqlite3_step error code: %d", retcode); errmsg = sqlite3_errmsg(lnav_data.ld_db); - retval = errmsg; + retval = "error: " + string(errmsg); done = true; } break; @@ -2158,7 +2160,7 @@ string execute_sql(string sql, string &alt_msg) "in the log view"); } } - else { + else if (sqlite3_stmt_readonly(stmt.in())) { retval = "No rows matched"; alt_msg = ""; } @@ -2243,7 +2245,7 @@ static void execute_file(string path) } } -void execute_init_commands(readline_curses *rc) +void execute_init_commands(vector > &msgs) { if (lnav_data.ld_commands.empty()) { return; @@ -2267,10 +2269,8 @@ void execute_init_commands(readline_curses *rc) execute_file(iter->substr(1)); break; } - if (rc != NULL) { - rc->set_value(msg); - rc->set_alt_value(alt_msg); - } + + msgs.push_back(make_pair(msg, alt_msg)); } lnav_data.ld_commands.clear(); } @@ -3313,7 +3313,18 @@ static void looper(void) session_loaded = true; } - execute_init_commands(lnav_data.ld_rl_view); + { + vector > msgs; + + execute_init_commands(msgs); + + if (!msgs.empty()) { + pair last_msg = msgs.back(); + + lnav_data.ld_rl_view->set_value(last_msg.first); + lnav_data.ld_rl_view->set_alt_value(last_msg.second); + } + } } else { if (FD_ISSET(STDIN_FILENO, &ready_rfds)) { @@ -4097,9 +4108,12 @@ int main(int argc, char *argv[]) } if (lnav_data.ld_flags & LNF_HEADLESS) { + std::vector > msgs; + std::vector >::iterator msg_iter; textview_curses *tc; attr_line_t al; const std::string &line = al.get_string(); + bool found_error = false; alerter::singleton().enabled(false); @@ -4108,9 +4122,24 @@ int main(int argc, char *argv[]) lnav_data.ld_views[LNV_LOG].set_top(vis_line_t(0)); - execute_init_commands(NULL); + execute_init_commands(msgs); + if (rescan_files()) { + rebuild_indexes(true); + } + + for (msg_iter = msgs.begin(); + msg_iter != msgs.end(); + ++msg_iter) { + if (strncmp("error:", msg_iter->first.c_str(), 6) != 0) { + continue; + } + + fprintf(stderr, "%s\n", msg_iter->first.c_str()); + found_error = true; + } - if (!lnav_data.ld_view_stack.empty() && + if (!found_error && + !lnav_data.ld_view_stack.empty() && !lnav_data.ld_stdout_used) { bool suppress_empty_lines = false; list_overlay_source *los; diff --git a/test/Makefile.am b/test/Makefile.am index 02df099a..06707275 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -129,11 +129,13 @@ scripty_SOURCES = scripty.cc dist_noinst_SCRIPTS = \ parser_debugger.py \ + test_cmds.sh \ test_data_parser.sh \ test_grep_proc.sh \ test_line_buffer.sh \ test_listview.sh \ test_logfile.sh \ + test_sql.sh \ test_sql_coll_func.sh \ test_sql_str_func.sh \ test_sql_fs_func.sh \ @@ -162,6 +164,7 @@ dist_noinst_DATA = \ logfile_empty.0 \ logfile_generic.0 \ logfile_glog.0 \ + logfile_multiline.0 \ logfile_strace_log.0 \ logfile_syslog.0 \ logfile_syslog.1 \ @@ -185,6 +188,7 @@ TESTS = \ test_auto_fd \ test_auto_mem \ test_bookmarks \ + test_cmds.sh \ test_line_buffer.sh \ test_line_buffer2 \ test_listview.sh \ diff --git a/test/Makefile.in b/test/Makefile.in index 74c2aa63..33b1a563 100644 --- a/test/Makefile.in +++ b/test/Makefile.in @@ -92,7 +92,7 @@ check_PROGRAMS = drive_data_scanner$(EXEEXT) \ test_pcrepp$(EXEEXT) test_top_status$(EXEEXT) \ test_yajlpp$(EXEEXT) TESTS = test_ansi_scrubber$(EXEEXT) test_auto_fd$(EXEEXT) \ - test_auto_mem$(EXEEXT) test_bookmarks$(EXEEXT) \ + test_auto_mem$(EXEEXT) test_bookmarks$(EXEEXT) test_cmds.sh \ test_line_buffer.sh test_line_buffer2$(EXEEXT) \ test_listview.sh test_grep_proc.sh test_grep_proc2$(EXEEXT) \ test_hist_source$(EXEEXT) test_pcrepp$(EXEEXT) test_sql.sh \ @@ -684,11 +684,13 @@ slicer_LDADD = ../src/libdiag.a scripty_SOURCES = scripty.cc dist_noinst_SCRIPTS = \ parser_debugger.py \ + test_cmds.sh \ test_data_parser.sh \ test_grep_proc.sh \ test_line_buffer.sh \ test_listview.sh \ test_logfile.sh \ + test_sql.sh \ test_sql_coll_func.sh \ test_sql_str_func.sh \ test_sql_fs_func.sh \ @@ -717,6 +719,7 @@ dist_noinst_DATA = \ logfile_empty.0 \ logfile_generic.0 \ logfile_glog.0 \ + logfile_multiline.0 \ logfile_strace_log.0 \ logfile_syslog.0 \ logfile_syslog.1 \ @@ -1138,6 +1141,13 @@ test_bookmarks.log: test_bookmarks$(EXEEXT) --log-file $$b.log --trs-file $$b.trs \ $(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \ "$$tst" $(AM_TESTS_FD_REDIRECT) +test_cmds.sh.log: test_cmds.sh + @p='test_cmds.sh'; \ + b='test_cmds.sh'; \ + $(am__check_pre) $(LOG_DRIVER) --test-name "$$f" \ + --log-file $$b.log --trs-file $$b.trs \ + $(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \ + "$$tst" $(AM_TESTS_FD_REDIRECT) test_line_buffer.sh.log: test_line_buffer.sh @p='test_line_buffer.sh'; \ b='test_line_buffer.sh'; \ diff --git a/test/logfile_multiline.0 b/test/logfile_multiline.0 new file mode 100644 index 00000000..b1c7de06 --- /dev/null +++ b/test/logfile_multiline.0 @@ -0,0 +1,3 @@ +2009-07-20 22:59:27,672:DEBUG:Hello, World! + How are you today? +2009-07-20 22:59:30,221:ERROR:Goodbye, World! diff --git a/test/test_cmds.sh b/test/test_cmds.sh new file mode 100644 index 00000000..c1508686 --- /dev/null +++ b/test/test_cmds.sh @@ -0,0 +1,98 @@ +#! /bin/bash + +lnav_test="${top_builddir}/src/lnav-test" + + +run_test ${lnav_test} -n \ + -c ":adjust-log-time 2010-01-01T00:00:00" \ + ${test_dir}/logfile_access_log.0 + +check_output "adjust-log-time is not working" <