Skip to content

Commit ef6352d

Browse files
authored
Merge pull request #5 from kforner/cli_table
switched to clitable.
2 parents 67c6d74 + e989664 commit ef6352d

16 files changed

Lines changed: 260 additions & 280 deletions

.devcontainer/devcontainer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
// search for APT R packages with: `apt-cache search "^r-.*" | sort`
2424
,"ghcr.io/rocker-org/devcontainer-features/apt-packages:1.0.2": {
25-
"packages": "bash-completion,pandoc,qpdf,r-cran-assertthat,r-cran-debugme,r-cran-cli,r-cran-covr,r-cran-dt,r-cran-htmltools,r-cran-huxtable,r-cran-igraph,r-cran-scales"
25+
"packages": "bash-completion,pandoc,qpdf,r-cran-assertthat,r-cran-debugme,r-cran-cli,r-cran-clitable,r-cran-covr,r-cran-dt,r-cran-htmltools,r-cran-igraph"
2626
}
2727

2828
},

DESCRIPTION

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,20 @@ Description: Manage a collection/library of R source packages. Discover, documen
99
License: GPL (>= 3)
1010
Encoding: UTF-8
1111
Roxygen: list(markdown = TRUE)
12-
RoxygenNote: 7.3.1
12+
RoxygenNote: 7.3.2
1313
URL: https://github.com/kforner/srcpkgs
1414
BugReports: https://github.com/kforner/srcpkgs/issues
1515
Imports:
1616
cli,
17+
clitable,
1718
devtools,
1819
pkgload,
1920
testthat,
2021
stats,
2122
utils
2223
Suggests:
23-
huxtable,
2424
knitr,
2525
rmarkdown,
26-
scales,
2726
withr
2827
Config/testthat/edition: 3
2928
VignetteBuilder: knitr

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,7 @@ export(pkgs_test)
3030
export(reset)
3131
export(settings)
3232
export(unhack_r_loaders)
33+
importFrom(cli,cli_text)
34+
importFrom(clitable,cli_table)
3335
importFrom(testthat,test_dir)
3436
importFrom(utils,getFromNamespace)

R/1_imports.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#' @importFrom cli cli_text
2+
#' @importFrom clitable cli_table
3+
NULL

R/pkg_test.R

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#' @param export_all passed to [pkg_load()]. Enables the test functions to easily access to non-exported
99
#' functions. Caveat: If the pkg is already loaded and up-to-date with export_all=FALSE, it will not work.
1010
#' @param ... passed to `testthat::test_dir()`
11-
#' @return the results as a `pkg_test` object, or NULL if no tests found
11+
#' @return the results as a `pkg_test` object, which is an empty listL if no tests were found
1212
#' @importFrom testthat test_dir
1313
#' @export
1414
#' @examples
@@ -32,16 +32,22 @@ pkg_test <- function(pkgid, filter = NULL, src_pkgs = get_srcpkgs(), export_all
3232
pkg_load(pkg, src_pkgs, quiet = quiet, export_all = export_all)
3333

3434
test_path <- file.path(pkg$path, "tests/testthat")
35-
if (!dir.exists(test_path) || length(dir(test_path)) == 0) return(invisible())
3635

37-
res <- testthat::test_dir(test_path, filter = filter, stop_on_failure = FALSE, ...)
36+
if (!dir.exists(test_path) || length(dir(test_path)) == 0) {
37+
# no tests found, return an empty pkg_test
38+
return(invisible(new_pkg_test(pkg)))
39+
}
3840

39-
attr(res, 'pkg') <- pkg
40-
class(res) <- c('pkg_test', class(res))
41+
res <- testthat::test_dir(test_path, filter = filter, stop_on_failure = FALSE, ...)
4142

42-
invisible(res)
43+
invisible(new_pkg_test(pkg, res))
4344
}
4445

46+
new_pkg_test <- function(pkg_name, test_results = list()) {
47+
attr(test_results, 'pkg') <- pkg_name
48+
class(test_results) <- c('pkg_test', class(test_results))
49+
test_results
50+
}
4551

