Skip to content

Commit

Permalink
adds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JBGruber committed Sep 17, 2023
1 parent 258cb63 commit adc48a3
Show file tree
Hide file tree
Showing 18 changed files with 235 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
^README\.Rmd$
^doc$
^Meta$
^\.github$
^codecov\.yml$
1 change: 1 addition & 0 deletions .github/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.html
49 changes: 49 additions & 0 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: R-CMD-check

jobs:
R-CMD-check:
runs-on: ${{ matrix.config.os }}

name: ${{ matrix.config.os }} (${{ matrix.config.r }})

strategy:
fail-fast: false
matrix:
config:
- {os: macos-latest, r: 'release'}
- {os: windows-latest, r: 'release'}
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
- {os: ubuntu-latest, r: 'release'}
- {os: ubuntu-latest, r: 'oldrel-1'}

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_KEEP_PKG_SOURCE: yes

steps:
- uses: actions/checkout@v3

- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.config.r }}
http-user-agent: ${{ matrix.config.http-user-agent }}
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::rcmdcheck
needs: check

- uses: r-lib/actions/check-r-package@v2
with:
upload-snapshots: true
50 changes: 50 additions & 0 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: test-coverage

jobs:
test-coverage:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

steps:
- uses: actions/checkout@v3

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::covr
needs: coverage

- name: Test coverage
run: |
covr::codecov(
quiet = FALSE,
clean = FALSE,
install_path = file.path(Sys.getenv("RUNNER_TEMP"), "package")
)
shell: Rscript {0}

- name: Show testthat output
if: always()
run: |
## --------------------------------------------------------------------
find ${{ runner.temp }}/package -name 'testthat.Rout*' -exec cat '{}' \; || true
shell: bash

- name: Upload test results
if: failure()
uses: actions/upload-artifact@v3
with:
name: coverage-test-failures
path: ${{ runner.temp }}/package
9 changes: 7 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Suggests:
knitr,
rmarkdown
rmarkdown,
testthat (>= 3.0.0),
withr
VignetteBuilder: knitr
Imports:
cli,
stringi,
openssl,
rappdirs,
stringi,
tibble,
urltools,
vctrs
Config/testthat/edition: 3
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

