Skip to content

Commit

Permalink
complete roxygen for is_treesitter_installed() and a manual test file
Browse files Browse the repository at this point in the history
  • Loading branch information
radbasa committed Aug 1, 2024
1 parent f8bcf3f commit 716247d
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
2 changes: 1 addition & 1 deletion R/namespaced_function_calls.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#'
#' @export
namespaced_function_calls <- function(allow = NULL) {
if (is_treesitter_installed()) {
if (!is_treesitter_installed()) {
cli::cli_alert_warning(
paste(
"The packages {{treesitter}} and {{treesitter.r}} are required by",
Expand Down
19 changes: 17 additions & 2 deletions R/style_box_use.R
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ rebuild_source_file <- function(source_file_lines, retain_lines, transformed_box

#' @keywords internal
check_treesitter_installed <- function() {
if (is_treesitter_installed()) {
if (!is_treesitter_installed()) {
cli::cli_abort(
paste(
"The packages {{treesitter}} and {{treesitter.r}} are required for styling.",
Expand All @@ -540,8 +540,23 @@ check_treesitter_installed <- function() {
#'
#' Treesitter required R >= 4.3.0. Treesitter is required by a few `{box.linters}` functions.
#'
#'
#' @examples
#' \dontrun{
#'
#' # Bare environment
#'
#' is_treesitter_installed()
#' #> [1] FALSE
#'
#' install.packages(c("treesitter", "treesitter.r"))
#' is_treesitter_installed()
#' #> [1] TRUE
#' }
#'
#' @return Logical TRUE/FALSE if the `treesitter` dependencies exist.
#' @export
is_treesitter_installed <- function() {
treesitter_pkgs <- c("treesitter", "treesitter.r")
length(find.package(treesitter_pkgs, quiet = TRUE)) < length(treesitter_pkgs)
length(find.package(treesitter_pkgs, quiet = TRUE)) >= length(treesitter_pkgs)
}
17 changes: 17 additions & 0 deletions man/is_treesitter_installed.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions tests/e2e/test-is_treesitter_installed.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# How to test is_treesitter_installed()
#
# `withr` provides a function to temporarily install packages `withr::with_package()` in an
# environment. There is, however, no currently existing way to temporarily uninstalled packages
# to test environments without certain packages.
#
# To test `is_treesitter_installed()`, a manual test has to be executed.

devtools::load_all()

remove.packages(c("treesitter", "treesitter.r"))

testthat::test_that("is_treesitter_installed() returns FALSE when none is installed", {
result <- is_treesitter_installed()

testthat::expect_false(result)
})

testthat::test_that("is_treesitter_installed() returns FALSE when only treesitter is installed", {
install.packages("treesitter")

result <- is_treesitter_installed()

testthat::expect_false(result)
remove.packages("treesitter")
})

testthat::test_that("is_treesitter_installed() returns FALSE when only treesitter.r is installed", {
install.packages("treesitter.r")

result <- is_treesitter_installed()

testthat::expect_false(result)
remove.packages("treesitter.r")
})

install.packages(c("treesitter", "treesitter.r"))

testthat::test_that("is_treesitter_installed() returns TRUE", {
result <- is_treesitter_installed()

testthat::expect_true(result)
})

0 comments on commit 716247d

Please sign in to comment.