Skip to content
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

Initial infrastructure for editions #6203

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Collate:
'coord-transform.R'
'data.R'
'docs_layer.R'
'edition.R'
'facet-.R'
'facet-grid-.R'
'facet-null.R'
Expand Down
73 changes: 73 additions & 0 deletions R/edition.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@

ggplot_edition <- new.env(parent = emptyenv())

local_edition <- function(edition, .env = parent.frame()) {
stopifnot(is_zap(edition) || (is.numeric(edition) && length(edition) == 1))
pkg <- get_pkg_name(.env)
local_bindings(!!pkg := edition, .env = ggplot_edition, .frame = .env)
}

edition_get <- function(.env = parent.frame(), default = 2024L) {
pkg <- get_pkg_name(.env)

# Try to query edition from cache
edition <- env_get(ggplot_edition, nm = pkg, default = NULL)
if (!is.null(edition)) {
return(edition)
}

# Try to query package description
desc_file <- find_description(path = ".", package = pkg)
if (is.null(desc_file)) {
return(default)

Check warning on line 22 in R/edition.R

View check run for this annotation

Codecov / codecov/patch

R/edition.R#L20-L22

Added lines #L20 - L22 were not covered by tests
}

# Look up edition from the description
field_name <- "Config/ggplot2/edition"
edition <- as.integer(read.dcf(desc_file, fields = field_name))

Check warning on line 27 in R/edition.R

View check run for this annotation

Codecov / codecov/patch

R/edition.R#L26-L27

Added lines #L26 - L27 were not covered by tests

# Cache result
env_bind(ggplot_edition, !!pkg := edition)
return(edition)

Check warning on line 31 in R/edition.R

View check run for this annotation

Codecov / codecov/patch

R/edition.R#L30-L31

Added lines #L30 - L31 were not covered by tests
}

edition_deprecate <- function(edition, ..., .env = parent.frame()) {
check_number_whole(edition)
if (edition_get(.env) < edition) {
return(invisible(NULL))
}

edition <- I(paste0("edition ", edition))
lifecycle::deprecate_stop(edition, ...)
}

edition_require <- function(edition, what, .env = parent.frame()) {
check_number_whole(edition)
current <- edition_get(.env)
if (current >= edition) {
return(invisible(NULL))
}
msg <- paste0(what, " requires the ", edition, " edition of {.pkg ggplot2}.")
cli::cli_abort(msg)
}

find_description <- function(path, package = NULL) {
if (!is.null(package)) {
path <- system.file(package = package, "DESCRIPTION")

Check warning on line 56 in R/edition.R

View check run for this annotation

Codecov / codecov/patch

R/edition.R#L55-L56

Added lines #L55 - L56 were not covered by tests
} else {
path <- file.path(path, "DESCRIPTION")

Check warning on line 58 in R/edition.R

View check run for this annotation

Codecov / codecov/patch

R/edition.R#L58

Added line #L58 was not covered by tests
}
if (!file.exists(path)) {
return(NULL)

Check warning on line 61 in R/edition.R

View check run for this annotation

Codecov / codecov/patch

R/edition.R#L60-L61

Added lines #L60 - L61 were not covered by tests
}
path

Check warning on line 63 in R/edition.R

View check run for this annotation

Codecov / codecov/patch

R/edition.R#L63

Added line #L63 was not covered by tests
}

get_pkg_name <- function(env = parent.frame()) {
env <- topenv(env)
name <- environmentName(env)
if (!isNamespace(env) && name != "R_GlobalEnv") {
return(NULL)

Check warning on line 70 in R/edition.R

View check run for this annotation

Codecov / codecov/patch

R/edition.R#L70

Added line #L70 was not covered by tests
}
name
}
16 changes: 16 additions & 0 deletions tests/testthat/_snaps/edition.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# basic edition infrastructure works as intended

Code
edition_deprecate(2025, what = "foo()")
Condition
Error:
! `foo()` was deprecated in ggplot2 edition 2025 and is now defunct.

---

Code
edition_require(2025, what = "foo()")
Condition
Error in `edition_require()`:
! foo() requires the 2025 edition of ggplot2.

10 changes: 10 additions & 0 deletions tests/testthat/test-edition.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
test_that("basic edition requirement and deprecation works as intended", {

local_edition(2025)
expect_snapshot(edition_deprecate(2025, what = "foo()"), error = TRUE)
expect_silent(edition_require(2025, what = "foo()"))

local_edition(2024)
expect_silent(edition_deprecate(2025, what = "foo()"))
expect_snapshot(edition_require(2025, what = "foo()"), error = TRUE)
})
Loading