From 2eb95ee757e2ff6e6ae7ae254b05b80294a79bef Mon Sep 17 00:00:00 2001 From: kpiyush04 <04kpiyush@gmail.com> Date: Wed, 14 Jul 2021 20:20:36 +0530 Subject: [PATCH 1/3] updated the post_bug fucntion and added testcases to test post_bug fucntion --- DESCRIPTION | 7 +++++-- NAMESPACE | 2 ++ R/post_bug.R | 31 ++++++++++++++++++------------- man/post_bug.Rd | 5 +---- tests/testthat/_snaps/post_bug.md | 5 +++++ tests/testthat/test-post_bug.R | 11 +++++++++++ 6 files changed, 42 insertions(+), 19 deletions(-) create mode 100644 tests/testthat/_snaps/post_bug.md create mode 100644 tests/testthat/test-post_bug.R diff --git a/DESCRIPTION b/DESCRIPTION index ca85fd9..1346ef7 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -17,14 +17,17 @@ Imports: httr, methods, utils, - tools + tools, + jsonlite, + reshape2 Suggests: covr, knitr, reprex, rmarkdown, testthat (>= 3.0.0), - vcr + vcr, + mockr VignetteBuilder: knitr Config/testthat/edition: 3 diff --git a/NAMESPACE b/NAMESPACE index 8fb5035..d922666 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -12,6 +12,8 @@ export(get_history) export(post_bug) export(post_comment) export(set_key) +import(jsonlite) +import(reshape2) importFrom(cli,cat_rule) importFrom(cli,cli_alert) importFrom(cli,cli_alert_danger) diff --git a/R/post_bug.R b/R/post_bug.R index 8eebc36..6307f01 100644 --- a/R/post_bug.R +++ b/R/post_bug.R @@ -8,8 +8,8 @@ components <- c("Accuracy", "Add-ons", "Analyses", "Documentation", "Graphics", #' #' Guides through the process of creating an issue. #' Requires an user and an API key. -#' @param text A character vector with the text of the bug you want to -#' open. +# #' @param text A character vector with the text of the bug you want to +# #' open. #' @param title A character vector with the title of the bug. #' @param component A character with the component of R you want to fill an issue with. #' @param version A character of the Version you want to use eg "R 4.0.0". @@ -18,14 +18,17 @@ components <- c("Accuracy", "Add-ons", "Analyses", "Documentation", "Graphics", #' @param ... Named arguments passed to the API [check documentation](https://bugzilla.readthedocs.io/en/latest/api/core/v1/bug.html) #' @inheritParams create_bugzilla_key #' @importFrom utils menu +#' @import jsonlite +#' @import reshape2 #' @importFrom cli cli_alert #' @export #' @seealso To obtain and use the API key see create_bugzilla_key(). #' [Webpage](https://bugs.r-project.org/bugzilla/enter_bug.cgi) for manual entry #' @return The ID of the issue posted. -post_bug <- function(text, title, component, ..., - version, product, host, key) { +post_bug <- function(title, component, ..., + version, product, host, key) { # Provide some checks/questions to the users + text <- readline(prompt = "Bug Title: ") # Fill description, version and summary if (missing(component)) { cli::cli_alert("Please, pick a component:") @@ -46,13 +49,15 @@ post_bug <- function(text, title, component, ..., } ask_final_confirmation() url <- paste0(host, "rest/bug") - # bugs <- httr::POST(url, - # description = text, - # product = product, - # component = component, - # summary = title, - # version = version, - # ..., - # headers) - # bugs <- httr::content(bugs) + data <- list( + product = product, + component = component, + version = version, + summary = text + ) + bugs <- httr::POST(url, body = jsonlite::toJSON(data, pretty = TRUE, auto_unbox = TRUE), ..., + httr::add_headers(`accept` = 'application/json'), + httr::content_type('application/json')) + bugs <- httr::content(bugs) + return(bugs) } diff --git a/man/post_bug.Rd b/man/post_bug.Rd index 7d03940..d8fb16c 100644 --- a/man/post_bug.Rd +++ b/man/post_bug.Rd @@ -4,12 +4,9 @@ \alias{post_bug} \title{Open an issue.} \usage{ -post_bug(text, title, component, ..., version, product, host, key) +post_bug(title, component, ..., version, product, host, key) } \arguments{ -\item{text}{A character vector with the text of the bug you want to -open.} - \item{title}{A character vector with the title of the bug.} \item{component}{A character with the component of R you want to fill an issue with.} diff --git a/tests/testthat/_snaps/post_bug.md b/tests/testthat/_snaps/post_bug.md new file mode 100644 index 0000000..9429828 --- /dev/null +++ b/tests/testthat/_snaps/post_bug.md @@ -0,0 +1,5 @@ +# post_bug() works [plain] + + Code + expect_type(post_bug, "closure") + diff --git a/tests/testthat/test-post_bug.R b/tests/testthat/test-post_bug.R new file mode 100644 index 0000000..a6bd4f2 --- /dev/null +++ b/tests/testthat/test-post_bug.R @@ -0,0 +1,11 @@ +local({ + with_mock(post_bug, post_bug()) + cli::test_that_cli(configs = "plain", "post_bug() works", { + skip_if_not(interactive()) + expect_snapshot( + expect_type(post_bug, "closure") + # expect_error(post_bug(), "Do it and then return please."), + # expect_error(post_bug(), "Do it, later if you are still sure it is a bug then return please.") + ) + }) +}) From 69fe8daa5c2c3f201dc6abc56ef93dcafa87dc34 Mon Sep 17 00:00:00 2001 From: kpiyush04 <04kpiyush@gmail.com> Date: Thu, 15 Jul 2021 22:35:48 +0530 Subject: [PATCH 2/3] updated post_bug fucntion and the testcases --- DESCRIPTION | 7 ++----- NAMESPACE | 2 -- R/post_bug.R | 14 +++++-------- tests/fixtures/post_bug_works.yml | 35 +++++++++++++++++++++++++++++++ tests/testthat/_snaps/post_bug.md | 2 +- tests/testthat/test-post_bug.R | 25 ++++++++++++++-------- 6 files changed, 59 insertions(+), 26 deletions(-) create mode 100644 tests/fixtures/post_bug_works.yml diff --git a/DESCRIPTION b/DESCRIPTION index 1346ef7..ca85fd9 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -17,17 +17,14 @@ Imports: httr, methods, utils, - tools, - jsonlite, - reshape2 + tools Suggests: covr, knitr, reprex, rmarkdown, testthat (>= 3.0.0), - vcr, - mockr + vcr VignetteBuilder: knitr Config/testthat/edition: 3 diff --git a/NAMESPACE b/NAMESPACE index d922666..8fb5035 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -12,8 +12,6 @@ export(get_history) export(post_bug) export(post_comment) export(set_key) -import(jsonlite) -import(reshape2) importFrom(cli,cat_rule) importFrom(cli,cli_alert) importFrom(cli,cli_alert_danger) diff --git a/R/post_bug.R b/R/post_bug.R index 6307f01..dfdabc3 100644 --- a/R/post_bug.R +++ b/R/post_bug.R @@ -18,8 +18,6 @@ components <- c("Accuracy", "Add-ons", "Analyses", "Documentation", "Graphics", #' @param ... Named arguments passed to the API [check documentation](https://bugzilla.readthedocs.io/en/latest/api/core/v1/bug.html) #' @inheritParams create_bugzilla_key #' @importFrom utils menu -#' @import jsonlite -#' @import reshape2 #' @importFrom cli cli_alert #' @export #' @seealso To obtain and use the API key see create_bugzilla_key(). @@ -28,7 +26,7 @@ components <- c("Accuracy", "Add-ons", "Analyses", "Documentation", "Graphics", post_bug <- function(title, component, ..., version, product, host, key) { # Provide some checks/questions to the users - text <- readline(prompt = "Bug Title: ") + title <- readline(prompt = "Bug Title: ") # Fill description, version and summary if (missing(component)) { cli::cli_alert("Please, pick a component:") @@ -48,16 +46,14 @@ post_bug <- function(title, component, ..., return() } ask_final_confirmation() - url <- paste0(host, "rest/bug") + url <- paste0(host, "rest/bug?") data <- list( product = product, component = component, version = version, - summary = text + summary = title ) - bugs <- httr::POST(url, body = jsonlite::toJSON(data, pretty = TRUE, auto_unbox = TRUE), ..., - httr::add_headers(`accept` = 'application/json'), - httr::content_type('application/json')) + bugs <- httr::POST(url, body = data, encode = "json") bugs <- httr::content(bugs) - return(bugs) + print(bugs) } diff --git a/tests/fixtures/post_bug_works.yml b/tests/fixtures/post_bug_works.yml new file mode 100644 index 0000000..a16868a --- /dev/null +++ b/tests/fixtures/post_bug_works.yml @@ -0,0 +1,35 @@ +http_interactions: +- request: + method: post + uri: https://bugs.r-project.org/bugzilla/rest/bug + body: + encoding: '' + string: '{"product":"R","component":"Misc","version":"4.0.x","summary":"test!!!"}' + headers: + Accept: application/json, text/xml, application/xml, */* + Content-Type: application/json + response: + status: + status_code: 401 + category: Client error + reason: Unauthorized + message: 'Client error: (401) Unauthorized' + headers: + date: Thu, 15 Jul 2021 17:00:35 GMT + server: gazelle + etag: 8hv3qhJNknTVRt2cjTD2dw + content-length: '157' + content-type: application/json; charset=UTF-8 + access-control-allow-headers: accept, content-type, origin, user-agent, x-requested-with, + x-bugzilla-api-key, x-bugzilla-password, x-bugzilla-token, x-bugzilla-login + access-control-allow-origin: '*' + x-content-type-options: nosniff + x-frame-options: SAMEORIGIN + x-xss-protection: 1; mode=block + body: + encoding: UTF-8 + file: no + string: '{"code":"410","message":"You must log in before using this part of + Bugzilla.","error":true,"documentation":"https://bugzilla.readthedocs.org/en/latest/api/"}' + recorded_at: 2021-07-15 17:00:35 GMT + recorded_with: vcr/1.0.2, webmockr/0.8.0 diff --git a/tests/testthat/_snaps/post_bug.md b/tests/testthat/_snaps/post_bug.md index 9429828..9170eb5 100644 --- a/tests/testthat/_snaps/post_bug.md +++ b/tests/testthat/_snaps/post_bug.md @@ -1,5 +1,5 @@ # post_bug() works [plain] Code - expect_type(post_bug, "closure") + expect_type(pg, "list") diff --git a/tests/testthat/test-post_bug.R b/tests/testthat/test-post_bug.R index a6bd4f2..6fb65a8 100644 --- a/tests/testthat/test-post_bug.R +++ b/tests/testthat/test-post_bug.R @@ -1,11 +1,18 @@ -local({ - with_mock(post_bug, post_bug()) - cli::test_that_cli(configs = "plain", "post_bug() works", { - skip_if_not(interactive()) - expect_snapshot( - expect_type(post_bug, "closure") - # expect_error(post_bug(), "Do it and then return please."), - # expect_error(post_bug(), "Do it, later if you are still sure it is a bug then return please.") - ) +cli::test_that_cli(configs = "plain", "post_bug() works", { + skip_if_not(interactive()) + vcr::use_cassette("post_bug_works", { + pg <- post_bug() }) + expect_snapshot( + expect_type(pg, "list") + ) }) + + +# cli::test_that_cli(configs = "plain", "post_bug() fails", { +# skip_if_not(interactive()) +# pg1 <- post_bug() +# expect_snapshot( +# expect_error(pg1, "Do it and then return please.") +# ) +# }) From 3938b963acec6560047be5058375cff6d3521bdf Mon Sep 17 00:00:00 2001 From: kpiyush04 <04kpiyush@gmail.com> Date: Thu, 22 Jul 2021 15:29:44 +0530 Subject: [PATCH 3/3] updated the fucntion and added the testcases --- R/post_bug.R | 18 +++++++++-------- man/post_bug.Rd | 5 ++++- tests/testthat/_snaps/post_bug.md | 33 +++++++++++++++++++++++++++++++ tests/testthat/test-post_bug.R | 15 +++++++------- 4 files changed, 54 insertions(+), 17 deletions(-) diff --git a/R/post_bug.R b/R/post_bug.R index dfdabc3..c8c5537 100644 --- a/R/post_bug.R +++ b/R/post_bug.R @@ -8,8 +8,8 @@ components <- c("Accuracy", "Add-ons", "Analyses", "Documentation", "Graphics", #' #' Guides through the process of creating an issue. #' Requires an user and an API key. -# #' @param text A character vector with the text of the bug you want to -# #' open. +#' @param text A character vector with the text of the bug you want to +#' open. #' @param title A character vector with the title of the bug. #' @param component A character with the component of R you want to fill an issue with. #' @param version A character of the Version you want to use eg "R 4.0.0". @@ -23,10 +23,8 @@ components <- c("Accuracy", "Add-ons", "Analyses", "Documentation", "Graphics", #' @seealso To obtain and use the API key see create_bugzilla_key(). #' [Webpage](https://bugs.r-project.org/bugzilla/enter_bug.cgi) for manual entry #' @return The ID of the issue posted. -post_bug <- function(title, component, ..., +post_bug <- function(text, title, component, ..., version, product, host, key) { - # Provide some checks/questions to the users - title <- readline(prompt = "Bug Title: ") # Fill description, version and summary if (missing(component)) { cli::cli_alert("Please, pick a component:") @@ -51,9 +49,13 @@ post_bug <- function(title, component, ..., product = product, component = component, version = version, - summary = title + summary = title, + description = text ) - bugs <- httr::POST(url, body = data, encode = "json") + bugs <- httr::POST(url, body = data, encode = "json", ..., + add_headers(.headers = c( + "Content-Type"="application/json", + "Ocp-Apim-Subscription-Key"="my_subscrition_key"))) bugs <- httr::content(bugs) - print(bugs) + return(bugs) } diff --git a/man/post_bug.Rd b/man/post_bug.Rd index d8fb16c..7d03940 100644 --- a/man/post_bug.Rd +++ b/man/post_bug.Rd @@ -4,9 +4,12 @@ \alias{post_bug} \title{Open an issue.} \usage{ -post_bug(title, component, ..., version, product, host, key) +post_bug(text, title, component, ..., version, product, host, key) } \arguments{ +\item{text}{A character vector with the text of the bug you want to +open.} + \item{title}{A character vector with the title of the bug.} \item{component}{A character with the component of R you want to fill an issue with.} diff --git a/tests/testthat/_snaps/post_bug.md b/tests/testthat/_snaps/post_bug.md index 9170eb5..854518f 100644 --- a/tests/testthat/_snaps/post_bug.md +++ b/tests/testthat/_snaps/post_bug.md @@ -3,3 +3,36 @@ Code expect_type(pg, "list") +# post_bug() fails [plain] + + Code + expect_error(post_bug("This is the test description", "TEST!!!"), + "Do it and then return please.") + Message + > Please, pick a component: + Output + + 1: Accuracy 2: Add-ons + 3: Analyses 4: Documentation + 5: Graphics 6: I/O + 7: Installation 8: Language + 9: Low-level 10: Mac GUI/Mac specific + 11: Misc 12: Models + 13: S4methods 14: Startup + 15: System-specific 16: Translations + 17: Windows GUI/Window specific 18: Wishlist + + + -- Documentation --------------------------------------------------------------- + Message + > Have you read the documentation recently? + Output + (0 to cancel) + + 1: Yup + 2: Negative + 3: Nope + + Message + v Opening documentation... + diff --git a/tests/testthat/test-post_bug.R b/tests/testthat/test-post_bug.R index 6fb65a8..06f3d59 100644 --- a/tests/testthat/test-post_bug.R +++ b/tests/testthat/test-post_bug.R @@ -1,7 +1,7 @@ cli::test_that_cli(configs = "plain", "post_bug() works", { skip_if_not(interactive()) vcr::use_cassette("post_bug_works", { - pg <- post_bug() + pg <- post_bug("This is the test description", "TEST!!!") }) expect_snapshot( expect_type(pg, "list") @@ -9,10 +9,9 @@ cli::test_that_cli(configs = "plain", "post_bug() works", { }) -# cli::test_that_cli(configs = "plain", "post_bug() fails", { -# skip_if_not(interactive()) -# pg1 <- post_bug() -# expect_snapshot( -# expect_error(pg1, "Do it and then return please.") -# ) -# }) +cli::test_that_cli(configs = "plain", "post_bug() fails", { + skip_if_not(interactive()) + expect_snapshot( + expect_error(post_bug("This is the test description", "TEST!!!"), "Do it and then return please.") + ) +})