4652
# tells if the test is successful
4753
# N.B: this is not a roxygen comment ON PURPOSE
@@ -80,14 +86,32 @@ fortify_pkg_test <- function(df) {
8086

8187
#' @export
8288
as.data.frame.pkg_test <- function(x, ...) {
89+
if (!length(x)) {
90+
return(data.frame(
91+
file = character(0),
92+
test = character(0),
93+
nb = integer(0),
94+
failed = integer(0),
95+
passed = integer(0),
96+
skipped = logical(0),
97+
error = logical(0),
98+
warning = integer(0),
99+
time = numeric(0))
100+
)
101+
}
83102
fortify_pkg_test(NextMethod())
84103
}
85104

86105
#' @export
87106
print.pkg_test <- function(x, ...) {
88107
pkg <- attr(x, 'pkg')
89-
df <- as.data.frame(x)
90108

109+
if (!length(x)) {
110+
cli::cli_h1(paste0("package ", pkg$package, " has no tests"))
111+
return(invisible())
112+
}
113+
114+
df <- as.data.frame(x)
91115
results <- df$result
92116

93117
### by test
@@ -115,6 +139,19 @@ print.pkg_test <- function(x, ...) {
115139
# N.B: this is not a roxygen comment ON PURPOSE
116140
#' @export
117141
summary.pkg_test <- function(object, col = 'file', ...) {
142+
if (!length(object)) {
143+
return(data.frame(
144+
file = character(0),
145+
nb = integer(0),
146+
failed = integer(0),
147+
passed = integer(0),
148+
skipped = logical(0),
149+
error = logical(0),
150+
warning = integer(0),
151+
time = numeric(0))
152+
)
153+
}
154+
118155
df <- as.data.frame(object)
119156

120157
stop_unless(length(col) <= 1, "col must be a column name or NULL")

R/pkgs_test.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ pkgs_test <- function(pkgids = names(filter_srcpkgs(src_pkgs, filter)), src_pkgs
2424
#' @export
2525
as.data.frame.pkgs_test <- function(x, ...) {
2626
.row <- function(test_res) {
27+
if (!length(test_res)) {
28+
return(data.frame(nb = 0L, failed = 0L, passed = 0L, skipped = 0L, error = 0L, warning = 0L, time = 0))
29+
}
30+
2731
if (is_error(test_res)) {
2832
# trick to create an empty data frame like test_results
2933

R/text_table.R

Lines changed: 5 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,6 @@
1-
print_text_table_with_huxtable <- function(df, title = NULL, footnote = NULL,
2-
heatmap_columns = NULL, hilite_rows = NULL,
3-
styler = huxtable_text_table_default_styler, ...)
4-
{
5-
### build the huxtable
6-
tt <- huxtable::as_hux(df)
71

8-
if (length(footnote)) tt <- huxtable::add_footnote(tt, footnote)
9-
if (length(title)) tt <- huxtable::set_caption(tt, title)
10-
11-
12-
if (length(styler)) tt <- styler(tt, heatmap_columns = heatmap_columns, hilite_rows = hilite_rows)
13-
14-
# now print
15-
lines <- huxtable::to_screen(tt, max_width = Inf, colnames = FALSE)
16-
cat(lines, sep = '\n')
17-
18-
invisible(tt)
19-
}
20-
21-
huxtable_text_table_default_styler <- function(tt,
22-
heatmap_columns = NULL, heatmap_colorspace = c('green', 'red'),
23-
hilite_rows = NULL, hilite_bg = 'red'
24-
, fg = 'black', bg = '#f7f7f7')
25-
{
26-
### set the title position: top
27-
huxtable::caption_pos(tt) <- 'top'
28-
### make the header line BOLD
29-
tt <- huxtable::set_bold(tt, 1, huxtable::everywhere, TRUE)
30-
31-
### colors
32-
tt <- huxtable::set_background_color(tt, 1, huxtable::everywhere, bg)
33-
tt <- huxtable::set_text_color(tt, 1, huxtable::everywhere, fg)
34-
35-
### hilite
36-
tt <- huxtable::set_background_color(tt, hilite_rows + 1, huxtable::everywhere, hilite_bg)
37-
38-
### heatmap
39-
if (length(heatmap_columns))
40-
for (col in heatmap_columns)
41-
tt <- huxtable::map_background_color(tt, huxtable::everywhere, col, huxtable::by_colorspace(heatmap_colorspace))
42-
43-
44-
### borders
45-
tt <- huxtable::set_bottom_border(tt, 1, huxtable::everywhere, 1)
46-
tt <- huxtable::set_left_border(tt, 1)
47-
tt <- huxtable::set_outer_borders(tt, 1)
48-
tt <- huxtable::set_bottom_border(tt, huxtable::final(1), huxtable::everywhere, 1)
49-
50-
### alignment
51-
tt <- huxtable::set_align(tt, value = 'left')
52-
53-
tt
54-
}
55-
56-
print_text_table_with_base <- function(df, ...) {
57-
print(df)
58-
invisible()
59-
}
60-
61-
print_text_table <- function(df, ...) {
62-
if (!requireNamespace("huxtable", quietly = TRUE)) {
63-
print_text_table_with_base(df, ...)
64-
} else {
65-
print_text_table_with_huxtable(df, ...)
66-
}
67-
invisible()
68-
}
2+
print_text_table <- function(df, title = NULL, header_style = "bold", border_style = "double-single", ...) {
3+
if (length(title)) cli::cli_h1(title)
4+
ct <- cli_table(df, header_style = header_style, border_style = border_style, ...)
5+
cat(ct, sep = "\n")
6+
}

dev.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
library(devtools)
22

3-
43
check_man()
54

65
test()
7-
test(filter = "pkg_load")
6+
test(filter = "config")
87
test(filter = "pkg_check")
8+
test(filter = "pkg_load")
9+
test(filter = "pkg_test")
10+
test(filter = "pkgs_test")
911
check()
12+
13+
options(width = Sys.getenv("COLUMNS", 80))

man/pkg_test.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)