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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
# test that write_utf8() does not alter active project and
# does not consult active project for line ending
test_that("write_utf8(): no active project, write path outside project", {
local_project(NULL)
expect_false(proj_active())
dir <- withr::local_tempdir(pattern = "write-utf8-nonproject")
expect_false(possibly_in_proj(dir))
write_utf8(path(dir, "letters_LF"), letters[1:2], line_ending = "\n")
expect_equal(
readBin(path(dir, "letters_LF"), what = "raw", n = 3),
charToRaw("a\nb")
)
write_utf8(path(dir, "letters_CRLF"), letters[1:2], line_ending = "\r\n")
expect_equal(
readBin(path(dir, "letters_CRLF"), what = "raw", n = 3),
charToRaw("a\r\n")
)
expect_false(proj_active())
})
test_that("write_utf8(): no active project, write to path inside a project", {
local_project(NULL)
expect_false(proj_active())
dir <- withr::local_tempdir(pattern = "write-utf8-in-a-project")
file_create(path(dir, ".here"))
expect_true(possibly_in_proj(dir))
with_project(dir, use_rstudio(line_ending = "posix"))
write_utf8(path(dir, "letters"), letters[1:2])
expect_equal(
readBin(path(dir, "letters"), what = "raw", n = 3),
charToRaw("a\nb")
)
file_delete(path(dir, paste0(path_file(dir), ".Rproj")))
with_project(dir, use_rstudio(line_ending = "windows"))
write_utf8(path(dir, "letters"), letters[1:2])
expect_equal(
readBin(path(dir, "letters"), what = "raw", n = 3),
charToRaw("a\r\n")
)
expect_false(proj_active())
})
test_that("write_utf8(): in an active project, write path outside project", {
proj <- create_local_project(rstudio = TRUE)
expect_true(proj_active())
dir <- withr::local_tempdir(pattern = "write-utf8-nonproject")
expect_false(possibly_in_proj(dir))
write_utf8(path(dir, "letters_LF"), letters[1:2], line_ending = "\n")
expect_equal(
readBin(path(dir, "letters_LF"), what = "raw", n = 3),
charToRaw("a\nb")
)
write_utf8(path(dir, "letters_CRLF"), letters[1:2], line_ending = "\r\n")
expect_equal(
readBin(path(dir, "letters_CRLF"), what = "raw", n = 3),
charToRaw("a\r\n")
)
expect_equal(proj_get(), proj)
})
test_that("write_utf8(): in an active project, write path in other project", {
proj <- create_local_project(rstudio = TRUE)
expect_true(proj_active())
dir <- withr::local_tempdir(pattern = "write-utf8-in-a-project")
file_create(path(dir, ".here"))
expect_true(possibly_in_proj(dir))
with_project(dir, use_rstudio(line_ending = "posix"))
write_utf8(path(dir, "letters"), letters[1:2])
expect_equal(
readBin(path(dir, "letters"), what = "raw", n = 3),
charToRaw("a\nb")
)
file_delete(path(dir, paste0(path_file(dir), ".Rproj")))
with_project(dir, use_rstudio(line_ending = "windows"))
write_utf8(path(dir, "letters"), letters[1:2])
expect_equal(
readBin(path(dir, "letters"), what = "raw", n = 3),
charToRaw("a\r\n")
)
expect_equal(proj_get(), proj)
})
test_that("write_utf8() can append text when requested", {
path <- file_temp()
write_utf8(path, "x", line_ending = "\n")
write_utf8(path, "x", line_ending = "\n", append = TRUE)
expect_equal(readChar(path, 4), "x\nx\n")
})
test_that("write_utf8() respects line ending", {
path <- file_temp()
write_utf8(path, "x", line_ending = "\n")
expect_equal(detect_line_ending(path), "\n")
write_utf8(path, "x", line_ending = "\r\n")
expect_equal(detect_line_ending(path), "\r\n")
})
# TODO: explore more edge cases re: active project on both sides
test_that("write_utf8() can operate outside of a project", {
dir <- withr::local_tempdir(pattern = "write-utf8-test")
withr::local_dir(dir)
local_project(NULL)
expect_false(proj_active())
expect_no_error(write_utf8(path = "foo", letters[1:3]))
})
# https://github.com/r-lib/usethis/issues/514
test_that("write_utf8() always produces a trailing newline", {
path <- file_temp()
write_utf8(path, "x", line_ending = "\n")
expect_equal(readChar(path, 2), "x\n")
})
test_that("write_union() writes a de novo file", {
tmp <- file_temp()
expect_false(file_exists(tmp))
write_union(tmp, letters[1:3], quiet = TRUE)
expect_identical(read_utf8(tmp), letters[1:3])
})
test_that("write_union() leaves file 'as is'", {
tmp <- file_temp()
writeLines(letters[1:3], tmp)
before <- read_utf8(tmp)
write_union(tmp, "b", quiet = TRUE)
expect_identical(before, read_utf8(tmp))
})
test_that("write_union() adds lines", {
tmp <- file_temp()
writeLines(letters[1:3], tmp)
write_union(tmp, letters[4:5], quiet = TRUE)
expect_setequal(read_utf8(tmp), letters[1:5])
})
# https://github.com/r-lib/usethis/issues/526
test_that("write_union() doesn't remove duplicated lines in the input", {
tmp <- file_temp()
before <- rep(letters[1:2], 3)
add_me <- c("z", "a", "c", "a", "b")
writeLines(before, tmp)
expect_identical(before, read_utf8(tmp))
write_union(tmp, add_me, quiet = TRUE)
expect_identical(read_utf8(tmp), c(before, c("z", "c")))
})
test_that("same_contents() detects if contents are / are not same", {
tmp <- file_temp()
x <- letters[1:3]
writeLines(x, con = tmp, sep = "\n")
expect_true(same_contents(tmp, x))
expect_false(same_contents(tmp, letters[4:6]))
})
test_that("write_over() leaves file 'as is' (outside of a project)", {
local_interactive(FALSE)
tmp <- withr::local_file(file_temp())
writeLines(letters[1:3], tmp)
before <- read_utf8(tmp)
write_over(tmp, letters[4:6], quiet = TRUE)
expect_identical(read_utf8(tmp), before)
# usethis.overwrite shouldn't matter for a file outside of a project
withr::with_options(
list(usethis.overwrite = TRUE),
{
write_over(tmp, letters[4:6], quiet = TRUE)
expect_identical(read_utf8(tmp), before)
}
)
})
test_that("write_over() works in active project", {
local_interactive(FALSE)
create_local_project()
tmp <- proj_path("foo.txt")
writeLines(letters[1:3], tmp)
before <- read_utf8(tmp)
write_over(tmp, letters[4:6], quiet = TRUE)
expect_identical(read_utf8(tmp), before)
use_git()
withr::with_options(
list(usethis.overwrite = TRUE),
{
write_over(tmp, letters[4:6], quiet = TRUE)
expect_identical(read_utf8(tmp), letters[4:6])
}
)
})
test_that("write_over() works for a file in a project that is not active", {
local_interactive(FALSE)
owd <- getwd()
proj <- create_local_project()
use_git()
tmp <- proj_path("foo.txt")
writeLines(letters[1:3], tmp)
withr::local_dir(owd)
local_project(NULL)
expect_false(proj_active())
tmp <- path(proj, "foo.txt")
before <- read_utf8(tmp)
withr::with_options(
list(usethis.overwrite = FALSE),
{
write_over(tmp, letters[4:6], quiet = TRUE)
expect_identical(read_utf8(tmp), before)
}
)
withr::with_options(
list(usethis.overwrite = TRUE),
{
write_over(tmp, letters[4:6], quiet = TRUE)
expect_identical(read_utf8(tmp), letters[4:6])
}
)
expect_false(proj_active())
})
test_that("write_union() messaging is correct with weird working directory", {
create_local_project()
use_directory("aaa/bbb")
setwd("aaa/bbb")
withr::local_options(usethis.quiet = FALSE)
expect_snapshot(
write_union(proj_path("somefile"), letters[4:6])
)
})
|