Skip to content

Friendlier error when supplying extension-less files #5202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions R/save.r
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ plot_dev <- function(device, filename = NULL, dpi = 300, call = caller_env()) {

if (is.null(device)) {
device <- to_lower_ascii(tools::file_ext(filename))
if (identical(device, "")) {
cli::cli_abort("{.arg filename} has no file extension and {.arg device} is {.val NULL}.", call = call)
}
}

if (!is.character(device) || length(device) != 1) {
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-ggsave.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ test_that("ggsave warns about empty or multiple filenames", {
})
})

test_that("ggsave fails informatively for no-extension filenames", {
plot <- ggplot(mtcars, aes(disp, mpg)) + geom_point()
expect_error(
ggsave(tempfile(), plot),
'`filename` has no file extension and `device` is "NULL"'
)
})

# plot_dim ---------------------------------------------------------------

test_that("guesses and informs if dim not specified", {
Expand Down