From 7df365d4450eb342c49a60698a15bcf476f475a8 Mon Sep 17 00:00:00 2001 From: Colin Rundel Date: Thu, 15 Aug 2024 13:21:05 -0700 Subject: [PATCH 1/2] Handle malformed version information from INSIDE_EMACS --- R/num-ansi-colors.R | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/R/num-ansi-colors.R b/R/num-ansi-colors.R index c23cffa5f..3fb82f15d 100644 --- a/R/num-ansi-colors.R +++ b/R/num-ansi-colors.R @@ -310,6 +310,10 @@ emacs_version <- function() { if (ver == "") return(NA_integer_) ver <- gsub("'", "", ver) + + if (!grepl("^\\d+\\.\\d+",ver)) # check for malformed version #689 + return(NA_integer_) + ver <- strsplit(ver, ",", fixed = TRUE)[[1]] ver <- strsplit(ver, ".", fixed = TRUE)[[1]] as.numeric(ver) From 96cf5865312af75c6b5f75205642a3500bba7d40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Cs=C3=A1rdi?= Date: Wed, 28 Aug 2024 13:41:38 +0200 Subject: [PATCH 2/2] Simplify non-numeric INSIDE_EMACS handling + tests + NEWS --- NEWS.md | 3 +++ R/num-ansi-colors.R | 5 +---- tests/testthat/test-num-ansi-colors.R | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/NEWS.md b/NEWS.md index 1d7f4ca47..077a98b5c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # cli (development version) +* `num_ansi_colors()` now does not warn in Emacs if the `INSIDE_EMACS` + environment variable is not a proper version number (@rundel, #689). + # cli 3.6.3 * cli now builds on ARM Windows. diff --git a/R/num-ansi-colors.R b/R/num-ansi-colors.R index 3fb82f15d..1263ba149 100644 --- a/R/num-ansi-colors.R +++ b/R/num-ansi-colors.R @@ -311,12 +311,9 @@ emacs_version <- function() { ver <- gsub("'", "", ver) - if (!grepl("^\\d+\\.\\d+",ver)) # check for malformed version #689 - return(NA_integer_) - ver <- strsplit(ver, ",", fixed = TRUE)[[1]] ver <- strsplit(ver, ".", fixed = TRUE)[[1]] - as.numeric(ver) + suppressWarnings(as.numeric(ver)) } win10_build <- function() { diff --git a/tests/testthat/test-num-ansi-colors.R b/tests/testthat/test-num-ansi-colors.R index 17834b986..961e521a5 100644 --- a/tests/testthat/test-num-ansi-colors.R +++ b/tests/testthat/test-num-ansi-colors.R @@ -180,3 +180,19 @@ test_that("ESS_BACKGROUND_MODE", { withr::local_envvar(ESS_BACKGROUND_MODE = "dark") expect_true(detect_dark_theme("auto")) }) + +test_that("emacs_version", { + withr::local_envvar(INSIDE_EMACS = "") + expect_true(is.na(emacs_version())) + withr::local_envvar(INSIDE_EMACS = "foobar") + expect_true(is.na(emacs_version())) + + withr::local_envvar(INSIDE_EMACS = "23.2.3") + expect_equal(emacs_version(), c(23, 2, 3)) + withr::local_envvar(INSIDE_EMACS = "23.2.3,foobar") + expect_equal(emacs_version(), c(23, 2, 3)) + withr::local_envvar(INSIDE_EMACS = "'23.2.3'") + expect_equal(emacs_version(), c(23, 2, 3)) + withr::local_envvar(INSIDE_EMACS = "'23.2.3',foobar") + expect_equal(emacs_version(), c(23, 2, 3)) +})