diff --git a/.dev/run-test-files-in-random-order.R b/.dev/run-test-files-in-random-order.R new file mode 100644 index 000000000..7c769bb05 --- /dev/null +++ b/.dev/run-test-files-in-random-order.R @@ -0,0 +1,54 @@ +library(cli) +library(glue) +withr::local_envvar(TESTTHAT_PARALLEL = "FALSE") +pkgload::load_all(".") + +test_script_paths <- testthat::find_test_scripts("tests/testthat") +seed <- sample.int(1e6, 1L) +cli_inform("Chosen seed for the current test run: {seed}") +set.seed(seed) +randomized_test_script_paths <- sample(test_script_paths) + +any_test_failures <- FALSE +any_test_errors <- FALSE + +test_path <- function(path) { + report <- as.data.frame(testthat::test_file(path, reporter = "silent")) + has_test_failures <- any(report$failed == 1L) + has_test_errors <- any(report$error == 1L) + if (has_test_failures) { + cli_alert_danger(glue("Tests in `{path}` are failing.")) + any_test_failures <<- TRUE + failed_tests <- tibble::as_tibble(subset(report, failed == 1L)[, c("test", "result")]) + print(glue::glue_data(failed_tests, "Test `{test}` is failing:\n{purrr::pluck(result, 1L, 1L)}")) + } + if (has_test_errors) { + cli_alert_danger(glue("There was error while running tests in `{path}`.")) + any_test_errors <<- TRUE + errored_tests <- tibble::as_tibble(subset(report, error == 1L)[, c("test", "result")]) + print(glue::glue_data(errored_tests, "Test `{test}` has error:\n{purrr::pluck(result, 1L, 1L)}")) + } + if (!has_test_failures && !has_test_errors) { + cli_alert_success(glue("All tests passing in `{path}`.")) + } +} + +cli_rule() +cli_inform("Running tests in random order:") +cli_rule() + +purrr::walk(randomized_test_script_paths, test_path) + +cli_rule() +if (any_test_failures) { + cli_abort("Tests in some files are failing.") +} + +if (any_test_errors) { + cli_abort("There was error while running tests in some files.") +} + +if (!any_test_failures && !any_test_errors) { + cli_alert_success("Tests from all files are passing!") +} +cli_rule() diff --git a/.github/workflows/check-random-test-order.yaml b/.github/workflows/check-random-test-order.yaml new file mode 100644 index 000000000..77dea2b53 --- /dev/null +++ b/.github/workflows/check-random-test-order.yaml @@ -0,0 +1,35 @@ +# Run tests in random order +on: + push: + branches: [main, master] + pull_request: + branches: [main, master] + +name: check-random-test-order + +jobs: + check-random-test-order: + runs-on: ubuntu-latest + env: + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + + steps: + - uses: actions/checkout@v3 + + - uses: r-lib/actions/setup-r@v2 + with: + r-version: "release" + use-public-rspm: true + + - uses: r-lib/actions/setup-r-dependencies@v2 + with: + pak-version: devel + extra-packages: | + local::. + + - name: Run Tests in Random Order + run: | + options(crayon.enabled = TRUE) + callr::rscript(".dev/run-test-files-in-random-order.R") + + shell: Rscript {0}