Skip to content

Commit

Permalink
test: make non-interactive tests fail on mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
adamblake committed Oct 27, 2023
1 parent 74effc0 commit 514ff4e
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 17 deletions.
17 changes: 12 additions & 5 deletions bin/load-non-interactive-quickstart.r
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
#!/usr/bin/env Rscript

options(
coursekata.quiet = FALSE,
coursekata.quickstart = TRUE
)
Sys.setenv(`_R_S3_METHOD_REGISTRATION_NOTE_OVERWRITES_` = "false")
options(coursekata.quiet = FALSE, coursekata.quickstart = TRUE, cli.width = 80)

# ensure this package is uninstalled
suppressMessages(try(remove.packages("fivethirtyeightdata"), silent = TRUE))

# function to compare output to a snapshot
test_output_snapshot <- function(expr, snapshot) {
output <- capture.output(expr, type = "message")
output <- trimws(output, "right")
output <- paste(output, collapse = "\n")
comp <- waldo::compare(output, snapshot)
if (length(comp) > 0) stop(paste0(comp, collapse = "\n\n"), call. = FALSE)
}

# loading should not trigger the install prompt or show any messages
library(coursekata)
test_output_snapshot(library(coursekata), "")
24 changes: 18 additions & 6 deletions bin/load-non-interactive-quiet.r
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
#!/usr/bin/env Rscript

options(
coursekata.quiet = TRUE,
coursekata.quickstart = FALSE
)
Sys.setenv(`_R_S3_METHOD_REGISTRATION_NOTE_OVERWRITES_` = "false")
options(coursekata.quiet = TRUE, coursekata.quickstart = FALSE, cli.width = 80)

# ensure this package is uninstalled
suppressMessages(try(remove.packages("fivethirtyeightdata"), silent = TRUE))

# loading should trigger the install prompt if interactive, and not if not
# function to compare output to a snapshot
test_output_snapshot <- function(expr, snapshot) {
output <- capture.output(expr, type = "message")
output <- trimws(output, "right")
output <- paste(output, collapse = "\n")
comp <- waldo::compare(output, snapshot)
if (length(comp) > 0) stop(paste0(comp, collapse = "\n\n"), call. = FALSE)
}

# individual package start messages should not be printed (only the CourseKata message)
library(coursekata)
test_output_snapshot(library(coursekata), trimws("
── CourseKata packages ──────────────────────────────────── coursekata 0.14.1 ──
✔ dslabs 0.7.6 ✔ Metrics 0.1.4
✔ Lock5withR 1.2.2 ✔ lsr 0.5.2
x fivethirtyeightdata ✔ mosaic 1.8.4.2
✔ fivethirtyeight 0.6.2 ✔ supernova 2.5.7
"))
63 changes: 57 additions & 6 deletions bin/load-non-interactive.r
Original file line number Diff line number Diff line change
@@ -1,12 +1,63 @@
#!/usr/bin/env Rscript

options(
coursekata.quiet = FALSE,
coursekata.quickstart = FALSE
)
Sys.setenv(`_R_S3_METHOD_REGISTRATION_NOTE_OVERWRITES_` = "false")
options(coursekata.quiet = FALSE, coursekata.quickstart = FALSE, cli.width = 80)

# ensure this package is uninstalled
suppressMessages(try(remove.packages("fivethirtyeightdata"), silent = TRUE))

# loading should trigger the install prompt if interactive, and not if not
library(coursekata)
# function to compare output to a snapshot
test_output_snapshot <- function(expr, snapshot) {
output <- capture.output(expr, type = "message")
output <- trimws(output, "right")
output <- paste(output, collapse = "\n")
comp <- waldo::compare(output, snapshot)
if (length(comp) > 0) stop(paste0(comp, collapse = "\n\n"), call. = FALSE)
}

test_output_snapshot(library(coursekata), trimws("
Loading required package: dslabs
Loading required package: Lock5withR
Loading required package: fivethirtyeight
Some larger datasets need to be installed separately, like senators and
house_district_forecast. To install these, we recommend you install the
fivethirtyeightdata package by running:
install.packages('fivethirtyeightdata', repos =
'https://fivethirtyeightdata.github.io/drat/', type = 'source')
Loading required package: Metrics
Loading required package: lsr
Loading required package: mosaic
The 'mosaic' package masks several functions from core packages in order to add
additional features. The original behavior of these functions should not be affected by this.
Attaching package: ‘mosaic’
The following objects are masked from ‘package:dplyr’:
count, do, tally
The following object is masked from ‘package:Matrix’:
mean
The following object is masked from ‘package:ggplot2’:
stat
The following objects are masked from ‘package:stats’:
binom.test, cor, cor.test, cov, fivenum, IQR, median, prop.test,
quantile, sd, t.test, var
The following objects are masked from ‘package:base’:
max, mean, min, prod, range, sample, sum
Loading required package: supernova
── CourseKata packages ──────────────────────────────────── coursekata 0.14.1 ──
✔ dslabs 0.7.6 ✔ Metrics 0.1.4
✔ Lock5withR 1.2.2 ✔ lsr 0.5.2
x fivethirtyeightdata ✔ mosaic 1.8.4.2
✔ fivethirtyeight 0.6.2 ✔ supernova 2.5.7
"))

0 comments on commit 514ff4e

Please sign in to comment.