diff --git a/NEWS.md b/NEWS.md index f964828c..5c097af5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # desc (development version) +* An empty `Depends` field is now properly normalized and formatted. + (#148, @kevinushey) + # desc 1.4.3 * `$set()` and `desc_set()` now can omit checks if `check = FALSE` diff --git a/R/deps.R b/R/deps.R index c7303cb5..09dd3aa0 100644 --- a/R/deps.R +++ b/R/deps.R @@ -96,6 +96,7 @@ parse_deps <- function(type, deps) { deps <- str_trim(strsplit(deps, ",")[[1]]) deps <- lapply(strsplit(deps, "\\("), str_trim) deps <- lapply(deps, sub, pattern = "\\)$", replacement = "") + deps <- deps[vapply(deps, function(x) length(x) != 0, FUN.VALUE = logical(1))] res <- data.frame( stringsAsFactors = FALSE, type = if (length(deps)) type else character(), @@ -103,7 +104,7 @@ parse_deps <- function(type, deps) { version = vapply(deps, "[", "", 2) ) res$version <- gsub("\\s+", " ", res$version) - res$version [ is.na(res$version) ] <- "*" + res$version[is.na(res$version)] <- "*" res } diff --git a/R/str.R b/R/str.R index d24ac93c..a4ad597f 100644 --- a/R/str.R +++ b/R/str.R @@ -61,8 +61,8 @@ format.DescriptionField <- function(x, ..., width = 75) { format.DescriptionDependencyList <- function(x, ...) { paste0( - cli::col_blue(x$key), ":\n", - paste0( + cli::col_blue(x$key), if (nzchar(x$value)) ":\n" else ":", + if (nzchar(x$value)) paste0( " ", sort(str_trim(strsplit(color_bad(x), ",", fixed = TRUE)[[1]])), collapse = ",\n" diff --git a/tests/testthat/test-deps.R b/tests/testthat/test-deps.R index 313c62ac..c5b106d4 100644 --- a/tests/testthat/test-deps.R +++ b/tests/testthat/test-deps.R @@ -234,3 +234,19 @@ test_that("extra whitespace is removed from deps, but kept in raw data", { fixed = TRUE ) }) + +test_that("empty fields are accepted during normalization", { + + x <- desc("!new") + x$set("Depends", "") + x$normalize() + expect_equal(x$get("Depends"), c(Depends = "")) + + file <- tempfile("desc-") + on.exit(unlink(file), add = TRUE) + x$write(file) + d <- desc(file) + + expect_equal(d$get("Depends"), c(Depends = "")) + +})