From 5334eac30f16729d1a9cf4dcb8d47ef9be5c534b Mon Sep 17 00:00:00 2001 From: jonmcalder Date: Fri, 21 Oct 2016 03:30:08 +0200 Subject: [PATCH 1/2] Add a basic check to test_course()/test_lesson() to identify potential course name inconsistencies in lesson.yaml files, and also include a test for this test function. This resolves issue #33. --- R/test_lesson.R | 10 ++++++ tests/testthat/test_test_course.R | 56 +++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 tests/testthat/test_test_course.R diff --git a/R/test_lesson.R b/R/test_lesson.R index f7240a3..4b9ce9f 100644 --- a/R/test_lesson.R +++ b/R/test_lesson.R @@ -74,6 +74,10 @@ test_lesson_by_name <- function(){ lesson_dir_path <- getOption("swirlify_lesson_dir_path") les <- yaml.load_file(getOption("swirlify_lesson_file_path")) + # Get name of course folder from path + # and replace underscores with spaces + course_folder_name <- gsub(pattern = "_", replacement = " ", basename(course_dir_path)) + # for (R_file in c("customTests.R", "initLesson.R")){ # R_file_path <- file.path(lesson_dir_path, R_file) # if(file.exists(R_file_path)) source(R_file_path,local = e) @@ -87,6 +91,12 @@ test_lesson_by_name <- function(){ if(is.null(question[[i]])) message("Please provide a value for the ", i, " key in the meta question.") } + if(question[["Course"]] != course_folder_name) { + message("Course name '" , question[["Course"]] , "' for ", + getOption("swirlify_lesson_name"), "\n", + "is inconsistent with the ", "(directory) course name: '", + course_folder_name, "'") + } } else if(question$Class == "cmd_question"){ for(i in c("Output", "CorrectAnswer", "AnswerTests", "Hint")){ if(is.null(question[[i]])) message("Please provide a value for the ", diff --git a/tests/testthat/test_test_course.R b/tests/testthat/test_test_course.R new file mode 100644 index 0000000..dca1b57 --- /dev/null +++ b/tests/testthat/test_test_course.R @@ -0,0 +1,56 @@ +context("Test test_course()") + +path <- tempdir() + +setwd(path) + +new_lesson("Test Lesson 1", "Test Course", open_lesson = FALSE) +add_license(author = "Test Author") +add_to_manifest() +wq_command("0", "0", "0", "0") +wq_message("0") + +new_lesson("Test Lesson 2", "Test Course", open_lesson = FALSE) +add_to_manifest() +wq_command("0", "0", "0", "0") +wq_message("0") + +test_lesson_2_path <- file.path(getOption("swirlify_lesson_dir_path"), "lesson.yaml") + +# Overwrite lesson.yaml for Test Lesson 2 +# replacing "Test Course" with "Inconsistent Course Name" +updated_yaml <- sub("Test Course", "Inconsistent Course Name", + readLines(test_lesson_2_path)) +cat(updated_yaml, file = test_lesson_2_path, sep = "\n") + +new_lesson("Test Lesson 3", "Test Course", open_lesson = FALSE) +add_to_manifest() +wq_command("0", "0", "0", "0") +wq_message("0") + + +zz <- file(file.path(path, "test.log"), open = "wt") +sink(zz) +sink(zz, type = "message") + +test_course() + +sink(type = "message") +sink() + +correct_output <- c("##### Begin testing: Test Lesson 1 #####", + "##### End testing: Test Lesson 1 #####", + "", + "##### Begin testing: Test Lesson 2 #####", + "Course name 'Inconsistent Course Name' for Test Lesson 2", + "is inconsistent with the (directory) course name: 'Test Course'", + "##### End testing: Test Lesson 2 #####", + "", + "##### Begin testing: Test Lesson 3 #####", + "##### End testing: Test Lesson 3 #####") + +test_that("test_course() passes with message about inconsistent course name", { + expect_true(all(correct_output %in% readLines(file.path(path, "test.log")))) +}) + +unlink(getOption("swirlify_course_dir_path"), recursive = TRUE, force = TRUE) \ No newline at end of file From 82d78041be6781037c30457a77cd695eb820b5e0 Mon Sep 17 00:00:00 2001 From: jonmcalder Date: Thu, 17 Nov 2016 22:24:59 +0200 Subject: [PATCH 2/2] Get and store currently set lesson at the outset of test_course(), and restore this lesson after the tests have run. --- R/test_lesson.R | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/R/test_lesson.R b/R/test_lesson.R index 4b9ce9f..f7bdf7a 100644 --- a/R/test_lesson.R +++ b/R/test_lesson.R @@ -34,6 +34,9 @@ test_lesson <- function(){ test_course <- function(){ lesson_file_check() + # Get currently set lesson + current_lesson_file_path <- getOption("swirlify_lesson_file_path") + if(!any(file.exists(file.path(getOption("swirlify_course_dir_path"), c("LICENSE.txt", "LICENSE", "LICENCE.txt", "LICENCE"))))){ message("It seems this course does not contian a LICENSE.txt file.\nYou can easily add a license with add_license().\n") } @@ -59,6 +62,8 @@ test_course <- function(){ set_lesson(path_to_yaml = lesson, open_lesson = FALSE, silent = TRUE) test_lesson_by_name() } + # Re-set lesson to restore to orginal state + set_lesson(current_lesson_file_path) } # Test all cmd and mult questions of any lesson of current course.