1

I was running some tests on my R package. I noticed that the order of files output by list.files in the console and inside R CMD check was different. I suspect that this is because of the locale dependency of the sort function discussed here:

R CMD check fails, devtools::test() works fine

My question is as follows: how does one eliminate the locale dependency in list.files and make it produce consistent output for both interactive sessions and R CMD check runs?

Reproducible example

Inside the testthat folder, create the abc folder with the following empty files:

  1. c-123.txt

  2. c-456.txt

  3. T.db

Inside the interactive session, list.files returns:

[1] "c-123.txt" "c-456.txt" "T.db"   

Yet the following test fails when run using R CMD check because T.db comes first:

context("ls")

test_that("correct order", {
  expect_true(list.files("abc")[1] == "c-123.txt")
})

This test is passed when using devtools::test_file('tests/testthat/test-ls.R') in the console.

slava-kohut
  • 4,203
  • 1
  • 7
  • 24
  • other option would be to log it as a bug here: https://github.com/r-lib/testthat – Bulat Aug 14 '19 at 19:18
  • @MrFlick included an example. – slava-kohut Aug 14 '19 at 19:44
  • Does order really matter? Does your package only work in certain locales? What exactly are you testing? What locale are you using in R and what locale does your system use? – MrFlick Aug 14 '19 at 20:34
  • The idea behind the test was to do some manipulations on one of text files in some folder. This file was fetched by list.files[1]. While I can for sure rewrite the test, it was interesting to understand what was going on with list.files. The locale business has nothing to do with my package. – slava-kohut Aug 15 '19 at 13:18

0 Answers0