Skip to content

Commit 7bebef6

Browse files
committed
Make get_hex and get_description more robust
1 parent 71fe09a commit 7bebef6

15 files changed

+149
-46
lines changed

NEWS.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
## Bug fixes
44

5-
- CRAN's VMs are having issues.
5+
* CRAN's VMs are having issues.
6+
* Reduce `docs` size by rendering PNG instead of html in `depgraph` vignette.
7+
* Get code coverage back up to 91%+
8+
* Revamp `get_hex` and `get_description`
9+
- Use lists more consistently
10+
- More robust in general
611

712
# rworkflows 0.99.11
813

R/check_paths.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
check_paths <- function(refs,
2+
paths,
3+
verbose=TRUE){
4+
if(!is.null(refs)){
5+
if(length((paths))!=length(refs)){
6+
messager("When refs is provided, paths must have the same length",
7+
"(or be set to NULL).","Setting paths=NULL.",v=verbose)
8+
paths <- list(NULL)
9+
}
10+
}
11+
return(paths)
12+
}

R/get_authors.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
get_authors <- function(ref=NULL,
1111
add_html=FALSE){
1212

13-
d <- get_description(refs = ref[1])[[1]]
13+
d <- get_description(refs = ref,
14+
paths = NULL)[[1]]
15+
if(is.list(d)) d <- d[[1]]
1416
if(is.null(d)) return(NULL)
15-
field <- grep("Authors",d$fields(),value = TRUE)[[1]]
17+
field <- grep("Authors",d$fields(),value = TRUE)
1618
if(length(field)>0){
1719
authors <- d$get_field(field)
1820
auths <- eval(parse(text = gsub('person','c',authors)));

R/get_description.R

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,23 @@ get_description <- function(refs=NULL,
4141
verbose=TRUE){
4242
# devoptera::args2vars(get_description)
4343

44+
paths <- check_paths(refs = refs,
45+
paths = paths,
46+
verbose = verbose)
47+
refs <- refs_to_list(refs = refs)
48+
paths <- refs_to_list(refs = paths)
49+
if(methods::is(refs[[1]],"description")) {
50+
refs <- get_description_check(dl = refs,
51+
verbose = verbose)
52+
return(refs)
53+
}
54+
if(methods::is(paths[[1]],"description")) {
55+
paths <- get_description_check(dl = paths,
56+
verbose = verbose)
57+
return(paths)
58+
}
4459
if(all(is.na(refs))) refs <- NULL
45-
if(methods::is(refs[[1]],"description")) return(refs)
46-
if(methods::is(paths[[1]],"description")) return(paths)
60+
4761
#### Method 1 ####
4862
dl1 <- get_description_manual(refs=refs,
4963
paths=paths,
@@ -52,13 +66,15 @@ get_description <- function(refs=NULL,
5266
use_wd=use_wd,
5367
verbose=verbose)
5468
dl1 <- get_description_check(dl = dl1,
55-
verbose=verbose)
56-
if(!is.null(refs) && all(!sapply(dl1,is.null)) ){
57-
if(all(basename(refs) %in% basename(names(dl1)))) {
69+
verbose=verbose)
70+
refs <- names(dl1)
71+
if(!is.null(unlist(dl1))){
72+
if(all(basename(unlist(refs)) %in% basename(names(dl1)))) {
5873
return(dl1)
5974
}
6075
}
61-
if(isFALSE(use_repos) || is.null(refs)){
76+
if(isFALSE(use_repos) ||
77+
is.null(refs)){
6278
return(dl1)
6379
} else {
6480
#### Method 2 ####

R/get_description_check.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
get_description_check <- function(dl,
22
verbose=TRUE){
33
### Make sure it gets a name ####
4-
if(is.null(dl)) return(NULL)
5-
for(i in seq_len(length(dl))){
6-
if(is.null(names(dl[i])) &&
4+
if(is.null(unlist(dl))) return(NULL)
5+
for(i in seq(length(dl))){
6+
nm <- names(dl[i])
7+
if((is.null(nm) || nm=="NULL") &&
78
methods::is(dl[[i]],"description") ){
89
names(dl)[i] <- dl[[i]]$get_field("Package")
910
}

R/get_description_manual.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ get_description_manual <- function(refs=NULL,
2121
refs),
2222
function(i){
2323
ref <- refs[[i]]
24-
path <- paths[[i]]
24+
path <- if(length(unlist(paths))==0) NULL else paths[[i]]
2525
wrn <- if(is.null(ref)){
2626
paste("Cannot import DESCRIPTION file:",paths)
2727
} else{

R/get_hex.R

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,14 @@ get_hex <- function(refs=NULL,
2424
verbose=TRUE){
2525
# devoptera::args2vars(get_hex)
2626

27+
if(isTRUE(hex_path)) hex_path <- "inst/hex/hex.png"
28+
dl <- get_description(refs = refs,
29+
paths = paths)
30+
refs <- names(dl)
2731
if(!is.null(refs)) {
2832
messager("Finding hex sticker(s) for",
2933
formatC(length(refs),big.mark = ","),"package(s).",v=verbose)
30-
}
31-
if(!is.null(refs)){
32-
if(length((paths))!=length(refs)){
33-
messager("When refs is provided, paths must have the same length",
34-
"(or be set to NULL).","Setting paths=NULL.",v=verbose)
35-
paths <- NULL
36-
}
37-
}
38-
if(isTRUE(hex_path)){
39-
hex_path <- "inst/hex/hex.png"
4034
}
41-
dl <- get_description(refs = refs,
42-
paths = paths)
4335
#### Iterate over refs ####
4436
hexes <- lapply(stats::setNames(seq_len(length(dl)),
4537
names(dl)),

R/refs_to_list.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
refs_to_list <- function(refs){
2+
if(is.list(refs)){
3+
refs
4+
} else if(is.null(refs)){
5+
list(NULL)
6+
} else if(is.character(refs)){
7+
as.list(refs)
8+
} else (
9+
list(refs)
10+
)
11+
}

reports/rworkflows_depgraph.png

407 KB
Loading

tests/testthat/test-get_authors.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
test_that("get_authors works", {
2+
3+
true_auths <- "Brian Schilder, Alan Murphy, Nathan Skene"
4+
#### ref is NULL ####
5+
auths1 <- get_authors(ref = NULL)
6+
testthat::expect_null(auths1)
7+
#### Ref is filled: has authors ####
8+
auths2 <- get_authors(ref = "rworkflows")
9+
testthat::expect_equal(auths2, true_auths)
10+
#### Ref is filled: has modified authors ####
11+
d <- get_description(refs="neurogenomics/rworkflows")[[1]]
12+
tmp <- tempfile(pattern = "DESCRIPTION")
13+
d$del("Authors@R")
14+
d$del("Author")
15+
auths3 <- get_authors(ref = d)
16+
testthat::expect_null(auths3)
17+
})

0 commit comments

Comments
 (0)