rstudio / shinytest

Automated testing for shiny apps
https://rstudio.github.io/shinytest/
Other
225 stars 55 forks source link

$getdebugLog("shiny_console") has problems with malformed character strings #363

Closed DavidPatShuiFong closed 3 years ago

DavidPatShuiFong commented 4 years ago

I'm afraid I am not very helpful, but this is an error message I'm seeing on shinytest 1.4.0

> app$getDebugLog("shiny_console")
Error in gsub("\f", "", str, fixed = TRUE) : 
  input string 5 is invalid in this locale

running the debugger I see the following relevant calls

  "!DEBUG sd_getDebugLog"
  type <- as_debug(type)
  output <- list()
  if (!is.null(private$shinyProcess) && "shiny_console" %in% 
    type) {
    "!DEBUG sd_getDebugLog shiny_console"
    out <- readLines(private$shinyProcess$get_output_file(), 
      warn = FALSE)
    err <- readLines(private$shinyProcess$get_error_file(), 
      warn = FALSE)
    output$shiny_console <- make_shiny_console_log(out = out, 
      err = err)
  }

make_shiny_console_log is defined as ...

function (out, err) 
{
  out <- data.frame(stringsAsFactors = FALSE, level = if (length(out)) 
    "INFO"
  else character(), timestamp = if (length(out)) 
    as.POSIXct(NA)
  else as.POSIXct(character()), message = filter_log_text(out), 
    type = if (length(out)) 
      "shiny_console"
    else character())
  err <- data.frame(stringsAsFactors = FALSE, level = if (length(err)) 
    "ERROR"
  else character(), timestamp = if (length(err)) 
    as.POSIXct(NA)
  else as.POSIXct(character()), message = filter_log_text(err), 
    type = if (length(err)) 
      "shiny_console"
    else character())
  rbind(out, err)
}

when err is being defined, filter_log_text is called ...

function (str) 
{
  gsub("\f", "", str, fixed = TRUE)
}

and the str when the error message happens starts as ...

  [1] "Running application in test mode."                                                                               
  [2] "Loading required package: shiny"                                                                                 
  [3] ""                                                                                                                
  [4] "Listening on http://127.0.0.1:6173"                                                                              
  [5] "Warning in force(expr) : input string '\003\xe9' cannot be translated to UTF-8, is it valid in 'ANSI_X3.4-1968'?"
  [6] "Warning in force(expr) : input string '\003\xe9' cannot be translated to UTF-8, is it valid in 'ANSI_X3.4-1968'?"
  [7] "Warning in is.na(x) :"                                                                                           

The problem is in line 5 and 6, if I 'manually' remove those lines during the debug (e.g. with str <- str[-c(5,6)]) then the function works normally.

I'm afraid I don't know where the force came from, it is not a function I am calling explicitly.

DavidPatShuiFong commented 3 years ago

The problem might be due to improperly setup locales, and/or the locale of installed packages not matching the local locale. Solution to a similar problem described in https://github.com/rstudio/shiny/issues/2933

(I was getting the error force(expr) : input string '�' cannot be translated to UTF-8, is it valid in 'ANSI_X3.4-1968'? when trying to use recordTest)

to fix

and for good measure, add to .Renviron

LC_COLLATE  = "en_US.UTF-8"
LC_CTYPE    = "en_US.UTF-8"
LC_MONETARY = "en_US.UTF-8"
LC_NUMERIC  = "en_US.UTF-8"
LC_TIME     = "en_US.UTF-8"