-
Notifications
You must be signed in to change notification settings - Fork 27
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
Support loading check.env #160
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7621c05
Support loading check.env
gaborcsardi 3a38158
inst/check.env -> tools/check.env
gaborcsardi 2b68d5c
Allow ignoring useless NOTES with Config/rcmdcheck/
gaborcsardi 2a1e1ab
Handle encodings better:
gaborcsardi f4a0abe
Do not consider NOT_CRAN for check.env loading
gaborcsardi 38a748b
Document turning off checks
gaborcsardi 898259e
Document RCMDCHECK_LOAD_CHECK_ENV env var
gaborcsardi da337fd
Set error_on from env var by default
gaborcsardi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
|
||
set_env <- function(path, targz, desc, envir = parent.frame()) { | ||
pkg <- desc$get("Package") | ||
ign <- as_flag(desc$get("Config/rcmdcheck/ignore-inconsequential-notes")) | ||
if (ign) ignore_env(envir = envir) | ||
load_env(path, targz, pkg, envir = envir) | ||
} | ||
|
||
ignore_env_config <- function() { | ||
data_literal( | ||
"docs" | "envvar" | "value", | ||
# --------------------------------------------------------------------- | ||
"report large package sizes" | ||
| "_R_CHECK_PKG_SIZES_" | FALSE, | ||
"check cross-references in Rd files" | ||
| "_R_CHECK_RD_XREFS_" | FALSE, | ||
"NOTE if package requires GNU make" | ||
| "_R_CHECK_CRAN_INCOMING_NOTE_GNU_MAKE_" | FALSE, | ||
"report marked non-ASCII strings in datasets" | ||
| "_R_CHECK_PACKAGE_DATASETS_SUPPRESS_NOTES_" | TRUE | ||
) | ||
} | ||
|
||
format_env_docs <- function() { | ||
envs <- ignore_env_config() | ||
paste0( | ||
"* ", envs$docs, " (`", envs$envvar, " = ", envs$value, "`)", | ||
collapse = ",\n" | ||
) | ||
} | ||
|
||
ignore_env <- function(to_ignore = NULL, envir = parent.frame()) { | ||
if (is.null(to_ignore)) { | ||
conf <- ignore_env_config() | ||
to_ignore <- structure(conf$value, names = conf$envvar) | ||
} | ||
withr::local_envvar(to_ignore, .local_envir = envir) | ||
} | ||
|
||
load_env <- function(path, targz, package, envir = parent.frame()) { | ||
should_load <- as_flag(Sys.getenv("RCMDCHECK_LOAD_CHECK_ENV"), TRUE) | ||
if (!should_load) return() | ||
|
||
env <- NULL | ||
if (file.info(path)$isdir) { | ||
env_path <- file.path(path, "tools", "check.env") | ||
} else { | ||
dir.create(tmp <- tempfile()) | ||
on.exit(unlink(tmp, recursive = TRUE), add = TRUE) | ||
utils::untar( | ||
targz, | ||
file.path(package, "tools", "check.env"), | ||
exdir = tmp, tar = "internal" | ||
) | ||
env_path <- file.path(tmp, package, "tools", "check.env") | ||
} | ||
|
||
if (file.exists(env_path)) { | ||
load_env_file(env_path, envir = envir) | ||
} | ||
} | ||
|
||
load_env_file <- function(path, envir = parent.frame()) { | ||
env <- readLines(path, warn = FALSE) | ||
env <- ignore_comments(env) | ||
env <- ignore_empty_lines(env) | ||
if (length(env) == 0) return(invisible()) | ||
|
||
env <- lapply(env, parse_dot_line) | ||
envc <- structure( | ||
vapply(env, "[[", character(1), "value"), | ||
names = vapply(env, "[[", character(1), "key") | ||
) | ||
withr::local_envvar(envc, .local_envir = envir) | ||
} | ||
|
||
ignore_comments <- function(lines) { | ||
grep("^#", lines, invert = TRUE, value = TRUE) | ||
} | ||
|
||
ignore_empty_lines <- function(lines) { | ||
grep("^\\s*$", lines, invert = TRUE, value = TRUE) | ||
} | ||
|
||
line_regex <- paste0( | ||
"^\\s*", # leading whitespace | ||
"(?<export>export\\s+)?", # export, if given | ||
"(?<key>[^=]+)", # variable name | ||
"=", # equals sign | ||
"(?<q>['\"]?)", # quote if present | ||
"(?<value>.*)", # value | ||
"\\g{q}", # the same quote again | ||
"\\s*", # trailing whitespace | ||
"$" # end of line | ||
) | ||
|
||
parse_dot_line <- function(line) { | ||
match <- regexpr(line_regex, line, perl = TRUE) | ||
if (match == -1) { | ||
stop("Cannot parse check.env: ", substr(line, 1, 40), call. = FALSE) | ||
} | ||
as.list(extract_match(line, match)[c("key", "value")]) | ||
} | ||
|
||
extract_match <- function(line, match) { | ||
tmp <- mapply( | ||
attr(match, "capture.start"), | ||
attr(match, "capture.length"), | ||
FUN = function(start, length) { | ||
tmp <- substr(line, start, start + length - 1) | ||
} | ||
) | ||
names(tmp) <- attr(match, "capture.names") | ||
tmp | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this use
readRenviron()
instead of using our own implementation?I guess the downside would be restoring the old envvars afterwards. But you could do
Sys.getenv()
to save them beforehand I guess.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really like its way of handling errors, it just ignores them, and especially on a CI, it is better to know about them.
So it was easier to copy this code from another package of mine.
But I don't really mind, we can switch to
readRenviron()
as well.