Skip to content

Commit 2fc6214

Browse files
authored
Refactor write_lines tests (#1331)
1 parent 53d3a76 commit 2fc6214

File tree

1 file changed

+25
-62
lines changed

1 file changed

+25
-62
lines changed

tests/testthat/test-utils-io.R

Lines changed: 25 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,40 @@
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")
205

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+
})
2310

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")
2414
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")
2518
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")
2819
})
2920

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")
3924

40-
empty_file <- tempfile()
25+
empty_file <- withr::local_tempfile()
4126
file.create(empty_file)
27+
expect_equal(detect_line_ending(empty_file), "\n")
28+
})
4229

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")
5333
write_lines("baz", win_nl)
5434
expect_equal(readChar(win_nl, 100), "baz\r\n")
5535

36+
unix_nl <- withr::local_tempfile()
37+
write_lines("baz", unix_nl, line_ending = "\n")
5638
write_lines("baz", unix_nl)
5739
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)
7740
})

0 commit comments

Comments
 (0)