File: reporter-teamcity.R

package info (click to toggle)
r-cran-testthat 3.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,452 kB
  • sloc: cpp: 9,261; ansic: 37; sh: 14; makefile: 5
file content (82 lines) | stat: -rw-r--r-- 2,193 bytes parent folder | download | duplicates (3)
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
#' Test reporter: Teamcity format.
#'
#' This reporter will output results in the Teamcity message format.
#' For more information about Teamcity messages, see
#' http://confluence.jetbrains.com/display/TCD7/Build+Script+Interaction+with+TeamCity
#'
#' @export
#' @family reporters
TeamcityReporter <- R6::R6Class("TeamcityReporter",
  inherit = Reporter,
  public = list(
    i = NA_integer_,

    initialize = function(...) {
      super$initialize(...)
      self$capabilities$parallel_support <- TRUE
    },

    start_context = function(context) {
      private$report_event("testSuiteStarted", context)
    },

    end_context = function(context) {
      private$report_event("testSuiteFinished", context)
      self$cat_line()
      self$cat_line()
    },

    start_test = function(context, test) {
      private$report_event("testSuiteStarted", test)
      self$i <- 1L
    },

    end_test = function(context, test) {
      private$report_event("testSuiteFinished", test)
      self$cat_line()
    },

    add_result = function(context, test, result) {
      testName <- paste0("expectation ", self$i)
      self$i <- self$i + 1L

      if (expectation_skip(result)) {
        private$report_event("testIgnored", testName, message = format(result))
        return()
      }

      private$report_event("testStarted", testName)

      if (!expectation_ok(result)) {
        lines <- strsplit(format(result), "\n")[[1]]

        private$report_event(
          "testFailed", testName, message = lines[1],
          details = paste(lines[-1], collapse = "\n")
        )
      }
      private$report_event("testFinished", testName)
    }
  ),

  private = list(
    report_event = function(event, name, ...) {
      values <- list(name = name, ...)

      values <- vapply(values, teamcity_escape, character(1))
      if (length(values) == 0) {
        value_string <- ""
      } else {
        value_string <- paste0(names(values), "='", values, "'", collapse = " ")
      }

      self$cat_line("##teamcity[", event, " ", value_string, "]")
    }
  )
)

# teamcity escape character is |
teamcity_escape <- function(s) {
  s <- gsub("(['|]|\\[|\\])", "|\\1", s)
  gsub("\n", "|n", s)
}