-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don’t fail if ‘rstudioapi’ is not installed.
Fall back to 'tools:rstudio' inside RStudio where appropriate and possible. Generate a warning otherwise. Fixes #293.
- Loading branch information
Showing
3 changed files
with
105 additions
and
8 deletions.
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
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,70 @@ | ||
context('RStudio') | ||
|
||
with_mock_rstudio = function (expr) { | ||
Sys.setenv(RSTUDIO = "1") | ||
|
||
old_gui = .Platform$GUI | ||
unlockBinding('.Platform', .BaseNamespaceEnv) | ||
.BaseNamespaceEnv$.Platform$GUI = 'RStudio' | ||
Sys.unsetenv('TESTTHAT') | ||
|
||
on.exit({ | ||
Sys.unsetenv("RSTUDIO") | ||
.BaseNamespaceEnv$.Platform$GUI = old_gui | ||
lockBinding('.Platform', .BaseNamespaceEnv) | ||
Sys.setenv(TESTTHAT = 'true') | ||
}) | ||
|
||
expr | ||
} | ||
|
||
with_mock_rstudio_tools_path = function (path, expr) { | ||
rstudio_tools_env = new.env() | ||
local(envir = rstudio_tools_env, { | ||
.rs.api.getActiveDocumentContext = function () { # nolint | ||
list( | ||
path = path, | ||
contents = '', | ||
selection = list(list(range = rep(1L, 4L), text = '')) | ||
) | ||
} | ||
.rs.api.versionInfo = function () list() | ||
}) | ||
on.exit(detach('tools:rstudio')) | ||
attach(rstudio_tools_env, name = 'tools:rstudio') | ||
|
||
expr | ||
} | ||
|
||
test_that('Path of active file in RStudio is found without ‘rstudioapi’', { | ||
skip_on_ci() | ||
skip_on_cran() | ||
# Assume that we cannot edit package library path on other systems. | ||
skip_outside_source_repos() | ||
|
||
pkg_path = dirname(dirname(attr(suppressWarnings(packageDescription('rstudioapi')), 'file'))) | ||
tmp_path = paste0(pkg_path, '-x') | ||
|
||
unloadNamespace('rstudioapi') | ||
expect_true(file.rename(pkg_path, tmp_path)) | ||
on.exit(file.rename(tmp_path, pkg_path)) | ||
|
||
file_path = with_mock_rstudio({ | ||
with_mock_rstudio_tools_path('/rstudio/test.r', box::file()) | ||
}) | ||
|
||
expect_paths_equal(file_path, '/rstudio') | ||
expect_false(isNamespaceLoaded('rstudioapi')) | ||
}) | ||
|
||
test_that('Path of active file in RStudio is found with ‘rstudioapi’', { | ||
skip_on_cran() | ||
skip_if(is.na(packageDescription('rstudioapi'))) | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
file_path = with_mock_rstudio({ | ||
with_mock_rstudio_tools_path('/rstudio/test.r', box::file()) | ||
}) | ||
|
||
expect_paths_equal(file_path, '/rstudio') | ||
expect_true(isNamespaceLoaded('rstudioapi')) | ||
}) |
Wrap in
tryCatch(…)
: currently the code accounts for changes to the environment name, but not to the internal API inside that environment.