export(add_cookies)
export(decrypt_vec)
export(default_jar)
export(encrypt_vec)
export(get_cookies)
Expand Down
7 changes: 4 additions & 3 deletions R/encryption.r
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,19 @@ encrypt_vec <- function(vec) {

#' encrypt a single element
#' @noRd
enc <- function(x, key) {
enc <- function(x, key = NULL) {
openssl::aes_ctr_encrypt(charToRaw(x), key)
}

#' @rdname encrypt_vec
#' @export
decrypt_vec <- function(vec) {
key <- openssl::sha256(charToRaw(Sys.getenv("COOKIE_KEY", unset = "supergeheim")))
vapply(vec, dec, FUN.VALUE = character(1))
vapply(vec, dec, key, FUN.VALUE = character(1))
}

#' decrypt a single element
#' @noRd
dec <- function(x, key) {
dec <- function(x, key = NULL) {
rawToChar(openssl::aes_ctr_decrypt(x, key))
}
4 changes: 2 additions & 2 deletions R/get.r
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#' @export
#'
#' @examples
#' get_cookies("https://hb.cran.dev") # Reach into your cookie jar and enjoy!
#' \dontrun{get_cookies("https://hb.cran.dev")} # Reach into your cookie jar and enjoy!
#' @seealso \code{\link{add_cookies}}
get_cookies <- function(domain, jar = default_jar(), as = c("data.frame", "string", "vector")) {

Expand Down Expand Up @@ -63,7 +63,7 @@ prep_cookies <- function(tbl, as_list = FALSE) {
if (!as_list) {
return(paste0(tbl$name, "=", tbl$value, collapse = "; "))
} else {
return(setNames(tbl$value, tbl$name))
return(stats::setNames(tbl$value, tbl$name))
}
}
}
6 changes: 5 additions & 1 deletion R/set.r
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ add_cookies <- function(cookiefile, cookiestring, domain = NULL) {
#' @export
#'
#' @examples
#' store_cookies(cookies)
#' \dontrun{store_cookies(cookies)}
store_cookies <- function(cookies, jar = default_jar()) {
cookies$value <- encrypt_vec(cookies$value)
dir.create(jar, showWarnings = FALSE)
Expand Down Expand Up @@ -126,6 +126,10 @@ parse_cookiestring <- function(cookiestring, domain) {
cookiestring <- stringi::stri_split_fixed(cookiestring, pattern = "; ")[[1]]
tibble::tibble(
domain = domain,
flag = NA,
path = NA,
secure = NA,
expiration = NA,
name = stringi::stri_extract_first_regex(cookiestring, "^.*?(?==)"),
value = stringi::stri_extract_first_regex(cookiestring, "(?<==).*$")
)
Expand Down
3 changes: 3 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ knitr::opts_chunk$set(

<!-- badges: start -->
[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[![CRAN status](https://www.r-pkg.org/badges/version/cookiemonster)](https://CRAN.R-project.org/package=cookiemonster)
[![R-CMD-check](https://github.com/JBGruber/cookiemonster/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/JBGruber/cookiemonster/actions/workflows/R-CMD-check.yaml)
[![Codecov test coverage](https://codecov.io/gh/JBGruber/cookiemonster/branch/main/graph/badge.svg)](https://app.codecov.io/gh/JBGruber/cookiemonster?branch=main)
<!-- badges: end -->

Welcome to the `cookiemonster` package, your friendly solution to managing browser cookies in R! 🍪
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

[![Lifecycle:
experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[![CRAN
status](https://www.r-pkg.org/badges/version/cookiemonster)](https://CRAN.R-project.org/package=cookiemonster)
[![R-CMD-check](https://github.com/JBGruber/cookiemonster/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/JBGruber/cookiemonster/actions/workflows/R-CMD-check.yaml)
[![Codecov test
coverage](https://codecov.io/gh/JBGruber/cookiemonster/branch/main/graph/badge.svg)](https://app.codecov.io/gh/JBGruber/cookiemonster?branch=main)
<!-- badges: end -->

Welcome to the `cookiemonster` package, your friendly solution to
Expand Down
14 changes: 14 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
comment: false

coverage:
status:
project:
default:
target: auto
threshold: 1%
informational: true
patch:
default:
target: auto
threshold: 1%
informational: true
2 changes: 1 addition & 1 deletion man/get_cookies.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/store_cookies.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file is part of the standard setup for testthat.
# It is recommended that you do not modify it.
#
# Where should you do additional test configuration?
# Learn more about the roles of various files in:
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
# * https://testthat.r-lib.org/articles/special-files.html

library(testthat)
library(cookiemonster)

test_check("cookiemonster")
7 changes: 7 additions & 0 deletions tests/testthat/test-encryption.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
test_that("encryption and decryption", {
expect_equal({
df <- data.frame(vec = c("foo", "bar"))
df$vec <- encrypt_vec(df$vec)
decrypt_vec(df$vec)
}, c("foo", "bar"))
})
28 changes: 28 additions & 0 deletions tests/testthat/test-get.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
test_that("getting cookies works", {
jar <- options(cookie_dir = tempdir())
withr::defer(options(jar))
withr::defer(unlink(file.path(tempdir(), paste0("cookies.rds"))))

expect_error(get_cookies("tests.com"),
"does not contain any cookies yet")

expect_equal({
add_cookies(cookiestring = "test=true; success=yes", domain = "tests.com")
out <- get_cookies("tests.com", as = "data.frame")
c(class(out), colnames(out), out$domain, out$value)
}, c("tbl_df", "tbl", "data.frame", "domain", "flag", "path", "secure",
"expiration", "name", "value", "tests.com", "tests.com", "true",
"yes"))

expect_equal({
add_cookies(cookiestring = "test=true; success=yes", domain = "tests.com")
get_cookies("tests.com", as = "string")
}, "test=true; success=yes")

expect_equal({
add_cookies(cookiestring = "test=true; success=yes", domain = "tests.com")
get_cookies("tests.com", as = "vector")
}, c(test = "true", success = "yes"))
})


43 changes: 43 additions & 0 deletions tests/testthat/test-set.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
test_that("setting cookies works", {
expect_equal(
default_jar(),
rappdirs::user_cache_dir("r_cookies")
)

jar <- options(cookie_dir = tempdir())
withr::defer(options(jar))
withr::defer(unlink(c(file.path(tempdir(), paste0("cookies.rds")),
"test.txt")))

expect_equal(default_jar(), tempdir())

expect_error(
add_cookies(cookiefile = system.file("extdata", "cookies.txt", package = "cookiemonster"),
cookiestring = "test=true; success=yes"),
"not both"
)

expect_error(
add_cookies(),
"either cookie file or string"
)

expect_gte({
add_cookies(cookiefile = system.file("extdata", "cookies.txt", package = "cookiemonster"))
file.size(file.path(tempdir(), paste0("cookies.rds")))
}, 622)

expect_message({
l <- readLines(system.file("extdata", "cookies.txt", package = "cookiemonster"))
writeLines(l[5:7], "test.txt")
add_cookies(cookiefile = "test.txt")
}, "not seem to be a valid cookiefile")

expect_error(add_cookies(cookiestring = "test=true; success=yes"),
"provide a domain")

expect_gte({
add_cookies(cookiestring = "test=true; success=yes", domain = "tests.com")
file.size(file.path(tempdir(), paste0("cookies.rds")))
}, 754)
})

0 comments on commit adc48a3

Please sign in to comment.