1 |
#' @title Updates installed R packages |
|
2 |
#' @concept Job |
|
3 |
#' @description Run update only if needed |
|
4 |
#' @param doUpdate 1 to update |
|
5 |
#' other values to do nothing |
|
6 |
#' @param repo The repository used to download packages |
|
7 |
#' @param useCiOptions 1 (default) to use CI options as defined in optionsForCi.R, |
|
8 |
#' other values to not use them |
|
9 |
#' @return NULL |
|
10 |
#' @examples |
|
11 |
#' \dontrun{ |
|
12 |
#' citoolsr::updatePackages() |
|
13 |
#' } |
|
14 |
#' @export |
|
15 |
updatePackages <- function(doUpdate = 1, |
|
16 |
repo = "https://cloud.r-project.org", |
|
17 |
useCiOptions = 1) { |
|
18 | 1x |
if (useCiOptions == 1) { |
19 | ! |
beforeOptions <- citoolsr::setOptionsForCi() |
20 |
} |
|
21 | ||
22 | 1x |
message("Updating R packages...") |
23 | 1x |
if (doUpdate == 1) { |
24 | ! |
pkgsToUpdate <- utils::old.packages(repos = repo) |
25 | ! |
if (!is.null(pkgsToUpdate) && nrow(pkgsToUpdate) > 0) { |
26 | ! |
utils::update.packages(repos = repo, ask = FALSE) |
27 |
} |
|
28 |
} else { |
|
29 | 1x |
message("Package update disabled by parameter doUpdate") |
30 |
} |
|
31 | ||
32 | 1x |
if (useCiOptions == 1) { |
33 |
# Restore options |
|
34 | ! |
options(beforeOptions) |
35 |
} |
|
36 | ||
37 | 1x |
invisible(NULL) |
38 |
} |
1 |
#' @title Install a package only if needed |
|
2 |
#' @concept Install |
|
3 |
#' @description Check if a package is already installed, |
|
4 |
#' if not then install it |
|
5 |
#' @param packageToInstallName Name of the package to install |
|
6 |
#' @param doUpdate 1 to force re-installation |
|
7 |
#' other values to install only if not already installed |
|
8 |
#' @param repo The repository used to download packages |
|
9 |
#' @param useCiOptions 1 (default) to use CI options as defined in optionsForCi.R, |
|
10 |
#' other values to not use them |
|
11 |
#' @return NULL |
|
12 |
#' @examples |
|
13 |
#' \dontrun{ |
|
14 |
#' citoolsr::installPackageIfNeeded(packageToInstallName = "stringi", doUpdate = 0) |
|
15 |
#' } |
|
16 |
#' @export |
|
17 |
installPackageIfNeeded <- function(packageToInstallName, doUpdate, |
|
18 |
repo = "https://cloud.r-project.org", |
|
19 |
useCiOptions = 1) { |
|
20 | 33x |
if (useCiOptions == 1) { |
21 | ! |
beforeOptions <- citoolsr::setOptionsForCi() |
22 |
} |
|
23 | ||
24 | 33x |
if (doUpdate == 1) { |
25 | ! |
message("Force installing package \"", packageToInstallName, "\"...") |
26 | ! |
utils::install.packages(pkgs = packageToInstallName, repos = repo) |
27 |
} else { |
|
28 | 33x |
missingOrError <- checkPackageIsInstalledAndLoadable(packageToInstallName = packageToInstallName) |
29 | ||
30 | 33x |
if (missingOrError) { |
31 | 1x |
message("Package \"", packageToInstallName, "\" must be installed!") |
32 | 1x |
message("Installing package \"", packageToInstallName, "\"...") |
33 | 1x |
isErrorOrWarning <- tryCatch( |
34 | 1x |
expr = { |
35 | 1x |
utils::install.packages(pkgs = packageToInstallName, repos = repo) |
36 | ! |
FALSE |
37 |
}, |
|
38 | 1x |
error = function(e) { |
39 | ! |
return(TRUE) |
40 |
}, |
|
41 | 1x |
warning = function(e) { |
42 | 1x |
return(TRUE) |
43 |
}) |
|
44 | ||
45 | 1x |
if (isErrorOrWarning) { |
46 | 1x |
stop("A problem occurs when installing package \"", packageToInstallName, "\"!") |
47 |
} |
|
48 |
} |
|
49 |
} |
|
50 | ||
51 | 32x |
if (useCiOptions == 1) { |
52 |
# Restore options |
|
53 | ! |
options(beforeOptions) |
54 |
} |
|
55 | ||
56 | 32x |
invisible(NULL) |
57 |
} |
|
58 | ||
59 |
#' @title Check package installed |
|
60 |
#' @description Check if a package is already installed and loadable |
|
61 |
#' @param packageToInstallName Name of the package to check |
|
62 |
#' @return FALSE if installed and loadable, else TRUE |
|
63 |
#' @examples |
|
64 |
#' citoolsr:::checkPackageIsInstalledAndLoadable(packageToInstallName = "utils") |
|
65 |
#' @keywords Internal |
|
66 |
checkPackageIsInstalledAndLoadable <- function(packageToInstallName) { |
|
67 | 35x |
message("Checking if package \"", packageToInstallName, "\" need to be installed...") |
68 | ||
69 | 35x |
isInstalled <- rlang::is_installed(pkg = packageToInstallName) |
70 | ||
71 | 35x |
return(!isInstalled) |
72 |
} |
1 |
#' @title Coverage of the package |
|
2 |
#' @concept Job |
|
3 |
#' @description Compute the tests coverage |
|
4 |
#' @param useCiOptions 1 (default) to use CI options as defined in optionsForCi.R, |
|
5 |
#' other values to not use them |
|
6 |
#' @param repo The repository used to download packages |
|
7 |
#' @param errorExpected 0 (default) to expect this function run fine, |
|
8 |
#' other values to expect an error (useful for tests) |
|
9 |
#' @return NULL |
|
10 |
#' @examples |
|
11 |
#' \dontrun{ |
|
12 |
#' citoolsr::testsCoverage() |
|
13 |
#' } |
|
14 |
#' @export |
|
15 |
testsCoverage <- function(useCiOptions = 1, |
|
16 |
repo = "https://cloud.r-project.org", |
|
17 |
errorExpected = 0) { |
|
18 | 3x |
if (useCiOptions == 1) { |
19 | ! |
beforeOptions <- citoolsr::setOptionsForCi() |
20 |
} |
|
21 | ||
22 | 3x |
citoolsr::installPackageIfNeeded( |
23 | 3x |
packageToInstallName = "covr", |
24 | 3x |
doUpdate = 0, |
25 | 3x |
repo = repo, |
26 | 3x |
useCiOptions = 0 |
27 |
) |
|
28 | ||
29 | 3x |
coverageDir <- "./coverage" |
30 | 3x |
if (!dir.exists(coverageDir)) { |
31 | 2x |
dir.create(coverageDir, recursive = TRUE) |
32 |
} |
|
33 | ||
34 | 3x |
cov <- tryCatch( |
35 | 3x |
expr = { |
36 | 3x |
covr::package_coverage() |
37 |
}, |
|
38 | 3x |
error = function(e) { |
39 | 1x |
return(FALSE) |
40 |
}) |
|
41 | ||
42 | 3x |
print(cov) |
43 | ||
44 | 3x |
if (is.logical(cov) && cov == FALSE) { |
45 | 1x |
if (errorExpected != 1) { |
46 | ! |
stop("Error but none expected!") |
47 |
} |
|
48 |
} else { |
|
49 | 2x |
if (errorExpected == 1) { |
50 | 1x |
stop("No error but one is expected!") |
51 |
} |
|
52 | ||
53 | 1x |
citoolsr::installPackageIfNeeded( |
54 | 1x |
packageToInstallName = "DT", |
55 | 1x |
doUpdate = 0, |
56 | 1x |
repo = repo, |
57 | 1x |
useCiOptions = 0 |
58 |
) |
|
59 | ||
60 | 1x |
citoolsr::installPackageIfNeeded( |
61 | 1x |
packageToInstallName = "htmltools", |
62 | 1x |
doUpdate = 0, |
63 | 1x |
repo = repo, |
64 | 1x |
useCiOptions = 0 |
65 |
) |
|
66 | ||
67 | 1x |
print("Generate covr report...") |
68 | 1x |
filename <- file.path("coverage", "coverage-report.html") |
69 | 1x |
res <- covr::report(x = cov, file = filename, browse = FALSE) |
70 | 1x |
print(res) |
71 | 1x |
if (!file.exists(filename)) { |
72 | ! |
stop("File \"", filename, "\" not found!") |
73 |
} |
|
74 | ||
75 | 1x |
print("Generate cobertura report...") |
76 | 1x |
filename <- file.path("coverage", "cobertura.xml") |
77 | 1x |
res <- covr::to_cobertura(cov = cov, filename = filename) |
78 | 1x |
print(res) |
79 | 1x |
if (!file.exists(filename)) { |
80 | ! |
stop("File \"", filename, "\" not found!") |
81 |
} |
|
82 |
} |
|
83 | ||
84 | 2x |
print("End script...") |
85 | ||
86 | 2x |
if (useCiOptions == 1) { |
87 |
# Restore options |
|
88 | ! |
options(beforeOptions) |
89 |
} |
|
90 | ||
91 | 2x |
invisible(NULL) |
92 |
} |
1 |
#' @title devtools::install_dev_deps with error management |
|
2 |
#' @concept Install |
|
3 |
#' @description Add an error and warning management around |
|
4 |
#' devtools::install_dev_deps call. |
|
5 |
#' Useful for any R CI. |
|
6 |
#' @param doUpgrades TRUE to pass "always" to sub-function, this force upgrade of dependencies. |
|
7 |
#' FALSE to pass "never" to sub-function, dependencies will never be upgraded. |
|
8 |
#' @param warningsAsErrors Boolean, |
|
9 |
#' TRUE to manage warnings and errors as errors. |
|
10 |
#' FALSE to manage only errors as errors. |
|
11 |
#' @param printAllMessages Boolean, TRUE to print all messages |
|
12 |
#' printed by devtools::install_dev_deps |
|
13 |
#' @param instDevDeps 1 (default) to install also development dependencies ("Suggests") |
|
14 |
#' other values to install only "Depends", "Imports" and "LinkingTo" |
|
15 |
#' @param repo The repository used to download packages |
|
16 |
#' @param useCiOptions 1 (default) to use CI options as defined in optionsForCi.R, |
|
17 |
#' other values to not use them |
|
18 |
#' @param errorExpected 0 (default) to expect this function run fine, |
|
19 |
#' other values to expect an error (useful for tests) |
|
20 |
#' @return NULL (instead this function raise an error or not) |
|
21 |
#' @examples |
|
22 |
#' \dontrun{ |
|
23 |
#' citoolsr::installDepsWithErrManagementCi( |
|
24 |
#' doUpgrades = TRUE, |
|
25 |
#' warningsAsErrors = TRUE, |
|
26 |
#' printAllMessages = TRUE) |
|
27 |
#' } |
|
28 |
#' @export |
|
29 |
installDepsWithErrManagementCi <- function(doUpgrades = TRUE, |
|
30 |
warningsAsErrors = FALSE, |
|
31 |
printAllMessages = TRUE, |
|
32 |
instDevDeps = 1, |
|
33 |
repo = "https://cloud.r-project.org", |
|
34 |
useCiOptions = 1, |
|
35 |
errorExpected = 0) { |
|
36 | 3x |
if (useCiOptions == 1) { |
37 | ! |
beforeOptions <- citoolsr::setOptionsForCi() |
38 |
} |
|
39 | ||
40 | 3x |
resList <- citoolsr::installDepsWithErrManagement( |
41 | 3x |
doUpgrades = doUpgrades, |
42 | 3x |
warningsAsErrors = warningsAsErrors, |
43 | 3x |
printAllMessages = printAllMessages, |
44 | 3x |
instDevDeps = instDevDeps, |
45 | 3x |
repo = repo) |
46 | ||
47 | 3x |
if (resList[["errorOccurs"]]) { |
48 | 1x |
message <- "Some error occurs in devtools::install_dev_deps call" |
49 | 1x |
if (errorExpected == 1) { |
50 | 1x |
message(message) |
51 |
} else { |
|
52 | ! |
stop(message) |
53 |
} |
|
54 |
} |
|
55 | ||
56 | 3x |
if (resList[["errorOccurs"]] == FALSE && errorExpected == 1) { |
57 | 1x |
stop("An error is expected but none occurs") |
58 |
} |
|
59 | ||
60 | 2x |
if (useCiOptions == 1) { |
61 |
# Restore options |
|
62 | ! |
options(beforeOptions) |
63 |
} |
|
64 | ||
65 | 2x |
invisible(NULL) |
66 |
} |
|
67 | ||
68 |
#' @title devtools::install_dev_deps with error management |
|
69 |
#' @concept Install |
|
70 |
#' @description Add an error and warning management around |
|
71 |
#' devtools::install_dev_deps call. |
|
72 |
#' Useful for any R CI. |
|
73 |
#' @param doUpgrades TRUE to pass "always" to sub-function, this force upgrade of dependencies. |
|
74 |
#' FALSE to pass "never" to sub-function, dependencies will never be upgraded. |
|
75 |
#' @param warningsAsErrors Boolean, |
|
76 |
#' TRUE to manage warnings and errors as errors. |
|
77 |
#' FALSE to manage only errors as errors. |
|
78 |
#' @param printAllMessages Boolean, TRUE to print all messages |
|
79 |
#' printed by devtools::install_dev_deps |
|
80 |
#' @param instDevDeps 1 (default) to install also development dependencies ("Suggests") |
|
81 |
#' other values to install only "Depends", "Imports" and "LinkingTo" |
|
82 |
#' @param repo The repository used to download packages |
|
83 |
#' @return A list with: |
|
84 |
#' - errorOccurs: a boolean at TRUE if an error or a warning occurs |
|
85 |
#' - outputs: a vector containing all outputs |
|
86 |
#' generated by devtools::install_dev_deps |
|
87 |
#' - messages: a vector containing all messages |
|
88 |
#' generated by devtools::install_dev_deps |
|
89 |
#' @examples |
|
90 |
#' \dontrun{ |
|
91 |
#' citoolsr::installDepsWithErrManagement( |
|
92 |
#' doUpgrades = TRUE, |
|
93 |
#' warningsAsErrors = TRUE, |
|
94 |
#' printAllMessages = TRUE) |
|
95 |
#' } |
|
96 |
#' @export |
|
97 |
installDepsWithErrManagement <- function(doUpgrades = TRUE, |
|
98 |
warningsAsErrors = FALSE, |
|
99 |
printAllMessages = TRUE, |
|
100 |
instDevDeps = 1, |
|
101 |
repo = "https://cloud.r-project.org") { |
|
102 | 3x |
errorOccurs <- installDepsWithSink( |
103 | 3x |
doUpgrades = doUpgrades, |
104 | 3x |
warningsAsErrors = warningsAsErrors, |
105 | 3x |
instDevDeps = instDevDeps, |
106 | 3x |
repo = repo) |
107 | ||
108 |
# Print all messages to console |
|
109 | 3x |
if (printAllMessages) { |
110 | 3x |
print(errorOccurs) |
111 |
} |
|
112 | ||
113 | 3x |
if (!errorOccurs$errorOccurs) { |
114 | 3x |
citoolsr::installPackageIfNeeded( |
115 | 3x |
packageToInstallName = "stringi", |
116 | 3x |
doUpdate = 0, |
117 | 3x |
repo = repo, |
118 | 3x |
useCiOptions = 0 |
119 |
) |
|
120 | ||
121 |
# Check for other problems: |
|
122 |
# - Check for unavailable packages |
|
123 | 3x |
searchingMessage <- "Skipping \\d* packages not available:" |
124 | 3x |
pbs1 <- stringi::stri_detect_regex( |
125 | 3x |
str = errorOccurs$outputs, |
126 | 3x |
pattern = searchingMessage, |
127 | 3x |
case_insensitive = FALSE) |
128 | 3x |
pbs2 <- stringi::stri_detect_regex( |
129 | 3x |
str = errorOccurs$messages, |
130 | 3x |
pattern = searchingMessage, |
131 | 3x |
case_insensitive = FALSE) |
132 | 3x |
if (any(pbs1) || any(pbs2)) { |
133 | 1x |
message <- "Error: Some packages have not been installed" |
134 | 1x |
message(message) |
135 | 1x |
errorOccurs$errorOccurs <- TRUE |
136 | 1x |
errorOccurs$condition <- simpleError(message = message) |
137 |
} |
|
138 |
} |
|
139 | ||
140 | 3x |
if (errorOccurs$errorOccurs) { |
141 | 1x |
message("Some error occurs in devtools::install_dev_deps call") |
142 |
} |
|
143 | ||
144 | 3x |
return(errorOccurs) |
145 |
} |
|
146 | ||
147 |
#' @title devtools::install_dev_deps with error and warning catching |
|
148 |
#' and all outputs messages grabbed |
|
149 |
#' @description Add message grabbing. |
|
150 |
#' @param doUpgrades TRUE to pass "always" to sub-function, this force upgrade of dependencies. |
|
151 |
#' FALSE to pass "never" to sub-function, dependencies will never be upgraded. |
|
152 |
#' @param warningsAsErrors Boolean, |
|
153 |
#' TRUE to manage warnings and errors as errors. |
|
154 |
#' FALSE to manage only errors as errors. |
|
155 |
#' @param instDevDeps 1 (default) to install also development dependencies ("Suggests") |
|
156 |
#' other values to install only "Depends", "Imports" and "LinkingTo" |
|
157 |
#' @param repo The repository used to download packages |
|
158 |
#' @return A list with: |
|
159 |
#' - errorOccurs: a boolean at TRUE if an error is caught |
|
160 |
#' - condition: a condition object related to the error caught |
|
161 |
#' (or NULL if no error caught) |
|
162 |
#' - outputs: a vector containing all outputs |
|
163 |
#' generated by devtools::install_dev_deps |
|
164 |
#' - messages: a vector containing all messages |
|
165 |
#' generated by devtools::install_dev_deps |
|
166 |
#' @examples |
|
167 |
#' \dontrun{ |
|
168 |
#' citoolsr:::installDepsWithSink(doUpgrades = TRUE, warningsAsErrors = FALSE) |
|
169 |
#' } |
|
170 |
#' @keywords Internal |
|
171 |
installDepsWithSink <- function(doUpgrades = TRUE, |
|
172 |
warningsAsErrors = FALSE, |
|
173 |
instDevDeps = 1, |
|
174 |
repo = "https://cloud.r-project.org") { |
|
175 | 3x |
outputs <- vector("character") |
176 | 3x |
messages <- vector("character") |
177 | ||
178 | 3x |
con1 <- textConnection( |
179 | 3x |
object = "outputs", |
180 | 3x |
open = "wr", |
181 | 3x |
local = TRUE) |
182 | 3x |
con2 <- textConnection( |
183 | 3x |
object = "messages", |
184 | 3x |
open = "wr", |
185 | 3x |
local = TRUE) |
186 | ||
187 | 3x |
sink(file = con1, type = "output") |
188 | 3x |
sink(file = con2, type = "message") |
189 | ||
190 | 3x |
errorOccurs <- installDepsWithWarnCatching( |
191 | 3x |
doUpgrades = doUpgrades, |
192 | 3x |
warningsAsErrors = warningsAsErrors, |
193 | 3x |
instDevDeps = instDevDeps, |
194 | 3x |
repo = repo) |
195 | ||
196 | 3x |
sink(type = "output") |
197 | 3x |
sink(type = "message") |
198 | ||
199 | 3x |
close(con1) |
200 | 3x |
close(con2) |
201 | ||
202 | 3x |
return(list(errorOccurs = errorOccurs$errorOccurs, |
203 | 3x |
condition = errorOccurs$condition, |
204 | 3x |
outputs = outputs, |
205 | 3x |
messages = messages)) |
206 |
} |
|
207 | ||
208 |
#' @title devtools::install_dev_deps with error and warning catching |
|
209 |
#' @description Add an error and warning catching around |
|
210 |
#' devtools::install_dev_deps call. |
|
211 |
#' @param doUpgrades TRUE to pass "always" to sub-function, this force upgrade of dependencies. |
|
212 |
#' FALSE to pass "never" to sub-function, dependencies will never be upgraded. |
|
213 |
#' @param warningsAsErrors Boolean, |
|
214 |
#' TRUE to manage warnings and errors as errors. |
|
215 |
#' FALSE to manage only errors as errors. |
|
216 |
#' @param instDevDeps 1 (default) to install also development dependencies ("Suggests") |
|
217 |
#' other values to install only "Depends", "Imports" and "LinkingTo" |
|
218 |
#' @param repo The repository used to download packages |
|
219 |
#' @return A list with: |
|
220 |
#' - errorOccurs: a boolean at TRUE if an error is caught |
|
221 |
#' - condition: a condition object related to the error caught |
|
222 |
#' (or NULL if no error caught) |
|
223 |
#' @examples |
|
224 |
#' \dontrun{ |
|
225 |
#' citoolsr:::installDepsWithWarnCatching(doUpgrades = TRUE, warningsAsErrors = FALSE) |
|
226 |
#' } |
|
227 |
#' @keywords Internal |
|
228 |
installDepsWithWarnCatching <- function(doUpgrades = TRUE, |
|
229 |
warningsAsErrors = FALSE, |
|
230 |
instDevDeps = 1, |
|
231 |
repo = "https://cloud.r-project.org") { |
|
232 | 3x |
if (warningsAsErrors) { |
233 | ! |
errorOccurs <- tryCatch( |
234 | ! |
expr = { |
235 | ! |
installDepsWithErrCatching( |
236 | ! |
doUpgrades = doUpgrades, |
237 | ! |
instDevDeps = instDevDeps, |
238 | ! |
repo = repo) |
239 |
}, |
|
240 | ! |
warning = function(w) { |
241 | ! |
return(list(errorOccurs = TRUE, condition = w)) |
242 |
}) |
|
243 |
} else { |
|
244 | 3x |
errorOccurs <- installDepsWithErrCatching( |
245 | 3x |
doUpgrades = doUpgrades, |
246 | 3x |
instDevDeps = instDevDeps, |
247 | 3x |
repo = repo) |
248 |
} |
|
249 | 3x |
return(errorOccurs) |
250 |
} |
|
251 | ||
252 |
#' @title devtools::install_dev_deps with error catching |
|
253 |
#' @description Catch any error raised by devtools::install_dev_deps and return it as string |
|
254 |
#' @param doUpgrades TRUE to pass "always" to sub-function, this force upgrade of dependencies. |
|
255 |
#' FALSE to pass "never" to sub-function, dependencies will never be upgraded. |
|
256 |
#' @param instDevDeps 1 (default) to install also development dependencies ("Suggests") |
|
257 |
#' other values to install only "Depends", "Imports" and "LinkingTo" |
|
258 |
#' @param repo The repository used to download packages |
|
259 |
#' @return A list with: |
|
260 |
#' - errorOccurs: a boolean at TRUE if an error is caught |
|
261 |
#' - condition: a condition object related to the error caught |
|
262 |
#' (or NULL if no error caught) |
|
263 |
#' @examples |
|
264 |
#' \dontrun{ |
|
265 |
#' citoolsr:::installDepsWithErrCatching(doUpgrades = TRUE) |
|
266 |
#' } |
|
267 |
#' @keywords Internal |
|
268 |
installDepsWithErrCatching <- function(doUpgrades = TRUE, |
|
269 |
instDevDeps = 1, |
|
270 |
repo = "https://cloud.r-project.org") { |
|
271 | 3x |
citoolsr::installPackageIfNeeded( |
272 | 3x |
packageToInstallName = "devtools", |
273 | 3x |
doUpdate = 0, |
274 | 3x |
repo = repo, |
275 | 3x |
useCiOptions = 0 |
276 |
) |
|
277 | ||
278 | 3x |
errorOccurs <- tryCatch( |
279 | 3x |
expr = { |
280 | 3x |
message("Installing dependencies (upgrade=", doUpgrades, ")...") |
281 | 3x |
if (instDevDeps == 1) { |
282 | 3x |
devtools::install_dev_deps( |
283 | 3x |
dependencies = TRUE, |
284 | 3x |
upgrade = doUpgrades, |
285 | 3x |
repos = repo) |
286 |
} else { |
|
287 | ! |
devtools::install_deps( |
288 | ! |
dependencies = NA, |
289 | ! |
upgrade = doUpgrades, |
290 | ! |
repos = repo) |
291 |
} |
|
292 | 3x |
list(errorOccurs = FALSE, condition = NULL) |
293 |
}, |
|
294 | 3x |
error = function(e) { |
295 | ! |
return(errorOccurs = TRUE, condition = e) |
296 |
}) |
|
297 | 3x |
return(errorOccurs) |
298 |
} |
1 |
#' @title Run lintr |
|
2 |
#' @concept Job |
|
3 |
#' @description Run the lintr static analysis |
|
4 |
#' @param useCiOptions 1 (default) to use CI options as defined in optionsForCi.R, |
|
5 |
#' other values to not use them |
|
6 |
#' @param repo The repository used to download packages |
|
7 |
#' @param errorExpected 0 (default) to expect this function run fine, |
|
8 |
#' other values to expect an error (useful for tests) |
|
9 |
#' @return NULL |
|
10 |
#' @examples |
|
11 |
#' \dontrun{ |
|
12 |
#' citoolsr::runLintr() |
|
13 |
#' } |
|
14 |
#' @export |
|
15 |
runLintr <- function(useCiOptions = 1, |
|
16 |
repo = "https://cloud.r-project.org", |
|
17 |
errorExpected = 0) { |
|
18 | 3x |
if (useCiOptions == 1) { |
19 | ! |
beforeOptions <- citoolsr::setOptionsForCi() |
20 |
} |
|
21 | ||
22 | 3x |
citoolsr::installPackageIfNeeded( |
23 | 3x |
packageToInstallName = "lintr", |
24 | 3x |
doUpdate = 0, |
25 | 3x |
repo = repo, |
26 | 3x |
useCiOptions = 0 |
27 |
) |
|
28 | ||
29 | 3x |
message("Checking lintr globally...") |
30 | 3x |
res1 <- lintr::lint_package() |
31 | ||
32 | 3x |
print(res1) |
33 | ||
34 | 3x |
message("Checking lintr on man/examples/...") |
35 | 3x |
res2 <- lintr::lint_dir(path = "man/examples/") |
36 | ||
37 | 3x |
print(res2) |
38 | ||
39 | 3x |
message("Checking lintr on vignettes/...") |
40 | 3x |
res3 <- lintr::lint_dir(path = "vignettes/") |
41 | ||
42 | 3x |
print(res3) |
43 | ||
44 | 3x |
if (length(res1) > 0 || length(res2) > 0 || length(res3) > 0) { |
45 | 1x |
if (errorExpected != 1) { |
46 | ! |
stop("Error but none expected!") |
47 |
} |
|
48 |
} else { |
|
49 | 2x |
if (errorExpected == 1) { |
50 | 1x |
stop("No error but one is expected!") |
51 |
} |
|
52 |
} |
|
53 | ||
54 | 2x |
if (useCiOptions == 1) { |
55 |
# Restore options |
|
56 | ! |
options(beforeOptions) |
57 |
} |
|
58 | ||
59 | 2x |
invisible(NULL) |
60 |
} |
1 |
#' @title Run the tests of the package |
|
2 |
#' @concept Job |
|
3 |
#' @description Run all tests |
|
4 |
#' @param useCiOptions 1 (default) to use CI options as defined in optionsForCi.R, |
|
5 |
#' other values to not use them |
|
6 |
#' @param repo The repository used to download packages |
|
7 |
#' @param errorExpected 0 (default) to expect this function run fine, |
|
8 |
#' other values to expect an error (useful for tests) |
|
9 |
#' @return NULL |
|
10 |
#' @examples |
|
11 |
#' \dontrun{ |
|
12 |
#' citoolsr::runTests() |
|
13 |
#' } |
|
14 |
#' @export |
|
15 |
runTests <- function(useCiOptions = 1, |
|
16 |
repo = "https://cloud.r-project.org", |
|
17 |
errorExpected = 0) { |
|
18 | 4x |
if (useCiOptions == 1) { |
19 | ! |
beforeOptions <- citoolsr::setOptionsForCi() |
20 |
} |
|
21 | ||
22 | 4x |
citoolsr::installPackageIfNeeded( |
23 | 4x |
packageToInstallName = "testthat", |
24 | 4x |
doUpdate = 0, |
25 | 4x |
repo = repo, |
26 | 4x |
useCiOptions = 0 |
27 |
) |
|
28 | ||
29 | 4x |
citoolsr::installPackageIfNeeded( |
30 | 4x |
packageToInstallName = "devtools", |
31 | 4x |
doUpdate = 0, |
32 | 4x |
repo = repo, |
33 | 4x |
useCiOptions = 0 |
34 |
) |
|
35 | ||
36 |
# Use custom testthat reporter in order to disable test progression tick |
|
37 |
# (which is not adapted to the runner log) |
|
38 | 4x |
myReporter <- testthat::ProgressReporter$new(update_interval = Inf, min_time = Inf) |
39 | ||
40 | 4x |
res <- devtools::test(reporter = myReporter) |
41 | ||
42 | 4x |
print(res) |
43 | ||
44 | 4x |
if (any(as.data.frame(res)[["failed"]] > 0) || any(as.data.frame(res)[["warning"]] > 0)) { |
45 | 2x |
if (errorExpected != 1) { |
46 | ! |
stop("Some tests failed.") |
47 |
} |
|
48 |
} else { |
|
49 | 2x |
if (errorExpected == 1) { |
50 | 1x |
stop("Some tests must failed, but none failed.") |
51 |
} |
|
52 |
} |
|
53 | ||
54 | 3x |
if (useCiOptions == 1) { |
55 |
# Restore options |
|
56 | ! |
options(beforeOptions) |
57 |
} |
|
58 | ||
59 | 3x |
invisible(NULL) |
60 |
} |
1 |
#' @title Doc website |
|
2 |
#' @concept Job |
|
3 |
#' @description Generate the doc website with pkgdown |
|
4 |
#' @param useCiOptions 1 (default) to use CI options as defined in optionsForCi.R, |
|
5 |
#' other values to not use them |
|
6 |
#' @param repo The repository used to download packages |
|
7 |
#' @param errorExpected 0 (default) to expect this function run fine, |
|
8 |
#' other values to expect an error (useful for tests) |
|
9 |
#' @return NULL |
|
10 |
#' @examples |
|
11 |
#' \dontrun{ |
|
12 |
#' citoolsr::generateDocWebsite() |
|
13 |
#' } |
|
14 |
#' @export |
|
15 |
generateDocWebsite <- function(useCiOptions = 1, |
|
16 |
repo = "https://cloud.r-project.org", |
|
17 |
errorExpected = 0) { |
|
18 | 3x |
if (useCiOptions == 1) { |
19 | ! |
beforeOptions <- citoolsr::setOptionsForCi() |
20 |
} |
|
21 | ||
22 | 3x |
citoolsr::installPackageIfNeeded( |
23 | 3x |
packageToInstallName = "pkgdown", |
24 | 3x |
doUpdate = 0, |
25 | 3x |
repo = repo, |
26 | 3x |
useCiOptions = 0 |
27 |
) |
|
28 | ||
29 | 3x |
citoolsr::installPackageIfNeeded( |
30 | 3x |
packageToInstallName = "devtools", |
31 | 3x |
doUpdate = 0, |
32 | 3x |
repo = repo, |
33 | 3x |
useCiOptions = 0 |
34 |
) |
|
35 | ||
36 | 3x |
res <- tryCatch( |
37 | 3x |
expr = { |
38 | 3x |
pkgdown::check_pkgdown() |
39 | 2x |
devtools::build_site() |
40 |
}, |
|
41 | 3x |
error = function(e) { |
42 | 1x |
return(FALSE) |
43 |
}) |
|
44 | ||
45 | 3x |
if (is.logical(res) && res == FALSE) { |
46 | 1x |
if (errorExpected != 1) { |
47 | ! |
stop("Error but none expected!") |
48 |
} |
|
49 |
} else { |
|
50 | 2x |
if (errorExpected == 1) { |
51 | 1x |
stop("No error but one is expected!") |
52 |
} |
|
53 |
} |
|
54 | ||
55 | 2x |
if (useCiOptions == 1) { |
56 |
# Restore options |
|
57 | ! |
options(beforeOptions) |
58 |
} |
|
59 | ||
60 | 2x |
invisible(NULL) |
61 |
} |
1 |
#' @title Check the package |
|
2 |
#' @concept Job |
|
3 |
#' @description Run checks on the package |
|
4 |
#' @param checkLicense 1 to check the license, |
|
5 |
#' other values to not check the license |
|
6 |
#' @param useCiOptions 1 (default) to use CI options as defined in optionsForCi.R, |
|
7 |
#' other values to not use them |
|
8 |
#' @param repo The repository used to download packages |
|
9 |
#' @param errorExpected 0 (default) to expect this function run fine, |
|
10 |
#' other values to expect an error (useful for tests) |
|
11 |
#' @return NULL |
|
12 |
#' @examples |
|
13 |
#' \dontrun{ |
|
14 |
#' citoolsr::checkPackage() |
|
15 |
#' } |
|
16 |
#' @export |
|
17 |
checkPackage <- function(checkLicense = "TRUE", |
|
18 |
useCiOptions = 1, |
|
19 |
repo = "https://cloud.r-project.org", |
|
20 |
errorExpected = 0) { |
|
21 | 3x |
if (useCiOptions == 1) { |
22 | ! |
beforeOptions <- citoolsr::setOptionsForCi() |
23 |
} |
|
24 | ||
25 | 3x |
citoolsr::installPackageIfNeeded( |
26 | 3x |
packageToInstallName = "devtools", |
27 | 3x |
doUpdate = 0, |
28 | 3x |
repo = repo, |
29 | 3x |
useCiOptions = 0 |
30 |
) |
|
31 | ||
32 | 3x |
message("Checking package...") |
33 | ||
34 | 3x |
if (checkLicense == 1) { |
35 | ! |
sCheckLicense <- "TRUE" |
36 |
} else { |
|
37 | 3x |
sCheckLicense <- "FALSE" |
38 |
} |
|
39 | ||
40 | 3x |
checkDir <- "./check" |
41 | 3x |
if (!dir.exists(checkDir)) { |
42 | 2x |
dir.create(checkDir, recursive = TRUE) |
43 |
} |
|
44 | 3x |
res <- devtools::check( |
45 | 3x |
pkg = ".", |
46 | 3x |
document = TRUE, |
47 | 3x |
build_args = NULL, |
48 | 3x |
manual = FALSE, |
49 | 3x |
cran = TRUE, |
50 | 3x |
remote = TRUE, |
51 | 3x |
incoming = FALSE, |
52 | 3x |
force_suggests = TRUE, |
53 | 3x |
run_dont_test = FALSE, |
54 | 3x |
args = c("--timings", "--no-tests"), |
55 | 3x |
env_vars = c("NOT_CRAN" = "TRUE", "_R_CHECK_LICENSE_" = sCheckLicense), |
56 | 3x |
quiet = FALSE, |
57 | 3x |
check_dir = checkDir, |
58 | 3x |
vignettes = TRUE, |
59 | 3x |
error_on = "never") |
60 | ||
61 | 3x |
print(res) |
62 | ||
63 | 3x |
isProblems <- (length(res$errors) > 0 || length(res$warnings) > 0 || length(res$notes) > 0) |
64 | ||
65 | 3x |
if (isProblems) { |
66 | 1x |
if (errorExpected != 1) { |
67 | ! |
stop("Error but none expected!") |
68 |
} |
|
69 |
} else { |
|
70 | 2x |
if (errorExpected == 1) { |
71 | 1x |
stop("No error but one is expected!") |
72 |
} |
|
73 |
} |
|
74 | ||
75 | 2x |
if (useCiOptions == 1) { |
76 |
# Restore options |
|
77 | ! |
options(beforeOptions) |
78 |
} |
|
79 | ||
80 | 2x |
invisible(NULL) |
81 |
} |
1 |
#' @title Set options for R CI |
|
2 |
#' @concept Tools |
|
3 |
#' @description Set some options useful for CI |
|
4 |
#' @return A backup of options before this function call |
|
5 |
#' @examples |
|
6 |
#' beforeOptions <- citoolsr::setOptionsForCi() |
|
7 |
#' # Restore options |
|
8 |
#' options(beforeOptions) |
|
9 |
#' @export |
|
10 |
setOptionsForCi <- function() { |
|
11 | 2x |
optionsBackup <- options() |
12 | 2x |
options(showWarnCalls = TRUE, # Show a call stack for warnings |
13 | 2x |
showErrorCalls = TRUE, # Show a call stack for errors |
14 | 2x |
show.error.messages = TRUE, # Show error message with try or error handler |
15 | 2x |
warn = 2) # Warnings turned as errors |
16 |
# Return the value but don't print the value (if no assignement) |
|
17 | 2x |
invisible(optionsBackup) |
18 |
} |