1 2test_that("can unzip all", { 3 z <- make_a_zip() 4 5 tmp2 <- test_temp_dir() 6 zip::unzip(z$zip, exdir = tmp2) 7 8 expect_true(file.exists(file.path(tmp2, basename(z$ex), "file1"))) 9 expect_true(file.exists(file.path(tmp2, basename(z$ex), "dir"))) 10 expect_true(file.exists(file.path(tmp2, basename(z$ex), "dir", "file2"))) 11 12 expect_equal(readLines(file.path(tmp2, basename(z$ex), "file1")), "file1") 13 expect_equal( 14 readLines(file.path(tmp2, basename(z$ex), "dir", "file2")), "file2") 15}) 16 17test_that("unzip creates exdir if needed", { 18 z <- make_a_zip() 19 20 tmp2 <- test_temp_dir(create = FALSE) 21 expect_false(file.exists(tmp2)) 22 zip::unzip(z$zip, exdir = tmp2) 23 24 expect_true(file.exists(tmp2)) 25 26 expect_true(file.exists(file.path(tmp2, basename(z$ex), "file1"))) 27 expect_true(file.exists(file.path(tmp2, basename(z$ex), "dir"))) 28 expect_true(file.exists(file.path(tmp2, basename(z$ex), "dir", "file2"))) 29 30 expect_equal(readLines(file.path(tmp2, basename(z$ex), "file1")), "file1") 31 expect_equal( 32 readLines(file.path(tmp2, basename(z$ex), "dir", "file2")), "file2") 33}) 34 35test_that("unzip certain files only", { 36 z <- make_a_zip() 37 38 ## No files 39 tmp2 <- test_temp_dir() 40 zip::unzip(z$zip, character(), exdir = tmp2) 41 expect_true(file.exists(tmp2)) 42 expect_equal(dir(tmp2), character()) 43 44 ## File in directory 45 tmp3 <- test_temp_dir() 46 zip::unzip(z$zip, paste0(basename(z$ex), "/", "file1"), exdir = tmp3) 47 expect_true(file.exists(tmp3)) 48 expect_true(file.exists(file.path(tmp3, basename(z$ex), "file1"))) 49 expect_false(file.exists(file.path(tmp3, basename(z$ex), "dir"))) 50 expect_equal(readLines(file.path(tmp3, basename(z$ex), "file1")), "file1") 51 52 ## Only file(s) in root 53 f <- test_temp_file() 54 cat("foobar\n", file = f) 55 zip <- test_temp_file(".zip") 56 zipr(zip, f) 57 58 tmp4 <- test_temp_dir() 59 zip::unzip(zip, paste0(basename(f)), exdir = tmp4) 60 expect_true(file.exists(tmp4)) 61 expect_equal(dir(tmp4), basename(f)) 62 expect_equal(readLines(file.path(tmp4, basename(f))), "foobar") 63 64 ## Directory only 65 tmp5 <- test_temp_dir() 66 zip::unzip(z$zip, paste0(basename(z$ex), "/dir/"), exdir = tmp5) 67 expect_true(file.exists(tmp5)) 68 expect_true(file.exists(file.path(tmp5, basename(z$ex), "dir"))) 69 70 ## Files and dirs 71 tmp6 <- test_temp_dir() 72 zip::unzip(z$zip, paste0(basename(z$ex), c("/dir/file2", "/file1")), 73 exdir = tmp6) 74 75 expect_true(file.exists(file.path(tmp6, basename(z$ex), "file1"))) 76 expect_true(file.exists(file.path(tmp6, basename(z$ex), "dir"))) 77 expect_true(file.exists(file.path(tmp6, basename(z$ex), "dir", "file2"))) 78 79 expect_equal(readLines(file.path(tmp6, basename(z$ex), "file1")), "file1") 80 expect_equal( 81 readLines(file.path(tmp6, basename(z$ex), "dir", "file2")), "file2") 82}) 83 84test_that("unzip sets mtime correctly", { 85 ## ten minutes earlier 86 mtime <- Sys.time() - 60 * 10 87 z <- make_a_zip(mtime = mtime) 88 89 ## Some Windows file systems have a 2-second precision 90 three <- as.difftime(3, units = "secs") 91 expect_true(all(abs(zip_list(z$zip)$timestamp - mtime) < 3)) 92 93 tmp2 <- test_temp_dir() 94 zip::unzip(z$zip, exdir = tmp2) 95 96 ok <- function(...) { 97 t <- file.info(file.path(tmp2, basename(z$ex), ...))$mtime 98 expect_true(abs(t - mtime) < 3) 99 } 100 101 ok("file1") 102 ok("file11") 103 ok("dir") 104 ok("dir", "file2") 105 ok("dir", "file3") 106}) 107 108test_that("overwrite is FALSE", { 109 z <- make_a_zip() 110 tmp <- test_temp_dir() 111 zip::unzip(z$zip, exdir = tmp) 112 zip::unzip(z$zip, exdir = tmp) 113 expect_error( 114 zip::unzip(z$zip, overwrite = FALSE, exdir = tmp), 115 "Not overwriting") 116}) 117 118test_that("junkpaths is TRUE", { 119 z <- make_a_zip() 120 tmp <- test_temp_dir() 121 zip::unzip(z$zip, exdir = tmp, junkpaths = TRUE) 122 123 expect_true(file.exists(file.path(tmp, "file1"))) 124 expect_true(file.exists(file.path(tmp, "file2"))) 125 expect_true(file.exists(file.path(tmp, "file11"))) 126 expect_true(file.exists(file.path(tmp, "file3"))) 127 128 expect_false(file.exists(file.path(tmp, "dir"))) 129 130 expect_equal(readLines(file.path(tmp, "file1")), "file1") 131 expect_equal(readLines(file.path(tmp, "file2")), "file2") 132}) 133 134test_that("permissions as kept on Unix", { 135 skip_on_os("windows") 136 137 tmp <- test_temp_dir() 138 Sys.chmod(tmp, "0777", FALSE) 139 140 cat("foobar\n", file = f <- file.path(tmp, "file1")) 141 Sys.chmod(f, "0400", FALSE) 142 143 dir.create(f <- file.path(tmp, "dir")) 144 Sys.chmod(f, "0700", FALSE) 145 146 cat("foobar2\n", file = f <- file.path(tmp, "dir", "file2")) 147 Sys.chmod(f, "0755", FALSE) 148 149 cat("foobar3\n", file = f <- file.path(tmp, "dir", "file3")) 150 Sys.chmod(f, "0777", FALSE) 151 152 zip <- test_temp_file(".zip", create = FALSE) 153 zipr(zip, tmp) 154 155 tmp2 <- test_temp_dir() 156 zip::unzip(zip, exdir = tmp2) 157 158 check_perm <- function(mode, ...) { 159 f <- file.path(tmp2, ...) 160 expect_equal(file.info(f)$mode, as.octmode(mode)) 161 } 162 163 check_perm("0777", basename(tmp)) 164 check_perm("0400", basename(tmp), "file1") 165 check_perm("0700", basename(tmp), "dir") 166 check_perm("0755", basename(tmp), "dir", "file2") 167 check_perm("0777", basename(tmp), "dir", "file3") 168}) 169