Debian Codegraph @

inst/share/R/checkCode.r

raw symbols SCIP
1 ######################################################################
2 ## RUnit : A unit test framework for the R programming language
3 ## Copyright (C) 2003-2009 Thomas Koenig, Matthias Burger, Klaus Juenemann
4 ##
5 ## This program is free software; you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; version 2 of the License.
8 ##
9 ## This program is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details.
13 ##
14 ## You should have received a copy of the GNU General Public License
15 ## along with this program; if not, write to the Free Software
16 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18 ## $Id$
19
20
21 checkCodeFiles <- function(fileName) {
22 ##@bdescr
23 ## utility function for code checks of files outside usual R/ folder structure
24 ## requires package codetools
25 ##@edescr
26 ##
27 ##@in fileName : [character] vector of file names (including path, relative to pwd or absolute)
28 ##@ret : [list] with elements per function, that incurred any warning
29 ##
30 ##@depends : codetools
31 ##
32 ##@codestatus : untested
33
34
35 ## return list
36 retList <- list()
37
38 ## initialized before ls() call to avoid listing
39 ok <- x <- c()
40 tmpRetEnv <- new.env()
41 tmpRet <- NULL
42
43 ## generate listing of existing objects before first source'ing
44 lsTmp <- lsInit <- ls()
45
46 sapply(fileName, function(x) {
47 cat("\n file ",x)
48
49 ok <- try(utils::capture.output(source(x, local=TRUE, echo=FALSE)))
50 if (inherits(ok, "try-error")) {
51 cat("\n file",x,"could not be sourced:", geterrmessage(), "\n")
52 return()
53 }
54 newElements <- setdiff(ls(), lsTmp)
55 cat("\n functions",paste(newElements, collapse=", "))
56 lsTmp <- ls()
57 sapply(newElements, function(x) {
58 ok <- try(get(x))
59 if (!inherits(ok, "try-error") && identical(mode(ok), "function")) {
60 cat("\n ",x," (",is(ok)[1],"): ",sep="")
61
62 ## this function will be used in signalUsageIssue w$warn
63 reportFunc <- function(x) {
64 cat(x)
65 assign("tmpRet", c(tmpRet, x), pos=parent.env(tmpRetEnv))
66 }
67 codetools::checkUsage(ok, report=reportFunc, all=TRUE)
68
69 if (!is.null(tmpRet)) {
70 retList[[length(retList) + 1]] <<- tmpRet
71 names(retList)[length(retList)] <<- x
72 }
73 tmpRet <<- NULL
74 }
75 })
76 })
77
78 return(invisible(retList))
79 }
80
81
82 checkCodeFolders <- function(path=".") {
83 ##@bdescr
84 ## utility function
85 ## code checks of all .[RrSs] files found in one or more folders
86 ## requires package codetools
87 ##@edescr
88 ##
89 ##@in path : [character]
90 ##@ret : [list]
91 ##
92 ##@depends : codetools
93 ##
94 ##@codestatus : untested
95
96 stopifnot(require(codetools))
97 if (!is(path, "character")) {
98 stop("argument 'path' has to be of type 'character'.")
99 }
100 if (!all(file.exists(path))) {
101 stop("argument 'path' has to contain existing folder(s).")
102 }
103
104
105 fNames <- list.files(path=path, pattern="\\.[rRsS]$", full.names=TRUE)
106
107 checkCodeFiles(fNames)
108 }
109
110
111 checkCodeSweave <- function(path=".") {
112 ##@bdescr
113 ## utility function
114 ## code checks of all .[RS]nw files found in one or more folders
115 ## experimental: does not convert extracted code chunks to closures
116 ## thus only functions defined inside a chunk but nut all of the chunk code is checked
117 ##
118 ## requires package codetools
119 ##@edescr
120 ##
121 ##@in path : [character]
122 ##@ret : [list]
123 ##@depends : codetools
124 ##
125 ##@codestatus : untested
126
127 ## Issue:
128 ## local path e.g. 'RUnit/inst/doc'
129 ## is no expanded to full path
130 ## which I would wnat to use as absolute path
131 ## in the Stangle call
132
133 stopifnot(require(utils))
134 stopifnot(require(codetools))
135
136 if (!is(path, "character")) {
137 stop("argument 'path' has to be of type 'character'.")
138 }
139 if (!all(file.exists(path))) {
140 stop("argument 'path' has to contain existing folder(s).")
141 }
142 browser()
143
144 path <- path.expand(path)
145 pwd <- getwd()
146 if (length(path)) {
147 if (path == ".") {
148 path <- pwd
149 }
150 ## do we have a local path rather then an absolute
151 ## how to infer correct absolute path?
152 }
153
154 fName <- list.files(path=path, pattern="\\.[RS]nw$", full.names=TRUE)
155
156 timeStamp <- format(Sys.time(), "%y%m%d-%H%M")
157 tmpDir <- file.path(tempdir(), timeStamp)
158 print(tmpDir)
159 if(!file.exists(tmpDir)) {
160 stopifnot(dir.create(tmpDir, recursive=TRUE))
161 }
162 #on.exit(unlink(tmpDir, recursive=TRUE))
163
164 ## change to temp folder to dump Stangle output therein
165 setwd(tmpDir)
166 on.exit(setwd(pwd), add=TRUE)
167
168 codeFiles <- unlist(sapply(fName, function(x) {
169 Stangle(x)
170 x <- basename(x)
171 gsub("[RS]nw$", "R", x)
172 }))
173
174 ##
175 codeFiles <- file.path(tmpDir, codeFiles)
176 checkCodeFiles(codeFiles)
177 }