1# 2# test-codetools.R 3# 4# Copyright (C) 2021 by RStudio, PBC 5# 6# Unless you have received this program directly from RStudio pursuant 7# to the terms of a commercial license agreement with RStudio, then 8# this program is licensed to you under the terms of version 3 of the 9# GNU Affero General Public License. This program is distributed WITHOUT 10# ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT, 11# MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the 12# AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details. 13# 14# 15 16library(testthat) 17 18context("codetools") 19 20test_that(".rs.CRANDownloadOptionsString() generates a valid R expression", { 21 22 # restore options when done 23 op <- options() 24 on.exit(options(op), add = TRUE) 25 26 # set up options used by download string 27 options( 28 repos = c(CRAN = "https://cran.rstudio.com"), 29 download.file.method = "libcurl", 30 download.file.extra = NULL, 31 HTTPUserAgent = "dummy" 32 ) 33 34 # create a dummy environment that makes it easier for us 35 # to 'capture' the result of an options call 36 envir <- new.env(parent = globalenv()) 37 envir[["options"]] <- base::list 38 39 # check that we construct the right kind of R object after parse 40 # 41 # NOTE: we don't depend on the exact representation of the string as the 42 # code returned by R's deparser might differ from version to version, 43 # but should still produce the same result after evaluation 44 string <- .rs.CRANDownloadOptionsString() 45 actual <- eval(parse(text = string), envir = envir) 46 for (item in c("repos", "download.file.method", "HTTPUserAgent")) 47 expect_equal(actual[[item]], getOption(item)) 48 49 # https://github.com/rstudio/rstudio/issues/6597 50 options(download.file.extra = "embedded 'quotes'") 51 string <- .rs.CRANDownloadOptionsString() 52 actual <- eval(parse(text = string), envir = envir) 53 expect_equal(actual$download.file.extra, "embedded 'quotes'") 54 55 # NOTE: double-quotes are translated to single-quotes here as 56 # a workaround for issues with quotation of arguments when running 57 # commands on Windows 58 options(download.file.extra = "embedded \"quotes\"") 59 string <- .rs.CRANDownloadOptionsString() 60 actual <- eval(parse(text = string), envir = envir) 61 expect_equal(actual$download.file.extra, "embedded 'quotes'") 62 63}) 64 65test_that(".rs.CRANDownloadOptionsString() fills missing CRAN repo", { 66 # read default CRAN URL from user preferences 67 cran <- .rs.readUiPref("cran_mirror")$url 68 69 # unset repos 70 options(repos = NULL) 71 72 # create dummy environment 73 envir <- new.env(parent = globalenv()) 74 envir[["options"]] <- base::list 75 76 # evaluate the download string and confirm that it contains the CRAN URL from user prefs 77 string <- .rs.CRANDownloadOptionsString() 78 actual <- eval(parse(text = string), envir = envir) 79 expect_equal(unlist(actual[["repos"]]["CRAN"]), c(CRAN = cran)) 80}) 81 82 83test_that("HTML escaping escapes HTML entities", { 84 fake_header <- "<h1>Not a real header.</h1>" 85 86 escaped <- .rs.htmlEscape(fake_header) 87 expect_false(grepl(escaped, "<")) 88 expect_false(grepl(escaped, ">")) 89}) 90 91