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

Allow invalid CRAN names #883

Merged
merged 3 commits into from Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
@@ -1,5 +1,7 @@
# usethis (development version)

* `use_description()` and `create_package()` gain the argument `stop_for_name` to control whether to allow names invalid for CRAN (#883 @noamross)
noamross marked this conversation as resolved.
Show resolved Hide resolved

* New `use_lifecycle()` helper to import the lifecycle badges for functions and arguments in your package. See https://lifecycle.r-lib.org/.

* `git_sitrep()` reports email(s) associated with your GitHub account(#724, @dragosmg).
Expand Down
7 changes: 5 additions & 2 deletions R/create.R
Expand Up @@ -30,20 +30,23 @@
create_package <- function(path,
fields = NULL,
rstudio = rstudioapi::isAvailable(),
stop_for_name = TRUE,
open = interactive()) {
path <- user_path_prep(path)
check_path_is_directory(path_dir(path))

name <- path_file(path)
check_package_name(name)
if (stop_for_name) {
jennybc marked this conversation as resolved.
Show resolved Hide resolved
check_package_name(name, stop_for_name = stop_for_name)
}
check_not_nested(path_dir(path), name)

create_directory(path)
old_project <- proj_set(path, force = TRUE)
on.exit(proj_set(old_project), add = TRUE)

use_directory("R")
use_description(fields)
use_description(fields, stop_for_name = stop_for_name)
use_namespace()

if (rstudio) {
Expand Down
19 changes: 13 additions & 6 deletions R/description.R
Expand Up @@ -30,6 +30,8 @@
#' @param fields A named list of fields to add to `DESCRIPTION`, potentially
#' overriding default values. See [use_description()] for how you can set
#' personalized defaults using package options
#' @param stop_for_name Whether to throw an error if the package name is not
#' valid for CRAN
#' @seealso The [description chapter](https://r-pkgs.org/description.html#dependencies)
#' of [R Packages](https://r-pkgs.org).
#' @export
Expand All @@ -41,9 +43,9 @@
#'
#' use_description_defaults()
#' }
use_description <- function(fields = NULL) {
use_description <- function(fields = NULL, stop_for_name = FALSE) {
name <- project_name()
check_package_name(name)
check_package_name(name, stop_for_name = stop_for_name)
fields <- fields %||% list()
check_is_named_list(fields)
fields[["Package"]] <- name
Expand Down Expand Up @@ -97,15 +99,20 @@ build_description_list <- function(fields = list()) {
compact(utils::modifyList(defaults, fields))
}

check_package_name <- function(name) {
check_package_name <- function(name, stop_for_name) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the signature can revert to just being name, right?

if (!valid_package_name(name)) {
ui_stop(c(
"{ui_value(name)} is not a valid package name. It should:",
msg <- c(
"{ui_value(name)} is not a valid package name. To be allowed on CRAN, it should:",
"* Contain only ASCII letters, numbers, and '.'",
"* Have at least two characters",
"* Start with a letter",
"* Not end with '.'"
))
)
if (stop_for_name) {
ui_stop(msg)
} else {
ui_warn(stop_for_name)
}
}
jennybc marked this conversation as resolved.
Show resolved Hide resolved

}
Expand Down
6 changes: 5 additions & 1 deletion man/create_package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion man/use_description.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion tests/testthat/helper.R
Expand Up @@ -66,7 +66,8 @@ scoped_temporary_thing <- function(dir = file_temp(pattern = pattern),
withr::local_options(list(usethis.quiet = TRUE))
switch(
thing,
package = create_package(dir, rstudio = rstudio, open = FALSE),
package = create_package(dir, rstudio = rstudio, open = FALSE,
stop_for_name = FALSE),
project = create_project(dir, rstudio = rstudio, open = FALSE)
)
proj_set(dir)
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-use-description.R
Expand Up @@ -67,3 +67,13 @@ test_that("default description is tidy", {
desc_lines_after <- readLines(proj_path("DESCRIPTION"))
expect_identical(desc_lines_before, desc_lines_after)
})

test_that("valid CRAN names checked", {
withr::local_options(list(usethis.description = NULL, devtools.desc = NULL))
suppressWarnings(
scoped_temporary_package(dir = file_temp(pattern = "invalid_pkg_name"))
)
expect_warning(use_description())
expect_error(use_description(stop_for_name = TRUE))
jennybc marked this conversation as resolved.
Show resolved Hide resolved
})