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

Adding Unit Testing Framework #135

Merged
merged 40 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
ee2e074
Add tests
cansavvy Jun 25, 2024
950ed1d
Fix tests
cansavvy Jun 25, 2024
33ed9f3
Fix link
cansavvy Jun 25, 2024
e488cf7
Merge branch 'cansavvy/gha' into cansavvy/test-framework
cansavvy Jun 25, 2024
118d4f8
Update
cansavvy Jun 25, 2024
6aa771c
Merge remote-tracking branch 'origin/cansavvy/gha' into cansavvy/test…
cansavvy Jun 25, 2024
5143c2b
Update rendering tests
cansavvy Jun 25, 2024
ec4cb38
Fix the tests
cansavvy Jun 25, 2024
30c9010
Update tests
cansavvy Jun 27, 2024
8b97640
Update quiz checks because some are supposed to fail
cansavvy Jun 27, 2024
c590391
quiz_dir = NULL
cansavvy Jun 27, 2024
9abd2b2
update tests with quarto
cansavvy Jun 27, 2024
d3cd9cc
suppressWarnings
cansavvy Jun 27, 2024
8035ba7
environment set to docker image
cansavvy Jun 27, 2024
a5776e4
simplify
cansavvy Jun 27, 2024
a464b8c
simplify again
cansavvy Jun 27, 2024
2772292
GITHUB_PAT
cansavvy Jun 27, 2024
0f29909
switch to docker image with quarto
cansavvy Jun 27, 2024
676838c
Update test
cansavvy Jun 28, 2024
2a27605
Don't use main image
cansavvy Jun 28, 2024
76a688f
Docker image switch
cansavvy Jun 28, 2024
53585a7
quarto check
cansavvy Jun 28, 2024
083c922
Quarto check
cansavvy Jun 28, 2024
45594a8
Don't force install
cansavvy Jun 28, 2024
2fb50d4
"OTTR_Quarto-main"
cansavvy Jun 28, 2024
19dc238
Update quarto render
cansavvy Jun 28, 2024
a29e31e
as_job = FALSE
cansavvy Jun 28, 2024
b258b0b
OMG it was in front of me the whole time
cansavvy Jun 28, 2024
58cc9d2
Fix it
cansavvy Jun 28, 2024
1b10639
Ignore quarto session files
cansavvy Jun 28, 2024
f50f370
Update utils
cansavvy Jun 28, 2024
a9aa557
update man
cansavvy Jun 28, 2024
e2c02ff
Let's just devtools load this
cansavvy Jun 28, 2024
37266b5
Fix warnings
cansavvy Jun 28, 2024
12f49f7
Merge branch 'main' into cansavvy/test-framework
cansavvy Jun 28, 2024
aee7482
Update bookdown_to_leanpub.R
cansavvy Jun 29, 2024
d033e39
Update bookdown_to_leanpub.R
cansavvy Jun 29, 2024
41eaa57
Update bookdown_to_leanpub.R
cansavvy Jun 29, 2024
0565ebc
Update bookdown_to_leanpub.R
cansavvy Jun 29, 2024
38d6e4f
Fix it
cansavvy Jul 1, 2024
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
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ vignettes/.httr-oauth$
^package_bundles/leanbuild_0.1.2.tar.gz*
^package_bundles/leanbuild_0.1.2.tgz*
inst/extdata/tmp/default*
quarto-session*

^CRAN-SUBMISSION$
1 change: 0 additions & 1 deletion .github/.gitignore

This file was deleted.

57 changes: 10 additions & 47 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,68 +12,31 @@ name: R-CMD-check

jobs:
R-CMD-check:
runs-on: ${{ matrix.config.os }}

name: ${{ matrix.config.os }} (${{ matrix.config.r }})

strategy:
fail-fast: false
matrix:
config:
- {os: windows-latest, r: 'release'}
- {os: macOS-latest, r: 'release'}
- {os: ubuntu-20.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}
- {os: ubuntu-20.04, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}

env:
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
RSPM: ${{ matrix.config.rspm }}
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
runs-on: ubuntu-latest
container:
image: jhudsl/base_ottr

steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.config.r }}

- uses: r-lib/actions/setup-pandoc@v2

- 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: Restore R package cache
if: runner.os != 'Windows'
uses: actions/cache@v2
with:
path: ${{ env.R_LIBS_USER }}
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}
restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-

- name: Install system dependencies
if: runner.os == 'Linux'
run: |
while read -r cmd
do
eval sudo $cmd
done < <(Rscript -e 'writeLines(remotes::system_requirements("ubuntu", "20.04"))')

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

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

