Skip to content

Commit

Permalink
Merge branch '218-support-file-URLs' into dev
Browse files Browse the repository at this point in the history
* 218-support-file-URLs:
  Add unit test for issue #226
  Move checkpoint temp folder into session temp folder. #225
  Fix broken test. #226
  Fix typo: `require()` not `requires()`
  Added code of conduct
  Update mranUrl.R
  • Loading branch information
andrie committed Oct 26, 2016
2 parents 3a10db9 + b3cc224 commit 89d405a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 24 deletions.
39 changes: 26 additions & 13 deletions R/mranUrl.R
Expand Up @@ -113,13 +113,22 @@ setCheckpointUrl <- function(url){
#'
#' @export
getValidSnapshots <- function(mranRootUrl = mranUrl()){
text <- tryCatch(readLines(mranRootUrl, warn = TRUE), error=function(e)e)
if(inherits(text, "error")) {
stop(sprintf("Unable to download from MRAN: %s", text$message))
con <- url(mranRootUrl)
on.exit(close(con))
if (inherits(con, "file")) {
return(dir(summary(con)$description))
}
else {
text <- tryCatch(readLines(con, warn = TRUE), error = function(e) e)
if (inherits(text, "error")) {
stop(sprintf("Unable to download from MRAN: %s",
text$message))
}
ptn <- "\\d{4}-\\d{2}-\\d{2}"
idx <- grep(ptn, text)
return(gsub(sprintf("^<a href=.*?>(%s).*?</a>.*$", ptn),
"\\1", text[idx]))
}
ptn <- "\\d{4}-\\d{2}-\\d{2}"
idx <- grep(ptn, text)
gsub(sprintf("^<a href=.*?>(%s).*?</a>.*$", ptn), "\\1", text[idx])
}


Expand Down Expand Up @@ -178,13 +187,17 @@ is.404 <- function(mran, warn = TRUE){
}
con <- url(mran)
on.exit(close(con))
x <- suppressWarnings(
tryCatch(readLines(con, warn = FALSE),
error = function(e)e)
)
if(inherits(x, "error")) return(TRUE)
ptn <- "404.*Not Found"
any(grepl(ptn, x))
if(inherits(con, "file")) {
dirPath <- summary(con)$description
return(!dir.exists(dirPath))
} else {
x <- suppressWarnings(tryCatch(readLines(con, warn = FALSE),
error = function(e) e))
if (inherits(x, "error"))
return(TRUE)
ptn <- "404.*Not Found"
any(grepl(ptn, x))
}
}

getSnapshotUrl <- function(snapshotDate, mranRootUrl = mranUrl()){
Expand Down
7 changes: 6 additions & 1 deletion README.md
Expand Up @@ -60,7 +60,7 @@ Then send this script to your collaborators. When they run this script on their
When you create a checkpoint, the `checkpoint()` function performs the following:

- Creates a snapshot folder to install packages. This library folder is located at `~/.checkpoint`
- Scans your project folder for all packages used. Specifically, it searches for all instances of `library()` and `requires()` in your code.
- Scans your project folder for all packages used. Specifically, it searches for all instances of `library()` and `require()` in your code.
- Installs these packages from the MRAN snapshot into your snapshot folder using `install.packages()`
- Sets options for your CRAN mirror to point to a MRAN snapshot, i.e. modify `options(repos)`

Expand Down Expand Up @@ -178,3 +178,8 @@ https://github.com/RevolutionAnalytics/checkpoint-server
### Made by

[Microsoft](https://mran.microsoft.com/)

## Code of conduct

This project has adopted the [Microsoft Open Source Code of Conduct](http://microsoft.github.io/codeofconduct). For more information see the [Code of Conduct FAQ](http://microsoft.github.io/codeofconduct/faq.md) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.

16 changes: 7 additions & 9 deletions tests/testthat/test-3-checkpoint.R
Expand Up @@ -3,8 +3,6 @@ if(interactive()) library(testthat)

Sys.setenv("R_TESTS" = "") # Configure Travis for tests https://github.com/RevolutionAnalytics/checkpoint/issues/139

# MRAN.start = as.Date("2014-09-17")

current.R <- local({ x = getRversion(); paste(x$major, x$minor, sep=".")})

test.start <- switch(current.R,
Expand All @@ -16,10 +14,10 @@ test.start <- switch(current.R,

MRAN.default = test.start[1]

packages.to.test.base = c("MASS", "plyr", "httr", "XML", "checkpoint", "stats", "stats4", "compiler")
packages.to.test.base = c("MASS", "chron", "checkpoint", "stats", "stats4", "compiler")
packages.to.test.knitr = c("foreach")
checkpointLocation = dirname(tempdir())
packages.to.test.base <- c("MASS", "plyr", "httr", "XML", "checkpoint", "stats", "stats4", "compiler")
packages.to.test.base <- c("MASS", "chron", "checkpoint", "stats", "stats4", "compiler")
packages.to.test.knitr <- c("foreach")
checkpointLocation <- tempdir()
dir.create(file.path(checkpointLocation, ".checkpoint"), showWarnings = FALSE)


Expand Down Expand Up @@ -87,13 +85,13 @@ test_checkpoint <- function(https = FALSE, snap.dates){
it("does not display message whan scanForPackages=FALSE", {
expect_false(
isTRUE(
shows_message("Scanning for packages used in this project")(
checkpoint(snap_date, checkpointLocation = checkpointLocation,
shows_message("Scanning for packages used in this project",
checkpoint(snap_date, checkpointLocation = checkpointLocation,
project = project_root, scanForPackages=FALSE)
)
))
})

it("installs all packages correctly in local lib", {
pdbMRAN <- available.packages(contriburl = contrib.url(repos = getSnapshotUrl(snap_date)))
pdbLocal <- installed.packages(fields = "Date/Publication", noCache = TRUE)
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/test-4-tempdir.R
@@ -0,0 +1,16 @@
if(interactive()) library(testthat)

context("tempdir")

# CRAN policy does not allow any folders to be written anywhere other than the session temporary folder tempdir().
# Ensure that no .checkpoint files are written one level higher in the tempdir() tree

test_that("checkpointLocation uses session temp folder", {
expect_char0 <- function(object){
expect_is(object, "character")
expect_length(object, 0)
}
expect_char0(
dir(dirname(tempdir()), all.files = TRUE, pattern = ".checkpoint")
)
})
2 changes: 1 addition & 1 deletion vignettes/checkpoint.Rmd
Expand Up @@ -46,7 +46,7 @@ Using `checkpoint` is simple:
When you create a checkpoint, the `checkpoint()` function performs the following:

- Creates a snapshot folder to install packages. This library folder is located at `~/.checkpoint`
- Scans your project folder for all packages used. Specifically, it searches for all instances of `library()` and `requires()` in your code.
- Scans your project folder for all packages used. Specifically, it searches for all instances of `library()` and `require()` in your code.
- Installs these packages from the MRAN snapshot into your snapshot folder using `install.packages()`
- Sets options for your CRAN mirror to point to a MRAN snapshot, i.e. modify `options(repos)`

Expand Down

0 comments on commit 89d405a

Please sign in to comment.