tests/testthat/test-set_project_env.R

context("unit test for set_project_env")

test_that("set_project_env loads global variables from yaml file to .project env", {
    ###########
    ## Setup ##
    ###########
    .delete_project_env()
    path_proj <- .get_projet_dir()
    path_temp <- .get_temp_dir()
    .delete_and_create_dir(path_temp)
    ## Create yaml file
    tags <- list()
    tags$options$path_project <- 'tempdir()'
    tags$options$path_dumpster <- 'file.path(.project$path_project, "dumpster")'
    tags$options$path_c <- "C:/"
    yaml::write_yaml(tags, file.path(path_temp, "config.yml"))

    ###########
    ## Tests ##
    ###########
    ## Run function
    expect_silent(set_project_env(path = path_temp))
    ## Check the expressions exist
    expect_true(exists("path_project", envir = .project))
    expect_true(exists("path_dumpster", envir = .project))
    expect_true(exists("path_c", envir = .project))
    ## Check the expressions evaluations are correct
    expect_equal(.project$path_project, tempdir())
    expect_equal(.project$path_dumpster, file.path(tempdir(), "dumpster"))
    expect_equal(.project$path_c, "C:/")
    ## Calling the function doesn't change the working directory
    expect_identical(.get_projet_dir(), path_proj)
})

test_that("set_project_env fails given invalid input arguemtns", {
    ###########
    ## Setup ##
    ###########
    .delete_project_env()
    path_temp <- .get_temp_dir()
    .delete_and_create_dir(path_temp)

    ###########
    ## Tests ##
    ###########
    ## config.yml doesn't exist
    expect_error(set_project_env(path = path_temp))
    ## config.yml doesn't have "options" field
    yaml::write_yaml(list("no-options" = TRUE), file.path(path_temp, "config.yml"))
    expect_error(set_project_env(path = path_temp))
})
tidylab/tidylab.deployment documentation built on June 9, 2019, 11:41 a.m.