Skip to content

Commit

Permalink
Added check_installed_packages() for set_assignment and a few tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
oscpe436 committed Dec 30, 2015
1 parent bbedd3b commit 9f2ca62
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 8 deletions.
35 changes: 32 additions & 3 deletions R/set_assignment.R
Expand Up @@ -29,6 +29,8 @@ set_assignment <- function(path, auth_token = NULL){
assignment_yml_ok(path = temp_file)
file.copy(from = temp_file, to = dest, overwrite = TRUE)
assignment <- read_assignment_yml()
if("packages" %in% names(assignment))
check_installed_packages(assignment$packages)
message("Assignment set:\n", assignment$name, " : ", assignment$description)
invisible(dest)
}
Expand Down Expand Up @@ -74,8 +76,8 @@ assignment_yml_ok <- function(path = NULL){
#' boolean
#'
check_assignment_file <- function(assignment){
# The yml contain at most 4 slots.
check <- all(names(assignment) %in% c("name", "description", "reporter", "tasks", "mandatory"))
# The yml contain at most 6 slots.
check <- all(names(assignment) %in% c("name", "description", "reporter", "tasks", "mandatory", "packages"))
if(!check) stop("Assignment file contain erroneous parts (except name, desc., reporter, tasks and mandatory.")
# The name and description is of length 1
check <- all(unlist(lapply(assignment[c("name", "description")], length)) == 1)
Expand All @@ -85,7 +87,7 @@ check_assignment_file <- function(assignment){
if(inherits(urls, "try-error")) stop("All tasks in assignments file do not contain urls")
check <- !any(unlist(lapply(lapply(urls, path_type), class)) == "path_error")
if(!check) stop("Not all tasks in assignments have working urls.")

if("mandatory" %in% names(assignment)) {
# Check mandatory urls
urls <- try(as.list(assignment[["mandatory"]]$url), silent = TRUE)
Expand Down Expand Up @@ -193,3 +195,30 @@ show_tasks <- function(){
assignment <- read_assignment_yml()
names(assignment$task)
}


#' @title
#' Check whether required packages are installed and loaded.
#'
#' @description
#' Checks if the packages listed in assignment file are loaded and installed.
#' If not, a warning message is printed.
#' @param packages
#' Packages to check
#'
check_installed_packages <- function(packages) {

if(all(paste("package:", packages, sep="") %in% search())){
# All packages are loaded and installed
}else{
if(all(packages %in% rownames(installed.packages()))){
warning("The following packages need to be loaded:\n",
paste(packages[!paste("package:", packages, sep="") %in% search()], collapse = ", "))
}
else{
warning("The following packages need to be installed and then loaded:\n",
paste(packages[!packages %in% rownames(installed.packages())], collapse=", "))
}
}
}

15 changes: 15 additions & 0 deletions inst/extdata/example_assignment07_pkgs.yml
@@ -0,0 +1,15 @@
name : Test assignment 01
description : An example of how an assignment requiring packages can be constructed.
reporter : student
tasks:
task1:
url:
- https://raw.githubusercontent.com/MansMeg/markmyassignment/master/inst/extdata/example_task1.R
task2:
url:
- https://raw.githubusercontent.com/MansMeg/markmyassignment/master/inst/extdata/example_task2_a.R
- https://raw.githubusercontent.com/MansMeg/markmyassignment/master/inst/extdata/example_task2_b.R
mandatory:
url:
- https://raw.githubusercontent.com/MansMeg/markmyassignment/master/inst/extdata/example_mandatory.R
packages: ['ggplot2', 'stringr']
16 changes: 16 additions & 0 deletions man/check_installed_packages.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions tests/testthat/test-mark_my_assignment.R
Expand Up @@ -42,9 +42,10 @@ test_that(desc="Assertions on arguments in mark_my_assignment()",{


test_that(desc="mark_my_dir()",{
suppressMessages(set_assignment(paste0(system.file(package = "markmyassignment"), "/extdata/example_assignment01.yml")))
if(length(ls(name = .GlobalEnv)) == 0){
expect_is(mark_my_dir(paste0(system.file(package = "markmyassignment"), "/extdata/example_dir")), class = "data.frame")
expect_equal(nrow(mark_my_dir(paste0(system.file(package = "markmyassignment"), "/extdata/example_dir"))), 8)
}
test_assgn_file <- paste0(system.file(package = "markmyassignment"), "/extdata/example_assignment01.yml")
test_dir <- paste0(system.file(package = "markmyassignment"), "/extdata/example_dir")
res_mark <- mark_my_dir(directory = test_dir, lab_file = test_assgn_file)
expect_is(res_mark, class = "list")
expect_equal(length(res_mark), 2)
expect_is(res_mark[[1]], class = "testthat_results")
})
9 changes: 9 additions & 0 deletions tests/testthat/test-set_assignment.R
Expand Up @@ -37,3 +37,12 @@ test_that(desc="show_tasks()",{
suppressMessages(set_assignment(correct_local2))
expect_equal(show_tasks(), c("task1","task2"))
})

test_that(desc="check_installed_packages()",{
assgn_path <- paste0(system.file(package = "markmyassignment"), "/extdata/example_assignment07_pkgs.yml")
expect_warning(set_assignment(path = assgn_path))
if( all(c("ggplot2", "stringr") %in% installed.packages()) ){
library(ggplot2) ; library(stringr)
expect_is(suppressMessages(set_assignment(assgn_path)), "character")
}
})

0 comments on commit 9f2ca62

Please sign in to comment.