Where would you put a config.yml file when building a package?

Hi,

I am building a package and have the database info stored in the config.yml for the tests using testthat.

When the file is in the top level, devtools::tests() runs, but devtools::check()`fails:

checking top-level files ... NOTE
Non-standard file/directory found at top level:
β€˜config.yml’

If moved to the inst\extdata folder, (like in here) then everything fails.

It is an internal package, but I don't want to share the config.yml file in the package.

It is probably something like this, but with some adjustments to suit:

If the file is only used for the tests, why not put it under tests/testthat and access it via testthat::test_path()?

1 Like

Thanks @maelle. I will give that a go when I log back in to the work computer.

It is currently used in the tests, but also in a file where I am playing with the output by using devtools::load_all(). I think my current solution involves putting the config.yml file in both the top folder and in the inst folder, then hiding one or both in the .gitignore and .Rbuildignore files. This seems to work, but isn't ideal.

I'm not sure how this all works. I have tried this, but still have the same issues.

test_that("test checks", {
  
  # connect this way in the tests
  creds <- config::get("postgres", file = testthat::test_path("config.yml"))
  CON <- DBI::dbConnect(RPostgres::Postgres(),
                        host     = creds$host,
                        dbname   = creds$dbname,
                        user     = creds$user,
                        password = creds$password,
                        port     = creds$port)
  
  query <- # the function query that connects to a db
  expect_s3_class(query, "data.frame")

})

It would be good if there was an example for ?test_path. I found your blog post, which sort of explains some of it though, but still have the issue described earlier.

If you need the file to be available through load_all(), you could define a function like below in the file tests/testthat/helper-config.R (helper files are described in the same blog post :wink: )

example_config <- function() {
  testthat::test_path("config.yml")
}

with tests/testthat/config.yml. Would it help? (pun unintended!)

Thanks Maelle. I am still having issues though. It works fine when debugging with load_all() and works with test() but not with check().

What I have got:

  • example_config function in tests/testthat/helper-config.R - I have used yours
  • config.yml in tests/testthat/config.yml
  • no inst folder
  • no top level config.yml
  • none of the files in the tests folder are being ignored by .Rbuildignore

This is an example error:

══ Failed tests ════════════════════════════════════════════════════════════════
── Error (test_age_year_agg.R:5:3): age_year_agg checks ────────────────────────
Error: Config file config.yml not found in current working directory or parent directories
Backtrace:
β–ˆ

  1. └─config::get("postgres", file = example_config()) test_age_year_agg.R:5:2

And this is an example of the test:

test_that("test checks", {
  
 # # helper function for connection from 'tests/testthat/helper-config.R'
  creds <- config::get("postgres", file = example_config())
  CON <- DBI::dbConnect(RPostgres::Postgres(),
                        host     = creds$host,
                        dbname   = creds$dbname,
                        user     = creds$user,
                        password = creds$password,
                        port     = creds$port)
  
  query <- # the function query that connects to a db
  expect_s3_class(query, "data.frame")

})

Do you know what might be wrong and what the solution to this might be?

Could you check your .Rbuildignore does not remove the tests/testthat/config.yml file from the built package? That'd be my first guess. :thinking:

That shouldn't be it. The config.yml file isn't ignored. This is the contents of .Rbuildignore:

^.*\.Rproj$
^\.Rproj\.user$
^LICENSE\.md$
^data-raw$
^doc$
^Meta$
^tests/checks_not_run$

EDIT: Actually, that might be it. Checking again.

The config.yml file must have been removed somewhere along the line. Thanks for the help @maelle.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.