Skip to content

Commit

Permalink
Add tests to improve code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
andrie committed Jan 15, 2018
1 parent be2e5c8 commit 03415b1
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
8 changes: 5 additions & 3 deletions R/github-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ getPkgVersFromFile <- function(file) {
file <- sapply(strsplit(file, "\\.tgz"), "[[", 1)
pkg <- sapply(strsplit(file, "_"), "[[", 1)
vers <- sapply(strsplit(file, "_"), "[[", 2)
df <- data.frame(package = pkg, version = vers, stringsAsFactors = FALSE)
df[order(df$package),]
df <- data.frame(package = pkg, version = vers, stringsAsFactors = FALSE, row.names = NULL)
df <- df[order(df$package),]
row.names(df) <- seq_len(nrow(df))
df
} else {
NULL
data.frame(package = character(0), version = character(0))
}
}

Expand Down
Binary file added tests/testthat/mock_data/pdb.rds
Binary file not shown.
46 changes: 46 additions & 0 deletions tests/testthat/test-1-github-functions.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
if (interactive()) library(testthat)

# Copyright (c) Andrie de Vries 2018

context("github-functions")

test_that("package file extension is computed correctly", {
expect_equal(pkgFileExt("source"), ".tar.gz")
expect_equal(pkgFileExt("win.binary"), ".zip")
expect_equal(pkgFileExt("mac.binary"), ".tgz")
expect_error(pkgFileExt("nonsense"))
})

test_that("package verson is extracted correctly", {
ff <- c(
"KernSmooth_2.23-15.tar.gz",
"MASS_7.3-47.tar.gz",
"Matrix_1.2-12.tar.gz",
"boot_1.3-20.tar.gz"
)
expect_equal(
getPkgVersFromFile(ff),
data.frame(
package = c("boot", "KernSmooth", "MASS", "Matrix"),
version = c("1.3-20", "2.23-15", "7.3-47", "1.2-12"),
stringsAsFactors = FALSE,
row.names = NULL
)
)

expect_equal(
getPkgVersFromFile("nonsense"),
data.frame(package = character(0), version = character(0))
)

})

test_that("readDescription reads file", {
sf <- system.file("DESCRIPTION", package = "miniCRAN")
desc <- readDescription(sf)

expect_is(desc, "list")
expect_true(
all(c("Imports", "Suggests", "Package") %in% names(desc))
)
})
4 changes: 4 additions & 0 deletions tests/testthat/test-6-getCranDescription.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ context("get CRAN description")
test_that("can read CRAN description", {

skip_on_cran()
mockery::stub(getCranDescription,
what = "tools::CRAN_package_db",
function(...) readRDS("mock_data/pdb.rds")
)
p <- getCranDescription("miniCRAN", repos = c(CRAN = getOption("minicran.mran")))
expect_is(p, "data.frame")
expect_equal(p$Package[1], "miniCRAN")
Expand Down

0 comments on commit 03415b1

Please sign in to comment.