From cec9413b20b3b1c2851d86253b125e20d6c2d3ca Mon Sep 17 00:00:00 2001 From: Jenny Bryan Date: Tue, 30 Nov 2021 11:57:44 -0800 Subject: [PATCH] Refactor should_show_types(); add tests --- R/utils.R | 18 +++++++-- man/should_show_types.Rd | 11 ++++-- tests/testthat/_snaps/edition-1/col-spec.md | 42 +++++++++++++++++++++ tests/testthat/_snaps/edition-2/col-spec.md | 28 ++++++++++++++ tests/testthat/setup.R | 1 - tests/testthat/test-col-spec.R | 29 +++++++------- 6 files changed, 106 insertions(+), 23 deletions(-) create mode 100644 tests/testthat/_snaps/edition-1/col-spec.md create mode 100644 tests/testthat/_snaps/edition-2/col-spec.md diff --git a/R/utils.R b/R/utils.R index eb974616..735ca9db 100644 --- a/R/utils.R +++ b/R/utils.R @@ -27,12 +27,22 @@ show_progress <- function() { #' Determine whether column types should be shown #' -#' Column types are shown unless -#' - They are disabled by setting `options(readr.show_col_types = FALSE)` -#' - The column types are supplied with the `col_types` argument. +#' Wrapper around `getOption("readr.show_col_types")` that implements some fall +#' back logic if the option is unset. This returns: +#' * `TRUE` if the option is set to `TRUE` +#' * `FALSE` if the option is set to `FALSE` +#' * `FALSE` if the option is unset and we appear to be running tests +#' * `NULL` otherwise, in which case the caller determines whether to show +#' column types based on context, e.g. whether `show_col_types` or actual +#' `col_types` were explicitly specified #' @export should_show_types <- function() { - if (identical(getOption("readr.show_col_types", TRUE), FALSE)) { + opt <- getOption("readr.show_col_types", NA) + if (isTRUE(opt)) { + TRUE + } else if (identical(opt, FALSE)) { + FALSE + } else if (is.na(opt) && is_testing()) { FALSE } else { NULL diff --git a/man/should_show_types.Rd b/man/should_show_types.Rd index e9b36d96..bd995c95 100644 --- a/man/should_show_types.Rd +++ b/man/should_show_types.Rd @@ -7,9 +7,14 @@ should_show_types() } \description{ -Column types are shown unless +Wrapper around \code{getOption("readr.show_col_types")} that implements some fall +back logic if the option is unset. This returns: \itemize{ -\item They are disabled by setting \code{options(readr.show_col_types = FALSE)} -\item The column types are supplied with the \code{col_types} argument. +\item \code{TRUE} if the option is set to \code{TRUE} +\item \code{FALSE} if the option is set to \code{FALSE} +\item \code{FALSE} if the option is unset and we appear to be running tests +\item \code{NULL} otherwise, in which case the caller determines whether to show +column types based on context, e.g. whether \code{show_col_types} or actual +\code{col_types} were explicitly specified } } diff --git a/tests/testthat/_snaps/edition-1/col-spec.md b/tests/testthat/_snaps/edition-1/col-spec.md new file mode 100644 index 00000000..0c8ad674 --- /dev/null +++ b/tests/testthat/_snaps/edition-1/col-spec.md @@ -0,0 +1,42 @@ +# options(readr.show_col_spec) controls column specifications + + Code + out <- read_csv(readr_example("mtcars.csv")) + Message + + -- Column specification -------------------------------------------------------- + cols( + mpg = col_double(), + cyl = col_double(), + disp = col_double(), + hp = col_double(), + drat = col_double(), + wt = col_double(), + qsec = col_double(), + vs = col_double(), + am = col_double(), + gear = col_double(), + carb = col_double() + ) + +# `show_col_types` controls column specification + + Code + out <- read_csv(readr_example("mtcars.csv"), show_col_types = TRUE) + Message + + -- Column specification -------------------------------------------------------- + cols( + mpg = col_double(), + cyl = col_double(), + disp = col_double(), + hp = col_double(), + drat = col_double(), + wt = col_double(), + qsec = col_double(), + vs = col_double(), + am = col_double(), + gear = col_double(), + carb = col_double() + ) + diff --git a/tests/testthat/_snaps/edition-2/col-spec.md b/tests/testthat/_snaps/edition-2/col-spec.md new file mode 100644 index 00000000..f541714f --- /dev/null +++ b/tests/testthat/_snaps/edition-2/col-spec.md @@ -0,0 +1,28 @@ +# options(readr.show_col_spec) controls column specifications + + Code + out <- read_csv(readr_example("mtcars.csv")) + Message + Rows: 32 Columns: 11 + Message + -- Column specification -------------------------------------------------------- + Delimiter: "," + dbl (11): mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb + + i Use `spec()` to retrieve the full column specification for this data. + i Specify the column types or set `show_col_types = FALSE` to quiet this message. + +# `show_col_types` controls column specification + + Code + out <- read_csv(readr_example("mtcars.csv"), show_col_types = TRUE) + Message + Rows: 32 Columns: 11 + Message + -- Column specification -------------------------------------------------------- + Delimiter: "," + dbl (11): mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb + + i Use `spec()` to retrieve the full column specification for this data. + i Specify the column types or set `show_col_types = FALSE` to quiet this message. + diff --git a/tests/testthat/setup.R b/tests/testthat/setup.R index 6d8c34d0..8418daf0 100644 --- a/tests/testthat/setup.R +++ b/tests/testthat/setup.R @@ -1,4 +1,3 @@ pre_test_options <- options( - readr.show_col_types = FALSE, readr.show_progress = FALSE ) diff --git a/tests/testthat/test-col-spec.R b/tests/testthat/test-col-spec.R index 7722e5d2..2b333839 100644 --- a/tests/testthat/test-col-spec.R +++ b/tests/testthat/test-col-spec.R @@ -137,7 +137,6 @@ test_that("spec object attached to read data", { ) }) - test_that("print(col_spec) works with dates", { out <- col_spec_standardise("a,b,c\n", col_types = cols( @@ -317,21 +316,21 @@ test_that("as.character() works on col_spec objects", { expect_equal(as.character(spec), "ddddf") }) -test_that("options(readr.show_col_spec) can turn off showing column specifications", { - skip_if_edition_first() - - old <- options("readr.show_col_types") - on.exit(options(old)) - - options(readr.show_col_types = NULL) - expect_message( - expect_message( - expect_message( - read_csv(readr_example("mtcars.csv")) - ) - ) +test_that("options(readr.show_col_spec) controls column specifications", { + withr::local_options(list(readr.show_col_types = TRUE)) + expect_snapshot( + out <- read_csv(readr_example("mtcars.csv")), + variant = edition_variant() ) - options(readr.show_col_types = FALSE) + withr::local_options(list(readr.show_col_types = FALSE)) expect_silent(read_csv(readr_example("mtcars.csv"))) }) + +test_that("`show_col_types` controls column specification", { + expect_snapshot( + out <- read_csv(readr_example("mtcars.csv"), show_col_types = TRUE), + variant = edition_variant() + ) + expect_silent(read_csv(readr_example("mtcars.csv"), show_col_types = FALSE)) +})