Skip to content

Commit

Permalink
Merge pull request #158 from jhudsl/cansavvy/pair-program
Browse files Browse the repository at this point in the history
Unit Testing for make_screenshots and some other things 😬
  • Loading branch information
cansavvy authored Dec 19, 2024
2 parents 2e19782 + 7cb4d54 commit 49ef996
Show file tree
Hide file tree
Showing 60 changed files with 1,565 additions and 1,473 deletions.
25 changes: 14 additions & 11 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,37 @@ jobs:
R-CMD-check:
runs-on: ubuntu-latest
container:
image: jhudsl/base_ottr
image: jhudsl/base_ottr:main

steps:
- uses: actions/checkout@v4

- name: Query dependencies
run: |
install.packages('remotes')
saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2)
writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version")
shell: Rscript {0}

- name: Install dependencies
run: |
remotes::install_deps(dependencies = TRUE)
remotes::install_cran("devtools")
sessionInfo()
remotes::install_cran("rcmdcheck")
shell: Rscript {0}
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

- name: Check
env:
GH_PAT: ${{ secrets.GH_PAT }}
run: |
options(crayon.enabled = TRUE)
devtools::document()
devtools::load_all()
rcmdcheck::rcmdcheck(args = c("--no-manual"), check_dir = "check")
rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran"), check_dir = "check")
shell: Rscript {0}
env:
GH_PAT: ${{ secrets.GH_PAT }}

- name: Check testthat
id: check_check
run: |
Rscript -e "install.packages('tidyr', repos = 'http://cran.us.r-project.org')"
Rscript -e "devtools::test()"
error_num=$(Rscript --vanilla '.github/workflows/check_testthat.R')
echo "error_num=$error_num" >> $GITHUB_OUTPUT
Expand All @@ -52,5 +56,4 @@ jobs:
if: failure()
uses: actions/upload-artifact@main
with:
name: ${{ runner.os }}-r${{ matrix.config.r }}-results
path: check
31 changes: 7 additions & 24 deletions .github/workflows/check_testthat.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,21 @@ library(magrittr)
# Find .git root directory
root_dir <- rprojroot::find_root(rprojroot::has_dir(".git"))

