|
1 |
| -test_that("detect_line_ending works", { |
2 |
| - # write files with various newlines |
3 |
| - win_nl <- tempfile() |
4 |
| - win_nl_con <- file(win_nl, "wb") |
5 |
| - |
6 |
| - unix_nl <- tempfile() |
7 |
| - unix_nl_con <- file(unix_nl, "wb") |
8 |
| - |
9 |
| - non_existent_file <- tempfile() |
10 |
| - |
11 |
| - empty_file <- tempfile() |
12 |
| - file.create(empty_file) |
13 |
| - |
14 |
| - on.exit({ |
15 |
| - unlink(c(win_nl, unix_nl, empty_file)) |
16 |
| - }) |
17 |
| - |
18 |
| - base::writeLines(c("foo", "bar"), win_nl_con, sep = "\r\n") |
19 |
| - close(win_nl_con) |
| 1 | +test_that("write_lines uses specified line_ending", { |
| 2 | + win_nl <- withr::local_tempfile() |
| 3 | + write_lines("baz", win_nl, line_ending = "\r\n") |
| 4 | + expect_equal(readChar(win_nl, 100), "baz\r\n") |
20 | 5 |
|
21 |
| - base::writeLines(c("foo", "bar"), unix_nl_con, sep = "\n") |
22 |
| - close(unix_nl_con) |
| 6 | + unix_nl <- withr::local_tempfile() |
| 7 | + write_lines("baz", unix_nl, line_ending = "\n") |
| 8 | + expect_equal(readChar(unix_nl, 100), "baz\n") |
| 9 | +}) |
23 | 10 |
|
| 11 | +test_that("detect_line_ending captures existing line ending", { |
| 12 | + win_nl <- withr::local_tempfile() |
| 13 | + write_lines("baz", win_nl, line_ending = "\r\n") |
24 | 14 | expect_equal(detect_line_ending(win_nl), "\r\n")
|
| 15 | + |
| 16 | + unix_nl <- withr::local_tempfile() |
| 17 | + write_lines("baz", unix_nl, line_ending = "\n") |
25 | 18 | expect_equal(detect_line_ending(unix_nl), "\n")
|
26 |
| - expect_equal(detect_line_ending(non_existent_file), "\n") |
27 |
| - expect_equal(detect_line_ending(empty_file), "\n") |
28 | 19 | })
|
29 | 20 |
|
30 |
| -test_that("write_lines writes windows newlines for files with windows newlines, and unix newlines otherwise", { |
31 |
| - # write files with various newlines |
32 |
| - win_nl <- tempfile() |
33 |
| - win_nl_con <- file(win_nl, "wb") |
34 |
| - |
35 |
| - unix_nl <- tempfile() |
36 |
| - unix_nl_con <- file(unix_nl, "wb") |
37 |
| - |
38 |
| - non_existent_file <- tempfile() |
| 21 | +test_that("detect_line_ending defaults to unix", { |
| 22 | + non_existent_file <- withr::local_tempfile() |
| 23 | + expect_equal(detect_line_ending(non_existent_file), "\n") |
39 | 24 |
|
40 |
| - empty_file <- tempfile() |
| 25 | + empty_file <- withr::local_tempfile() |
41 | 26 | file.create(empty_file)
|
| 27 | + expect_equal(detect_line_ending(empty_file), "\n") |
| 28 | +}) |
42 | 29 |
|
43 |
| - on.exit({ |
44 |
| - unlink(c(win_nl, unix_nl, empty_file, non_existent_file)) |
45 |
| - }) |
46 |
| - |
47 |
| - base::writeLines(c("foo", "bar"), win_nl_con, sep = "\r\n") |
48 |
| - close(win_nl_con) |
49 |
| - |
50 |
| - base::writeLines(c("foo", "bar"), unix_nl_con, sep = "\n") |
51 |
| - close(unix_nl_con) |
52 |
| - |
| 30 | +test_that("write_lines preserves existing line ending", { |
| 31 | + win_nl <- withr::local_tempfile() |
| 32 | + write_lines("baz", win_nl, line_ending = "\r\n") |
53 | 33 | write_lines("baz", win_nl)
|
54 | 34 | expect_equal(readChar(win_nl, 100), "baz\r\n")
|
55 | 35 |
|
| 36 | + unix_nl <- withr::local_tempfile() |
| 37 | + write_lines("baz", unix_nl, line_ending = "\n") |
56 | 38 | write_lines("baz", unix_nl)
|
57 | 39 | expect_equal(readChar(unix_nl, 100), "baz\n")
|
58 |
| - |
59 |
| - write_lines("baz", non_existent_file) |
60 |
| - expect_equal(readChar(non_existent_file, 100), "baz\n") |
61 |
| - |
62 |
| - write_lines("baz", empty_file) |
63 |
| - expect_equal(readChar(empty_file, 100), "baz\n") |
64 |
| -}) |
65 |
| - |
66 |
| -test_that("write_lines writes unix-style line endings.", { |
67 |
| - path <- test_path("escapes.Rd") |
68 |
| - # skip if checked on windows with autocrlf = true |
69 |
| - skip_if(detect_line_ending(path) == "\r\n") |
70 |
| - |
71 |
| - temp <- withr::local_tempfile() |
72 |
| - write_lines(read_lines(path), temp) |
73 |
| - |
74 |
| - old_binary <- readBin(path, "raw", n = file.info(path)$size) |
75 |
| - new_binary <- readBin(temp, "raw", n = file.info(temp)$size) |
76 |
| - expect_equal(new_binary, old_binary) |
77 | 40 | })
|
0 commit comments