File: test-encoding.R

package info (click to toggle)
r-cran-sys 3.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 220 kB
  • sloc: ansic: 540; sh: 13; makefile: 2
file content (51 lines) | stat: -rw-r--r-- 1,734 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
context("test-encoding")

support_unicode_path <- function(){
  getRversion() >= "3.6.0" && grepl("(UTF-8|1252)", Sys.getlocale('LC_CTYPE'))
}

test_that("UTF-8 encoded text arguments", {
  txt <- readLines(system.file('utf8.txt', package = 'sys', mustWork = TRUE), encoding = 'UTF-8')
  res <- sys::exec_internal('echo', txt)
  expect_equal(res$status, 0)
  con <- rawConnection(res$stdout)
  output <- readLines(con, encoding = 'UTF-8')
  close(con)
  expect_equal(txt, output)
})

test_that("UTF-8 filenames, binary data", {
  skip_if_not(support_unicode_path(), 'System does not support unicode paths')
  tmp <- paste(tempdir(), "\u0420\u0423\u0421\u0421\u041a\u0418\u0419.txt", sep = "/")
  tmp <- normalizePath(tmp, mustWork = FALSE)
  f <- file(tmp, 'wb')
  serialize(iris, f)
  close(f)
  expect_true(file.exists(tmp))

  # As a file path
  res <- if(.Platform$OS.type == "windows"){
    sys::exec_internal('cmd', c("/C", "type", tmp))
  } else {
    sys::exec_internal('cat', tmp)
  }
  expect_equal(res$status, 0)
  expect_equal(unserialize(res$stdout), iris)
})

test_that("UTF-8 filename as std_in", {
  skip_if_not(support_unicode_path(), 'System does not support unicode paths')
  input <- c("foo", "bar", "baz")
  txt <- readLines(system.file('utf8.txt', package = 'sys', mustWork = TRUE), encoding = 'UTF-8')
  tmp <- normalizePath(paste(tempdir(), txt, sep = "/"), mustWork = FALSE)
  f <- file(tmp, 'wb')
  writeBin(charToRaw(paste(input, collapse = "\n")), con = f, useBytes = TRUE)
  close(f)
  expect_true(file.exists(tmp))
  res <- exec_internal('sort', std_in = tmp)
  expect_equal(res$status, 0)
  con <- rawConnection(res$stdout)
  output <- readLines(con)
  close(con)
  expect_equal(output, sort(input))
})