# Read in the testthat results
out_file <- list.files(pattern = "testthat.Rout$|Rout.fail$", file.path(root_dir, "check"),
out_file <- list.files(pattern = ".Rout$|Rout.fail$", file.path(root_dir, "check"),
recursive = TRUE, full.names = TRUE)

# Extract testhat results
testthat_check_content <- readLines(out_file)
testthat_result <- grep("\\[ FAIL", testthat_check_content, value = TRUE)[1]
testthat_result <- unlist(strsplit(testthat_result, "\\||\\[|\\]"))

# Read in standard check results
check_content <- readLines(file.path(root_dir, "check", "ottrpal.Rcheck", "00check.log"))

# Extract standard check results
check_result <- grep("Status\\:", check_content, value = TRUE)
check_result <- unlist(strsplit(check_result, ","))
check_result <- stringr::str_remove(check_result, "Status:| ")
check_content <- readLines(out_file)
test_result <- grep("\\[ FAIL", check_content, value = TRUE)[1]
test_result <- unlist(strsplit(test_result, "\\||\\[|\\]"))

# Format the data into a dataframe
testthat_result_df <- data.frame(result = trimws(testthat_result)) %>%
test_result_df <- data.frame(result = trimws(test_result)) %>%
dplyr::filter(result != "") %>%
tidyr::separate(result, sep = " ", into = c("test_name", "num")) %>%
dplyr::mutate(num = as.numeric(num))

# Do the same for the check results
check_result_df <- data.frame(result = trimws(check_result)) %>%
tidyr::separate(result, sep = " ", into = c("num", "test_name")) %>%
dplyr::mutate(num = as.numeric(num)) %>%
dplyr::select("test_name", "num")

# We only want warnings or errors or fails
fail_num <- dplyr::bind_rows(check_result_df, testthat_result_df) %>%
dplyr::filter(test_name %in% c("FAIL", "WARN", "WARNINGs", "ERROR")) %>%
fail_num <- test_result_df %>%
dplyr::filter(test_name %in% c("FAIL", "WARN")) %>%
dplyr::summarize(total = sum(num))

fail_num <- as.character(fail_num$total)
Expand Down
7 changes: 3 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ Imports:
dplyr,
fs,
httr,
janitor,
gitcreds,
jsonlite,
knitr (>= 1.33),
magrittr,
openssl,
purrr,
quarto,
R.utils,
readr,
rmarkdown (>= 2.10),
Expand All @@ -44,12 +45,10 @@ Suggests:
testthat (>= 3.0.0),
tibble,
utils
Remotes:
jhudsl/cow
VignetteBuilder:
knitr
ByteCompile: true
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Config/testthat/edition: 3
14 changes: 7 additions & 7 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,58 @@ export(auth_from_secret)
export(authorize)
export(bad_quiz_path)
export(check_all_questions)
export(check_git_repo)
export(check_question)
export(check_quiz)
export(check_quiz_attributes)
export(check_quiz_question_attributes)
export(check_quizzes)
export(clean_up)
export(convert_coursera_quizzes)
export(convert_quiz)
export(course_path)
export(course_to_book_txt)
export(download_ottr_template)
export(extract_meta)
export(extract_object_id)
export(extract_quiz)
export(get_chapters)
export(get_git_auth)
export(get_gs_pptx)
export(get_image_from_slide)
export(get_image_link_from_slide)
export(get_object_id_notes)
export(get_pages_url)
export(get_repo_info)
export(get_slide_id)
export(get_slide_page)
export(get_yaml_spec)
export(good_quiz_path)
export(gs_id_from_slide)
export(gs_png_download)
export(gs_png_url)
export(include_slide)
export(make_embed_markdown)
export(make_screenshots)
export(output_destination)
export(parse_q_tag)
export(parse_quiz)
export(parse_quiz_df)
export(pptx_notes)
export(pptx_slide_note_df)
export(pptx_slide_text_df)
export(qrmd_files)
export(render_without_toc)
export(set_knitr_image_path)
export(set_up_leanpub)
export(setup_ottr_template)
export(unzip_pptx)
export(website_to_embed_leanpub)
export(xml_notes)
import(dplyr)
importFrom(gitcreds,gitcreds_get)
importFrom(httr,GET)
importFrom(httr,accept_json)
importFrom(httr,authenticate)
importFrom(httr,config)
importFrom(httr,content)
importFrom(httr,oauth2.0_token)
importFrom(httr,oauth_app)
importFrom(httr,oauth_endpoints)
importFrom(janitor,make_clean_names)
importFrom(jsonlite,fromJSON)
importFrom(magrittr,"%>%")
importFrom(openssl,aes_cbc_decrypt)
Expand Down
113 changes: 113 additions & 0 deletions R/book_txt.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@

#' Create Book.txt file from files existing in quiz directory
#'
#' @param path path to the bookdown or quarto course repository, must have a `_bookdown.yml` or `_quarto.yml` file
#' @param md_files vector of file path of the md's to be included
#' @param output_dir output directory to put files. It should likely be
#' relative to path
#' @param quiz_dir Where are the quizzes stored? Default looks for folder called "quizzes".
#' @param verbose print diagnostic messages
#'
#' @return A list of quiz and chapter files in order in a file called Book.txt -- How Leanpub wants it.
#' @export
#'
course_to_book_txt <- function(path = ".",
md_files = NULL,
output_dir = "manuscript",
quiz_dir = "quizzes",
verbose = TRUE) {
# If md_files are not specified, then try to get them
if (is.null(md_files)) {
# Establish path
root_dir <- course_path(path = path)

rmd_regex <- "[.][q|R|r]md$"

# Extract the names of the Rmd files (the chapters)
md_files <- qrmd_files(path = root_dir)
}

if (!is.null(quiz_dir)) {
# Find the quiz files in the quiz directory
quiz_files <- list.files(pattern = "\\.md$", quiz_dir)

# Put files in one vector
all_files <- c(md_files, quiz_files)

# Make a vector specifying the file type: quiz or not
file_type <- c(
rep("non-quiz", length(md_files)),
rep("quiz", length(quiz_files))
)
} else {
all_files <- md_files
file_type <- rep("non-quiz", length(md_files))
}
# Put all files in one data.frame
all_files <- data.frame(
file_name = all_files,
file_type
) %>%
dplyr::mutate(
# Use this so we don't have to fiddle with case senstivity for the next step
lower_filename = tolower(file_name),
# Get the number from the file name and that will be the order
num = stringr::str_extract(file_name, "([0-9]+)"),
num = dplyr::case_when(
# Put index file first and about file last
lower_filename == "index.rmd" ~ "0",
lower_filename == "about.rmd" ~ as.character(length(all_files)),
TRUE ~ num
),
num = as.numeric(num),
file_name = gsub(".Rmd$", ".md", file_name)
) %>%
# Put quizzes in order!
dplyr::arrange(num, file_type) %>%
dplyr::pull(file_name)

# Declare output file name
book_txt <- file.path(output_dir, "Book.txt")

if (verbose) {
message(paste0("Autogenerated Book.txt saved to: ", book_txt))
}
# need to fix about quiz
writeLines(all_files, con = book_txt)
}

#' Get file paths to all qmds or rmds in the course website directory
#'
#' @param path Where to look for the _bookdown.yml or _quarto.yml file. Passes to get_yaml_spec() function. By default looks in current directory
#'
#' @return The file paths to rmds or wmds listed in the _bookdown.yml or _quarto.yml file.
#' @export
#'
qrmd_files <- function(path = ".") {
spec <- get_yaml_spec(file.path(path))

rmd_files <- spec$rmd_files
qmd_files <- grep(".qmd", unlist(spec$book$chapters), value = TRUE)

if (length(rmd_files) > 0 && length(qmd_files) > 0) stop("Both qmd and rmd files are found. Not sure what format to expect")

# Make files whichever ones exist here
if (length(rmd_files) > 0) files <- rmd_files
if (length(qmd_files) > 0) files <- qmd_files

if (length(rmd_files) == 0 && length(qmd_files) == 0) {
warning(
"No rmd or qmd files found specified in the _quarto.yml or _bookdown.yml file. Going to try to find files in the repo based on suffix"
)
root_dir <- course_path(path = file.path(path))
files <- list.files(
pattern = "[.]Rmd$|[.]qmd$", ignore.case = TRUE,
path = root_dir, full.names = FALSE
)
}

# If we don't find files STOP
if (length(files) == 0) stop("No rmd/qmd files found in the repo")

return(files)
}
Loading

0 comments on commit 49ef996

Please sign in to comment.