Skip to content
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

Let users run test with export_all=FALSE #1201

Closed
pitakakariki opened this issue Jun 3, 2016 · 4 comments
Closed

Let users run test with export_all=FALSE #1201

pitakakariki opened this issue Jun 3, 2016 · 4 comments

Comments

@pitakakariki
Copy link

At the moment test quietly calls load_all with the default export_all=TRUE. This means:

  • tests for S3 dispatch will pass when they shouldn't (e.g. print.foo will work even if it's not exported).
  • internal package functions will be left in the search path after test has run.
@hadley
Copy link
Member

hadley commented Jun 3, 2016

But if export_all = FALSE you can't test internal functions, which is extremely important. I don't think this should be an option.

@hadley hadley closed this as completed Jun 3, 2016
jimhester added a commit to jimhester/devtools that referenced this issue Jun 6, 2016
jimhester added a commit to jimhester/devtools that referenced this issue Jun 6, 2016
@jimhester
Copy link
Member

Actually devtools runs tests in an environment with the package under tests namespace as a parent. load_all() is used just to generate this environment.

So we can set export_all = FALSE unconditionally and the internal functions are still able to be tested without dirting the users search path.

#1210 does this.

@zdk123
Copy link

zdk123 commented Apr 13, 2018

I found this issue when searching for a work around that wouldn't require changing the devtools. I think this is a generic way to get around the export_all=TRUE, with apologies to Hadley for breaking the intended test setup.

in a test.R file:

context('...')
#  any setup stuff here
test_that('...', {
  ## set the parent of current environment to the global environment ##
  e <- environment()
  pe <- parent.env(e)
  parent.env(e) <- globalenv()
  pkgpath <- find.package('pkgname')
  devtools::load_all(pkgpath, export_all=FALSE, quiet=TRUE)

  ## Run all tests with nonexported functions ##

  
  ## Reset environment for subsequent tests ##
  parent.env(e) <- pe
  devtools::load_all(pkgpath, export_all=TRUE, quiet=TRUE)
})

Since devtools::test sets the package namespace as the parent of the testing environment, it wasn't enough for my purposes to simply reload the package with export_all=FALSE, though you need to do that as well. Calls like match.fun('internal.function') could still find internal.function in the parent environment.

However, it is possible to manipulate the environment stack from a calling environment. Above, I've set the parent to the global environment, but one could as easily set it to a new/empty one.

Just make sure to reset / reload or you might break other tests.

@lock
Copy link

lock bot commented Oct 10, 2018

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Oct 10, 2018
jimhester added a commit that referenced this issue Oct 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants