Debian Codegraph @

tests/test-encoding/test-all.R

raw
1 # Assume the working dir is the root dir of the shiny source package, and the
2 # shiny-examples repo (https://github.com/rstudio/shiny-examples) is under the
3 # same dir as the shiny package
4 library(shiny)
5
6
7 # Get the current locale settings in a named vector. According to
8 # ?Sys.setlocale, only some categories are set when setting LC_ALL. Since these
9 # tests set LC_ALL, we only need to keep these categories to restore later. Also,
10 # trying to restore some categories with Sys.setlocale will result in an error;
11 # these categories should be OK.
12 getLocale <- function() {
13 categories <- c("LC_COLLATE", "LC_CTYPE", "LC_MONETARY", "LC_TIME")
14
15 locale <- strsplit(Sys.getlocale(), ";")[[1]]
16 names(locale) <- sub("=.*", "", locale)
17 locale <- sub(".*=", "", locale)
18 locale <- locale[names(locale) %in% categories]
19 locale
20 }
21
22 restoreLocale <- function(locale) {
23 mapply(Sys.setlocale, names(locale), locale)
24 }
25
26 withLocale <- function(locale, expr) {
27 oldLocale <- getLocale()
28 on.exit(restoreLocale(oldLocale))
29 Sys.setlocale(locale = locale)
30
31 force(expr)
32 }
33
34
35 if (.Platform$OS.type == 'windows') {
36 locales <- c('Chinese', 'English')
37 } else {
38 # On non-Windows, only test with the default locale
39 locales <- getLocale()["LC_CTYPE"]
40 }
41
42 apps <- list.files('tests/test-encoding/', '^[0-9]{2}-', full.names = TRUE)
43 apps <- c(apps, '../shiny-examples/022-unicode-chinese/')
44
45 # Run these apps in the RStudio preview window one by one in different locales.
46 # Note 022-unicode-chinese may not work well in the English locale (explained at
47 # https://github.com/rstudio/shiny/pull/968)
48 for (app in apps) {
49 for (locale in locales) {
50 withLocale(locale, {
51 cat("Running app: ", app, "\n")
52 cat("Locale: ", Sys.setlocale(locale = locale))
53 tryCatch(shiny::runApp(app), interrupt = function(e) {})
54 })
55 }
56 }