From 852901ab3fb1b03ca77260950bcad1521333c670 Mon Sep 17 00:00:00 2001 From: ndelcamp Date: Thu, 15 Aug 2024 17:31:22 -0400 Subject: [PATCH] Fix bug where `session_info()` fails when `info` argument has length > 1 Fixes #96 --- NEWS.md | 2 ++ R/session-info.R | 5 ++--- tests/testthat/test-session-info.R | 36 ++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index 66cd1a9..fc75efb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,8 @@ # sessioninfo (development version) +* `session_info()` no longer produces an error when `info` has length > 1 (@nash-delcamp-slp, #96). + * Update pkgdown url to sessioninfo.r-lib.org * `session_diff()` now accepts the URL to a GitHub Actions log as the source for diff --git a/R/session-info.R b/R/session-info.R index 447b6fb..280b380 100644 --- a/R/session-info.R +++ b/R/session-info.R @@ -58,9 +58,6 @@ session_info <- function( if (missing(info)) info <- "auto" choices <- c("platform", "packages", "python", "external") - if (info != "auto" && info != "all") { - info <- match.arg(info, choices, several.ok = TRUE) - } if ("all" %in% info) { info <- choices } else if ("auto" %in% info) { @@ -69,6 +66,8 @@ session_info <- function( "packages", if (should_show_python(pkgs)) "python" ) + } else { + info <- match.arg(info, choices, several.ok = TRUE) } stopifnot(is_flag(to_file) || is_string(to_file)) diff --git a/tests/testthat/test-session-info.R b/tests/testthat/test-session-info.R index 0224307..c728664 100644 --- a/tests/testthat/test-session-info.R +++ b/tests/testthat/test-session-info.R @@ -13,3 +13,39 @@ test_that("print.session_info", { expect_output(print(si), "setting[ ]+value") expect_output(print(si), "package[ ]+\\* version[ ]+date[ ][(]UTC[)][ ]+lib[ ]+source") }) + +test_that("`info` can include one or multiple values", { + choices <- c("platform", "packages", "python", "external") + + # including "all" results in all info being selected + expect_named( + session_info(info = "all"), + choices, + ignore.order = TRUE + ) + # even when other items are included + expect_named( + session_info(info = c("all", "platform")), + choices, + ignore.order = TRUE + ) + + # including "auto" (and not "all") results in auto info being included + expect_gte( + length(names(session_info(info = "auto"))), + 2 + ) + # even when other items are included + expect_gte( + length(names(session_info(info = c("auto", "platform")))), + 2 + ) + + # with neither "all" or "auto", valid items are included + expect_named( + session_info(info = c("platform", "python", "external", "nonvalid-info")), + c("platform", "python", "external"), + ignore.order = TRUE + ) + +})