Skip to content

Commit

Permalink
test for get_latest()
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruno Rodrigues committed Apr 6, 2024
1 parent 1ba7e5e commit 30871d1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ Depends:
R (>= 2.10)
Imports:
codetools,
curl,
httr,
jsonlite,
sys,
utils
Suggests:
curl,
knitr,
rmarkdown,
testthat
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export(tar_nix_ga)
export(with_nix)
importFrom(codetools,checkUsage)
importFrom(codetools,findGlobals)
importFrom(curl,has_internet)
importFrom(httr,GET)
importFrom(httr,content)
importFrom(httr,http_error)
Expand Down
9 changes: 9 additions & 0 deletions R/get_latest.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
#' @return A character. The commit hash of the latest nixpkgs-unstable revision
#' @importFrom httr content GET stop_for_status
#' @importFrom jsonlite fromJSON
#' @importFrom curl has_internet
#'
#' @noRd
get_latest <- function() {
api_url <- "https://api.github.com/repos/NixOS/nixpkgs/commits?sha=nixpkgs-unstable"

is_online <- curl::has_internet()

if(!is_online){
stop("ERROR! You don't seem to be connected to the internet.")
} else {
tryCatch({
response <- httr::GET(url = api_url)
httr::stop_for_status(response)
Expand All @@ -19,4 +25,7 @@ get_latest <- function() {
cat("Error:", e$message, "\n")
return(NULL)
})

}

}
20 changes: 14 additions & 6 deletions dev/flat_get_latest.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ up-to-date version of R and R packages can do so:
#' @return A character. The commit hash of the latest nixpkgs-unstable revision
#' @importFrom httr content GET stop_for_status
#' @importFrom jsonlite fromJSON
#' @importFrom curl has_internet
#'
#' @examples
get_latest <- function() {
api_url <- "https://api.github.com/repos/NixOS/nixpkgs/commits?sha=nixpkgs-unstable"
is_online <- curl::has_internet()
if(!is_online){
stop("ERROR! You don't seem to be connected to the internet.")
} else {
tryCatch({
response <- httr::GET(url = api_url)
httr::stop_for_status(response)
Expand All @@ -33,17 +39,19 @@ get_latest <- function() {
cat("Error:", e$message, "\n")
return(NULL)
})
}
}
```

```{r, tests-get_latest}
testthat::test_that("get_latest() fails as expected if error", {
is_internet_down <- function() {
!curl::has_internet()
}
testthat::test_that("get_latest() fails gracefully if no internet", {
with_mock(`curl::has_internet` = function(...) FALSE,
expect_error(get_latest(), "You don't seem to be connected")
)
testthat::local_mocked_bindings(is_internet_down = function(...) TRUE)
testthat::expect_error(get_latest())
})
Expand Down
11 changes: 5 additions & 6 deletions tests/testthat/test-get_latest.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# WARNING - Generated by {fusen} from dev/flat_get_latest.Rmd: do not edit by hand

testthat::test_that("get_latest() fails as expected if error", {
is_internet_down <- function() {
!curl::has_internet()
}
testthat::test_that("get_latest() fails gracefully if no internet", {

with_mock(`curl::has_internet` = function(...) FALSE,
expect_error(get_latest(), "You don't seem to be connected")
)

testthat::local_mocked_bindings(is_internet_down = function(...) TRUE)
testthat::expect_error(get_latest())

})

0 comments on commit 30871d1

Please sign in to comment.