Skip to content

Commit

Permalink
Refine existing locale checker to work with LC_COLLATE
Browse files Browse the repository at this point in the history
  • Loading branch information
DavisVaughan committed Jul 11, 2022
1 parent 480587e commit 390fa80
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
28 changes: 18 additions & 10 deletions tests/testthat/helper-encoding.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,25 @@ get_alien_lang_string <- function() {
lang_strings$different[[1L]]
}

try_encoding <- function(enc) {
orig_encoding <- Sys.getlocale("LC_CTYPE")
on.exit(Sys.setlocale("LC_CTYPE", orig_encoding), add = TRUE)
tryCatch({
Sys.setlocale("LC_CTYPE", enc)
TRUE
},
warning = function(w) FALSE,
error = function(e) FALSE
has_locale <- function(locale, category) {
original <- Sys.getlocale(category = category)
on.exit(Sys.setlocale(category = category, locale = original), add = TRUE)

tryCatch(
expr = {
Sys.setlocale(category = category, locale = locale)
TRUE
},
warning = function(w) FALSE,
error = function(e) FALSE
)
}
has_collate_locale <- function(locale) {
has_locale(locale = locale, category = "LC_COLLATE")
}
has_ctype_locale <- function(enc) {
has_locale(locale = enc, category = "LC_CTYPE")
}

non_utf8_encoding <- function(enc = NULL) {
if (!l10n_info()$`UTF-8`) {
Expand All @@ -50,7 +58,7 @@ non_utf8_encoding <- function(enc = NULL) {
"fr_CH.ISO8859-1",
"fr_CH.ISO8859-15"
)
available <- vapply(enc, try_encoding, logical(1))
available <- vapply(enc, has_ctype_locale, logical(1))
if (any(available)) {
enc[available][1]
} else {
Expand Down
3 changes: 1 addition & 2 deletions tests/testthat/test-grouped-df.r
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,7 @@ test_that("groups are ordered in the C locale", {
})

test_that("using the global option `dplyr.legacy_group_by_locale` forces the system locale", {
on_mac <- identical(tolower(Sys.info()[["sysname"]]), "darwin")
skip_if_not(on_mac, message = "Not on Mac. Unsure if we can use 'en_US' locale.")
skip_if_not(has_collate_locale("en_US"), message = "Can't use 'en_US' locale")

local_options(dplyr.legacy_group_by_locale = TRUE)
withr::local_collate("en_US")
Expand Down

0 comments on commit 390fa80

Please sign in to comment.