From ece1e7dd1a5bdeb734e79782d215d0e949a0690e Mon Sep 17 00:00:00 2001 From: Florent Angly Date: Tue, 7 Mar 2017 12:49:18 +0100 Subject: [PATCH] Bugfix: Save cache in the requested directory (#213) --- R/cache.R | 2 +- R/expect_lint.R | 2 +- R/lint.R | 6 +++--- man/clear_cache.Rd | 3 +-- man/expect_lint.Rd | 3 +-- man/lint_file.Rd | 7 +++---- tests/testthat/test-cache.R | 12 ++++++++++++ 7 files changed, 22 insertions(+), 13 deletions(-) diff --git a/R/cache.R b/R/cache.R index 2a0c8c88d..327acf4a2 100644 --- a/R/cache.R +++ b/R/cache.R @@ -1,6 +1,6 @@ #' Clear the lintr cache #' -#' @param file filename whose cache to clear. If you pass \code{NULL} it will +#' @param file filename whose cache to clear. If you pass \code{NULL}, it will #' delete all of the caches. #' @param path directory to store caches. Reads option 'lintr.cache_directory' #' as the default. diff --git a/R/expect_lint.R b/R/expect_lint.R index ac9661220..086965598 100644 --- a/R/expect_lint.R +++ b/R/expect_lint.R @@ -9,7 +9,7 @@ #' \item named-vector check if the lint's field matches the named field. #' \item list-vectors check if the given lint matches (use if more than one lint is returned for the content) #' } -#' @param ... one or more linters to use for the check +#' @param ... arguments passed to \code{\link{lint}} including e.g. the linters or cache to use. #' @param file if not \code{NULL} read content from a file rather than from \code{content} expect_lint <- function(content, checks, ..., file = NULL) { diff --git a/R/lint.R b/R/lint.R index b03d9cf27..9eecbf2e7 100644 --- a/R/lint.R +++ b/R/lint.R @@ -15,8 +15,8 @@ NULL #' @param filename the given filename to lint. #' @param linters a list of linter functions to apply see \code{\link{linters}} #' for a full list of default and available linters. -#' @param cache toggle caching of lint results, if passed a character vector -#' uses that file as the cache. +#' @param cache given a logical, toggle caching of lint results. If passed a +#' character string, store the cache in this directory. #' @param ... additional arguments passed to \code{\link{exclude}}. #' @param parse_settings whether to try and parse the settings #' @export @@ -96,7 +96,7 @@ lint <- function(filename, linters = NULL, cache = FALSE, ..., parse_settings = if (isTRUE(cache)) { cache_file(lint_cache, filename, linters, lints) - save_cache(lint_cache, filename) + save_cache(lint_cache, filename, cache_dir) } res <- exclude(lints, ...) diff --git a/man/clear_cache.Rd b/man/clear_cache.Rd index f87f8237a..e164ee24c 100644 --- a/man/clear_cache.Rd +++ b/man/clear_cache.Rd @@ -7,7 +7,7 @@ clear_cache(file = NULL, path = NULL) } \arguments{ -\item{file}{filename whose cache to clear. If you pass \code{NULL} it will +\item{file}{filename whose cache to clear. If you pass \code{NULL}, it will delete all of the caches.} \item{path}{directory to store caches. Reads option 'lintr.cache_directory' @@ -16,4 +16,3 @@ as the default.} \description{ Clear the lintr cache } - diff --git a/man/expect_lint.Rd b/man/expect_lint.Rd index e4b96e26b..638ae4187 100644 --- a/man/expect_lint.Rd +++ b/man/expect_lint.Rd @@ -18,11 +18,10 @@ different checks depending on the value of checks. \item list-vectors check if the given lint matches (use if more than one lint is returned for the content) }} -\item{...}{one or more linters to use for the check} +\item{...}{arguments passed to \code{\link{lint}} including e.g. the linters or cache to use.} \item{file}{if not \code{NULL} read content from a file rather than from \code{content}} } \description{ Lint expectation } - diff --git a/man/lint_file.Rd b/man/lint_file.Rd index ab5203f98..a8ff6e491 100644 --- a/man/lint_file.Rd +++ b/man/lint_file.Rd @@ -1,8 +1,8 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/lint.R \name{lint_file} -\alias{lint} \alias{lint_file} +\alias{lint} \title{Lint a given file} \usage{ lint(filename, linters = NULL, cache = FALSE, ..., parse_settings = TRUE) @@ -13,8 +13,8 @@ lint(filename, linters = NULL, cache = FALSE, ..., parse_settings = TRUE) \item{linters}{a list of linter functions to apply see \code{\link{linters}} for a full list of default and available linters.} -\item{cache}{toggle caching of lint results, if passed a character vector -uses that file as the cache.} +\item{cache}{given a logical, toggle caching of lint results. If passed a +character string, store the cache in this directory.} \item{...}{additional arguments passed to \code{\link{exclude}}.} @@ -23,4 +23,3 @@ uses that file as the cache.} \description{ Apply one or more linters to a file and return a list of lints found. } - diff --git a/tests/testthat/test-cache.R b/tests/testthat/test-cache.R index f27767ff1..46d782df5 100644 --- a/tests/testthat/test-cache.R +++ b/tests/testthat/test-cache.R @@ -307,3 +307,15 @@ test_that("it returns the correct line if it is after the current line", { expect_equal(find_new_line(3, "test3", t1), 3) }) + + +test_that("lint() uses the provided cache directory", { + dir <- "temp_lintr_cache" + unlink(dir, recursive = TRUE) + expect_false(dir.exists(dir)) + expect_lint("a <- 1", NULL, assignment_linter, cache=dir) # create the cache + expect_true(dir.exists(dir)) + expect_lint("a <- 1", NULL, assignment_linter, cache=dir) # read the cache + expect_true(dir.exists(dir)) + unlink(dir, recursive = TRUE) +})