Skip to content

Commit

Permalink
Add tests for #23 close #23
Browse files Browse the repository at this point in the history
  • Loading branch information
chainsawriot committed Feb 11, 2023
1 parent 5bf4fea commit b1916ce
Showing 1 changed file with 68 additions and 2 deletions.
70 changes: 68 additions & 2 deletions tests/testthat/test_dockerize.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ test_that("integration of #20 to dockerize()", {
gran_ok <- readRDS("../testdata/gran_ok.RDS")
gran_ok$r_version <- "3.2.0"
dockerize(gran_ok, output_dir = temp_dir) ## cran_mirror = "https://cran.r-project.org/"
x <- readLines(file.path(temp_dir, "gran.R"))
x <- readLines(file.path(temp_dir, "gran.R"))
expect_false(any(grepl("^cran_mirror <- \"https://cran\\.r\\-project\\.org/\"", x)))
expect_true(any(grepl("^cran_mirror <- \"http://cran\\.r\\-project\\.org/\"", x)))
})
Expand All @@ -100,7 +100,7 @@ test_that("Docker R < 2.1", {
expect_error(dockerize(gran_rio, output_dir = temp_dir), NA)
gran_rio <- readRDS("../testdata/gran_rio_old.RDS")
gran_rio$r_version <- "2.0.0"
expect_error(dockerize(gran_rio, output_dir = temp_dir))
expect_error(dockerize(gran_rio, output_dir = temp_dir))
})

test_that(".consolidate_sysreqs and issue #21", {
Expand All @@ -123,3 +123,69 @@ test_that("Dockerize warning, issue #21", {
expect_warning(dockerize(graph, output_dir = temp_dir))
})

test_that("material_dir, non-existing, #23", {
## normal case
gran_rio <- readRDS("../testdata/gran_rio_old.RDS")
temp_dir <- .gen_temp_dir()
expect_error(dockerize(gran_rio, output_dir = temp_dir, materials_dir = NULL), NA)
## non-existing
fake_material_dir <- .gen_temp_dir()
expect_false(dir.exists(fake_material_dir))
expect_error(dockerize(gran_rio, output_dir = temp_dir, materials_dir = fake_material_dir))
})

test_that("material_dir, existing, no subdir, #23", {
## exist, but empty dir
## Pre R 3.1.0
gran_rio <- readRDS("../testdata/gran_rio_old.RDS")
temp_dir <- .gen_temp_dir()
fake_material_dir <- .gen_temp_dir()
dir.create(fake_material_dir)
dockerize(gran_rio, output_dir = temp_dir, materials_dir = fake_material_dir)
expect_true(dir.exists(file.path(temp_dir, "materials")))
expect_equal(list.files(file.path(temp_dir, "materials")), character(0))
expect_true(any(readLines(file.path(temp_dir, "Dockerfile")) == "COPY materials/ ./materials/"))
## Post R 3.1.0
graph <- readRDS("../testdata/graph.RDS")
temp_dir <- .gen_temp_dir()
fake_material_dir <- .gen_temp_dir()
dir.create(fake_material_dir)
dockerize(graph, output_dir = temp_dir, materials_dir = fake_material_dir)
expect_true(dir.exists(file.path(temp_dir, "materials")))
expect_equal(list.files(file.path(temp_dir, "materials")), character(0))
expect_true(any(readLines(file.path(temp_dir, "Dockerfile")) == "COPY materials/ ./materials/"))
## Will only test post 3.1.0 from now on
## some files in fake_material_dir
temp_dir <- .gen_temp_dir()
fake_material_dir <- .gen_temp_dir()
dir.create(fake_material_dir)
file.copy("../testdata/graph.RDS", file.path(fake_material_dir, "graph.RDS"))
writeLines(c("831721", "GESIS"), file.path(fake_material_dir, "test.R"))
dockerize(graph, output_dir = temp_dir, materials_dir = fake_material_dir)
expect_true(dir.exists(file.path(temp_dir, "materials")))
expect_equal(list.files(file.path(temp_dir, "materials")), c("graph.RDS", "test.R"))
expect_true(any(readLines(file.path(temp_dir, "Dockerfile")) == "COPY materials/ ./materials/"))
expect_true(file.exists(file.path(temp_dir, "materials", "graph.RDS")))
expect_true(file.exists(file.path(temp_dir, "materials", "test.R")))
content <- readLines(file.path(temp_dir, "materials", "test.R"))
expect_equal(content[1], "831721")
expect_equal(content[2], "GESIS")
})

test_that("material_dir, existing, with 1 subdir, #23", {
temp_dir <- .gen_temp_dir()
fake_material_dir <- .gen_temp_dir()
dir.create(fake_material_dir)
dir.create(file.path(fake_material_dir, "data"))
file.copy("../testdata/graph.RDS", file.path(fake_material_dir, "data", "graph.RDS"))
writeLines(c("831721", "GESIS"), file.path(fake_material_dir, "test.R"))
dockerize(graph, output_dir = temp_dir, materials_dir = fake_material_dir)
expect_true(dir.exists(file.path(temp_dir, "materials")))
expect_true(any(readLines(file.path(temp_dir, "Dockerfile")) == "COPY materials/ ./materials/"))
expect_true(dir.exists(file.path(temp_dir, "materials", "data")))
expect_true(file.exists(file.path(temp_dir, "materials", "data", "graph.RDS")))
expect_true(file.exists(file.path(temp_dir, "materials", "test.R")))
content <- readLines(file.path(temp_dir, "materials", "test.R"))
expect_equal(content[1], "831721")
expect_equal(content[2], "GESIS")
})

0 comments on commit b1916ce

Please sign in to comment.