Skip to content

Commit bd63208

Browse files
committed
Remove the vdiffr dependency and use testthat snaphots directly
1 parent ec40dd8 commit bd63208

File tree

2 files changed

+51
-39
lines changed

2 files changed

+51
-39
lines changed

DESCRIPTION

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ Suggests:
6060
shinytest (>= 1.3.0),
6161
curl,
6262
rmarkdown,
63-
vdiffr,
6463
Cairo,
6564
broom,
6665
webshot,

tests/testthat/helper-vdiffr.R

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ enable_vdiffr <- grepl("true", Sys.getenv("VDIFFR"), fixed = TRUE)
33

44
message("Visual testing is ", if (!enable_vdiffr) "not ", "enabled.")
55

6-
# start up the image server and let vdiffr's svg writing method know about it
6+
# start up the orca image server
77
if (enable_vdiffr) {
8-
98
# try 20 random ports
109
for (vdiff_port_tries in 1:20) {
1110
port <- floor(runif(1, 3001, 8000))
@@ -18,52 +17,66 @@ if (enable_vdiffr) {
1817
})
1918
if (success) break
2019
}
21-
22-
# define logic for writing svg in vdiffr
23-
write_plotly_svg <- function(p, file, title) {
24-
# before exporting, specify trace[i].uid so resulting svg is deterministic
25-
# https://github.com/plotly/orca/issues/133
26-
p <- plotly_build(p)
27-
uid_data <- paste0("-vdiffr-plotly-", seq_along(p$x$data))
28-
p$x$data <- Map(function(tr, id) { tr$uid <- id; tr }, p$x$data, uid_data)
29-
30-
# write svg to disk
31-
# NOTE TO SELF: yes, it would be great to use `orca_serve()` here, but it gives
32-
# slightly different results from `orca()` (ordering of attributes are different)
33-
# and `orca_serve()` doesn't seem to run reliably everywhere
34-
owd <- setwd(dirname(file))
35-
on.exit(setwd(owd))
36-
# NOTE: the dimensions here should match the server args part of xvfb-run
37-
orcaServer$export(p, file = basename(file), width = 640, height = 480)
38-
39-
# strip out non-deterministic fullLayout.uid
40-
# TODO: if and when plotly provides an API to pre-specify, use it!
41-
svg_txt <- readLines(file, warn = FALSE)
42-
strextract <- function(str, pattern) regmatches(str, regexpr(pattern, str))
43-
def <- strextract(svg_txt, 'defs id=\\"defs-[[:alnum:]]+\\"')
44-
uid <- sub("defs-", "", strextract(def, "defs-[[:alnum:]]+"))
45-
svg_txt <- gsub(uid, "", svg_txt, fixed = TRUE)
46-
writeLines(svg_txt, file)
47-
}
48-
4920
}
5021

5122
expect_doppelganger <- function(p, name, ...) {
5223

53-
if (enable_vdiffr) {
54-
# some plots have random characteristics, so make sure we always have the same seed,
55-
# otherwise comparing svg produces false positives
56-
set.seed(555)
57-
if (ggplot2::is.ggplot(p)) p <- ggplotly(p)
58-
vdiffr::expect_doppelganger(name, p, ..., writer = write_plotly_svg)
59-
} else {
60-
invisible(NULL)
24+
if (!enable_vdiffr) {
25+
return(invisible(NULL))
6126
}
6227

28+
testthat::local_edition(3)
29+
30+
# some plots have random characteristics, so make sure we always have the same seed,
31+
# otherwise comparing svg produces false positives
32+
set.seed(555)
33+
34+
name <- str_standardise(name)
35+
file <- paste0(name, ".svg")
36+
path <- tempfile(file, fileext = ".svg")
37+
write_plotly_svg(p, path)
38+
testthat::expect_snapshot_file(path = path, name = file, cran = FALSE)
6339
}
6440

6541
# run visual test and return 'built' data/layout
6642
expect_doppelganger_built <- function(p, name, ...) {
6743
expect_doppelganger(p, name, ...)
6844
plotly_build(p)$x[c("data", "layout")]
6945
}
46+
47+
48+
# define logic for writing svg
49+
write_plotly_svg <- function(p, file) {
50+
# before exporting, specify trace[i].uid so resulting svg is deterministic
51+
# https://github.com/plotly/orca/issues/133
52+
p <- plotly_build(p)
53+
uid_data <- paste0("-vdiffr-plotly-", seq_along(p$x$data))
54+
p$x$data <- Map(function(tr, id) { tr$uid <- id; tr }, p$x$data, uid_data)
55+
56+
# write svg to disk
57+
# NOTE TO SELF: yes, it would be great to use `orca_serve()` here, but it gives
58+
# slightly different results from `orca()` (ordering of attributes are different)
59+
# and `orca_serve()` doesn't seem to run reliably everywhere
60+
owd <- setwd(dirname(file))
61+
on.exit(setwd(owd))
62+
# NOTE: the dimensions here should match the server args part of xvfb-run
63+
orcaServer$export(p, file = basename(file), width = 640, height = 480)
64+
65+
# strip out non-deterministic fullLayout.uid
66+
# TODO: if and when plotly provides an API to pre-specify, use it!
67+
svg_txt <- readLines(file, warn = FALSE)
68+
strextract <- function(str, pattern) regmatches(str, regexpr(pattern, str))
69+
def <- strextract(svg_txt, 'defs id=\\"defs-[[:alnum:]]+\\"')
70+
uid <- sub("defs-", "", strextract(def, "defs-[[:alnum:]]+"))
71+
svg_txt <- gsub(uid, "", svg_txt, fixed = TRUE)
72+
writeLines(svg_txt, file)
73+
}
74+
75+
# copied from vdiffr
76+
str_standardise <- function(s, sep = "-") {
77+
stopifnot(rlang::is_scalar_character(s))
78+
s <- gsub("[^a-z0-9]", sep, tolower(s))
79+
s <- gsub(paste0(sep, sep, "+"), sep, s)
80+
s <- gsub(paste0("^", sep, "|", sep, "$"), "", s)
81+
s
82+
}

0 commit comments

Comments
 (0)