From 446e1b03d6d6f69cb5d908b163c2ab017cbce99f Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Fri, 20 Dec 2024 11:42:13 -0500 Subject: [PATCH 1/7] Add spell check --- R/auth.R | 11 ++- R/book_txt.R | 6 +- R/get_data.R | 26 +++---- R/github_handling.R | 4 +- R/google_slides.R | 1 - R/leanpub.R | 5 +- R/quiz_formatting.R | 1 - R/spell_check.R | 95 ++++++++++++++++++++++++ R/{token-handlers.R => token_handlers.R} | 0 R/{url-check.R => url_check.R} | 7 +- R/utils.R | 1 - inst/extdata/dictionary.txt | 67 +++++++++++++++++ inst/extdata/exclude_files.txt | 8 ++ inst/extdata/ignore-urls.txt | 5 ++ tests/testthat/test-spell-check.R | 64 ++++++++++++++++ 15 files changed, 264 insertions(+), 37 deletions(-) create mode 100644 R/spell_check.R rename R/{token-handlers.R => token_handlers.R} (100%) rename R/{url-check.R => url_check.R} (98%) create mode 100644 inst/extdata/dictionary.txt create mode 100644 inst/extdata/exclude_files.txt create mode 100644 inst/extdata/ignore-urls.txt create mode 100644 tests/testthat/test-spell-check.R diff --git a/R/auth.R b/R/auth.R index 25339e96..36a96039 100644 --- a/R/auth.R +++ b/R/auth.R @@ -16,7 +16,6 @@ #' authorize("github") #' #' authorize("google") -#' #' } authorize <- function(app_name = NULL, cache = FALSE, @@ -242,11 +241,11 @@ find_scopes <- function(app_name) { ### Declare all the scopes scopes <- list( google = c( - "https://www.googleapis.com/auth/drive", - "https://www.googleapis.com/auth/drive.file", - "https://www.googleapis.com/auth/drive.readonly", - "https://www.googleapis.com/auth/presentations", - "https://www.googleapis.com/auth/presentations.readonly" + "https://www.googleapis.com/auth/drive", + "https://www.googleapis.com/auth/drive.file", + "https://www.googleapis.com/auth/drive.readonly", + "https://www.googleapis.com/auth/presentations", + "https://www.googleapis.com/auth/presentations.readonly" ), github = c("repo") ) diff --git a/R/book_txt.R b/R/book_txt.R index 962a3fb4..a56e8c45 100644 --- a/R/book_txt.R +++ b/R/book_txt.R @@ -1,4 +1,3 @@ - #' 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 @@ -84,8 +83,7 @@ course_to_book_txt <- function(path = ".", #' @export #' qrmd_files <- function(path = ".") { - - yaml <- list.files(path = path, pattern ="_bookdown.yml|_quarto.yml", full.names = TRUE) + yaml <- list.files(path = path, pattern = "_bookdown.yml|_quarto.yml", full.names = TRUE) spec <- yaml::read_yaml(yaml) @@ -95,7 +93,7 @@ qrmd_files <- function(path = ".") { rmd_files <- spec$rmd_files } if (basename(yaml) == "_quarto.yml") { - qmd_files <- grep(".qmd", unlist(spec$book$chapters), value = TRUE) + 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") diff --git a/R/get_data.R b/R/get_data.R index 7d3b8d92..8b600157 100644 --- a/R/get_data.R +++ b/R/get_data.R @@ -46,19 +46,19 @@ setup_ottr_template <- function(dir = ".", type, render = TRUE) { ## Render it if (render) { - if (type == "rmd") bookdown::render_book(output_dir) - if (type == "rmd_website") rmarkdown::render_site(output_dir) - - if (type == "quarto" | type == "quarto_website") { - quarto::quarto_render(output_dir, as_job = FALSE) - } - if (type == "quarto") { - quarto::quarto_render(output_dir, - metadata = list(sidebar = F, toc = F), - quarto_args = c("--output-dir", "docs/no_toc/"), - as_job = FALSE - ) - } + if (type == "rmd") bookdown::render_book(output_dir) + if (type == "rmd_website") rmarkdown::render_site(output_dir) + + if (type == "quarto" | type == "quarto_website") { + quarto::quarto_render(output_dir, as_job = FALSE) + } + if (type == "quarto") { + quarto::quarto_render(output_dir, + metadata = list(sidebar = F, toc = F), + quarto_args = c("--output-dir", "docs/no_toc/"), + as_job = FALSE + ) + } } return(output_dir) } diff --git a/R/github_handling.R b/R/github_handling.R index 0c4c1029..5807060a 100644 --- a/R/github_handling.R +++ b/R/github_handling.R @@ -136,7 +136,6 @@ get_pages_url <- function(repo_name, get_repo_info <- function(repo_name, token = NULL, verbose = FALSE) { - # Try to get credentials other way if (is.null(token)) { # Get auth token @@ -205,8 +204,7 @@ get_repo_info <- function(repo_name, #' #' authorize("github") #' check_git_repo("jhudsl/OTTR_Template") -#' -#'} +#' } check_git_repo <- function(repo_name, token = NULL, silent = TRUE, diff --git a/R/google_slides.R b/R/google_slides.R index 80e41b60..958c262d 100644 --- a/R/google_slides.R +++ b/R/google_slides.R @@ -78,7 +78,6 @@ get_slide_page <- function(url) { #' @param output_dir path to output png #' @param overwrite should the slide PNG be overwritten? gs_png_download <- function(url, output_dir = ".", overwrite = TRUE) { - id <- get_slide_id(url) slide_id <- get_slide_page(url) url <- gs_png_url(url) diff --git a/R/leanpub.R b/R/leanpub.R index b9e51771..b546cbaf 100644 --- a/R/leanpub.R +++ b/R/leanpub.R @@ -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 clean_up Should the previous docs and manuscript folder be cleaned up? +#' @param clean_up Should the previous docs and manuscript folder be cleaned up? #' @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 @@ -52,7 +52,6 @@ website_to_embed_leanpub <- function(path = ".", remove_resources_start = FALSE, verbose = TRUE, footer_text = "") { - # Find the OTTR course root_dir <- course_path(path = path) @@ -60,7 +59,6 @@ website_to_embed_leanpub <- function(path = ".", rooted_quiz_dir <- file.path(root_dir, quiz_dir) if (clean_up) { - if (dir.exists(rooted_output_dir)) { message(paste("Clearing out old version of output files:", rooted_output_dir)) @@ -271,7 +269,6 @@ make_embed_markdown <- function(path = ".", get_chapters <- function(path = ".", html_page = file.path("docs", "index.html"), base_url = ".") { - # Put this relative to project path html_page <- file.path(root_dir, html_page) diff --git a/R/quiz_formatting.R b/R/quiz_formatting.R index 7a1956db..be92dd57 100644 --- a/R/quiz_formatting.R +++ b/R/quiz_formatting.R @@ -844,7 +844,6 @@ check_quizzes <- function(path = ".", write_report = TRUE, verbose = TRUE, ignore_coursera = TRUE) { - files <- list.files( pattern = "\\.md", ignore.case = TRUE, diff --git a/R/spell_check.R b/R/spell_check.R new file mode 100644 index 00000000..4e6b37e1 --- /dev/null +++ b/R/spell_check.R @@ -0,0 +1,95 @@ +#' Check spelling of all md,rmd, and qmd files +#' +#' @param path path to the bookdown or quarto course repository, must have a +#' `.github` folder which will be used to establish the top of the repo. +#' @param output_dir A relative file path to the folder (existing or not) that the +#' output check file should be saved to. Default is "check_reports" +#' @param resources_dir A relative file path to the folder (existing or not) that the +#' dictionary.txt file and exclude_files.txt will be found. Default is "resources". +#' If no dictionary.txt file and exclude_files.txt files are found, we will download one. +#' @param file_pattern A file pattern should we be looking for for the files whose +#' spelling should be tested. Default is "md$". Regex interpreted. +#' @return A file will be saved that lists the broken URLs will be saved to the specified output_dir. +#' @export +#' +#' @importFrom magrittr +#' +#' @examples +#' +#' rmd_dir <- setup_ottr_template(dir = ".", type = "rmd", render = FALSE) +#' +#' check_spelling(rmd_dir) +#' +#' # If there are broken URLs they will be printed in a list at "check_reports/url_checks.tsv" +#' +#' qmd_dir <- setup_ottr_template(dir = ".", type = "qmd", render = FALSE) +#' +#' check_spelling(qmd_dir) +#' +check_spelling <- function(path = ".", + output_dir = "check_reports", + resources_dir = "resources", + file_pattern = "md$") { + # Find .git root directory + root_dir <- rprojroot::find_root(path = path, rprojroot::has_dir(".github")) + + resources_dir <- file.path(root_dir, resources_dir) + output_dir <- file.path(root_dir, output_dir) + + if (!dir.exists(output_dir)) { + dir.create(output_dir, recursive = TRUE, showWarnings = FALSE) + } + if (!dir.exists(resources_dir)) { + dir.create(resources_dir, recursive = TRUE, showWarnings = FALSE) + } + + output_file <- file.path(output_dir, "spell_check_results.tsv") + dictionary_file <- file.path(resources_dir, "dictionary.txt") + exclude_file <- file.path(resources_dir, "exclude_files.txt") + + # Read in dictionary file if it exists + if (file.exists(dictionary_file)) { + dictionary <- readLines(dictionary_file) + } else { + dictionary <- "" + } + + # Read in ignore urls file if it exists + if (file.exists(exclude_file)) { + exclude_file <- readLines(exclude_file) + } else { + exclude_file <- "" + } + + # Only declare `.Rmd` files but not the ones in the style-sets directory + files <- list.files(pattern = file_pattern, recursive = TRUE, full.names = TRUE) + + if (exclude_file[1] != "") files <- grep(paste0(exclude_file, collapse = "|"), files, invert = TRUE, value = TRUE) + + tryCatch( + expr = { + # Run spell check + sp_errors <- spelling::spell_check_files(files, ignore = dictionary) + + if (nrow(sp_errors) > 0) { + sp_errors <- sp_errors %>% + data.frame() %>% + tidyr::unnest(cols = found) %>% + tidyr::separate(found, into = c("file", "lines"), sep = ":") + } else { + sp_errors <- data.frame(errors = NA) + } + }, + error = function(e) { + stop("Spell check did not work. Check that your dictionary is formatted correctly. You cannot have special characters (e.g., DinĂ©) in the dictionary.txt file. You need to use HTML formatting (e.g., Diné) for these.") + } + ) + + # Print out how many spell check errors + write(nrow(sp_errors), stdout()) + + # Save spell errors to file temporarily + readr::write_tsv(sp_errors, output_file) + + message(paste0("Saved to: ", output_file)) +} diff --git a/R/token-handlers.R b/R/token_handlers.R similarity index 100% rename from R/token-handlers.R rename to R/token_handlers.R diff --git a/R/url-check.R b/R/url_check.R similarity index 98% rename from R/url-check.R rename to R/url_check.R index a78f625a..5d0944a2 100644 --- a/R/url-check.R +++ b/R/url_check.R @@ -74,9 +74,9 @@ check_urls <- function(path = ".", if (nrow(all_urls_df) > 0) { if (!report_all) { - all_urls_df <- all_urls_df %>% - dplyr::filter(urls_status == "failed") %>% - readr::write_tsv(output_file) + all_urls_df <- all_urls_df %>% + dplyr::filter(urls_status == "failed") %>% + readr::write_tsv(output_file) } } else { all_urls_df <- data.frame(errors = NA) @@ -103,7 +103,6 @@ check_urls <- function(path = ".", #' @importFrom magrittr #' test_url <- function(url, ignore_urls = "") { - if (url %in% ignore_urls) { message(paste0("Ignoring: ", url)) return("ignored") diff --git a/R/utils.R b/R/utils.R index 7c6ee358..e7963b2b 100644 --- a/R/utils.R +++ b/R/utils.R @@ -17,7 +17,6 @@ utils::globalVariables(c( #' @export #' course_path <- function(path = ".") { - # Find .git root directory root_dir <- rprojroot::find_root(has_dir(".github"), path = path) diff --git a/inst/extdata/dictionary.txt b/inst/extdata/dictionary.txt new file mode 100644 index 00000000..1520d003 --- /dev/null +++ b/inst/extdata/dictionary.txt @@ -0,0 +1,67 @@ +AnVIL +Audiographer +Audiography +automagic +BIPOC +Bloomberg +Bookdown +chapt +Coursera +creativecommons +css +Datatrail +DataTrail +DISCOVERABLE +Dockerfile +Dockerhub +dropdown +enforceability +faq +favicon +FirstName +Funder +fyi +GDSCN +Generis +GH +Github +GitHub +https +ies +iframe +immunities +impactful +ITCR +itcrtraining +ITN +LastName +Leanpub +Licensor +LICENSOR +Markua +mentorship +MERCHANTABILITY +Muschelli +NCI +NHGRI +OTTR +ottrpal +ottrproject +Pandoc +png +pre +reproducibility +rmarkdown +RMarkdown +Savonen +sexualized +socio +synched +tada +UE +UE5 +underserved +Videographer +Videography +WIPO +www diff --git a/inst/extdata/exclude_files.txt b/inst/extdata/exclude_files.txt new file mode 100644 index 00000000..5525a403 --- /dev/null +++ b/inst/extdata/exclude_files.txt @@ -0,0 +1,8 @@ +About.Rmd +docs/* +style-sets/* +manuscript/* +CONTRIBUTING.md +LICENSE.md +code_of_conduct.md +README.md diff --git a/inst/extdata/ignore-urls.txt b/inst/extdata/ignore-urls.txt new file mode 100644 index 00000000..66f04c35 --- /dev/null +++ b/inst/extdata/ignore-urls.txt @@ -0,0 +1,5 @@ +https://www.someurl.html +https://www.contributor-covenant.org/version/2/0/code_of_conduct.html][v2.0]. +https://www.contributor-covenant.org/faq][FAQ]. +https://www.contributor-covenant.org/translations][translations]. +https://github.com/jhudsl/OTTR_Template/issues/new/choose)! diff --git a/tests/testthat/test-spell-check.R b/tests/testthat/test-spell-check.R new file mode 100644 index 00000000..70cd88de --- /dev/null +++ b/tests/testthat/test-spell-check.R @@ -0,0 +1,64 @@ + +output_file <- file.path("check_reports", "spell_check_results.tsv") + +test_that("Test spell checks for OTTR main", { + + rmd_dir <- setup_ottr_template(type = "rmd", render = FALSE) + + status <- check_spelling(rmd_dir) + testthat::expect_true(status == 0) + + # Make sure the report exists + testthat::expect_true(file.exists(file.path(rmd_dir, output_file))) + results <- readr::read_tsv(file.path(rmd_dir, output_file)) + + # It should be a data.frame + testthat::expect_true(is.data.frame(results)) + clean_up() +}) + +test_that("Test spell checks for OTTR Quarto main", { + qmd_dir <- setup_ottr_template(type = "qmd", render = FALSE) + + status <- check_spelling(qmd_dir) + testthat::expect_true(status == 0) + + # Make sure the report exists + testthat::expect_true(file.exists(file.path(qmd_dir, output_file))) + results <- readr::read_tsv(file.path(qmd_dir, output_file)) + + # It should be a data.frame + testthat::expect_true(is.data.frame(results)) + clean_up() +}) + +test_that("Test spell checks for OTTR web", { + rmd_web <- setup_ottr_template(type = "rmd_website", render = FALSE) + + status <- check_spelling(rmd_web) + testthat::expect_true(status == 0) + + # Make sure the report exists + testthat::expect_true(file.exists(file.path(rmd_web, output_file))) + results <- readr::read_tsv(file.path(rmd_web, output_file)) + + # It should be a data.frame + testthat::expect_true(is.data.frame(results)) + clean_up() +}) + +test_that("Test spell checks for OTTR Quarto web", { + ## Test spell + qmd_web <- setup_ottr_template(type = "quarto_website", render = FALSE) + + status <- check_spelling(qmd_web) + testthat::expect_true(status == 0) + + # Make sure the report exists + testthat::expect_true(file.exists(file.path(qmd_web, output_file))) + results <- readr::read_tsv(file.path(qmd_web, output_file)) + + # It should be a data.frame + testthat::expect_true(is.data.frame(results)) + clean_up() +}) From 86c034486e7a7eba9e9516237fdfba7c9465dec3 Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Fri, 20 Dec 2024 11:47:10 -0500 Subject: [PATCH 2/7] Fix spelling --- tests/testthat/test-check-urls.R | 2 +- tests/testthat/test-spell-check.R | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-check-urls.R b/tests/testthat/test-check-urls.R index ed335b1c..80428796 100644 --- a/tests/testthat/test-check-urls.R +++ b/tests/testthat/test-check-urls.R @@ -19,7 +19,7 @@ test_that("Test URL checks for OTTR main", { }) test_that("Test URL checks for OTTR Quarto main", { - qmd_dir <- setup_ottr_template(type = "qmd", render = FALSE) + qmd_dir <- setup_ottr_template(type = "quarto", render = FALSE) status <- check_urls(qmd_dir) testthat::expect_true(status == 0) diff --git a/tests/testthat/test-spell-check.R b/tests/testthat/test-spell-check.R index 70cd88de..33a6235a 100644 --- a/tests/testthat/test-spell-check.R +++ b/tests/testthat/test-spell-check.R @@ -18,7 +18,7 @@ test_that("Test spell checks for OTTR main", { }) test_that("Test spell checks for OTTR Quarto main", { - qmd_dir <- setup_ottr_template(type = "qmd", render = FALSE) + qmd_dir <- setup_ottr_template(type = "quarto", render = FALSE) status <- check_spelling(qmd_dir) testthat::expect_true(status == 0) From 7e7b57a6b087594e319f1fc9e259835c3c53c205 Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Fri, 20 Dec 2024 12:20:06 -0500 Subject: [PATCH 3/7] Return num of errors --- R/spell_check.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/R/spell_check.R b/R/spell_check.R index 4e6b37e1..efc17152 100644 --- a/R/spell_check.R +++ b/R/spell_check.R @@ -92,4 +92,6 @@ check_spelling <- function(path = ".", readr::write_tsv(sp_errors, output_file) message(paste0("Saved to: ", output_file)) + + return(nrow(sp_errors)) } From 5a03e57a6069bb1f93ee41c61ee32a400f2fe804 Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Fri, 20 Dec 2024 13:27:52 -0500 Subject: [PATCH 4/7] Add a spelling error --- R/spell_check.R | 4 ++-- R/url_check.R | 2 ++ tests/testthat/test-spell-check.R | 20 ++++++++++++++++---- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/R/spell_check.R b/R/spell_check.R index efc17152..eabc8143 100644 --- a/R/spell_check.R +++ b/R/spell_check.R @@ -62,7 +62,7 @@ check_spelling <- function(path = ".", } # Only declare `.Rmd` files but not the ones in the style-sets directory - files <- list.files(pattern = file_pattern, recursive = TRUE, full.names = TRUE) + files <- list.files(path = path, pattern = file_pattern, recursive = TRUE, full.names = TRUE) if (exclude_file[1] != "") files <- grep(paste0(exclude_file, collapse = "|"), files, invert = TRUE, value = TRUE) @@ -93,5 +93,5 @@ check_spelling <- function(path = ".", message(paste0("Saved to: ", output_file)) - return(nrow(sp_errors)) + return(as.numeric(nrow(sp_errors))) } diff --git a/R/url_check.R b/R/url_check.R index 5d0944a2..9f5fbdfd 100644 --- a/R/url_check.R +++ b/R/url_check.R @@ -89,6 +89,8 @@ check_urls <- function(path = ".", readr::write_tsv(all_urls_df, output_file) message(paste0("Saved to: ", output_file)) + + return(nrow(all_urls_df)) } diff --git a/tests/testthat/test-spell-check.R b/tests/testthat/test-spell-check.R index 33a6235a..d9913331 100644 --- a/tests/testthat/test-spell-check.R +++ b/tests/testthat/test-spell-check.R @@ -5,8 +5,11 @@ test_that("Test spell checks for OTTR main", { rmd_dir <- setup_ottr_template(type = "rmd", render = FALSE) + # Add in a spelling error + writeLines("A spelling error: sauibguiabsduvbasuidbv", file.path(qmd_web, "spell_test_error.md")) + status <- check_spelling(rmd_dir) - testthat::expect_true(status == 0) + testthat::expect_true(status > 0) # Make sure the report exists testthat::expect_true(file.exists(file.path(rmd_dir, output_file))) @@ -20,8 +23,11 @@ test_that("Test spell checks for OTTR main", { test_that("Test spell checks for OTTR Quarto main", { qmd_dir <- setup_ottr_template(type = "quarto", render = FALSE) + # Add in a spelling error + writeLines("A spelling error: sauibguiabsduvbasuidbv", file.path(qmd_web, "spell_test_error.md")) + status <- check_spelling(qmd_dir) - testthat::expect_true(status == 0) + testthat::expect_true(status > 0) # Make sure the report exists testthat::expect_true(file.exists(file.path(qmd_dir, output_file))) @@ -35,8 +41,11 @@ test_that("Test spell checks for OTTR Quarto main", { test_that("Test spell checks for OTTR web", { rmd_web <- setup_ottr_template(type = "rmd_website", render = FALSE) + # Add in a spelling error + writeLines("A spelling error: sauibguiabsduvbasuidbv", file.path(qmd_web, "spell_test_error.md")) + status <- check_spelling(rmd_web) - testthat::expect_true(status == 0) + testthat::expect_true(status > 0) # Make sure the report exists testthat::expect_true(file.exists(file.path(rmd_web, output_file))) @@ -51,8 +60,11 @@ test_that("Test spell checks for OTTR Quarto web", { ## Test spell qmd_web <- setup_ottr_template(type = "quarto_website", render = FALSE) + # Add in a spelling error + writeLines("A spelling error: sauibguiabsduvbasuidbv", file.path(qmd_web, "spell_test_error.md")) + status <- check_spelling(qmd_web) - testthat::expect_true(status == 0) + testthat::expect_true(status > 0) # Make sure the report exists testthat::expect_true(file.exists(file.path(qmd_web, output_file))) From 61957569251585e716c47524b5588056069eb993 Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Fri, 20 Dec 2024 13:41:09 -0500 Subject: [PATCH 5/7] Add tests --- inst/extdata/exclude_files.txt | 7 ------- tests/testthat/test-check-urls.R | 20 ++++++++++++++++---- tests/testthat/test-spell-check.R | 6 +++--- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/inst/extdata/exclude_files.txt b/inst/extdata/exclude_files.txt index 5525a403..a188e069 100644 --- a/inst/extdata/exclude_files.txt +++ b/inst/extdata/exclude_files.txt @@ -1,8 +1 @@ -About.Rmd docs/* -style-sets/* -manuscript/* -CONTRIBUTING.md -LICENSE.md -code_of_conduct.md -README.md diff --git a/tests/testthat/test-check-urls.R b/tests/testthat/test-check-urls.R index d30d4c4f..12d1caaf 100644 --- a/tests/testthat/test-check-urls.R +++ b/tests/testthat/test-check-urls.R @@ -5,8 +5,11 @@ test_that("Test URL checks for OTTR main", { rmd_dir <- setup_ottr_template(type = "rmd", render = FALSE) + # Add in a URL error + writeLines("A URL error: https://notawebsiteaaaaaaa.com", file.path(rmd_dir, "url_test_error.md")) + status <- check_urls(rmd_dir) - testthat::expect_true(status < 2) + testthat::expect_true(status > 0) # Make sure the report exists testthat::expect_true(file.exists(file.path(rmd_dir, output_file))) @@ -21,8 +24,11 @@ test_that("Test URL checks for OTTR main", { test_that("Test URL checks for OTTR Quarto main", { qmd_dir <- setup_ottr_template(type = "quarto", render = FALSE) + # Add in a URL error + writeLines("A URL error: https://notawebsiteaaaaaaa.com", file.path(qmd_dir, "url_test_error.md")) + status <- check_urls(qmd_dir) - testthat::expect_true(status < 2) + testthat::expect_true(status > 0) # Make sure the report exists testthat::expect_true(file.exists(file.path(qmd_dir, output_file))) @@ -36,8 +42,11 @@ test_that("Test URL checks for OTTR Quarto main", { test_that("Test URL checks for OTTR web", { rmd_web <- setup_ottr_template(type = "rmd_website", render = FALSE) + # Add in a URL error + writeLines("A URL error: https://notawebsiteaaaaaaa.com", file.path(rmd_web, "url_test_error.md")) + status <- check_urls(rmd_web, report_all = TRUE) - testthat::expect_true(status < 2) + testthat::expect_true(status > 0) # Make sure the report exists testthat::expect_true(file.exists(file.path(rmd_web, output_file))) @@ -52,8 +61,11 @@ test_that("Test URL checks for OTTR Quarto web", { ## Test URL qmd_web <- setup_ottr_template(type = "quarto_website", render = FALSE) + # Add in a URL error + writeLines("A URL error: https://notawebsiteaaaaaaa.com", file.path(qmd_web, "url_test_error.md")) + status <- check_urls(qmd_web, report_all = TRUE) - testthat::expect_true(status < 2) + testthat::expect_true(status > 0) # Make sure the report exists testthat::expect_true(file.exists(file.path(qmd_web, output_file))) diff --git a/tests/testthat/test-spell-check.R b/tests/testthat/test-spell-check.R index d9913331..d61c4df6 100644 --- a/tests/testthat/test-spell-check.R +++ b/tests/testthat/test-spell-check.R @@ -6,7 +6,7 @@ test_that("Test spell checks for OTTR main", { rmd_dir <- setup_ottr_template(type = "rmd", render = FALSE) # Add in a spelling error - writeLines("A spelling error: sauibguiabsduvbasuidbv", file.path(qmd_web, "spell_test_error.md")) + writeLines("A spelling error: sauibguiabsduvbasuidbv", file.path(rmd_dir, "spell_test_error.md")) status <- check_spelling(rmd_dir) testthat::expect_true(status > 0) @@ -24,7 +24,7 @@ test_that("Test spell checks for OTTR Quarto main", { qmd_dir <- setup_ottr_template(type = "quarto", render = FALSE) # Add in a spelling error - writeLines("A spelling error: sauibguiabsduvbasuidbv", file.path(qmd_web, "spell_test_error.md")) + writeLines("A spelling error: sauibguiabsduvbasuidbv", file.path(qmd_dir, "spell_test_error.md")) status <- check_spelling(qmd_dir) testthat::expect_true(status > 0) @@ -42,7 +42,7 @@ test_that("Test spell checks for OTTR web", { rmd_web <- setup_ottr_template(type = "rmd_website", render = FALSE) # Add in a spelling error - writeLines("A spelling error: sauibguiabsduvbasuidbv", file.path(qmd_web, "spell_test_error.md")) + writeLines("A spelling error: sauibguiabsduvbasuidbv", file.path(rmd_web, "spell_test_error.md")) status <- check_spelling(rmd_web) testthat::expect_true(status > 0) From 37195cb40ebede51dd1eee670f4d97f3802fbe11 Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Fri, 20 Dec 2024 13:47:38 -0500 Subject: [PATCH 6/7] Simplify check --- .github/workflows/R-CMD-check.yaml | 8 -------- 1 file changed, 8 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index c77e8a3a..390f4821 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -32,14 +32,6 @@ jobs: remotes::install_cran("rcmdcheck") shell: Rscript {0} - - name: Check - env: - GH_PAT: ${{ secrets.GH_PAT }} - run: | - options(crayon.enabled = TRUE) - rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran"), check_dir = "check") - shell: Rscript {0} - - name: Check package uses: r-lib/actions/check-r-package@v2 with: From 7205c38764473bdcef62ebca2c95590e4454161f Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Fri, 20 Dec 2024 13:55:25 -0500 Subject: [PATCH 7/7] Update docs --- NAMESPACE | 11 +++++++++ R/spell_check.R | 6 ++++- R/url_check.R | 29 ++++++++++++++++++++--- man/authorize.Rd | 1 - man/cache_secrets_folder.Rd | 2 +- man/check_git_repo.Rd | 1 - man/check_spelling.Rd | 46 +++++++++++++++++++++++++++++++++++++ man/check_urls.Rd | 46 +++++++++++++++++++++++++++++++++++++ man/get_urls.Rd | 26 +++++++++++++++++++++ man/setup_ottr_template.Rd | 4 +++- man/supported_endpoints.Rd | 2 +- man/test_url.Rd | 31 +++++++++++++++++++++++++ 12 files changed, 196 insertions(+), 9 deletions(-) create mode 100644 man/check_spelling.Rd create mode 100644 man/check_urls.Rd create mode 100644 man/get_urls.Rd create mode 100644 man/test_url.Rd diff --git a/NAMESPACE b/NAMESPACE index b87daad8..71d667a5 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -11,6 +11,8 @@ export(check_quiz) export(check_quiz_attributes) export(check_quiz_question_attributes) export(check_quizzes) +export(check_spelling) +export(check_urls) export(clean_up) export(convert_coursera_quizzes) export(convert_quiz) @@ -30,6 +32,7 @@ export(get_pages_url) export(get_repo_info) export(get_slide_id) export(get_slide_page) +export(get_urls) export(good_quiz_path) export(gs_id_from_slide) export(gs_png_download) @@ -46,10 +49,12 @@ export(pptx_slide_text_df) export(qrmd_files) export(render_without_toc) export(setup_ottr_template) +export(test_url) export(unzip_pptx) export(website_to_embed_leanpub) export(xml_notes) import(dplyr) +import(stringr) importFrom(gitcreds,gitcreds_get) importFrom(httr,GET) importFrom(httr,accept_json) @@ -64,7 +69,13 @@ importFrom(magrittr,"%>%") importFrom(readr,write_tsv) importFrom(rprojroot,find_root) importFrom(rprojroot,has_dir) +importFrom(rvest,html_attr) +importFrom(rvest,html_nodes) +importFrom(rvest,read_html) +importFrom(spelling,spell_check_files) importFrom(stringr,str_to_title) +importFrom(tidyr,separate) +importFrom(tidyr,unnest) importFrom(utils,browseURL) importFrom(utils,download.file) importFrom(utils,installed.packages) diff --git a/R/spell_check.R b/R/spell_check.R index eabc8143..e4f328be 100644 --- a/R/spell_check.R +++ b/R/spell_check.R @@ -12,7 +12,11 @@ #' @return A file will be saved that lists the broken URLs will be saved to the specified output_dir. #' @export #' -#' @importFrom magrittr +#' @importFrom magrittr %>% +#' @importFrom spelling spell_check_files +#' @importFrom rprojroot find_root has_dir +#' @importFrom tidyr unnest separate +#' @importFrom readr write_tsv #' #' @examples #' diff --git a/R/url_check.R b/R/url_check.R index 9f5fbdfd..56c03065 100644 --- a/R/url_check.R +++ b/R/url_check.R @@ -12,7 +12,10 @@ #' @return A file will be saved that lists the broken URLs will be saved to the specified output_dir. #' @export #' -#' @importFrom magrittr +#' @importFrom magrittr %>% +#' @importFrom rprojroot find_root has_dir +#' @importFrom tidyr unnest separate +#' @importFrom readr write_tsv #' #' @examples #' @@ -102,7 +105,18 @@ check_urls <- function(path = ".", #' @return a logical TRUE/FALSE for whether the URL is legitimate. #' @export #' -#' @importFrom magrittr +#' @importFrom magrittr %>% +#' @importFrom httr GET +#' +#' @examples /dontrun { +#' +#' # This should print out "failed" +#' test_url("https://notawebsiteaaaaaaa.com") +#' +#' +#' # This should print out "success" +#' test_url("https://github.com") +#' } #' test_url <- function(url, ignore_urls = "") { if (url %in% ignore_urls) { @@ -134,7 +148,16 @@ test_url <- function(url, ignore_urls = "") { #' @return a data.frame of all the URLs identified in the given rmd/qmd/md file #' @export #' -#' @importFrom magrittr +#' @importFrom magrittr %>% +#' @importFrom rvest html_nodes read_html html_attr +#' @import stringr +#' +#' @examples +#' +#' # Add in a URL error +# writeLines("A URL error: https://notawebsiteaaaaaaa.com", "url_test_error.md") +#' +#' get_urls("url_test_error.md") #' get_urls <- function(file, ignore_urls = "") { message(paste("##### Testing URLs from file:", file)) diff --git a/man/authorize.Rd b/man/authorize.Rd index 89799f4f..538c48c9 100644 --- a/man/authorize.Rd +++ b/man/authorize.Rd @@ -27,6 +27,5 @@ authorize() authorize("github") authorize("google") - } } diff --git a/man/cache_secrets_folder.Rd b/man/cache_secrets_folder.Rd index 5679b29c..55a7d980 100644 --- a/man/cache_secrets_folder.Rd +++ b/man/cache_secrets_folder.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/token-handlers.R +% Please edit documentation in R/token_handlers.R \name{cache_secrets_folder} \alias{cache_secrets_folder} \title{See where your cached secrets are being stored} diff --git a/man/check_git_repo.Rd b/man/check_git_repo.Rd index dfa35389..60819167 100644 --- a/man/check_git_repo.Rd +++ b/man/check_git_repo.Rd @@ -38,6 +38,5 @@ Given a repository name, check with git ls-remote whether the repository exists authorize("github") check_git_repo("jhudsl/OTTR_Template") - } } diff --git a/man/check_spelling.Rd b/man/check_spelling.Rd new file mode 100644 index 00000000..ba8ae893 --- /dev/null +++ b/man/check_spelling.Rd @@ -0,0 +1,46 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/spell_check.R +\name{check_spelling} +\alias{check_spelling} +\title{Check spelling of all md,rmd, and qmd files} +\usage{ +check_spelling( + path = ".", + output_dir = "check_reports", + resources_dir = "resources", + file_pattern = "md$" +) +} +\arguments{ +\item{path}{path to the bookdown or quarto course repository, must have a +`.github` folder which will be used to establish the top of the repo.} + +\item{output_dir}{A relative file path to the folder (existing or not) that the +output check file should be saved to. Default is "check_reports"} + +\item{resources_dir}{A relative file path to the folder (existing or not) that the +dictionary.txt file and exclude_files.txt will be found. Default is "resources". +If no dictionary.txt file and exclude_files.txt files are found, we will download one.} + +\item{file_pattern}{A file pattern should we be looking for for the files whose +spelling should be tested. Default is "md$". Regex interpreted.} +} +\value{ +A file will be saved that lists the broken URLs will be saved to the specified output_dir. +} +\description{ +Check spelling of all md,rmd, and qmd files +} +\examples{ + +rmd_dir <- setup_ottr_template(dir = ".", type = "rmd", render = FALSE) + +check_spelling(rmd_dir) + +# If there are broken URLs they will be printed in a list at "check_reports/url_checks.tsv" + +qmd_dir <- setup_ottr_template(dir = ".", type = "qmd", render = FALSE) + +check_spelling(qmd_dir) + +} diff --git a/man/check_urls.Rd b/man/check_urls.Rd new file mode 100644 index 00000000..9259829b --- /dev/null +++ b/man/check_urls.Rd @@ -0,0 +1,46 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/url_check.R +\name{check_urls} +\alias{check_urls} +\title{Check URLs of all md,rmd, and qmd files} +\usage{ +check_urls( + path = ".", + output_dir = "check_reports", + resources_dir = "resources", + report_all = FALSE +) +} +\arguments{ +\item{path}{path to the bookdown or quarto course repository, must have a +`.github` folder which will be used to establish the top of the repo.} + +\item{output_dir}{A relative file path to the folder (existing or not) that the +output check file should be saved to. Default is "check_reports"} + +\item{resources_dir}{A relative file path to the folder (existing or not) that the +ignore_urls.txt file and exclude_files.txt will be found. Default is "resources". +If no ignore_urls.txt file and exclude_files.txt files are found, we will download one.} + +\item{report_all}{Should all URLs that were tested be returned? Default is FALSE +meaning only broken URLs will be reported in the url_checks.tsv file.} +} +\value{ +A file will be saved that lists the broken URLs will be saved to the specified output_dir. +} +\description{ +Check URLs of all md,rmd, and qmd files +} +\examples{ + +rmd_dir <- setup_ottr_template(dir = ".", type = "rmd", render = FALSE) + +check_urls(rmd_dir) + +# If there are broken URLs they will be printed in a list at "check_reports/url_checks.tsv" + +qmd_dir <- setup_ottr_template(dir = ".", type = "qmd", render = FALSE) + +check_urls(qmd_dir) + +} diff --git a/man/get_urls.Rd b/man/get_urls.Rd new file mode 100644 index 00000000..b6c56cec --- /dev/null +++ b/man/get_urls.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/url_check.R +\name{get_urls} +\alias{get_urls} +\title{Identify and collect URLs in a single rmd/qmd/md file} +\usage{ +get_urls(file, ignore_urls = "") +} +\arguments{ +\item{file}{A file path to a rmd/qmd/md file that contains URLs to be check} + +\item{ignore_url}{A vector of URLs which to ignore.} +} +\value{ +a data.frame of all the URLs identified in the given rmd/qmd/md file +} +\description{ +Identify and collect URLs in a single rmd/qmd/md file +} +\examples{ + +# Add in a URL error + +get_urls("url_test_error.md") + +} diff --git a/man/setup_ottr_template.Rd b/man/setup_ottr_template.Rd index a0fee5b6..d4a981cb 100644 --- a/man/setup_ottr_template.Rd +++ b/man/setup_ottr_template.Rd @@ -4,12 +4,14 @@ \alias{setup_ottr_template} \title{Download and render files from main OTTR_Template to test} \usage{ -setup_ottr_template(dir = "inst/extdata", type) +setup_ottr_template(dir = ".", type, render = TRUE) } \arguments{ \item{dir}{What relative file path should the files be downloaded} \item{type}{Which OTTR repo are we downloading? Options are "rmd", "quarto", "rmd_website", "quarto_website"} + +\item{render}{Should the OTTR repo be rendered after downloading? Default is TRUE} } \value{ This downloads the main branch repo files from the respective repo for testing purposes diff --git a/man/supported_endpoints.Rd b/man/supported_endpoints.Rd index 7a74d439..bdf2c943 100644 --- a/man/supported_endpoints.Rd +++ b/man/supported_endpoints.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/token-handlers.R +% Please edit documentation in R/token_handlers.R \name{supported_endpoints} \alias{supported_endpoints} \title{Supported endpoints} diff --git a/man/test_url.Rd b/man/test_url.Rd new file mode 100644 index 00000000..605534e5 --- /dev/null +++ b/man/test_url.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/url_check.R +\name{test_url} +\alias{test_url} +\title{Test a URL} +\usage{ +test_url(url, ignore_urls = "") +} +\arguments{ +\item{url}{A single URL that will be checked whether it is real.} + +\item{ignore_url}{A vector of URLs which to ignore.} +} +\value{ +a logical TRUE/FALSE for whether the URL is legitimate. +} +\description{ +Test a URL +} +\examples{ +/dontrun { + +# This should print out "failed" +test_url("https://notawebsiteaaaaaaa.com") + + +# This should print out "success" +test_url("https://github.com") +} + +}