Skip to content

Commit c74ff19

Browse files
committed
started gist_create_git test file, fixed internals to check that file exists to fail better, #57 fix #61
1 parent 45f9f6a commit c74ff19

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

R/gist_create_git.R

+11-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
#' artifacts of certain file exensions. Default: \code{FALSE}
99
#' @param git_method (character) One of ssh (default) or https. If a remote already
1010
#' exists, we use that remote, and this parameter is ignored.
11+
#' @param sleep (integer) Seconds to sleep after creating gist, but before
12+
#' collecting metadata on the gist. If uploading a lot of stuff, you may want to
13+
#' set this to a higher value, otherwise, you may not get accurate metadata for
14+
#' your gist. You can of course always refresh afterwards by calling \code{gist}
15+
#' with your gist id.
1116
#'
1217
#' @details Note that when \code{browse=TRUE} there is a slight delay in when
1318
#' we open up the gist in your default browser and when the data will display
@@ -118,7 +123,8 @@
118123
gist_create_git <- function(files = NULL, description = "", public = TRUE, browse = TRUE,
119124
knit = FALSE, code = NULL, filename = "code.R",
120125
knitopts=list(), renderopts=list(), include_source = FALSE,
121-
artifacts = FALSE, imgur_inject = FALSE, git_method = "ssh", ...) {
126+
artifacts = FALSE, imgur_inject = FALSE, git_method = "ssh",
127+
sleep = 1, ...) {
122128

123129
if (!requireNamespace("git2r", quietly = TRUE)) {
124130
stop("Please install git2r", call. = FALSE)
@@ -199,6 +205,9 @@ gist_create_git <- function(files = NULL, description = "", public = TRUE, brows
199205
}
200206
}
201207

208+
# wait a bit before collecting metadata
209+
Sys.sleep(sleep)
210+
202211
# refresh gist metadata
203212
gst <- gist(gst$id)
204213
message("The file list for your gist may not be accurate if you are uploading a lot of files")
@@ -225,6 +234,7 @@ makefiles <- function(x) {
225234
}
226235

227236
unpack <- function(z) {
237+
if (!file.exists(z)) stop(sprintf("'%s' does not exist", z), call. = FALSE)
228238
if (file.info(z)$isdir) {
229239
list.files(z, full.names = TRUE)
230240
} else {

man/gist_create_git.Rd

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
gist_create_git(files = NULL, description = "", public = TRUE,
88
browse = TRUE, knit = FALSE, code = NULL, filename = "code.R",
99
knitopts = list(), renderopts = list(), include_source = FALSE,
10-
artifacts = FALSE, imgur_inject = FALSE, git_method = "ssh", ...)
10+
artifacts = FALSE, imgur_inject = FALSE, git_method = "ssh",
11+
sleep = 1, ...)
1112
}
1213
\arguments{
1314
\item{files}{Files to upload}
@@ -46,6 +47,12 @@ anyway. Default: FALSE}
4647
\item{git_method}{(character) One of ssh (default) or https. If a remote already
4748
exists, we use that remote, and this parameter is ignored.}
4849
50+
\item{sleep}{(integer) Seconds to sleep after creating gist, but before
51+
collecting metadata on the gist. If uploading a lot of stuff, you may want to
52+
set this to a higher value, otherwise, you may not get accurate metadata for
53+
your gist. You can of course always refresh afterwards by calling \code{gist}
54+
with your gist id.}
55+
4956
\item{...}{Further args passed on to \code{link[httr]{POST}}}
5057
}
5158
\description{

tests/testthat/test-gist_create_git.R

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
context("gist_create_git")
2+
3+
test_that("gist_create_git works xxx", {
4+
skip_on_cran()
5+
6+
unlink("~/gitgist", recursive = TRUE)
7+
dir.create("~/gitgist")
8+
file <- system.file("examples", "stuff.md", package = "gistr")
9+
writeLines(readLines(file), con = file.path("~/gitgist", "stuff.md"))
10+
11+
aa <- gist_create_git(files = file.path("~/gitgist", "stuff.md"), browse = FALSE)
12+
13+
expect_is(aa, "gist")
14+
expect_named(aa$files, "stuff.md")
15+
expect_is(aa$files$stuff.md$content, "character")
16+
expect_true(aa$public)
17+
18+
# cleanup
19+
invisible(suppressMessages(lapply(list(aa), delete)))
20+
})
21+
22+
test_that("gist_create_git fails well", {
23+
skip_on_cran()
24+
25+
expect_error(gist_create_git(), "invalid 'file' argument")
26+
expect_error(gist_create_git("tttttt"), "'tttttt' does not exist")
27+
})

0 commit comments

Comments
 (0)