Skip to content

Commit

Permalink
Fix Windows problems (#565)
Browse files Browse the repository at this point in the history
* fix Windows tests

* use \r\n only on Windows

* avoid using devtools::document()

* don't warn

* use read_lines_enc()

* use AppVeyor

* refine test

* oops

* add comment
  • Loading branch information
krlmlr authored and hadley committed Jan 25, 2017
1 parent 3a0a9b9 commit c1a1e25
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 17 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Expand Up @@ -8,3 +8,4 @@
^roxygen.pro.user$
^revdep$
^codecov\.yml$
^appveyor\.yml$
8 changes: 8 additions & 0 deletions R/enc.R
@@ -0,0 +1,8 @@
read_lines_enc <- function(path, file_encoding = "UTF-8", n = -1L, ok = TRUE, skipNul = FALSE) {
con <- file(path, encoding = file_encoding)
on.exit(close(con), add = TRUE)

lines <- readLines(con, warn = FALSE, n = n, ok = ok, skipNul = skipNul)
Encoding(lines) <- "UTF-8"
lines
}
5 changes: 2 additions & 3 deletions R/parse.R
Expand Up @@ -27,9 +27,8 @@ parse_text <- function(text, registry = default_tags(), global_options = list())

parse_blocks <- function(file, env, registry, global_options = list(), fileEncoding = "UTF-8") {

con <- file(file, encoding = fileEncoding)
on.exit(close(con), add = TRUE)
parsed <- parse(con, keep.source = TRUE, srcfile = srcfile(file, encoding = fileEncoding))
lines <- read_lines_enc(file, file_encoding = fileEncoding)
parsed <- parse(text = lines, keep.source = TRUE, srcfile = srcfilecopy(file, lines, isFile = TRUE))
if (length(parsed) == 0) return()

refs <- utils::getSrcref(parsed)
Expand Down
6 changes: 5 additions & 1 deletion R/source.R
Expand Up @@ -29,7 +29,11 @@ source_package <- function(path) {
}

sys_source <- function(file, envir = baseenv(), fileEncoding = "UTF-8") {
source(file, encoding = fileEncoding, keep.source = FALSE, local = envir)
exprs <- parse(text = read_lines_enc(file, file_encoding = fileEncoding))
for (expr in exprs) {
eval(expr, envir = envir)
}
invisible()
}

# Assume that the package has already been loaded by other means
Expand Down
9 changes: 5 additions & 4 deletions R/utils.R
Expand Up @@ -80,9 +80,7 @@ write_if_different <- function(path, contents, check = TRUE) {
FALSE
} else {
cat(sprintf('Writing %s\n', name))
con <- file(path, encoding = "UTF-8")
on.exit(close(con), add = TRUE)
writeLines(contents, con)
writeLines(contents, path, useBytes = TRUE)
TRUE
}
}
Expand All @@ -91,6 +89,9 @@ same_contents <- function(path, contents) {
if (!file.exists(path)) return(FALSE)

contents <- paste0(paste0(contents, collapse = "\n"), "\n")
if (.Platform$OS.type == "windows") {
contents <- gsub("\n", "\r\n", contents, fixed = TRUE)
}

text_hash <- digest::digest(contents, serialize = FALSE)
file_hash <- digest::digest(file = path)
Expand All @@ -108,7 +109,7 @@ ignore_files <- function(rfiles, path) {
return(rfiles)

# Strip leading directory and slashes
rfiles_relative <- sub(normalizePath(path), "", normalizePath(rfiles), fixed = TRUE)
rfiles_relative <- sub(normalizePath(path, winslash = "/"), "", normalizePath(rfiles, winslash = "/"), fixed = TRUE)
rfiles_relative <- sub("^[/]*", "", rfiles_relative)

# Remove any files that match any perl-compatible regexp
Expand Down
45 changes: 45 additions & 0 deletions appveyor.yml
@@ -0,0 +1,45 @@
# DO NOT CHANGE the "init" and "install" sections below

# Download script file from GitHub
init:
ps: |
$ErrorActionPreference = "Stop"
Invoke-WebRequest http://raw.github.com/krlmlr/r-appveyor/master/scripts/appveyor-tool.ps1 -OutFile "..\appveyor-tool.ps1"
Import-Module '..\appveyor-tool.ps1'
install:
ps: Bootstrap

cache:
- C:\RLibrary

# Adapt as necessary starting from here

build_script:
- travis-tool.sh install_deps

test_script:
- travis-tool.sh run_tests

on_failure:
- 7z a failure.zip *.Rcheck\*
- appveyor PushArtifact failure.zip

artifacts:
- path: '*.Rcheck\**\*.log'
name: Logs

- path: '*.Rcheck\**\*.out'
name: Logs

- path: '*.Rcheck\**\*.fail'
name: Logs

- path: '*.Rcheck\**\*.Rout'
name: Logs

- path: '\*_*.tar.gz'
name: Bits

- path: '\*_*.zip'
name: Bits
3 changes: 2 additions & 1 deletion src/parser2.cpp
Expand Up @@ -149,7 +149,8 @@ List tokenise_block(CharacterVector lines, std::string file = "",
_["file"] = file,
_["line"] = rows[i] + 1,
_["tag"] = tags[i],
_["val"] = stripTrailingNewline(vals[i])
// Rcpp::String() necessary to tag string as UTF-8
_["val"] = Rcpp::String(stripTrailingNewline(vals[i]))
);
out[i].attr("class") = "roxy_tag";
}
Expand Down
11 changes: 6 additions & 5 deletions tests/testthat/test-Rbuildignore.R
@@ -1,11 +1,12 @@
context("Rbuildignore")

test_that("roxygen ignores files with matching pattern in .Rbuildignore", {
test_pkg <- temp_copy_pkg(test_path("testRbuildignore"))
on.exit(unlink(test_pkg, recursive = TRUE))
test_pkg <- temp_copy_pkg(test_path("testRbuildignore"))
on.exit(unlink(test_pkg, recursive = TRUE))

expect_equal(basename(package_files(test_pkg)), c("a.R", "ignore_me.R"))
expect_equal(basename(package_files(test_pkg)), c("a.R", "ignore_me.R"))

writeLines("^R/ignore_me.R$", file.path(test_pkg, ".Rbuildignore"))
expect_equal(basename(package_files(test_pkg)), "a.R")
#writeLines("^R/ignore_me.R$", file.path(test_pkg, ".Rbuildignore"))
writeChar("^R/ignore_me.R$\n", file.path(test_pkg, ".Rbuildignore"), eos = NULL)
expect_equal(basename(package_files(test_pkg)), "a.R")
})
9 changes: 6 additions & 3 deletions tests/testthat/test-nonASCII.R
Expand Up @@ -2,15 +2,18 @@ context("nonASCII")

test_that("can generate nonASCII document", {
test_pkg <- temp_copy_pkg('testNonASCII')
on.exit(unlink(test_pkg, recursive = TRUE))
on.exit(unlink(test_pkg, recursive = TRUE), add = TRUE)

expect_output(roxygenize(test_pkg), "printChineseMsg[.]Rd")
expect_output(roxygenise(test_pkg, roclets = "rd"), "printChineseMsg[.]Rd")
expect_true(file.exists(file.path(test_pkg, "man", "printChineseMsg.Rd")))

cnChar <- readLines(file.path(test_pkg, "man", "printChineseMsg.Rd"), encoding = "UTF-8")
cnChar <- read_lines_enc(file.path(test_pkg, "man", "printChineseMsg.Rd"))

# Because the parse in testthat::test don't specify encoding to UTF-8 as well,
# so we have to use unicode escapes.
expect_true(any(grepl("\u6211\u7231\u4e2d\u6587", cnChar)))
expect_true(any(grepl("\u4e2d\u6587\u6ce8\u91ca", cnChar)))

# No output on second run
expect_output(roxygenise(test_pkg, roclets = "rd"), NA)
})

0 comments on commit c1a1e25

Please sign in to comment.