File: test-get-zip-data-path.R

package info (click to toggle)
r-cran-zip 2.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 768 kB
  • sloc: ansic: 8,079; makefile: 2
file content (107 lines) | stat: -rw-r--r-- 2,836 bytes parent folder | download
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
test_that("get_zip_data", {
  on.exit(try(unlink(tmp, recursive = TRUE)), add = TRUE)
  dir.create(tmp <- tempfile())

  expect_equal(
    get_zip_data_path_recursive(tmp),
    df(paste0(tmp, "/"), normalizePath(tmp), TRUE)
  )
  expect_equal(get_zip_data_path_recursive(tmp), get_zip_data_path(tmp, TRUE))

  foobar <- file.path(tmp, "foobar")
  cat("foobar", file = foobar)

  expect_equal(
    get_zip_data_path_recursive(foobar),
    df(foobar, normalizePath(foobar), FALSE)
  )
  expect_equal(
    get_zip_data_path_recursive(foobar),
    get_zip_data_path(foobar, TRUE)
  )

  expect_equal(
    get_zip_data_path_recursive(tmp),
    df(
      c(paste0(tmp, "/"), file.path(tmp, "foobar")),
      normalizePath(c(tmp, foobar)),
      c(TRUE, FALSE)
    )
  )
  expect_equal(get_zip_data_path_recursive(tmp), get_zip_data_path(tmp, TRUE))

  expect_equal(
    withr::with_dir(tmp, get_zip_data_path_recursive(".")),
    df(c("./", "./foobar"), normalizePath(c(tmp, foobar)), c(TRUE, FALSE))
  )
  withr::with_dir(
    tmp,
    expect_equal(get_zip_data_path_recursive("."), get_zip_data_path(".", TRUE))
  )

  dir.create(file.path(tmp, "empty"))
  dir.create(file.path(tmp, "foo"))
  bar <- file.path(tmp, "foo", "bar")
  cat("bar\n", file = bar)

  data <- df(
    c(
      paste0(tmp, "/"),
      paste0(file.path(tmp, "empty"), "/"),
      paste0(file.path(tmp, "foo"), "/"),
      file.path(tmp, "foo", "bar"),
      file.path(tmp, "foobar")
    ),
    normalizePath(c(
      tmp,
      file.path(tmp, "empty"),
      file.path(tmp, "foo"),
      bar,
      file.path(tmp, "foobar")
    )),
    c(TRUE, TRUE, TRUE, FALSE, FALSE)
  )
  data <- data[order(data$file), ]
  rownames(data) <- NULL

  data2 <- get_zip_data_path_recursive(tmp)
  data2 <- data2[order(data2$file), ]
  rownames(data2) <- NULL

  expect_equal(data2, data)
  expect_equal(get_zip_data_path(tmp, TRUE), data)

  expect_equal(
    get_zip_data_path(c(foobar, bar), TRUE),
    df(c(foobar, bar), normalizePath(c(foobar, bar)), c(FALSE, FALSE))
  )

  expect_equal(
    get_zip_data_path(file.path(tmp, "foo"), TRUE),
    df(
      c(paste0(file.path(tmp, "foo"), "/"), file.path(tmp, "foo", "bar")),
      normalizePath(c(file.path(tmp, "foo"), file.path(tmp, "foo", "bar"))),
      c(TRUE, FALSE)
    )
  )
})

test_that("get_zip_data relative paths", {
  on.exit(try(unlink(tmp, recursive = TRUE)), add = TRUE)
  dir.create(tmp <- tempfile())

  dir.create(file.path(tmp, "foo"))
  dir.create(file.path(tmp, "foo", "bar"))

  withr::with_dir(
    file.path(tmp, "foo"),
    expect_equal(
      get_zip_data_path(file.path("..", "foo"), TRUE),
      df(
        paste0(c(file.path("..", "foo"), file.path("..", "foo", "bar")), "/"),
        normalizePath(c(file.path(tmp, "foo"), file.path(tmp, "foo", "bar"))),
        c(TRUE, TRUE)
      )
    )
  )
})