reg-packages.R 3.86 KB
Newer Older
Radford Neal's avatar
Radford Neal committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
## PR 1271  detach("package:base") crashes R.
try(detach("package:base"))


## invalid 'lib.loc'
stopifnot(length(installed.packages("mgcv")) == 0)
## gave a low-level error message


## package.skeleton() with metadata-only code
## work in current (= ./tests/ directory):
tmp <- tempfile()
writeLines(c('setClass("foo", contains="numeric")',
             'setMethod("show", "foo",',
             '          function(object) cat("I am a \\"foo\\"\\n"))'),
           tmp)
if(file.exists("myTst")) unlink("myTst", recursive=TRUE)
package.skeleton("myTst", code_files = tmp)# with a file name warning
file.copy(tmp, (tm2 <- paste(tmp,".R", sep="")))
unlink("myTst", recursive=TRUE)
op <- options(warn=2) # *NO* "invalid file name" warning {failed in 2.7.[01]}:
package.skeleton("myTst", code_files = tm2, namespace=TRUE)
options(op)
##_2_ only a class, no generics/methods:
writeLines(c('setClass("DocLink",',
             'representation(name="character",',
             '               desc="character"))'), tmp)
if(file.exists("myTst2")) unlink("myTst2", recursive=TRUE)
package.skeleton("myTst2", code_files = tmp)
##- end_2_ # failed in R 2.11.0
stopifnot(1 == grep("setClass",
	  readLines(list.files("myTst/R", full.names=TRUE))),
	  c("foo-class.Rd","show-methods.Rd") %in% list.files("myTst/man"))
## failed for several reasons in R < 2.7.0
##
## Part 2: -- build, install, load and "inspect" the package:
dir.exists <- function(x)
    is.character(x) && file.exists(x) && file.info(path.expand(x))$isdir
build.pkg <- function(dir) {
    stopifnot(dir.exists(dir))
Radford Neal's avatar
Radford Neal committed
41 42
    patt <- paste(basename(dir), ".*tar\\.gz$", sep="_")
    unlink(dir('.', pattern = patt))
Radford Neal's avatar
Radford Neal committed
43
    Rcmd <- paste(file.path(R.home("bin"), "R"), "CMD")
Radford Neal's avatar
Radford Neal committed
44 45
    r <- tail(system(paste(Rcmd, "build --keep-empty-dirs", dir),
                     intern = TRUE), 3)
Radford Neal's avatar
Radford Neal committed
46
    ## return name of tar file built
Radford Neal's avatar
Radford Neal committed
47
    dir('.', pattern = patt)
Radford Neal's avatar
Radford Neal committed
48 49 50 51 52 53 54 55
}
build.pkg("myTst")
## clean up any previous attempt (which might have left a 00LOCK)
unlink("myLib", recursive = TRUE)
dir.create("myLib")
install.packages("myTst", lib = "myLib", repos=NULL, type = "source") # with warnings
print(installed.packages(lib.loc= "myLib", priority= "NA"))## (PR#13332)
stopifnot(require("myTst",lib = "myLib"))
Radford Neal's avatar
Radford Neal committed
56 57
sm <- findMethods(show, where= as.environment("package:myTst"))
stopifnot(names(sm@names) == "foo")
Radford Neal's avatar
Radford Neal committed
58 59 60 61 62 63 64 65 66
unlink("myTst_*")

## More building & installing packages
## NB: tests were added here for 2.11.0.
## NB^2: do not do this in the R sources!
## and this testdir is not installed.
pkgSrcPath <- file.path(Sys.getenv("SRCDIR"), "Pkgs")
if(file_test("-d", pkgSrcPath)) {
    ## could use file.copy(recursive = TRUE)
Radford Neal's avatar
Radford Neal committed
67
    system(paste('cp -r', shQuote(pkgSrcPath), shQuote(tempdir())))
Radford Neal's avatar
Radford Neal committed
68
    pkgPath <- file.path(tempdir(), "Pkgs")
Radford Neal's avatar
Radford Neal committed
69
#    op <- options(warn = 2)    # There should be *NO* warnings here!
Radford Neal's avatar
Radford Neal committed
70 71 72
    ## pkgB tests an empty R directory
    dir.create(file.path(pkgPath, "pkgB", "R"), recursive = TRUE,
               showWarnings = FALSE)
Radford Neal's avatar
Radford Neal committed
73
    p.lis <- c("pkgA", "pkgB", "exNSS4")
Radford Neal's avatar
Radford Neal committed
74 75 76 77 78 79 80 81 82 83
    for(p. in p.lis) {
        cat("building package", p., "...\n")
        r <- build.pkg(file.path(pkgPath, p.))
        cat("installing package", p., "using file", r, "...\n")
        ## we could install the tar file ... (see build.pkg()'s definition)
        install.packages(r, lib = "myLib", repos=NULL, type = "source")
        stopifnot(require(p.,lib = "myLib", character.only=TRUE))
        detach(pos = match(p., sub("^package:","", search())))
    }
    ## TODO: not just print, but check the "list":
Radford Neal's avatar
Radford Neal committed
84 85
    res <- installed.packages(lib.loc = "myLib", priority = "NA")
    print(res)
Radford Neal's avatar
Radford Neal committed
86
#    options(op)
Radford Neal's avatar
Radford Neal committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100
    unlink("myLib", recursive = TRUE)
    unlink(file.path(pkgPath), recursive = TRUE)
}
unlink("myTst", recursive=TRUE)


## getPackageName()  for "package:foo":
require('methods')
library(tools)
oo <- options(warn=2)
detach("package:tools", unload=TRUE)
options(oo)
## gave warning (-> Error) about creating package name