Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP for reprex_scrape() #199

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ Suggests:
rstudioapi,
shiny,
styler (>= 1.0.2),
testthat (>= 2.0.0)
testthat (>= 2.0.0),
gh
VignetteBuilder: knitr
Encoding: UTF-8
LazyData: true
Expand Down
82 changes: 77 additions & 5 deletions R/reprex-undo.R
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,82 @@ reprex_rescue <- function(input = NULL,
)
}

reprex_scrape <- function(input,
outfile = NULL,
comment = opt("#>")) {
venue <- id_venue(input)
src <- switch(
venue,
"gh" = pull_gh_issue(input),
"so" = pull_so_question(input)
)
src <- strsplit(src, "\r?\n")[[1]]

reprex_undo(
src,
is_md = TRUE,
venue = venue,
outfile = outfile,
comment = comment
)
}

id_venue <- function(input) {
if (grepl("^https?://stackoverflow\\.com", input)) {
"so"
} else if (grepl("^https?://github\\.com/", input)) {
"gh"
} else {
stop(
"`input` needs to be a URL to a GitHub issue or Stack Overflow question",
call. = FALSE
)
}
}

pull_gh_issue <- function(input) {

if (!requireNamespace("gh", quietly = TRUE)) {
stop(
"Install the `gh` package in order to use `reprex_scrape()` ",
"with GitHub issues", call. = FALSE
)
}

no_host <- sub("^https?://github\\.com/", "", input)
paths <- strsplit(no_host, "/")[[1]]
if (!(identical(paths[3], "issues") && length(paths) == 4)) {
stop(
"GitHub URLs need to point to an issue and be in the form:\n",
"https://github.com/tidyverse/reprex/issues/168",
call. = FALSE
)
}

resp <- gh::gh(
"/repos/:owner/:repo/issues/:number",
owner = paths[1], repo = paths[2], number = paths[4]
)
resp[["body"]]
}

# stub
pull_so_question <- function(input) {
txt <- c(
"I would like to extract `\"/arsenal-vs-man-city/\"` from",
"",
"<!-- language-all: lang-r -->",
"",
" library(stringr)",
" str_extract_all(\"/sports/football/arsenal-vs-man-city/stats/\", \"/.*?-vs-.*?/\")",
" #> [[1]]",
" #> [1] \"/sports/football/arsenal-vs-man-city/\"",
"",
"I'd like to know what the correct way to do this is and also why my way is wrong."
)
paste0(txt, collapse = "\n")
}

reprex_undo <- function(input = NULL,
outfile = NULL,
venue,
Expand Down Expand Up @@ -232,9 +308,5 @@ classify_lines <- function(x, comment = "^#>") {
wut <- ifelse(wut == "code" & grepl(comment, x), "output", wut)

so_special <- "<!-- language-all: lang-r -->"
if (identical(x[1], so_special)) {
wut[1] <- "so_header"
}

wut
ifelse(x == so_special, "so_header", wut)
}
2 changes: 1 addition & 1 deletion tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ expect_error_free <- function(...) {

## set wd to session temp dir, execute testing code, restore previous wd
temporarily <- function(env = parent.frame()) {
withr::local_dir(path_temp(), .local_envir = env)
withr::local_dir(fs::path_temp(), .local_envir = env)
}

## call during interactive test development to fake being "in tests" and thereby
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-filepaths.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ test_that("make_filebase() defaults to 'reprex' inside a dir inside tempdir", {
x <- make_filebase(outfile = NULL, infile = NULL)
expect_equal(fs::path_file(x), "reprex")
expect_match(fs::path_file(fs::path_dir(x)), "^reprex")
temp <- fs::path_real(path_temp())
temp <- fs::path_real(fs::path_temp())
expect_identical(fs::path_common(c(x, temp)), temp)
})

Expand All @@ -22,9 +22,9 @@ test_that("make_filebase() works from relative infile, outfile", {
test_that("make_filebase() works from absolute infile, outfile", {
x <- make_filebase(outfile = NA, infile = fs::path_temp("abcde"))
expect_match(fs::path_file(x), "^abcde")
expect_equal(fs::path_dir(x), path_temp())
expect_equal(fs::path_dir(x), fs::path_temp())

x <- make_filebase(outfile = fs::path_temp("abcde"))
expect_match(fs::path_file(x), "^abcde")
expect_equal(fs::path_dir(x), path_temp())
expect_equal(fs::path_dir(x), fs::path_temp())
})
2 changes: 1 addition & 1 deletion tests/testthat/test-input.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ test_that("newlines in code are protected and uniformly so across venues", {
input <- 'paste(letters[1:3], collapse = "\n")\n'
chr_input <- reprex(input = input, render = FALSE)

input_file <- path_temp("foo.R")
input_file <- fs::path_temp("foo.R")
withr::local_file(input_file)
writeLines(
escape_newlines('paste(letters[1:3], collapse = "\n")'),
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-undo.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,17 @@ test_that("reprex_invert() can name outfile based on input filepath", {
out <- reprex_invert(input = "a_reprex.md", outfile = NA)
expect_identical(readLines("a_reprex_clean.R"), out)
})

test_that("reprex_scrap() outputs the expected lines", {
skip_on_cran()
expected <- c(
"#' This is a sample reprex.",
"# adding two vectors",
"x <- 1:4",
"y <- 2:5",
"x + y",
"#' Created on 2018-08-31 by the [reprex package](http://reprex.tidyverse.org) (v0.2.0)."
)
out <- reprex_scrape("https://github.com/r-lib/ghurl/issues/7")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this the use of ghurl that you had in mind?

expect_identical(expected, out)
})
2 changes: 1 addition & 1 deletion tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ context("utils")

test_that("locate_input() works", {
expect_identical("clipboard", locate_input(NULL))
expect_identical("path", locate_input(path_temp()))
expect_identical("path", locate_input(fs::path_temp()))
expect_identical("input", locate_input(c("a", "b")))
expect_identical("input", locate_input("a\n"))
})
Expand Down