Skip to content

Commit

Permalink
Skip language tests if set to C locale
Browse files Browse the repository at this point in the history
Closes #264
  • Loading branch information
lionel- committed Jan 9, 2024
1 parent 786f312 commit 5f49227
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
13 changes: 7 additions & 6 deletions tests/testthat/helper-pkgload.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ suppress_output <- function(expr) {
)
}

# Need to also specify `LC_ALL` because `LANGUAGE` is ignored when
# `LANG` is set (here via `LC_ALL`) to `C` or `C.UTF-8`
with_lang <- function(lc, language, expr) {
withr::local_envvar(c(LC_ALL = lc))
withr::local_language(language)
expr
# Used to skip tests that will fail when locale is set to C, for
# instance `with_language()` tests. These tests should only be run
# when using a locale like `en_US`. The check is cautious and simple:
# If unset we assume the default might be C or maybe `LANG` is set to C.
skip_if_c_locale <- function() {
lc_all <- Sys.getenv("LC_ALL", "")
skip_if(lc_all %in% c("", "C", "C.UTF-8"))
}
10 changes: 7 additions & 3 deletions tests/testthat/test-po.R
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
local_load_all_quiet()

test_that("translation domain correctly loaded", {
skip_if_c_locale()

load_all(test_path("testTranslations"))
withr::defer(unload("testTranslations"))

expect_equal(with_lang("fr_FR", "fr", hello()), "Bonjour")
expect_equal(with_language("fr", hello()), "Bonjour")

load_all(test_path("testTranslations"))
expect_equal(length(temp_po_dirs("testTranslations")), 1)
})

test_that("modified translations are correctly reloaded", {
skip_if_c_locale()

pkg <- withr::local_tempdir()
file.copy(dir(test_path("testTranslations"), full.names = TRUE), pkg, recursive = TRUE)

# Load package and generate translation
load_all(pkg)
withr::defer(unload("testTranslations"))
with_lang("fr_FR", "fr", hello())
with_language("fr", hello())

# Modify .po file
po_path <- file.path(pkg, "po", "R-fr.po")
Expand All @@ -33,5 +37,5 @@ test_that("modified translations are correctly reloaded", {

# Re-load and re-translate
load_all(pkg)
expect_equal(with_lang("fr_FR", "fr", hello()), "Salut")
expect_equal(with_language("fr", hello()), "Salut")
})

0 comments on commit 5f49227

Please sign in to comment.