-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathtinytest.R
More file actions
1205 lines (1073 loc) · 40 KB
/
tinytest.R
File metadata and controls
1205 lines (1073 loc) · 40 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#' @importFrom utils install.packages file_test capture.output getFromNamespace
#' @importFrom parallel makeCluster parLapply stopCluster
{}
if (!exists("nullfile", mode = "function", envir = baseenv())) {
nullfile <- function() if (.Platform$OS.type == "windows") "nul:" else "/dev/null"
}
# directory from which run_test_file() was called (i.e. before it temporarily
# changes directory
call_wd <- (function(){
CALLDIR <- ""
function(dir=NULL){
if (is.null(dir)){
return(CALLDIR)
} else {
# only set when not set previously
if (CALLDIR == "" || dir == "") CALLDIR <<- dir
}
CALLDIR
}
})()
set_call_wd <- function(dir){
call_wd(dir)
}
#' Get workding dir from where a test was initiated
#'
#' A test runner, like \code{\link{run_test_file}} changes
#' R's working directory to the location of the test file temporarily
#' while the tests run. This function can be used from within the
#' test file to get R's working directory at the time \code{run_test_file}
#' (or one of it's siblings)
#' was called.
#'
#'
#' @return \code{[character]} A path.
#' @examples
#' get_call_wd()
#' @export
get_call_wd <- function(){
call_wd()
}
# reference object to store or ignore output
# of 'expect' functions
output <- function(){
e <- new.env()
r <- 0 # number of results
n <- 0 # number of tests
m <- 0 # number of passes
s <- 0 # number of side-effects
re <- "^T[0-9]+"
e$add <- function(x){
r <<- r + 1
e[[sprintf("T%04d",r)]] <- x
if ( isTRUE(x) || isFALSE(x) ){
n <<- n + 1
m <<- m + as.integer(x)
} else if (is.na(x)){
s <<- s + 1
}
}
e$gimme <- function(){
vr <- ls(e,pattern = re)
lapply(vr, function(i) e[[i]])
}
e$rm_last <- function(){
x <- ls(e,pattern = re)
i <- x[length(x)]
if ( isTRUE(e[[i]]) ) m <<- m - 1
# note: we never ignore a call to envdiff,
# so no need to check for is.na(e[i]).
rm(list=i, envir=e)
n <<- n-1
r <<- r-1
}
e$ntest <- function() n
e$npass <- function() m
e$nfail <- function() n - m
e$nside <- function() s
# metadata will be provided by run_test_file
e$fst <- 0
e$lst <- 0
e$call <- ""
e$file
# will be set by exit_file()
e$exit <- FALSE
e$exitmsg <- ""
e$exit_msg <- function() sprintf("[Exited at #%d: %s]", e$fst, e$exitmsg)
e
}
capture <- function(fun, env){
# avoid lazy eval when looping over functions as a variable
# e.g. when loading extensions.
force(fun)
function(...){
out <- fun(...)
if ( inherits(out, "tinytest") ){
attr(out,"file") <- env$file
attr(out,"fst") <- env$fst
attr(out,"lst") <- env$lst
attr(out,"call") <- env$call
attr(out,"trace")<- sys.calls()
# if not NA, the result is from an expect_ function
# if NA, it is a side-effect, and we do not attempt to
# improve the call's format
if (!is.na(out) && env$lst - env$fst >=3)
attr(out,"call") <- match.call(fun)
env$add(out)
attr(out,"env") <- env
}
out
}
}
# RUnit style checking functions expect_xfoo -> checkXfoo
add_RUnit_style <- function(e){
fns <- ls(e, pattern="^expect_")
# snake to camelCase
fns_RUnit <- sub("_(.)", "\\U\\1", fns, perl=TRUE)
fns_RUnit <- sub("expect","check",fns_RUnit)
# add checkHaha for each expect_hihi (lol no for each expect_haha)
for (i in seq_along(fns)) assign(fns_RUnit[i], e[[fns[i]]], envir=e)
}
#' Ignore the output of an expectation
#'
#' Ignored expectations are not reported in the test results.
#' Ignoring is only useful for test files, and not for use directly
#' at the command-line. See also the package vignette: \code{vignette("using_tinytest")}.
#'
#' @param fun \code{[function]} An \code{expect_} function
#'
#' @return An ignored \code{function}
#' @family test-functions
#'
#'
#' @section Details:
#'
#' \code{ignore} is a higher-order function: a function that returns another function.
#' In particular, it accepts a function and returns a function that is almost identical
#' to the input function. The only difference is that the return value of the function
#' returned by \code{ignore} is not caught by \code{\link{run_test_file}} and friends.
#' For example, \code{ignore(expect_true)} is a function, and we can use it as
#' \code{ignore(expect_true)( 1 == 1)}. The return value of \code{ignore(expect_true)(1==1)}
#' is exactly the same as that for \code{expect_true(1==1)}.
#'
#'
#' @examples
#' \donttest{
#' ## The result of 'expect_warning' is not stored in the test result when
#' ## this is run from a file.
#' expect_true( ignore(expect_warning)(warning("foo!")) )
#' ## Note the placement of the brackets in ignore(expect_warning)(...).
#' }
#'
#'
#' @export
ignore <- function(fun){
function(...){
out <- fun(...)
if ( !is.null(attr(out, "env")) ){
attr(out,"env")$rm_last()
attr(out,"env") <- NULL
}
out
}
}
#' Stop testing (conditionally)
#'
#' Use \code{exit_file} to exit a file with a custom message, or use
#' \code{exit_if_not} to exit if one or more conditions are not met. \code{exit_if_not}
#' will create a message akin to messages created by \code{\link[base]{stopifnot}}.
#'
#' @param msg \code{[character]} An optional message to print after exiting.
#' @param ... A comma-separated list of conditions.
#'
#' @return The exit message
#'
#' @examples
#' exit_file("I'm too tired to test")
#' exit_if_not(packageVersion("tinytest") >= "1.0.0")
#' \dontrun{
#' exit_if_not(requireNamespace("foo",quietly=TRUE))
#' }
#' @family test-files
#' @export
exit_file <- function(msg="") msg
# masking function to to call within run_test_file
capture_exit <- function(fun, env){
function(...){
out <- fun(...)
if (!is.null(out)) env$exit <- TRUE
if (is.character(out)){
env$exitmsg <- out
} else {
env$exitmsg <- tryCatch(as.character(out), error=function(e) "???")
}
}
}
#' @rdname exit_file
#' @export
exit_if_not <- function(...){
L <- as.list(substitute(list(...))[-1])
msg <- NULL
for ( e in L ){
if ( !isTRUE(eval(e)) ){
str <- paste0(deparse(e), collapse=" ")
msg <- sprintf("'%s' is not TRUE", str)
break
}
}
msg
}
# we need a special capture function for
# Sys.setenv because it's return value does
# not inlcude argument names (it is an unnamed
# logical vector). We need the names to be able to
# unset the env vars later on.
capture_envvar <- function(fun, env){
function(...){
for ( x in names(list(...)) ){
# record the first occurrence so we capture the
# original value
if ( !x %in% ls(envir=env) ) env[[x]] <- Sys.getenv(x)
}
out <- fun(...)
invisible(out)
}
}
unset_envvar <- function(env){
L <- as.list(env)
# Sys.setenv crashes with empty list
if ( length(L)>0 ) do.call(Sys.setenv, L)
}
# locale: old locale settings, recorded before running the
# file. (character scalar).
reset_locale <- function(locale){
if ( identical(locale, Sys.getlocale()) ) return()
lcs <- strsplit(locale,";")[[1]]
vals <- sub("^.*=","",lcs)
names(vals) <- sub("=.*","", lcs)
for ( x in names(vals) ){
# we use tryCatch as Sys.getlocale() may retrieve locale
# settings that can not be set by Sys.setlocale()
tryCatch(Sys.setlocale(category = x, locale = vals[x])
, error = function(e) NULL, warning = function(w) NULL)
}
invisible(NULL)
}
capture_options <- function(fun, env){
function(...){
out <- fun(...)
for ( x in names(out) ){
# record only the first occurrence so we capture
# the original value
if (!x %in% ls(envir=env)) env[[x]] <- out[[x]]
}
invisible(out)
}
}
reset_options <- function(env){
options(as.list(env))
}
# envir : an environment where test files are evaluated
# output: an environment where test results are captured
add_locally_masked_functions <- function(envir, output){
# Local masking of native functions. 'manually' because
# it is faster then loading via getFromNamespace()
envir$expect_equal <- capture(expect_equal, output)
envir$expect_equivalent <- capture(expect_equivalent, output)
envir$expect_length <- capture(expect_length, output)
envir$expect_true <- capture(expect_true, output)
envir$expect_false <- capture(expect_false, output)
envir$expect_inherits <- capture(expect_inherits, output)
envir$expect_null <- capture(expect_null, output)
envir$expect_message <- capture(expect_message, output)
envir$expect_warning <- capture(expect_warning, output)
envir$expect_error <- capture(expect_error, output)
envir$expect_stdout <- capture(expect_stdout, output)
envir$expect_identical <- capture(expect_identical, output)
envir$expect_silent <- capture(expect_silent, output)
envir$expect_equal_to_reference <- capture(expect_equal_to_reference, output)
envir$expect_equivalent_to_reference <- capture(expect_equivalent_to_reference, output)
envir$exit_file <- capture_exit(exit_file, output)
envir$exit_if_not <- capture_exit(exit_if_not, output)
envir$expect_match <- capture(expect_match, output)
envir$ignore <- ignore
envir$at_home <- tinytest::at_home
## add 'checkFoo' equivalents of 'expect_foo' (native functions only)
if ( getOption("tt.RUnitStyle", TRUE) ) add_RUnit_style(envir)
envir$using <- capture_using(using, envir, output)
}
#' Use an extension package.
#'
#' Loads and attaches a package to the search path, and picks up the
#' \pkg{tinytest} extension functions registered by the package. Package
#' authors \emph{must} call this function in \emph{every} test file where an
#' extension is used, or otherwise results from the extension package are not
#' recorded (without a warning). Calling \code{using} in every file
#' where an extension is used also ensures that tests can be run in parallel.
#'
#'
#' @param package the name of the extension package, given as name or character string.
#' @param quietly Passed to \code{\link{require}}.
#'
#' @return A named \code{list}, with the package name and the names of the
#' functions registered by \code{package} to extend \pkg{tinytest}. A message
#' is emitted when the package registers no extension functions.
#'
#' @examples
#' \dontrun{
#' # In interactive session: see which functions are exported
#' # by checkmate.tinytest
#' out <- using(checkmate.tinytest)
#' print(out)
#' }
#'
#' @family extensions
#' @export
using <- function(package, quietly=TRUE){
pkg <- as.character(substitute(package))
if ( !require(pkg, quietly=quietly, character.only=TRUE) ){
stopf("Package %s could not be loaded",pkg)
}
ext <- getOption("tt.extensions", FALSE)
out <- if ( isFALSE(ext) ){
msgf("Package '%s' registered no tinytest extensions.")
list(character(0))
} else {
ext
}
if (length(out) == 1) names(out) <- pkg
invisible(out)
}
capture_using <- function(fun, envir, output){
function(...){
# call user-facing function
ext <- fun(...)
# get package name
pkgs <- names(ext)
for ( pkg in pkgs ){
functions <- ext[[pkg]]
for ( func in functions ){ # get funcy!
# get function object from namespace
f <- tryCatch(getFromNamespace(func, pkg)
, error = function(e){
msg <- sprintf("Loading '%s' extensions failed with message:\n'%s'"
, pkg, e$message)
warning(msg, call.=FALSE)
})
# mask'm like there's no tomorrow
envir[[func]] <- capture(f, output)
}
}
invisible(ext)
}
}
#' Register or unregister extension functions
#'
#' Functions to use in \code{.onLoad} and \code{.onUnload} by packages that
#' extend \pkg{tinytest}.
#'
#' @param pkg \code{[character]} scalar. Name of the package providing extensions.
#' @param functions \code{[character]} vector. Name of the functions in the package that must be added.
#'
#'
#' @section The tinytest API:
#'
#' Packages can extend \pkg{tinytest} with expectation functions \emph{if and only
#' if} the following requirements are satisfied.
#'
#' \enumerate{
#' \item{The extending functions return a \code{\link{tinytest}} object. This
#' can be created by calling \code{tinytest()} with the arguments (defaults,
#' if any, are in brackets):
#' \itemize{
#' \item{\code{result}: A \code{logical} scalar: \code{TRUE} or \code{FALSE} (not
#' \code{NA}) }
#' \item{\code{call}: The \code{call} to the expectation function. Usually the
#' result of \code{sys.call(sys.parent(1))} }
#' \item{\code{diff} (\code{NA_character_}): A \code{character} scalar, with a long description of the
#' difference. Sentences may be separated by \code{"\\n"}.}
#' \item{\code{short} (\code{NA_character_}): Either \code{"data"}, if the difference is in the
#' data. \code{"attr"} when attributes differ or \code{"xcpt"} when
#' an expectation about an exception is not met. If there are
#' differences in data and in attributes, the attributes take
#' precedence.}
#' \item{\code{info}} (\code{NA_character_}): A user-defined message.
#' }
#' Observe that this requires the extending package to add \pkg{tinytest} to
#' the \code{Imports} field in the package's \code{DESCRIPTION} file (this
#' also holds for the following requirement).
#' }
#' \item{Functions are registered in \code{.onLoad()} using
#' \code{register_tinytest_extension()}. Functions that are already
#' registered, including \pkg{tinytest} functions will be overwritten.}
#' }
#' It is \emph{recommended} to:
#' \enumerate{
#' \item{Follow the syntax conventions of \pkg{tinytest} so expectation
#' functions start with \code{expect_}.}
#' \item{Explain to users of the extension package how to use the extension
#' (see \code{\link{using}}).}
#' \item{include an \code{info} argument to \code{expect_} functions that
#' is passed to \code{tinytest()}}.
#' }
#'
#'
#' @section Minimal example packages:
#'
#' \itemize{
#' \item{Extending \pkg{tinytest}:
#' \href{https://github.com/markvanderloo/tinytest.extension}{tinytest.extension}.}
#' \item{Using a \pkg{tinytest} extension:
#' \href{https://github.com/markvanderloo/uses.tinytest.extension}{using.tinytest.extension}.}
#' }
#' @family extensions
#' @export
register_tinytest_extension <- function(pkg, functions){
ext <- getOption("tt.extensions",FALSE)
if (isFALSE(ext)){
L <-list(functions)
names(L) <- pkg
options(tt.extensions = L)
} else {
ext[[pkg]] <- functions
options(tt.extensions = ext)
}
}
#' Run an R file containing tests; gather results
#'
#' @param file \code{[character]} File location of a .R file.
#' @param at_home \code{[logical]} toggle local tests.
#' @param verbose \code{[integer]} verbosity level. 0: be quiet, 1: print
#' status per file, 2: print status and increase counter after each test expression.
#' @param color \code{[logical]} toggle colorize counts in verbose mode (see Note)
#' @param remove_side_effects \code{[logical]} toggle remove user-defined side
#' effects? See section on side effects.
#' @param side_effects \code{[logical|list]} Either a logical,
#' or a list of arguments to pass to \code{\link{report_side_effects}}.
#' @param set_env \code{[named list]}. Key=value pairs of environment variables
#' that will be set before the test file is run and reset afterwards. These are not
#' counted as side effects of the code under scrutiny.
#' @param encoding \code{[character]} Define encoding argument passed to \code{\link[base]{parse}}
#' when parsing \code{file}.
#' @param ... Currently unused
#'
#' @details
#'
#' In \pkg{tinytest}, a test file is just an R script where some or all
#' of the statements express an \code{\link[=expect_equal]{expectation}}.
#' \code{run_test_file} runs the file while gathering results of the
#' expectations in a \code{\link{tinytests}} object.
#'
#' The graphics device is set to \code{pdf(file=tempfile())} for the run of the
#' test file.
#'
#' @section Side-effects caused by test code:
#'
#' All calls to \code{\link{Sys.setenv}} and \code{\link{options}}
#' defined in a test file are captured and undone once the test file has run,
#' if \code{remove_side_effects} is set to \code{TRUE}.
#'
#' @section Tracking side effects:
#'
#' Certain side effects can be tracked, even when they are not explicitly
#' evoked in the test file. See \code{\link{report_side_effects}} for side
#' effects tracked by \pkg{tinytest}. Calls to \code{report_side_effects}
#' within the test file overrule settings provided with this function.
#'
#'
#'
#' @note
#' Not all terminals support ansi escape characters, so colorized output can be
#' switched off. This can also be done globally by setting
#' \code{options(tt.pr.color=FALSE)}. Some terminals that do support ansi
#' escape characters may contain bugs. An example is the RStudio terminal
#' (RStudio 1.1) running on Ubuntu 16.04 (and possibly other OSs).
#'
#' @return A \code{list} of class \code{tinytests}, which is a list of
#' \code{\link{tinytest}} objects.
#'
#' @examples
#' # create a test file, in temp directory
#' tests <- "
#' addOne <- function(x) x + 2
#'
#' Sys.setenv(lolz=2)
#'
#' expect_true(addOne(0) > 0)
#' expect_equal(2, addOne(1))
#'
#' Sys.unsetenv('lolz')
#' "
#' testfile <- tempfile(pattern="test_", fileext=".R")
#' write(tests, testfile)
#'
#' # run test file
#' out <- run_test_file(testfile,color=FALSE)
#' out
#' # print everything in short format, include passes in print.
#' print(out, nlong=0, passes=TRUE)
#'
#' # run test file, track supported side-effects
#' run_test_file(testfile, side_effects=TRUE)
#'
#' # run test file, track only changes in working directory
#' run_test_file(testfile, side_effects=list(pwd=TRUE, envvar=FALSE))
#'
#'
#' @family test-files
#' @seealso \code{\link{ignore}}
#' @export
run_test_file <- function( file
, at_home=TRUE
, verbose = getOption("tt.verbose", 2)
, color = getOption("tt.pr.color", TRUE)
, remove_side_effects = TRUE
, side_effects = FALSE
, set_env = list()
, encoding="unknown"
, ...){
if (!file_test("-f", file)){
stop(sprintf("'%s' does not exist or is a directory",file),call.=FALSE)
}
t0 <- Sys.time()
# set environment variables (if any) to control the R environment during testing.
if (length(set_env) > 0){
# first, record current settings
old_env_var <- sapply(names(set_env), Sys.getenv, unset=NA_character_, USE.NAMES=TRUE)
# new settings
do.call(Sys.setenv, set_env)
}
## where to come back after running the file
oldwd <- getwd()
set_call_wd(oldwd)
# make sure that plots get redirected to oblivion
grDevices::pdf(file=nullfile())
## this will store the names of all environment
## variables created while running the file.
envvar <- new.env()
## this will store option values that are overwritten by
## the user when running the file.
oldop <- new.env()
## Store locale settings that may be overwritten
## by the user when running the file
locale <- Sys.getlocale()
## clean up side effects
on.exit({
## Clean up tinytest side effects
# go back to the original working directory
setwd(oldwd)
set_call_wd("")
# unset 'at_home' marker
Sys.unsetenv("TT_AT_HOME")
if ( remove_side_effects ){ ## Clean up user side effects
# unset env vars set by the user in 'file'
unset_envvar(envvar)
# reset options to the state before running 'file'
reset_options(oldop)
# reset locale settings to starting values
reset_locale(locale)
}
grDevices::dev.off()
# return env var to values before running run_test_file
if (exists("old_env_var")){
unset <- is.na(old_env_var)
Sys.unsetenv(names(old_env_var)[unset])
if (any(!unset)) do.call(Sys.setenv, as.list(old_env_var)[!unset])
}
})
setwd(dirname(file))
file <- basename(file)
if (at_home) Sys.setenv(TT_AT_HOME=TRUE)
# An environment to capture the output in.
o <- output()
# An environment to run the test scripts in
e <- new.env(parent=globalenv())
# We locally mask expectation functions in the evaluation
# environment 'e' so their output will be captured in 'o'
add_locally_masked_functions(envir = e, output=o)
## Reduce user side effects by making sure that any env var set
## in a test file is unset after running it.
e$Sys.setenv <- capture_envvar(Sys.setenv, envvar)
## Reduce user side effects by capturing options that will be reset
## on exit
e$options <- capture_options(options, oldop)
## Set useFancyQuotes, which is usually done by startup.Rs, the location
## of which is defined by envvar R_TESTS, which we set to empty now.
## See GH issues 36,37
options(useFancyQuotes=FALSE)
Sys.setenv(R_TESTS="")
## Make sure that we catch side-effects if the user asks for it.
# an environment to store side-effects, and wheter we report them.
sidefx <- new.env()
e$report_side_effects <- capture_se(report_side_effects, sidefx)
do.call(e$report_side_effects, as.list(side_effects))
# internal side-effect tracker: make sure results are exported to user.
local_report_envvar <- capture(report_envvar, o)
local_report_cwd <- capture(report_cwd, o)
local_report_files <- capture(report_files, o)
local_report_locale <- capture(report_locale, o)
# parse file, store source reference.
check_double_colon(filename=file)
parsed <- parse(file=file, keep.source=TRUE, encoding=encoding)
src <- attr(parsed, "srcref")
o$file <- file
# format file name for printing while running.
prfile <- basename(file)
if (nchar(prfile) > 30 ){
prfile <- paste0("..",substr(prfile, nchar(prfile)-27,nchar(prfile)))
}
prfile <- gsub(" ",".",sprintf("%-30s",basename(file)))
for ( i in seq_along(parsed) ){
expr <- parsed[[i]]
o$fst <- src[[i]][1]
o$lst <- src[[i]][3]
o$call <- expr
if ( !o$exit ) eval(expr, envir=e) else break
local_report_envvar(sidefx)
local_report_cwd(sidefx)
local_report_files(sidefx)
local_report_locale(sidefx)
if (verbose == 2) print_status(prfile, o, color, print=TRUE)
}
td <- abs(Sys.time() - t0)
tx <- humanize(td, color=color)
if (verbose == 1){
# always when run in parallel. And we can only print once in that case
str <- print_status(prfile, o, color, print=FALSE)
if (o$exit) catf("%s %s %s\n", str, tx, o$exit_msg())
else catf("%s %s\n", str, tx)
}
if (verbose >= 2){
str <- if (o$exit) catf("%s %s\n", tx, o$exit_msg())
else catf("%s\n", tx)
}
# returns a 'list' of 'tinytest' objects
test_output <- o$gimme()
structure(test_output, class="tinytests", duration=td)
}
# readable output from a number of seconds.
humanize <- function(x, color=TRUE){
x <- as.numeric(x)
# ms units
str <- if (x < 0.1){
trimws(sprintf("%4.0fms",1000*x))
} else if (x < 60 ){
trimws(sprintf("%3.1fs",x))
} else if (x < 3600){
m <- x %/% 60
s <- x - m*60
trimws(sprintf("%2.0fm %3.1fs", m, s))
} else {
# fall-through: hours, minutes, seconds.
h <- x %/% 3600
m <- (x - 3600 * h)%/% 60
s <- x - 3600 * h - 60*m
sprintf("%dh %dm %3.1fs", h,m,s)
}
col <- if (x<0.1) "cyan" else "blue"
if (color) color_str(str, col) else str
}
color_str <- function(x, color){
cmap <- c(cyan=36, red=31, green=32, blue = 34)
sprintf("\033[0;%dm%s\033[0m", cmap[color], x)
}
check_double_colon <- function(filename){
txt <- readLines(filename, warn=FALSE)
i <- grepl("tinytest::expect", txt) & !grepl("#.*tinytest::expect", txt)
if (!any(i)) return(NULL)
line_numbers <- which(i)
occurrences <- sub("^.*tinytest::expect","tinytest::expect",txt[i])
occurrences <- sub("\\(.*","",occurrences)
prefix <-
" You are using 'tinytest::' to express test expectations.
The results from these tests are not collected. Found the following occurrences:
"
issues <- sprintf("> %s#%03d: %s",basename(filename),line_numbers,occurrences)
issues <- paste(issues, collapse="\n ")
postfix <- "\n Remove the 'tinytest::' prefix to register the test results."
message(paste(prefix, issues, postfix), call.=FALSE)
}
print_status <- function(filename, env, color, print=TRUE){
prefix <- sprintf("\r%s %4d tests", filename, env$ntest())
# print status after counter
fails <- if ( env$ntest() == 0 ) " " # print nothing if nothing was tested
else if ( env$nfail() == 0 ) sprintf(if(color) "\033[0;32mOK\033[0m" else "OK")
else sprintf(if (color) "\033[0;31m%d fails\033[0m" else "%d fails", env$nfail())
side <- if (env$nside() == 0) ""
else sprintf(if (color) "\033[0;93m%d side-effects\033[0m " else "%d side-effects ", env$nside())
if(print) cat(prefix, fails, side, sep=" ")
else paste(prefix, fails, side, sep=" ")
}
#' Run all tests in a directory
#'
#' \code{run_test_dir} runs all test files in a directory.
#'
#'
#' @param dir \code{[character]} path to directory
#' @param pattern \code{[character]} A regular expression that is used to find
#' scripts in \code{dir} containing tests (by default \code{.R} or \code{.r}
#' files starting with \code{test}).
#' @param at_home \code{[logical]} toggle local tests.
#' @param verbose \code{[logical]} toggle verbosity during execution
#' @param color \code{[logical]} toggle colorize output
#' @param remove_side_effects \code{[logical]} toggle remove user-defined side
#' effects. Environment variables (\code{Sys.setenv()}) and options (\code{options()})
#' defined in a test file are reset before running the next test file (see details).
#' @param cluster A \code{\link{makeCluster}} object.
#' @param lc_collate \code{[character]} Locale setting used to sort the
#' test files into the order of execution. The default \code{NA} ensures
#' current locale is used. Set this e.g. to \code{"C"} to ensure bytewise
#' and more platform-independent sorting (see details).
#' @param ... Arguments passed to \code{run_test_file}
#'
#' @section Details:
#'
#' We cannot guarantee that files will be run in any particular order accross
#' all platforms, as it depends on the available collation charts (a chart that
#' determines how alphabets are sorted). For this reason it is a good idea to
#' create test files that run independent of each other so their order of
#' execution does not matter. In tinytest, test files cannot share variables.
#' The default behavior of test runners further discourages interdependence by
#' resetting environment variables and options that are set in a test file
#' after the file is executed. If an environment variable needs to survive a
#' single file, use \code{base::Sys.setenv()} explicitly. Similarly, if an
#' option setting needs to survive, use \code{base::options()}
#'
#' @section Parallel tests:
#'
#' If \code{inherits(cluster, "cluster")} the tests are paralellized over a
#' cluster of worker nodes. \pkg{tinytest} will be loaded onto each cluster
#' node. All other preparation, including loading code from the tested package,
#' must be done by the user. It is also up to the user to clean up the cluster
#' after running tests. See the 'using tinytest' vignette for examples:
#' \code{vignette("using_tinytest")}.
#'
#'
#' @return A \code{tinytests} object
#'
#'
#' @examples
#' # create a test file in tempdir
#' tests <- "
#' addOne <- function(x) x + 2
#'
#' expect_true(addOne(0) > 0)
#' expect_equal(2, addOne(1))
#' "
#' testfile <- tempfile(pattern="test_", fileext=".R")
#' write(tests, testfile)
#'
#' # extract testdir
#' testdir <- dirname(testfile)
#' # run all files starting with 'test' in testdir
#' out <- run_test_dir(testdir)
#' print(out)
#' dat <- as.data.frame(out)
#'
#' @family test-files
#' @seealso \code{\link{makeCluster}},
#' \code{\link{clusterEvalQ}}, \code{\link{clusterExport}}
#'
#' @export
run_test_dir <- function(dir="inst/tinytest", pattern="^test.*\\.[rR]$"
, at_home = TRUE
, verbose = getOption("tt.verbose", 2)
, color = getOption("tt.pr.color",TRUE)
, remove_side_effects = TRUE
, cluster = NULL
, lc_collate = getOption("tt.collate",NA)
, ... ){
t0 <- Sys.time()
testfiles <- dir(dir, pattern=pattern, full.names=TRUE)
testfiles <- locale_sort(testfiles, lc_collate=lc_collate)
if ( !inherits(cluster, "cluster") ){
# set pwd here, to save time in run_test_file.
oldwd <- getwd()
set_call_wd(oldwd)
on.exit({setwd(oldwd); set_call_wd("")})
setwd(dir)
test_output <- lapply(basename(testfiles), run_test_file
, at_home = at_home
, verbose = verbose
, color = color
, remove_side_effects = remove_side_effects
, ...)
} else {
parallel::clusterEvalQ(cluster, library(tinytest))
test_output <- parallel::parLapply(cluster, testfiles
, run_test_file, at_home = at_home, verbose = min(verbose,1)
, color = color, remove_side_effects = TRUE, ...)
}
td <- abs(as.numeric(Sys.time()) - as.numeric(t0))
# by using '(parL)|(l)apply' we get a list of tinytests objects. We need to unwind
# one level to a list of 'tinytest' objects and class it 'tinytests'.
structure(unlist(test_output,recursive=FALSE), class="tinytests", duration=td)
}
# Sort according to LC_COLLATE
locale_sort <- function(x, lc_collate=NA, ...){
if (is.na(lc_collate)) return(sort(x,...))
# catch current locale
old_collate <- Sys.getlocale("LC_COLLATE")
# set to user-defined locale if possible, otherwise sort using current locale
colset <- tryCatch({
Sys.setlocale("LC_COLLATE", lc_collate)
TRUE
}, warning=function(e){
msg <- sprintf("Could not sort test files in 'C' locale, using %s\n"
, old_collate)
message(paste(msg, e$message,"\n"))
FALSE
}, error=warning)
out <- sort(x)
# reset to old locale
if (colset) Sys.setlocale("LC_COLLATE", old_collate)
out
}
#' Test a package during development
#'
#' \code{test_all} is a convenience function for package development, that
#' wraps \code{run_test_dir}. By default, it runs all files starting with
#' \code{test} in \code{./inst/tinytest/}. It is assumed that all functions to
#' be tested are loaded.
#'
#'
#' @param pkgdir \code{[character]} scalar. Root directory of the package (i.e.
#' direcory where \code{DESCRIPTION} and \code{NAMESPACE} reside).
#' @param testdir \code{[character]} scalar. Subdirectory where test files are
#' stored.
#'
#' @rdname run_test_dir
#'
#' @export
test_all <- function(pkgdir="./", testdir="inst/tinytest", ...){
run_test_dir( file.path(pkgdir,testdir), ...)
}
#' Detect not on CRANity
#'
#' Detect whether we are running at home (i.e. not on CRAN, BioConductor, ...)
#'
#'
#' @examples
#' # test will run locally, but not on CRAN
#' if ( at_home() ){
#' expect_equal(2, 1+1)
#' }
#' @export
#' @family test-functions test-file
at_home <- function(){
identical(Sys.getenv("TT_AT_HOME"),"TRUE")
}
#' Test a package during R CMD check or after installation
#'
#' Run all tests in an installed package. Throw an error and print all failed test
#' results when one or more tests fail if not in interactive mode (e.g. when
#' R CMD check tests a package). This function is intended to be
#' used by \code{R CMD check} or by a user that installed a package that
#' uses the \pkg{tinytest} test infrastructure.
#'
#' @param pkgname \code{[character]} scalar. Name of the package, as in the \code{DESCRIPTION} file.
#' @param testdir \code{[character]} scalar. Path to installed directory. By default
#' tinytest assumes that test files are in \code{inst/tinytest/}, which means
#' that after installation and thus during \code{R CMD check} they are in
#' \code{tinytest/}. See details for using alternate paths.
#' @param lib.loc \code{[character]} scalar. location where the package is installed.
#' @param at_home \code{[logical]} scalar. Are we at home? (see Details)
#' @param ncpu A positive integer, or a \code{\link{makeCluster}} object.
#' @param ... extra arguments passed to \code{\link{run_test_dir}} (e.g. \code{ncpu}).
#'
#'
#' @section Details:
#' We set \code{at_home=FALSE} by default so \code{R CMD check} will run the
#' same as at CRAN. See the package vignette (Section 4) for tips on how to set
#' up the package structure.
#' \code{vignette("using_tinytest",package="tinytest")}.
#'
#' Package authors who want to avoid installing tests with the package can
#' create a directory under \code{tests}. If the test directoy is called
#' \code{"tests/foo"}, use \code{test_package("pkgname", testdir="foo")} in
#' \code{tests/tinytest.R}.
#'
#'
#'
#' @return If \code{interactive()}, a \code{tinytests} object. If not
#' \code{interactive()}, an error is thrown when at least one test fails.
#'
#' @family test-files
#' @seealso \code{\link{setup_tinytest}}
#' @examples
#' \dontrun{
#' # Create a file with the following content, to use
#' # tinytest as your unit testing framework:
#' if (requireNamespace("tinytest", quietly=TRUE))
#' tinytest::test_package("your package name")
#' }
#' @export