From 11e9ed30cefc02103425f4b35ca592e5a70f252c Mon Sep 17 00:00:00 2001 From: MEO265 <99362508+MEO265@users.noreply.github.com> Date: Fri, 3 May 2024 21:17:12 +0200 Subject: [PATCH] test: Add test for `convert_to_csv` --- R/utils.R | 2 +- tests/testthat/_snaps/utils/test.csv | 9 +++++++++ tests/testthat/_snaps/utils/test_with_lf.csv | 10 ++++++++++ tests/testthat/test-utils.R | 20 +++++++++++++++++++- tests/testthat/testdata/test.loggit | 4 ++-- 5 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 tests/testthat/_snaps/utils/test.csv create mode 100644 tests/testthat/_snaps/utils/test_with_lf.csv diff --git a/R/utils.R b/R/utils.R index f999aa1..71e1299 100644 --- a/R/utils.R +++ b/R/utils.R @@ -99,7 +99,7 @@ find_call <- function() { convert_to_csv <- function (file, logfile = get_logfile(), remove_message_lf = TRUE, ...) { log <- read_logs(logfile = logfile) - if(!remove_message_lf) { + if(remove_message_lf) { msg_flag <- log$log_lvl == "INFO" msg <- log$log_msg[msg_flag] log$log_msg[msg_flag] <- gsub("\n$", "", msg) diff --git a/tests/testthat/_snaps/utils/test.csv b/tests/testthat/_snaps/utils/test.csv new file mode 100644 index 0000000..2256981 --- /dev/null +++ b/tests/testthat/_snaps/utils/test.csv @@ -0,0 +1,9 @@ +"timestamp" "log_lvl" "log_msg" "extra" +"1" "2024-05-01T20:09:27+0200" "INFO" "log_1" "extra +" +"2" "2024-05-01T20:09:27+0200" "WARN" "log_96 +" NA +"3" "2024-05-01T20:09:27+0200" "INFO" "log_97" NA +"4" "2024-05-01T20:09:27+0200" "INFO" "log_98" NA +"5" "2024-05-01T20:09:27+0200" "INFO" "log_99" NA +"6" "2024-05-01T20:09:27+0200" "INFO" "log_100 " "extra" diff --git a/tests/testthat/_snaps/utils/test_with_lf.csv b/tests/testthat/_snaps/utils/test_with_lf.csv new file mode 100644 index 0000000..89b1536 --- /dev/null +++ b/tests/testthat/_snaps/utils/test_with_lf.csv @@ -0,0 +1,10 @@ +"timestamp" "log_lvl" "log_msg" "extra" +"1" "2024-05-01T20:09:27+0200" "INFO" "log_1" "extra +" +"2" "2024-05-01T20:09:27+0200" "WARN" "log_96 +" NA +"3" "2024-05-01T20:09:27+0200" "INFO" "log_97" NA +"4" "2024-05-01T20:09:27+0200" "INFO" "log_98" NA +"5" "2024-05-01T20:09:27+0200" "INFO" "log_99" NA +"6" "2024-05-01T20:09:27+0200" "INFO" "log_100 +" "extra" diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index 0163aa6..faff3ad 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -69,4 +69,22 @@ test_that("rotate_logs preserves sanitization", { rotate_logs(rotate_lines = rotate_lines, logfile = tmp_log) expect_snapshot_file(tmp_log) -}) \ No newline at end of file +}) + +test_that("read_logs on empty log", { + set_logfile(get_logfile(), confirm = FALSE, create = TRUE) + log_df <- read_logs() + expect_identical(log_df, data.frame(timestamp = character(), log_lvl = character(), log_msg = character())) +}) +cleanup() + +test_that("convert_to_csv", { + tmp_dir <- tempdir() + convert_to_csv(file = file.path(tmp_dir, "test.csv"), logfile = "testdata/test.loggit") + convert_to_csv( + file = file.path(tmp_dir, "test_with_lf.csv"), remove_message_lf = FALSE, logfile = "testdata/test.loggit" + ) + + expect_snapshot_file(file.path(tmp_dir, "test.csv")) + expect_snapshot_file(file.path(tmp_dir, "test_with_lf.csv")) +}) diff --git a/tests/testthat/testdata/test.loggit b/tests/testthat/testdata/test.loggit index f4aef44..971da15 100644 --- a/tests/testthat/testdata/test.loggit +++ b/tests/testthat/testdata/test.loggit @@ -1,5 +1,5 @@ -{"timestamp": "2024-05-01T20:09:27+0200", "log_lvl": "INFO", "log_msg": "log_1", "extra": "extra"} -{"timestamp": "2024-05-01T20:09:27+0200", "log_lvl": "INFO", "log_msg": "log_96"} +{"timestamp": "2024-05-01T20:09:27+0200", "log_lvl": "INFO", "log_msg": "log_1", "extra": "extra__LF__"} +{"timestamp": "2024-05-01T20:09:27+0200", "log_lvl": "WARN", "log_msg": "log_96 __LF__"} {"timestamp": "2024-05-01T20:09:27+0200", "log_lvl": "INFO", "log_msg": "log_97"} {"timestamp": "2024-05-01T20:09:27+0200", "log_lvl": "INFO", "log_msg": "log_98"} {"timestamp": "2024-05-01T20:09:27+0200", "log_lvl": "INFO", "log_msg": "log_99"}