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

Use stricter regexes for package and file name checks #742

Merged
merged 1 commit into from
May 9, 2019
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
6 changes: 3 additions & 3 deletions R/description.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ build_description_list <- function(fields = list()) {
}

check_package_name <- function(name) {
if (!valid_name(name)) {
if (!valid_package_name(name)) {
ui_stop(c(
"{ui_value(name)} is not a valid package name. It should:",
"* Contain only ASCII letters, numbers, and '.'",
Expand All @@ -110,8 +110,8 @@ check_package_name <- function(name) {

}

valid_name <- function(x) {
grepl("^[[:alpha:]][[:alnum:].]+$", x) && !grepl("\\.$", x)
valid_package_name <- function(x) {
grepl("^[a-zA-Z][a-zA-Z0-9.]+$", x) && !grepl("\\.$", x)
}

tidy_desc <- function(desc) {
Expand Down
2 changes: 1 addition & 1 deletion R/r.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ check_file_name <- function(name) {
}

valid_file_name <- function(x) {
grepl("^[[:alnum:]_-]+$", x)
grepl("^[a-zA-Z0-9._-]+$", x)
}

get_active_r_file <- function(path = "R") {
Expand Down
28 changes: 28 additions & 0 deletions tests/testthat/test-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,31 @@ test_that("use_description_field() ignores whitespace", {
use_description_field(name = "foo", value = "bar")
expect_identical(c(foo = "\n bar"), desc::desc_get("foo", pkg))
})

test_that("valid_package_name() enforces valid package names", {
# Contain only ASCII letters, numbers, and '.'
# Have at least two characters
# Start with a letter
# Not end with '.'

expect_true(valid_package_name("aa"))
expect_true(valid_package_name("a7"))
expect_true(valid_package_name("a.2"))

expect_false(valid_package_name("a"))
expect_false(valid_package_name("a-2"))
expect_false(valid_package_name("2fa"))
expect_false(valid_package_name(".fa"))
expect_false(valid_package_name("aa\u00C0")) # \u00C0 is a-grave
expect_false(valid_package_name("a3."))
})

test_that("valid_file_name() enforces valid file names", {
# Contain only ASCII letters, numbers, '-', and '_'
expect_true(valid_file_name("aa.R"))
expect_true(valid_file_name("a7.R"))
expect_true(valid_file_name("a-2.R"))
expect_true(valid_file_name("a_2.R"))
expect_false(valid_file_name("aa\u00C0.R")) # \u00C0 is a-grave
expect_false(valid_file_name("a?3.R"))
})