-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.test_course.R
131 lines (122 loc) · 4.97 KB
/
.test_course.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
library(methods)
library(yaml)
library(swirl)
argv <- if (interactive()) "commit" else commandArgs(TRUE)
options(repos = c(CRAN="https://mran.revolutionanalytics.com/snapshot/2017-11-01"))
stopifnot(length(argv) == 1)
stopifnot(argv %in% c("commit", "push"))
test_lesson = function(lesson_dir){
print(paste("####Begin testing", lesson_dir))
.ls <- ls(envir = globalenv(), all.names = TRUE)
tryCatch({
.e <- environment(swirl:::any_of_exprs)
attach(.e)
e = new.env()
e$path <- lesson_dir
e$lesPath <- lesson_dir
# Since the initLesson might change working directory, load lesson yaml first before that happens.
lesson = yaml.load_file(paste0(lesson_dir,"/lesson.yaml"))
stopifnot(lesson[[1]]$Lesson == lesson_dir)
for(R_file in c("/../.initCourse.R", "/initLesson.R", "/customTests.R")){
R_file_path = paste0(lesson_dir, R_file)
if(file.exists(R_file_path)) source(R_file_path,local = e)
}
for(.i in seq_along(lesson)) {
question <- lesson[[.i]]
if(question$Class %in% c("cmd_question", "mult_question", "script")) {
cat(sprintf("(%d) > %s\n", .i, question$CorrectAnswer))
switch(question$Class,
"cmd_question" = {
suppressWarnings({
e$val <- eval(parse(text=question$CorrectAnswer), envir = e)
e$expr <- parse(text = question$CorrectAnswer)[[1]]
tryCatch({
attach(e)
stopifnot(eval(parse(text=question$AnswerTests), envir = e))
}, finally = {
detach(e)
})
})
if (grepl("correctVal", question$AnswerTests) && grepl("omnitest", question$AnswerTests)) {
# Test if correctVal only compare with character or numeric vector of length 1
stopifnot(is.character(e$val) || is.numeric(e$val))
if (mode(e$val) == "numeric") stopifnot(length(e$val) == 1)
}
},
"mult_question" = {
e$val <- as.character(question$CorrectAnswer)
stopifnot(eval(parse(text = question$AnswerTests), envir = e))
},
"script" = {
question$correctScript <- file.path(lesson_dir, "scripts", paste(tools::file_path_sans_ext(question$Script), "-correct.R", sep = ""))
if (file.exists(question$correctScript)) {
cat("Testing script...\n")
file.copy(question$correctScript, e$script_temp_path <- file.path(tempdir(), question$Script), overwrite = TRUE)
tryCatch({
attach(e)
source(question$correctScript, local = globalenv())
stopifnot(eval(parse(text = question$AnswerTests), envir = e))
}, finally = {
detach(e)
})
}
}
)
}
}
}, finally = {
detach(.e)
.ls2 <- ls(envir = globalenv(), all.names = TRUE)
rm(list = setdiff(.ls2, .ls), envir = globalenv())
gc()
})
print(paste("-----Testing", lesson_dir, "Done"))
}
course_name <- basename(getwd())
setwd("..");install_course_directory(course_name);setwd(course_name)
course_list <- list.dirs(".", recursive = FALSE)
course_list <- grep("^\\./(\\d{2})|(Project)|(Optional)-", course_list, value = TRUE)
course_list <- substring(course_list, 3, nchar(course_list))
course_list <- grep("^[^\\.]", course_list, value = TRUE)
course_list <- setdiff(course_list, c("Project-ROpenData-DataTaipei", "Optional-RDataMining-01-Clustering",
"Optional-RDataMining-02-Classification", "Optional-RDataMining-03-Association-Rule",
"Optional-RDataMining-04-Text-Mining"))
result.path <- ".result.csv"
result <- if (file.exists(result.path) && argv == "commit") {
read.csv(result.path, header = TRUE, stringsAsFactors = FALSE)
} else {
data.frame(course = course_list, result = FALSE, hash = "", stringsAsFactors = FALSE)
}
for(course in setdiff(course_list, result$course)) {
result <- rbind(result, data.frame(course = course, result = FALSE, hash = "", stringsAsFactors = FALSE))
}
for(course in course_list) {
course.result <- result[course == result$course,]
## check result
if (course.result$result) {
## check hash
if (tools::md5sum(file.path(course, "lesson.yaml")) == course.result$hash) {
cat(sprintf("course: %s is passed! \n", course))
next
}
}
if (course %in% "Optional-RDataMining-02-Classification") {
if (Sys.info()["sysname"] == "Windows") {
Sys.setlocale(locale = "cht")
}
test_lesson(course)
} else {
if (Sys.info()["sysname"] == "Windows") {
for(locale in c("Chinese", "cht")) {
Sys.setlocale(locale = locale)
test_lesson(course)
}
} else {
test_lesson(course)
}
}
## update result
result$hash[result$course == course] <- tools::md5sum(file.path(course, "lesson.yaml"))
result$result[result$course == course] <- TRUE
write.csv(result, file = result.path, row.names = FALSE)
}