- name: Check testthat
if: runner.os != 'Windows'
Expand Down
File renamed without changes.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: ottrpal
Title: Companion Tools for Open-Source Tools for Training Resources (OTTR)
Version: 1.2.1
Version: 1.3.0
Authors@R: c(
person("Candace", "Savonen", , c("[email protected]","[email protected]"), role = c("aut", "cre"),
comment = c(ORCID = "0000-0001-6331-7070")),
Expand Down Expand Up @@ -40,7 +40,7 @@ Imports:
yaml
Suggests:
remotes,
testthat,
testthat (>= 3.0.0),
tibble,
utils
VignetteBuilder:
Expand All @@ -49,3 +49,4 @@ ByteCompile: true
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.3.1
Config/testthat/edition: 3
8 changes: 5 additions & 3 deletions R/bookdown_to_leanpub.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' Column names `url`, `chapt_title`, and `img_path` must be used.
#' If no chapter title column supplied, the basename of the url will be used,
#' If no image column supplied, default image used.
#' @param bookdown_index The file path of the rendered bookdown index.html file
#' @param html_page The file path of the rendered index.html file
#' @param base_url The base url of where the chapters are published -- the url to provide to the iframe in Leanpub
#' e.g. https://jhudatascience.org/OTTR_Template/coursera
#' @param default_img A google slide link to the default image to be used for all chapters
Expand Down Expand Up @@ -40,7 +40,7 @@
#' }
bookdown_to_embed_leanpub <- function(path = ".",
chapt_img_key = NULL,
bookdown_index = file.path(base_url, "index.html"),
html_page = file.path(base_url, "index.html"),
base_url = NULL,
clean_up = FALSE,
default_img = NULL,
Expand Down Expand Up @@ -79,6 +79,7 @@ bookdown_to_embed_leanpub <- function(path = ".",
}
chapt_df <- get_chapters(
html_page = paste0(base_url, "index.html"),
base_url = base_url
) %>%
dplyr::mutate(chapt_title = gsub("\\:|\\?|\\&|\\!|\\'", "", chapt_title))
}
Expand Down Expand Up @@ -293,7 +294,7 @@ make_embed_markdown <- function(url,

#' Make Leanpub file that has embed webpage of a chapter
#'
#' @param bookdown_index The file path of the rendered bookdown index.html file
#' @param html_page The file path of the rendered index.html file. It can be a url
#' @param base_url The base url of where the chapters are published -- the url to provide to the iframe in Leanpub
#' e.g. https://jhudatascience.org/OTTR_Template/coursera
#'
Expand All @@ -302,6 +303,7 @@ make_embed_markdown <- function(url,
#'
#' @export
#'

get_chapters <- function(html_page = file.path("docs", "index.html"),
base_url = ".") {
# Read in html
Expand Down
6 changes: 4 additions & 2 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ download_ottr_template <- function(dir = "inst/extdata", type = "rmd") {

url <- switch(type,
rmd = "https://github.com/jhudsl/OTTR_Template/archive/refs/heads/main.zip",
quarto = "https://github.com/fhdsl/ottr/archive/refs/heads/main.zip",
quarto = "https://github.com/fhdsl/OTTR_Quarto/archive/refs/heads/main.zip",
rmd_website = "https://github.com/jhudsl/OTTR_Template_Website/archive/refs/heads/main.zip",
quarto_website = "https://github.com/fhdsl/OTTR_Quarto_Website/archive/refs/heads/main.zip"
)
Expand All @@ -37,10 +37,12 @@ download_ottr_template <- function(dir = "inst/extdata", type = "rmd") {
download.file(url,
destfile = file_path
)
}
output_dir <- stringr::str_remove(file.path(dir, file_name), ".zip")

if (!dir.exists(output_dir)) {
unzip(file_path, exdir = dir)
}
output_dir <- stringr::str_remove(file.path(dir, file_name), ".zip")

return(output_dir)
}
Expand Down
3 changes: 2 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

utils::globalVariables(c(
"num", "quiz_dir", "type_url", "file_name", "trimmed", "quiz",
"quiz_path", "type", "q_num", "verbose", "chapt_title", "data_path", "image_dir"
"quiz_path", "type", "q_num", "verbose", "chapt_title", "data_path", "image_dir",
"convert_footnotes"
))


Expand Down
Binary file removed man/figures/New.png
Binary file not shown.
Binary file removed man/figures/Upload.png
Binary file not shown.
Binary file removed man/figures/copy_url.png
Binary file not shown.
12 changes: 12 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file is part of the standard setup for testthat.
# It is recommended that you do not modify it.
#
# Where should you do additional test configuration?
# Learn more about the roles of various files in:
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
# * https://testthat.r-lib.org/articles/special-files.html

library(testthat)
library(ottrpal)

test_check("ottrpal")
34 changes: 34 additions & 0 deletions tests/testthat/test-quarto_leanpub_prep.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
test_that("Create Leanpub IFrames for Quarto", {

dir <- download_ottr_template(dir = ".", type = "quarto")

quarto::quarto_render(dir,
metadata = list(sidebar = F, toc = F),
quarto_args = c('--output-dir', 'docs/no_toc/'))

# TODO: This should be functionalized and incorporated into the package
# curl -o make_screenshots.R https://raw.githubusercontent.com/jhudsl/ottr-reports/main/scripts/make_screenshots.R
# chapt_urls=$(Rscript --vanilla make_screenshots.R
# --git_pat sys.getEnv("GH_PAT")
# --repo fhdsl/OTTR_Quarto
# --output_dir resources/chapt_screen_images)

## TEST HERE:
# 1. Does each chapter have screenshot?
# 2. Is the file 'resources/chapt_screen_images/chapter_urls.tsv' made fresh?
# 2. Does chapter_urls.tsv file made have columns with information that are labeled "url", "chapt_title" and "img_path"

#ottrpal::bookdown_to_embed_leanpub(
# render = FALSE,
# chapt_img_key = 'resources/chapt_screen_images/chapter_urls.tsv',
# make_book_txt = TRUE,
# quiz_dir = NULL)

## TEST HERE:
# 1. Did each chapter get a md in the manuscript folder?
# 2. Does each md link to the appropriate sceenshot?
# 3. Did the screenshot file path that's in the md lead to the appropriate file path?

unlink(dir, recursive = TRUE)
file.remove(paste0(dir, ".zip"))
})
28 changes: 28 additions & 0 deletions tests/testthat/test-quizzes.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
test_that("Quiz checks", {
# Using good quiz md example

quiz_path <- good_quiz_path()
good_quiz <- readLines(quiz_path)
good_quiz_specs <- parse_quiz(good_quiz)
good_quiz_checks <- check_all_questions(good_quiz_specs)

# Using bad quiz md example
bad_quiz <- readLines(bad_quiz_path())
bad_quiz_specs <- parse_quiz(bad_quiz)

# THe following checks *should fail* because we're giving it a bad quiz.
bad_quiz_checks <- suppressWarnings(check_all_questions(bad_quiz_specs))

## Make a temporary quiz directory
quiz_dir <- dirname(good_quiz_path())

## Now check the quizzes in that directory
# The following checks *should also fail* because the bad quiz is in there
all_quiz_results <- suppressWarnings(check_quizzes(quiz_dir = quiz_dir))

## TEST HERE:
# 1. quiz_error_report.tsv should be made
# 2. bad quiz should have errors
# 3. good quiz should pass!

})
54 changes: 54 additions & 0 deletions tests/testthat/test-rendering.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
test_that("Rmd Rendering", {
rmd_dir <- download_ottr_template(dir = ".", type = "rmd")

dir.exists(rmd_dir)

bookdown::render_book(rmd_dir)

unlink(rmd_dir, recursive = TRUE)
file.remove(paste0(rmd_dir, ".zip"))
})

test_that("Quarto Rendering", {
quarto_dir <- download_ottr_template(dir = ".", type = "quarto")

dir.exists(quarto_dir)

# Render it normal
quarto::quarto_render(quarto_dir, as_job = FALSE)

# Render it a different way
quarto::quarto_render(quarto_dir,
metadata = list(sidebar = F, toc = F),
quarto_args = c("--output-dir", "docs/no_toc/"),
as_job = FALSE

)
unlink(quarto_dir, recursive = TRUE)
file.remove(paste0(quarto_dir, ".zip"))
})

test_that("Rmd Website Rendering", {
rmd_web_dir <- download_ottr_template(dir = ".", type = "rmd_website")

dir.exists(rmd_web_dir)

rmarkdown::clean_site(rmd_web_dir, preview = FALSE)

rmarkdown::render_site(rmd_web_dir)

unlink(rmd_web_dir, recursive = TRUE)
file.remove(paste0(rmd_web_dir, ".zip"))
})


test_that("Quarto Website Rendering", {
quarto_web_dir <- download_ottr_template(dir = ".", type = "quarto_website")

dir.exists(quarto_web_dir)

quarto::quarto_render(quarto_web_dir, as_job = FALSE)

unlink(quarto_web_dir, recursive = TRUE)
file.remove(paste0(quarto_web_dir, ".zip"))
})
35 changes: 35 additions & 0 deletions tests/testthat/test-rmd_leanpub_prep.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

test_that("Create Leanpub IFrames for Rmd", {

dir <- download_ottr_template(dir = ".", type = "rmd")

dir.exists(dir)

bookdown::render_book(dir)

# TODO: This should be functionalized and incorporated into the package
# curl -o make_screenshots.R https://raw.githubusercontent.com/jhudsl/ottr-reports/main/scripts/make_screenshots.R
# Rscript --vanilla make_screenshots.R
# --git_pat sys.getEnv("GH_PAT")
# --repo fhdsl/OTTR_Template
# --output_dir resources/chapt_screen_images)

## TEST HERE:
# 1. Does each chapter have screenshot?
# 2. Is the file 'resources/chapt_screen_images/chapter_urls.tsv' made fresh?
# 2. Does chapter_urls.tsv file made have columns with information that are labeled "url", "chapt_title" and "img_path"

#ottrpal::bookdown_to_embed_leanpub(
# render = FALSE,
# chapt_img_key = 'resources/chapt_screen_images/chapter_urls.tsv',
# make_book_txt = TRUE,
# quiz_dir = NULL)

## TEST HERE:
# 1. Did each chapter get a md in the manuscript folder?
# 2. Does each md link to the appropriate sceenshot?
# 3. Did the screenshot file path that's in the md lead to the appropriate file path?

unlink(dir, recursive = TRUE)
file.remove(paste0(dir, ".zip"))
})
Loading