xref: /aosp_15_r20/external/rappor/analysis/R/run_tests.R (revision 2abb31345f6c95944768b5222a9a5ed3fc68cc00)
1*2abb3134SXin Li#!/usr/bin/env Rscript
2*2abb3134SXin Li#
3*2abb3134SXin Li# Copyright 2014 Google Inc. All rights reserved.
4*2abb3134SXin Li#
5*2abb3134SXin Li# Licensed under the Apache License, Version 2.0 (the "License");
6*2abb3134SXin Li# you may not use this file except in compliance with the License.
7*2abb3134SXin Li# You may obtain a copy of the License at
8*2abb3134SXin Li#
9*2abb3134SXin Li#     http://www.apache.org/licenses/LICENSE-2.0
10*2abb3134SXin Li#
11*2abb3134SXin Li# Unless required by applicable law or agreed to in writing, software
12*2abb3134SXin Li# distributed under the License is distributed on an "AS IS" BASIS,
13*2abb3134SXin Li# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*2abb3134SXin Li# See the License for the specific language governing permissions and
15*2abb3134SXin Li# limitations under the License.
16*2abb3134SXin Li
17*2abb3134SXin Li#
18*2abb3134SXin Li# Run unit tests for RAPPOR R code.
19*2abb3134SXin Li
20*2abb3134SXin Lilibrary(RUnit)
21*2abb3134SXin Li
22*2abb3134SXin Lirun_tests <- function() {
23*2abb3134SXin Li  dirs <- "analysis/R"  # Run from root
24*2abb3134SXin Li  test_suite <- defineTestSuite("rappor", dirs, testFileRegexp = "_test.R$",
25*2abb3134SXin Li                                testFuncRegexp = "^Test")
26*2abb3134SXin Li  stopifnot(isValidTestSuite(test_suite))
27*2abb3134SXin Li
28*2abb3134SXin Li  test_result <- runTestSuite(test_suite)
29*2abb3134SXin Li
30*2abb3134SXin Li  printTextProtocol(test_result)  # print to stdout
31*2abb3134SXin Li
32*2abb3134SXin Li  result <- test_result[[1]]  # Result for our only suite
33*2abb3134SXin Li
34*2abb3134SXin Li  # Sanity check: fail if there were no tests found.
35*2abb3134SXin Li  if (result$nTestFunc == 0) {
36*2abb3134SXin Li    cat("No tests found.\n")
37*2abb3134SXin Li    return(FALSE)
38*2abb3134SXin Li  }
39*2abb3134SXin Li  if (result$nFail != 0 || result$nErr != 0) {
40*2abb3134SXin Li    cat("Some tests failed.\n")
41*2abb3134SXin Li    return(FALSE)
42*2abb3134SXin Li  }
43*2abb3134SXin Li  return(TRUE)
44*2abb3134SXin Li}
45*2abb3134SXin Li
46*2abb3134SXin Liif (!run_tests()) {
47*2abb3134SXin Li  quit(status = 1)
48*2abb3134SXin Li}
49