| 1 |
|
| 2 |
test_that("we can read different types of DCFs", { |
| 3 |
|
| 4 |
expected <- list(A = "1", B = "2") |
| 5 |
|
| 6 |
# plain jane |
| 7 |
actual <- renv_dcf_read(text = "A: 1\nB: 2") |
| 8 |
expect_equal(actual, expected) |
| 9 |
|
| 10 |
# extra whitespace between fields |
| 11 |
actual <- renv_dcf_read(text = "A: 1\n\nB: 2\n") |
| 12 |
expect_equal(actual, expected) |
| 13 |
|
| 14 |
}) |
| 15 |
|
| 16 |
test_that("we allow for unindented continuations", { |
| 17 |
|
| 18 |
actual <- renv_dcf_read(text = "A: This field\nisn't indented.\nB: 42") |
| 19 |
expected <- list(A = "This field isn't indented.", B = "42") |
| 20 |
expect_equal(actual, expected) |
| 21 |
|
| 22 |
}) |
| 23 |
|
| 24 |
test_that("we can read a latin-1 DESCRIPTION file", { |
| 25 |
|
| 26 |
# declared latin1; is latin1 |
| 27 |
contents <- heredoc({' |
| 28 |
Encoding: latin1 |
| 29 |
Dessert: crème brûlée |
| 30 |
'}) |
| 31 |
|
| 32 |
latin1 <- iconv(enc2utf8(contents), from = "UTF-8", to = "latin1") |
| 33 |
file <- renv_scope_tempfile("DESCRIPTION-") |
| 34 |
writeLines(latin1, con = file, useBytes = TRUE) |
| 35 |
|
| 36 |
dcf <- renv_dcf_read(file) |
| 37 |
expect_equal(dcf$Dessert, "crème brûlée") |
| 38 |
|
| 39 |
}) |
| 40 |
|
| 41 |
test_that("we can read a custom encoded DESCRIPTION file", { |
| 42 |
|
| 43 |
skip_if(!"CP936" %in% iconvlist()) |
| 44 |
|
| 45 |
nihao <- enc2utf8("\u4f60\u597d") |
| 46 |
|
| 47 |
# declared CP936, is CP936 |
| 48 |
contents <- heredoc({' |
| 49 |
Encoding: CP936 |
| 50 |
Greeting: \u4f60\u597d |
| 51 |
'}) |
| 52 |
|
| 53 |
bytes <- iconv( |
| 54 |
x = enc2utf8(contents), |
| 55 |
from = "UTF-8", |
| 56 |
to = "CP936", |
| 57 |
toRaw = TRUE |
| 58 |
) |
| 59 |
|
| 60 |
file <- renv_scope_tempfile("DESCRIPTION-") |
| 61 |
writeBin(bytes[[1L]], con = file) |
| 62 |
|
| 63 |
dcf <- renv_dcf_read(file) |
| 64 |
expect_equal(dcf$Greeting, nihao) |
| 65 |
|
| 66 |
}) |
| 67 |
|
| 68 |
test_that("we can read mis-encoded DESCRIPTION files", { |
| 69 |
|
| 70 |
# declared UTF-8; but latin1 |
| 71 |
contents <- heredoc(' |
| 72 |
Encoding: UTF-8 |
| 73 |
Dessert: crème brûlée |
| 74 |
') |
| 75 |
|
| 76 |
latin1 <- iconv(enc2utf8(contents), from = "UTF-8", to = "latin1") |
| 77 |
file <- renv_scope_tempfile("DESCRIPTION-") |
| 78 |
writeLines(latin1, con = file, useBytes = TRUE) |
| 79 |
|
| 80 |
dcf <- renv_dcf_read(file) |
| 81 |
expect_equal(dcf$Dessert, "crème brûlée") |
| 82 |
|
| 83 |
}) |
| 84 |
|
| 85 |
test_that("we can read and write a dcf file", { |
| 86 |
|
| 87 |
contents <- heredoc(' |
| 88 |
Title: The title. |
| 89 |
Description: The Description field is quite long. |
| 90 |
It needs to wrap across multiple lines. |
| 91 |
') |
| 92 |
|
| 93 |
descfile <- renv_scope_tempfile("renv-description-") |
| 94 |
writeLines(contents, con = descfile) |
| 95 |
|
| 96 |
old <- renv_dcf_read(descfile) |
| 97 |
renv_dcf_write(old, file = descfile) |
| 98 |
new <- read.dcf(descfile, all = TRUE) |
| 99 |
|
| 100 |
expect_equal( |
| 101 |
gsub("[[:space:]]+", " ", old$Field), |
| 102 |
gsub("[[:space:]]+", " ", new$Field) |
| 103 |
) |
| 104 |
|
| 105 |
}) |