This took me all evening to figure out. OK, here goes. I think it’s right. My package build is failing for different reasons now.
-
Create your OAuth token file
I’m using Oliver Keyes’s driver package to grab documents from Google drive in my rchie package. So I use his
driver_connect()function to create an OAuth token with and save it to disk in mytests/testthat/directory.library(driver) d_token = driver_connect(drive.app, drive.secret) saveRDS(d_token, "tests/testthat/token_file.rds")Note that
drive.appanddrive.secretare keys I’ve created by creating an app with the Drive API.You can do the same with
httr::oauth2.0_token()to create your token for other APIs. -
Write a test
In
tests/testthat/, I also have a file calledtest_google_doc.Rwhich contains this test:context('Online Services') test_that("google doc imported correctly", { d_token = readRDS("token_file.rds") library(driver, quietly=TRUE) archie_test_id = '16WHsVRyCM6dHVHTvFYsTbNaIl1vavGPp8GU3OnUS7oE' meta_d = file_metadata(d_token, archie_test_id) tmp = tempfile() download_file(d_token, meta_d, 'text/plain', tmp) expect_identical( from_archie(tmp), from_archie("ArchieMLParserTestFromGoogle.txt") ) })The important thing here is that the test reads in the token file, then uses it to access a google doc via
file_metadata()anddownload_file(). Use your token for whatever purposes you wantin your package. This test should work on your local machine withdevtools::test()or the “Test” button in RStudio. -
Encode your token file
First, you need the travis CLI. Get it with
gem install travisYou need to be logged in to your Travis account. Do this with
travis loginNote you’ll use you GitHub password for this. There are other login options, which are described when you run this command.
Now encrypt your token:
travis encrypt-file tests/testthat/token_file.rds --add--addwill automatically add a decrypt command to your .travis.yml file. -
Ignore and commit.
Put
tests/testthat/token_file.rdsin your .gitignorePut
tests/testthat/token_file.rds.encin your .RbuildignoreDo not mix these up. You don’t want your token file on GitHub, and you don’t want the encoded version being distributed into everyone’s libraries. If you put
token_file.rdsin your.Rbuildignore, it will not be copied over into thepackage.rcheckdirectory when Travis runsR CMD CHECK, and your tests will fail. Over and over. As you bang your desk trying to figure out what’s wrong.Commit
tests/testthat/token_file.rds.encand your updated.travis.ymland push to GitHub. The tests should run on Travis!
Now, can someone tell me how to write the test so it runs on Travis-CI, but not on CRAN?