Skip to content

Commit

Permalink
Added lintr support (static code analysis). Fixed lintr findings. Add…
Browse files Browse the repository at this point in the history
…ed lintr check to Travis-CI
  • Loading branch information
aryoda committed Oct 3, 2019
1 parent 8c41495 commit 538f3ea
Show file tree
Hide file tree
Showing 39 changed files with 226 additions and 217 deletions.
2 changes: 1 addition & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ R/dump\.frames\.dev\.R
^appveyor\.yml$
^README\.md$
^LICENSE_HEADER_TEMPLATE.txt$

^\.lintr$
15 changes: 15 additions & 0 deletions .lintr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
linters: with_defaults(
line_length_linter = NULL,
object_name_linter = NULL,
object_length_linter = object_length_linter(34),
commented_code_linter = NULL)
exclusions: list(
"demo/tryCatchLog_demo.R",
"vignettes/01_license.Rmd_child",
"vignettes/02_toc.Rmd_child",
"vignettes/05_overview.Rmd_child",
"vignettes/10_standard_R_conditions.Rmd_child",
"vignettes/20_tryCatchLog.Rmd_child",
"vignettes/90_appendix.Rmd_child",
"vignettes/tryCatchLog-intro.R",
"vignettes/tryCatchLog-intro.Rmd")
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ r:

r_packages:
- covr
- lintr



# This option forces all WARNINGS from R CMD check to become build failures (default true).
Expand All @@ -22,6 +24,11 @@ branches:
only:
- master

# Taken from: https://github.com/lockedata/HIBPwned/blob/master/.travis.yml
after_script:
- R CMD INSTALL $PKG_TARBALL
- Rscript -e 'lintr::lint_package()'

# https://github.com/codecov/example-r
after_success:
- Rscript -e 'library(covr); codecov()'
4 changes: 2 additions & 2 deletions R/build_log_output.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
#' @note The logged call stack details (compact, full or both) can be configured globally
#' using the options \code{tryCatchLog.include.full.call.stack}
#' and \code{tryCatchLog.include.compact.call.stack}.
#'
#'
#' The result of the package internal function \code{\link{build.log.entry}}
#' can be passed as \code{log.results} argument.
build.log.output <- function(log.results,
Expand All @@ -73,7 +73,7 @@ build.log.output <- function(log.results,

# append every row of the log result to the output
res <- ""
i = 1
i <- 1
while (i <= NROW(log.results)) {

res <- paste0(res,
Expand Down
6 changes: 2 additions & 4 deletions R/get_pretty_call_stack.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@
#' since tryCatch unwinds the call stack to the tryCatch position and the source of the condition cannot be identified anymore.
#' @seealso \code{\link{tryCatchLog}}, \code{\link{tryLog}}, \code{\link{limitedLabelsCompact}}
#' @export
get.pretty.call.stack <- function(call.stack, omit.last.items = 0, compact = FALSE)
{
get.pretty.call.stack <- function(call.stack, omit.last.items = 0, compact = FALSE) {
if (is.null(call.stack))
return("")

Expand All @@ -53,8 +52,7 @@ get.pretty.call.stack <- function(call.stack, omit.last.items = 0, compact = FAL
- omit.last.items)]

pretty.call.stack <- limitedLabelsCompact(call.stack, compact)
call.stack.formatted <- paste(" ", 1:length(pretty.call.stack), pretty.call.stack, collapse = "\n")
call.stack.formatted <- paste(" ", seq_along(pretty.call.stack), pretty.call.stack, collapse = "\n")

return(call.stack.formatted)
}

42 changes: 21 additions & 21 deletions R/get_pretty_tryCatchLog_options.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,35 @@
#' This is a convenience function whose result can be used e. g. to log the current settings.
#'
#' If an option is not set the string "(not set)" is shown as value.
#'
#'
#' The data type is also indicated if an option is set (since a wrong data type may cause problems).
#'
#'
#' @return The current option settings as string (one per line as key/value pair), e. g.
#' \preformatted{
#' Option tryCatchLog.write.error.dump.file = FALSE (logical)
#' Option tryCatchLog.write.error.folder = . (character)
#' Option tryCatchLog.silent.warnings = FALSE (logical)
#' Option tryCatchLog.silent.messages = (not set)
#' }
#'
#'
#' @examples
#' cat(get.pretty.tryCatchLog.options()) # "cat" does apply new line escape characters
#'
#' @export
#' @export
get.pretty.tryCatchLog.options <- function() {

option.names <- c(
"tryCatchLog.write.error.dump.file",
"tryCatchLog.write.error.folder",
"tryCatchLog.silent.warnings",
"tryCatchLog.silent.messages"
)

res <- paste(lapply(option.names, get.pretty.option.value), collapse = tryCatchLog::platform.NewLine())



return (res)


return(res)
}


Expand All @@ -41,15 +41,15 @@ get.pretty.tryCatchLog.options <- function() {
#'
#' The data type is also indicated if an option is set (since a wrong data type may cause problems).
#' If an option is not set "(not set)" is shown as value.
#'
#'
#' THIS IS AN INTERNAL PRIVATE FUNCTION OF THE PACKAGE.
#'
#' @param option.name Name of the option (as character)
#'
#' @param option.name Name of the option (as character)
#'
#' @return The option as key/value string in one line
#'
#' @seealso \code{\link{get.pretty.tryCatchLog.options}}
#'
#'
#' @examples
#' \dontrun{
#' tryCatchLog:::get.pretty.option.value("warn")
Expand All @@ -59,21 +59,21 @@ get.pretty.option.value <- function(option.name) {
# Check preconditions
stopifnot(is.character(option.name))
stopifnot(length(option.name) == 1)
option.value = getOption(option.name)



option.value <- getOption(option.name)

if (is.null(option.value)) {
option.value <- "(not set)"
option.type <- ""
} else {
option.type <- paste0("(", typeof(option.value), ")")
}

log.msg <- paste("Option", option.name, "=", option.value, option.type)



return (log.msg)


return(log.msg)
}
8 changes: 4 additions & 4 deletions R/is_package_available.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
#' @param package.name Name of the package (as string)
#'
#' @return \code{TRUE} if the packages is installed, otherwise \code{FALSE}
#'
#'
#' http://r-pkgs.had.co.nz/description.html
#'
#' @examples
#' tryCatchLog:::is.package.available("tryCatchLog") # must be TRUE :-)
#'
#'
is.package.available <- function(package.name) {

return(requireNamespace(package.name, quietly = TRUE))

}
3 changes: 1 addition & 2 deletions R/last_tryCatchLog_result.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ last.tryCatchLog.result <- function() {
#' @note THIS IS A PACKAGE INTERNAL FUNCTION AND THEREFORE NOT EXPORTED.
reset.last.tryCatchLog.result <- function() {

.tryCatchLog.env$last.log = data.frame()
.tryCatchLog.env$last.log <- data.frame()

invisible(TRUE)

Expand Down Expand Up @@ -111,4 +111,3 @@ append.to.last.tryCatchLog.result <- function(new.log.entry) {
return(.tryCatchLog.env$last.log)

}

16 changes: 7 additions & 9 deletions R/limited_Labels_Compact.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,13 @@
#' @examples
#' limitedLabelsCompact(sys.calls(), TRUE)
#' @export
limitedLabelsCompact <- function(value, compact = FALSE, maxwidth = getOption("width") - 5L)
{
limitedLabelsCompact <- function(value, compact = FALSE, maxwidth = getOption("width") - 5L) {

# create vector of source references (file and row numbers) for each call item of the stack
srcrefs <- sapply(value, function(v)
{
srcref <- attr(v,"srcref")
if (!is.null(srcref))
{
srcrefs <- sapply(value, function(v) {

srcref <- attr(v, "srcref")
if (!is.null(srcref)) {
srcfile <- attr(srcref, "srcfile")
paste0(basename(srcfile$filename), "#", srcref[1L], ": ")
}
Expand All @@ -79,8 +78,7 @@ limitedLabelsCompact <- function(value, compact = FALSE, maxwidth = getOption("w
maxwidth <- min(maxwidth, 1000L)
value <- strtrim(value, maxwidth)

if (compact == TRUE)
{
if (compact == TRUE) {
# return only call stack items that contain a source reference
srcrefs.available <- srcrefs != ""
srcrefs.available[1] <- TRUE # always return the first row!
Expand Down
14 changes: 7 additions & 7 deletions R/log2console.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
#'
#' @examples
#' tryCatchLog:::log2console("WARN", "this is my last warning")
#'
#'
log2console <- function(severity.level, msg) {

if (is.na(msg))
msg = ""
msg <- ""



stopifnot(!is.null(severity.level))
stopifnot(severity.level %in% c("ERROR", "WARN", "INFO"))
stopifnot(is.character(msg))
Expand All @@ -28,11 +28,11 @@ log2console <- function(severity.level, msg) {
# (not UTC which would allow to combine different log output with
# different time zones more easily).
log.time <- format(Sys.time(), "%Y-%m-%d %H:%M:%S")

res <- sprintf("%s [%s] %s\n", severity.level, log.time, msg, "\n")

cat(res)


invisible(res)
}
2 changes: 1 addition & 1 deletion R/platform_newline.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ get.sys.name <- function() {
# Just a wrapper function to support mocking of the base function in testthat unit tests via "with_mock"
# (see ?with_mock)
get.platform.OS.type <- function() {
return(.Platform$OS.type)
return(.Platform$OS.type)
}


Expand Down
24 changes: 12 additions & 12 deletions R/set_logging_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@


#' Sets the logging functions that shall be used by \code{tryCatchLog} for the different severity levels
#'
#'
#' The logging functions must have at least one parameter: The logging message (as character)
#' which must be the first argument.
#'
#'
#' The default logging functions are internal functions without any dependencies to other
#' logging packages. They use the same logging output format as \pkg{futile.logger} version 1.4.3.
#'
#'
#' If you want to disable any logging output you should use a decent logging framework
#' which allows to set the logging threshold (e. g. futile.logger's \code{\link[futile.logger]{flog.threshold}}).
#'
#'
#' The package-internal default logging functions are only a minimal implementation
#' and are not meant to replace a decent logging framework.
#'
Expand All @@ -37,31 +37,31 @@
#'
#' @return Nothing
#'
#' @seealso \code{\link{tryCatchLog}}
#'
#' @seealso \code{\link{tryCatchLog}}
#'
#' @export
#'
#' @examples
#' # To disable any logging you could use "empty" functions
#' set.logging.functions( error.log.func = function(msg) invisible(),
#' warn.log.func = function(msg) invisible(),
#' info.log.func = function(msg) invisible())
#'
#'
set.logging.functions <- function(error.log.func = function(msg) tryCatchLog:::log2console("ERROR", msg)
, warn.log.func = function(msg) tryCatchLog:::log2console("WARN", msg)
, info.log.func = function(msg) tryCatchLog:::log2console("INFO", msg)
) {

stopifnot(is.function(error.log.func))
stopifnot(is.function(warn.log.func))
stopifnot(is.function(info.log.func))



# remember the active logging functions in the package-internal environment
.tryCatchLog.env$error.log.func <- error.log.func
.tryCatchLog.env$warn.log.func <- warn.log.func
.tryCatchLog.env$info.log.func <- info.log.func

invisible()
}
Loading

0 comments on commit 538f3ea

Please sign in to comment.