Skip to content

Commit f8dae93

Browse files
committed
Add tests
And fix a couple of edge cases that came up.
1 parent d2d3843 commit f8dae93

25 files changed

+818
-14
lines changed

DESCRIPTION

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ Imports:
99
rvest,
1010
tibble
1111
Suggests:
12-
testthat
12+
mockery,
13+
testthat (>= 3.0.0),
14+
webfakes,
15+
withr
1316
SystemRequirements: gzip, tar, unzip
1417
Config/testthat/edition: 3

NAMESPACE

Whitespace-only changes.

R/download.R

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
download_files_chunk_size <- function() 100
1+
download_files_chunk_size <- function() {
2+
as.integer(Sys.getenv("TEST_DOWNLOAD_CHUNK_SIZE", 100))
3+
}
24

35
download_files <- function(urls, destdir) {
46
chks <- chunk(urls, download_files_chunk_size())
@@ -23,9 +25,9 @@ download_files_chunk <- function(urls, destdir) {
2325
httpfail <- dlres$success & dlres$status_code >= 300
2426
dlres$success[httpfail] <- FALSE
2527
dlres$error[httpfail] <- substr(paste0(
26-
"HTTP error",
27-
dlres$status_code[httpfail],
28-
vapply(tmpfiles[httpfail], "", FUN = function(f) { read_file(f) })
28+
"HTTP error ",
29+
dlres$status_code[httpfail], ": ",
30+
vapply(tmpfiles[miss][httpfail], "", FUN = function(f) { read_file(f) })
2931
), 1, 300)
3032

3133
if (any(!dlres$success)) {

R/package-files.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ get_desc_data_tar <- function(f, p) {
2020
}
2121

2222
get_desc_data_zip <- function(f, p) {
23-
cmd <- glue::glue("unzip -p \"{f}\" {p}/DESCRIPTION")
24-
out <- system(cmd, intern = TRUE)
23+
cmd <- glue::glue("unzip -p \"{f}\" {p}/DESCRIPTION 2>/dev/null")
24+
out <- suppressWarnings(system(cmd, intern = TRUE))
2525
if (!is.null(attr(out, "status")) && attr(out, "status") >= 1) {
2626
NA_character_
2727
} else {

R/parse.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
get_cran_mirror <- function() {
2-
"https://cran.r-project.org"
2+
Sys.getenv("TEST_CRAN_PACKAGE_MIRROR", "https://cran.r-project.org")
33
}
44

55
parse_file_name <- function(x) {

R/utils.R

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
`%||%` <- function(l, r) if (is.null(l)) r else l
2-
31
`%&&%` <- function(l, r) if (is.null(l)) l else r
42

53
is_na1 <- function(x) {
@@ -11,7 +9,7 @@ is_na1 <- function(x) {
119
`%|NA|%` <- function(l, r) if (is_na1(l)) r else l
1210

1311
chunk <- function(x, size) {
14-
split(x, ceiling(seq_along(x)/size))
12+
unname(split(x, ceiling(seq_along(x)/size)))
1513
}
1614

1715
mkdirp <- function(x) {

tests/testthat.R

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This file is part of the standard setup for testthat.
2+
# It is recommended that you do not modify it.
3+
#
4+
# Where should you do additional test configuration?
5+
# Learn more about the roles of various files in:
6+
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
7+
# * https://testthat.r-lib.org/articles/special-files.html
8+
9+
library(testthat)
10+
library(cranmetadata)
11+
12+
test_check("cranmetadata")

tests/testthat/_snaps/download.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# download_files
2+
3+
Code
4+
res <- download_files(urls, destdir = tmp)
5+
Message
6+
i Downloading 10 packages.
7+
v Downloading 10 packages. ... done
8+
9+
i Downloading 10 packages.
10+
v Downloading 10 packages. ... done
11+
12+
i Downloading 6 packages.
13+
v Downloading 6 packages. ... done
14+
15+
16+
---
17+
18+
Code
19+
res <- download_files(urls, destdir = tmp)
20+
Message
21+
i Downloading 1 package.
22+
v Downloading 1 package. ... done
23+
24+
x Failed to download 1 package.
25+
First error message:
26+
HTTP error 404: oops
27+
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# get_desc_data_tar
2+
3+
Code
4+
dsc <- get_desc_data_tar(path, "test")
5+
dsc
6+
Output
7+
[1] "Package: test"
8+
[2] "Title: What the Package Does (One Line, Title Case)"
9+
[3] "Version: 0.0.0.9000"
10+
[4] "Authors@R: "
11+
[5] " person(\"First\", \"Last\", , \"[email protected]\", role = c(\"aut\", \"cre\"),"
12+
[6] " comment = c(ORCID = \"YOUR-ORCID-ID\"))"
13+
[7] "Description: What the package does (one paragraph)."
14+
[8] "License: `use_mit_license()`, `use_gpl3_license()` or friends to pick a"
15+
[9] " license"
16+
[10] "Encoding: UTF-8"
17+
[11] "Roxygen: list(markdown = TRUE)"
18+
[12] "RoxygenNote: 7.3.2"
19+
[13] "NeedsCompilation: no"
20+
[14] "Packaged: 2024-09-27 08:30:11 UTC; gaborcsardi"
21+
[15] "Author: First Last [aut, cre] (YOUR-ORCID-ID)"
22+
[16] "Maintainer: First Last <[email protected]>"
23+
24+
# get_desc_data_zip
25+
26+
Code
27+
get_desc_data_zip(path, "foobar")
28+
Output
29+
[1] "Package: foobar"
30+
[2] "Title: Foobar Package"
31+
[3] "Version: 1.0.0"
32+
[4] "Author: Gabor Csardi"
33+
[5] "Maintainer: Gabor Csardi <[email protected]>"
34+
[6] "Description: Just a package to use in test cases."
35+
[7] "License: MIT + file LICENSE"
36+
[8] "LazyData: true"
37+
[9] "URL: https://github.com/gaborcsardi/foobar"
38+
[10] "BugReports: https://github.com/gaborcsardi/foobar/issues"
39+
[11] "RoxygenNote: 6.0.1"
40+
[12] "Suggests: testthat"
41+
[13] "NeedsCompilation: no"
42+
[14] "Packaged: 2017-12-25 19:18:10 UTC; gaborcsardi"
43+
[15] "Built: R 3.4.3; ; 2017-12-25 19:19:13 UTC; unix"
44+
45+
# get_desc_data
46+
47+
Code
48+
get_desc_data(paths)
49+
Output
50+
# A tibble: 5 x 5
51+
file package sysreqs built published
52+
<chr> <chr> <chr> <chr> <chr>
53+
1 fixtures/test_0.0.0.9000.tar.gz test "" "" ""
54+
2 fixtures/test_0.0.0.9000.tgz test "" "R 4.4.1; ; 2024-09~ ""
55+
3 fixtures/foobar_1.0.0.zip foobar "" "R 3.4.3; ; 2017-12~ ""
56+
4 fixtures/foobar_1.0.1.zip foobar <NA> <NA> <NA>
57+
5 fixtures/foobar_1.0.2.zip foobar <NA> <NA> <NA>
58+

tests/testthat/_snaps/parse.md

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# parse_file_name
2+
3+
Code
4+
parse_file_name(c("package1_1.0.0.tar.gz", "package2_1.0.1.tgz",
5+
"package3_1.0.2.zip"))
6+
Output
7+
$package
8+
[1] "package1" "package2" "package3"
9+
10+
$version
11+
[1] '1.0.0' '1.0.1' '1.0.2'
12+
13+
14+
# parse_package_list
15+
16+
Code
17+
parse_package_list("src/contrib")
18+
Message
19+
i Downloading package list from <http://127.0.0.1:<port>//src/contrib>.
20+
Output
21+
# A tibble: 88 x 3
22+
file package version
23+
<chr> <chr> <pckg_vrs>
24+
1 A3_1.0.0.tar.gz A3 1.0.0
25+
2 AATtools_0.0.3.tar.gz AATtools 0.0.3
26+
3 ABACUS_1.0.0.tar.gz ABACUS 1.0.0
27+
4 ABC.RAP_0.9.0.tar.gz ABC.RAP 0.9.0
28+
5 ABCanalysis_1.2.1.tar.gz ABCanalysis 1.2.1
29+
6 ABCoptim_0.15.0.tar.gz ABCoptim 0.15.0
30+
7 ABCp2_1.2.tar.gz ABCp2 1.2
31+
8 ABHgenotypeR_1.0.1.tar.gz ABHgenotypeR 1.0.1
32+
9 ABM_0.4.1.tar.gz ABM 0.4.1
33+
10 ABPS_0.3.tar.gz ABPS 0.3
34+
# i 78 more rows
35+
36+
# parse_metadata_file
37+
38+
Code
39+
parse_metadata_file("src/contrib")
40+
Message
41+
i Parsing metadata from 'fixtures/metadata/src/contrib/METADATA2.gz'.
42+
Output
43+
# A tibble: 87 x 6
44+
file size sha sysreqs built published
45+
<chr> <int> <chr> <chr> <lgl> <chr>
46+
1 A3_1.0.0.tar.gz 42810 9c99d5ee74122d38a5~ "" NA 2015-08-~
47+
2 AATtools_0.0.3.tar.gz 252641 89d64a7ee0b8057475~ "" NA 2024-08-~
48+
3 ABACUS_1.0.0.tar.gz 84194 31b4292097ffd7db14~ "" NA 2019-09-~
49+
4 ABC.RAP_0.9.0.tar.gz 4769661 b3d77017c620f48488~ "GNU m~ NA 2016-10-~
50+
5 ABCanalysis_1.2.1.tar.gz 23436 91e3b8f19b291ec7d3~ "" NA 2017-03-~
51+
6 ABCoptim_0.15.0.tar.gz 13482 8bd894574dd5376eaf~ "" NA 2017-11-~
52+
7 ABCp2_1.2.tar.gz 7293 7d593601836835b284~ "" NA 2016-02-~
53+
8 ABHgenotypeR_1.0.1.tar.gz 100729 14c419abc4f717ec05~ "" NA 2016-02-~
54+
9 ABM_0.4.1.tar.gz 48025 57b9dd43e443895a71~ "" NA 2024-03-~
55+
10 ABPS_0.3.tar.gz 108733 940ac727fab90ebb81~ "" NA 2018-10-~
56+
# i 77 more rows
57+
58+
---
59+
60+
Code
61+
parse_metadata_file("src/contrib")
62+
Message
63+
! Metadata file does not exist: '<tempdir>/<tempfile>/metadata/src/contrib/METADATA2.gz'.
64+
Output
65+
# A tibble: 0 x 6
66+
# i 6 variables: file <chr>, size <int>, sha <chr>, sysreqs <chr>, built <chr>,
67+
# published <chr>
68+
69+
# get_state
70+
71+
Code
72+
get_state("src/contrib")
73+
Message
74+
i Downloading package list from <http://127.0.0.1:<port>//src/contrib>.
75+
i Parsing metadata from 'fixtures/metadata/src/contrib/METADATA2.gz'.
76+
Output
77+
$old
78+
# A tibble: 87 x 6
79+
file size sha sysreqs built published
80+
<chr> <int> <chr> <chr> <lgl> <chr>
81+
1 A3_1.0.0.tar.gz 42810 9c99d5ee74122d38a5~ "" NA 2015-08-~
82+
2 AATtools_0.0.3.tar.gz 252641 89d64a7ee0b8057475~ "" NA 2024-08-~
83+
3 ABACUS_1.0.0.tar.gz 84194 31b4292097ffd7db14~ "" NA 2019-09-~
84+
4 ABC.RAP_0.9.0.tar.gz 4769661 b3d77017c620f48488~ "GNU m~ NA 2016-10-~
85+
5 ABCanalysis_1.2.1.tar.gz 23436 91e3b8f19b291ec7d3~ "" NA 2017-03-~
86+
6 ABCoptim_0.15.0.tar.gz 13482 8bd894574dd5376eaf~ "" NA 2017-11-~
87+
7 ABCp2_1.2.tar.gz 7293 7d593601836835b284~ "" NA 2016-02-~
88+
8 ABHgenotypeR_1.0.1.tar.gz 100729 14c419abc4f717ec05~ "" NA 2016-02-~
89+
9 ABM_0.4.1.tar.gz 48025 57b9dd43e443895a71~ "" NA 2024-03-~
90+
10 ABPS_0.3.tar.gz 108733 940ac727fab90ebb81~ "" NA 2018-10-~
91+
# i 77 more rows
92+
93+
$new
94+
# A tibble: 88 x 3
95+
file package version
96+
<chr> <chr> <pckg_vrs>
97+
1 A3_1.0.0.tar.gz A3 1.0.0
98+
2 AATtools_0.0.3.tar.gz AATtools 0.0.3
99+
3 ABACUS_1.0.0.tar.gz ABACUS 1.0.0
100+
4 ABC.RAP_0.9.0.tar.gz ABC.RAP 0.9.0
101+
5 ABCanalysis_1.2.1.tar.gz ABCanalysis 1.2.1
102+
6 ABCoptim_0.15.0.tar.gz ABCoptim 0.15.0
103+
7 ABCp2_1.2.tar.gz ABCp2 1.2
104+
8 ABHgenotypeR_1.0.1.tar.gz ABHgenotypeR 1.0.1
105+
9 ABM_0.4.1.tar.gz ABM 0.4.1
106+
10 ABPS_0.3.tar.gz ABPS 0.3
107+
# i 78 more rows
108+
109+
$missing
110+
# A tibble: 1 x 3
111+
file package version
112+
<chr> <chr> <pckg_vrs>
113+
1 test_0.0.0.9000.tar.gz test 0.0.0.9000
114+
115+

tests/testthat/_snaps/update.md

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# get_package_directories
2+
3+
Code
4+
get_package_directories()
5+
Output
6+
[1] "src/contrib"
7+
[2] "bin/macosx/big-sur-arm64/contrib/4.2"
8+
[3] "bin/macosx/big-sur-x86_64/contrib/4.3"
9+
[4] "bin/macosx/big-sur-x86_64/contrib/4.4"
10+
[5] "bin/macosx/big-sur-arm64/contrib/4.3"
11+
[6] "bin/macosx/big-sur-arm64/contrib/4.4"
12+
[7] "bin/windows/contrib/4.2"
13+
[8] "bin/windows/contrib/4.3"
14+
[9] "bin/windows/contrib/4.4"
15+
[10] "bin/windows/contrib/4.5"
16+
17+
# update
18+
19+
Code
20+
update()
21+
Message
22+
i Updating 'src/contrib'.
23+
i Updating 'bin/macosx/big-sur-arm64/contrib/4.2'.
24+
i Updating 'bin/macosx/big-sur-x86_64/contrib/4.3'.
25+
i Updating 'bin/macosx/big-sur-x86_64/contrib/4.4'.
26+
i Updating 'bin/macosx/big-sur-arm64/contrib/4.3'.
27+
i Updating 'bin/macosx/big-sur-arm64/contrib/4.4'.
28+
i Updating 'bin/windows/contrib/4.2'.
29+
i Updating 'bin/windows/contrib/4.3'.
30+
i Updating 'bin/windows/contrib/4.4'.
31+
i Updating 'bin/windows/contrib/4.5'.
32+
33+
# update_dir
34+
35+
Code
36+
ret <- update_dir("src/contrib")
37+
Message
38+
i Downloading package list from <http://127.0.0.1:<port>//src/contrib>.
39+
i Parsing metadata from '<tempdir>/<tempfile>/metadata/src/contrib/METADATA2.gz'.
40+
i Will update 1 package.
41+
i Downloading 1 package.
42+
v Downloading 1 package. ... done
43+
44+
i Checking file sizes.
45+
i Calculating SHA256 hashes.
46+
i Writing new metadata
47+
v Metadata successfully updated for 'src/contrib'.
48+
49+
---
50+
51+
Code
52+
meta_new[grepl("test", meta_new$file), ]
53+
Output
54+
# A tibble: 1 x 6
55+
file size sha sysreqs built published
56+
<chr> <int> <chr> <chr> <lgl> <chr>
57+
1 test_0.0.0.9000.tar.gz 552 1445e9b41452f795c0a6cbfb~ "" NA ""
58+
59+
---
60+
61+
Code
62+
ret <- update_dir("src/contrib")
63+
Message
64+
i Downloading package list from <http://127.0.0.1:<port>//src/contrib>.
65+
i Parsing metadata from '<tempdir>/<tempfile>/metadata/src/contrib/METADATA2.gz'.
66+
v All packages are up to date.
67+
68+
---
69+
70+
Code
71+
ret <- update_dir("src/contrib")
72+
Message
73+
i Downloading package list from <http://127.0.0.1:<port>//src/contrib>.
74+
i Parsing metadata from '<tempdir>/<tempfile>/metadata/src/contrib/METADATA2.gz'.
75+
i Will update 1 package.
76+
i Checking file sizes.
77+
i Calculating SHA256 hashes.
78+
x Failed to update 1 package: test.
79+
i Writing new metadata
80+
v Metadata successfully updated for 'src/contrib'.
81+
82+
---
83+
84+
Code
85+
meta_upd[!meta_upd$file %in% meta_new$file, ]
86+
Output
87+
# A tibble: 1 x 6
88+
file size sha sysreqs built published
89+
<chr> <chr> <chr> <chr> <chr> <chr>
90+
1 test_0.0.0.1.tar.gz 1 <NA> "" <NA> ""
91+
9.53 KB
Binary file not shown.
9.08 KB
Binary file not shown.
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Not really a zip file
Binary file not shown.

0 commit comments

Comments
 (0)