Skip to content

Commit

Permalink
Merge pull request #28 from A2-ai/newline_yamls
Browse files Browse the repository at this point in the history
adds no trailing newline warning to checklist directory validation
  • Loading branch information
jenna-a2ai authored Feb 3, 2025
2 parents 067f2bd + 8af12d3 commit 55cd8fd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: ghqc
Title: Manage QC via GitHub Issues using Shiny Apps
Version: 0.3.3
Version: 0.3.4
Authors@R: c(
person("Anne", "Zheng", email = "[email protected]", role = c("aut")),
person("Jenna", "Johnson", email = "[email protected]", role = c("aut")),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# ghqc 0.3.4

- adds warning to checklist validation if checklist yamls lack a trailing newline

# ghqc 0.3.3

- refactors `setup_ghqc()` such that each interactive input is (y/N)
Expand Down
19 changes: 16 additions & 3 deletions R/checklist_validation.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,27 @@ validate_checklists <- function(config_path = ghqc_config_path()) {
lapply(checklist_ls, function(x) val_checklist(x))
}

checklist_has_trailing_newline <- function(file) {
content <- readChar(file, file.info(file)$size, useBytes = TRUE)
if (nchar(content) == 0) return(FALSE) # empty file
return(substr(content, nchar(content), nchar(content)) == "\n")
}

#' @importFrom yaml yaml.load_file
val_checklist <- function(checklist) {
content <- tryCatch({
yaml::yaml.load_file(checklist)
}, error = function(e) {
yaml::yaml.load_file(checklist, readLines.warn = FALSE)
},
error = function(e) {
NULL
})

if (is.null(content)) return(list(valid = FALSE, reason = "File is not a valid yaml format"))
if (length(content) == 0) return(list(valid = FALSE, reason = "File is blank")) #CASE 1: emply file
if (length(content) == 0) return(list(valid = FALSE, reason = "File is blank")) #CASE 1: empty file
if (length(content) != 1) return(list(valid = FALSE, reason = "There are too many top level elements. The only top level should be the checklist title")) # CASE 2: Multiple top levels that create multiple lists when reading in the yaml
if (!inherits(content, "list")) return(list(valid = FALSE, reason = "Only strings found. Use ':' for headers and '-' for checklist items")) # CASE 3: only a single. No top levels or elements
if (any(sapply(content[[1]], function(x) class(x) != "character"))) return(list(valid = FALSE, reason = "There are too many sublevels. Checklist only supports a title and one section level"))
if (!checklist_has_trailing_newline(checklist)) return(list(valid = TRUE, reason = "File does not have a trailing newline")) # checklist is valid, but give a warning
list(valid = TRUE, reason = NA)
}

Expand All @@ -40,13 +48,18 @@ invalid_checklist_rename <- function(checklist, check_structure) {
#' @importFrom cli cli_alert_info
print_checklists <- function(config_path) {
res <- validate_checklists(config_path = config_path)

print_check <- as.data.frame(sapply(names(res), function(x) {
elem <- gsub("INVALID - ", "", basename(x))
bullet <- "v"
if (!res[[x]]$valid) {
elem <- c(elem, res[[x]]$reason)
bullet <- c("x", " ")
}
else if (res[[x]]$valid && !is.na(res[[x]]$reason)) {
elem <- c(elem, res[[x]]$reason)
bullet <- c("!", " ")
}
list(elem = elem, bullet = bullet)
}))
checks <- unlist(print_check["elem", ])
Expand Down

0 comments on commit 55cd8fd

Please sign in to comment.