Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check for potential course name inconsistencies #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions R/test_lesson.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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.
Expand All @@ -74,6 +79,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)
Expand All @@ -87,6 +96,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 ",
Expand Down
56 changes: 56 additions & 0 deletions tests/testthat/test_test_course.R
Original file line number Diff line number Diff line change
@@ -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)