testthat
testthat copied to clipboard
test_file() provoking mysterious warning when test_that block includes a function that opens and closes an ODBC connection
I have a sample package, https://github.com/dcaseykc/odbctest, that contains one function-- hello
. hello
's purpose is to open a database connection via odbc, close it, and then return 1.
When I test the package (e.g. testthat::test_file) with valid credentials in the main test file I get a semi-mysterious error:
In is.call(expr) && as.character(expr[[1]]) %in% c("<-", "=") :
'length(x) = 3 > 1' in coercion to 'logical(1)'
Based on my research, it seems like this error is the R interpreter (or similar) finding an if statement with a logical vector of length > 1
When I reran things with Sys.setenv('_R_CHECK_LENGTH_1_LOGIC2_'='verbose')
I got the error trace I've posted below. I know the traceback mostly describes/refers to some code from the odbc package , but I've posted this issue here because the issue does not pop up when running the offending test code manually (e.g. outside the test_that
block. That said, please tell me if I should migrate this report to the odbc maintainers.
Also, apologizes for not having things entirely self contained. Setting up a temp DB that works with odbc is a bit outside of my wheelhouse. I will investigate how to do that and update this issue I've I'm able to figure it out.
> testthat::test_file(path = './tests/testthat/test-hello.R')
══ Testing test-hello.R ═════════════════════════════════════════════════════════════════
[ FAIL 0 | WARN 0 | SKIP 0 | PASS 1 ] Done!
----------- FAILURE REPORT --------------
--- failure: length > 1 in coercion to logical ---
--- srcref ---
:
--- package (from environment) ---
odbc
--- call from context ---
doTryCatch(return(expr), name, parentenv, handler)
--- call from argument ---
is.call(expr) && as.character(expr[[1]]) %in% c("<-", "=")
--- R stacktrace ---
where 1: doTryCatch(return(expr), name, parentenv, handler)
where 2: tryCatchOne(expr, names, parentenv, handlers[[1L]])
where 3: tryCatchList(expr, classes, parentenv, handlers)
where 4: tryCatch({
if (is.call(expr) && as.character(expr[[1]]) %in% c("<-",
"=") && "dbConnect" %in% as.character(expr[[3]][[1]])) {
on_connection_opened(eval(expr[[2]]), paste(c("library(DBI)",
deparse(expr)), collapse = "\n"))
}
}, error = function(e) {
warning("Could not notify connection observer. ", e$message,
call. = FALSE)
})
where 5: cb(expr, value, succeeded, visible)
--- value of length: 3 type: logical ---
[1] FALSE FALSE FALSE
--- function from context ---
function (expr, name, parentenv, handler)
{
.Internal(.addCondHands(name, list(handler), parentenv, environment(),
FALSE))
expr
}
<bytecode: 0x000001ec170c09f0>
<environment: 0x000001ec2b9c1438>
--- function search by body ---
----------- END OF FAILURE REPORT --------------
warning messages from top-level task callback '1'
Warning message:
Could not notify connection observer. 'length(x) = 3 > 1' in coercion to 'logical(1)'
sessionInfo:
R version 4.2.0 (2022-04-22 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8
[3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C
[5] LC_TIME=English_United States.utf8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] odbctest_0.1.0 testthat_3.1.4 keyring_1.3.0 DBI_1.1.2 odbc_1.3.3
loaded via a namespace (and not attached):
[1] xfun_0.30 remotes_2.4.2 purrr_0.3.4 rcmdcheck_1.4.0
[5] vctrs_0.4.1 usethis_2.1.5 utf8_1.2.2 blob_1.2.3
[9] rlang_1.0.2 pkgbuild_1.3.1 pillar_1.7.0 glue_1.6.2
[13] withr_2.5.0 waldo_0.4.0 xopen_1.0.0 bit64_4.0.5
[17] sessioninfo_1.2.2 lifecycle_1.0.1 stringr_1.4.0 devtools_2.4.3
[21] evaluate_0.15 memoise_2.0.1 knitr_1.39 callr_3.7.0
[25] fastmap_1.1.0 ps_1.7.0 curl_4.3.2 fansi_1.0.3
[29] highr_0.9 Rcpp_1.0.8.3 cachem_1.0.6 desc_1.4.1
[33] pkgload_1.2.4 fs_1.5.2 bit_4.0.4 brio_1.1.3
[37] hms_1.1.1 digest_0.6.29 stringi_1.7.6 processx_3.5.3
[41] rprojroot_2.0.3 cli_3.3.0 tools_4.2.0 magrittr_2.0.3
[45] tibble_3.1.6 crayon_1.5.1 whisker_0.4 pkgconfig_2.0.3
[49] ellipsis_0.3.2 data.table_1.14.2 xml2_1.3.3 prettyunits_1.1.1
[53] assertthat_0.2.1 roxygen2_7.1.2 rstudioapi_0.13 R6_2.5.1
[57] compiler_4.2.0
I've encountered the same issue. I'm trying to initialize a db connection using odbc driver in my helper.R.
I get the following error on executing devtools::load_all()
warning messages from top-level task callback '1'
Warning message:
In is.call(expr) && as.character(expr[[1]]) %in% c("<-", "=") :
'length(x) = 3 > 1' in coercion to 'logical(1)'
SessionInfo:
R version 4.2.0 (2022-04-22)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so
locale:
[1] LC_CTYPE=en_AU.UTF-8 LC_NUMERIC=C LC_TIME=en_AU.UTF-8 LC_COLLATE=en_AU.UTF-8 LC_MONETARY=en_AU.UTF-8
[6] LC_MESSAGES=en_AU.UTF-8 LC_PAPER=en_AU.UTF-8 LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_AU.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] DotmaticsUtils_0.0.0.9000 forcats_0.5.1 stringr_1.4.0 dplyr_1.0.9 purrr_0.3.4
[6] readr_2.1.2 tidyr_1.2.0 tibble_3.1.7 ggplot2_3.3.6 tidyverse_1.3.1
[11] testthat_3.1.4 devtools_2.4.3 usethis_2.1.6
loaded via a namespace (and not attached):
[1] fs_1.5.2 lubridate_1.8.0 bit64_4.0.5 filelock_1.0.2 httr_1.4.3 rprojroot_2.0.3 repr_1.1.4
[8] tools_4.2.0 backports_1.4.1 utf8_1.2.2 R6_2.5.1 DBI_1.1.2 colorspace_2.0-3 withr_2.5.0
[15] tidyselect_1.1.2 sodium_1.2.1 prettyunits_1.1.1 processx_3.5.3 bit_4.0.4 compiler_4.2.0 cli_3.3.0
[22] rvest_1.0.2 xml2_1.3.3 desc_1.4.1 scales_1.2.0 callr_3.7.0 odbc_1.3.3 askpass_1.1
[29] rappdirs_0.3.3 digest_0.6.29 base64enc_0.1-3 pkgconfig_2.0.3 htmltools_0.5.2 sessioninfo_1.2.2 dbplyr_2.2.1
[36] fastmap_1.1.0 rlang_1.0.3 readxl_1.4.0 keyring_1.3.0 rstudioapi_0.13 RSQLite_2.2.14 generics_0.1.2
[43] jsonlite_1.8.0 magrittr_2.0.3 Rcpp_1.0.8.3 munsell_0.5.0 fansi_1.0.3 lifecycle_1.0.1 stringi_1.7.6
[50] whisker_0.4 yaml_2.3.5 brio_1.1.3 pkgbuild_1.3.1 grid_4.2.0 blob_1.2.3 crayon_1.5.1
[57] haven_2.5.0 hms_1.1.1 knitr_1.39 ps_1.7.0 pillar_1.7.0 pkgload_1.3.0 reprex_2.0.1
[64] glue_1.6.2 remotes_2.4.2 modelr_0.1.8 vctrs_0.4.1 tzdb_0.3.0 cellranger_1.1.0 gtable_0.3.0
[71] assertthat_0.2.1 cachem_1.0.6 xfun_0.30 skimr_2.1.4 broom_1.0.0 memoise_2.0.1 ellipsis_0.3.2
I'm 90% certain this a odbc issue so can you please file it there?