reporting.odin 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. //+private
  2. package testing
  3. import "base:runtime"
  4. import "core:encoding/ansi"
  5. import "core:fmt"
  6. import "core:io"
  7. import "core:mem"
  8. import "core:path/filepath"
  9. import "core:strings"
  10. // Definitions of colors for use in the test runner.
  11. SGR_RESET :: ansi.CSI + ansi.RESET + ansi.SGR
  12. SGR_READY :: ansi.CSI + ansi.FG_BRIGHT_BLACK + ansi.SGR
  13. SGR_RUNNING :: ansi.CSI + ansi.FG_YELLOW + ansi.SGR
  14. SGR_SUCCESS :: ansi.CSI + ansi.FG_GREEN + ansi.SGR
  15. SGR_FAILED :: ansi.CSI + ansi.FG_RED + ansi.SGR
  16. MAX_PROGRESS_WIDTH :: 100
  17. // More than enough bytes to cover long package names, long test names, dozens
  18. // of ANSI codes, et cetera.
  19. LINE_BUFFER_SIZE :: (MAX_PROGRESS_WIDTH * 8 + 224) * runtime.Byte
  20. PROGRESS_COLUMN_SPACING :: 2
  21. Package_Run :: struct {
  22. name: string,
  23. header: string,
  24. frame_ready: bool,
  25. redraw_buffer: [LINE_BUFFER_SIZE]byte,
  26. redraw_string: string,
  27. last_change_state: Test_State,
  28. last_change_name: string,
  29. tests: []Internal_Test,
  30. test_states: []Test_State,
  31. }
  32. Report :: struct {
  33. packages: []Package_Run,
  34. packages_by_name: map[string]^Package_Run,
  35. pkg_column_len: int,
  36. test_column_len: int,
  37. progress_width: int,
  38. all_tests: []Internal_Test,
  39. all_test_states: []Test_State,
  40. }
  41. // Organize all tests by package and sort out test state data.
  42. make_report :: proc(internal_tests: []Internal_Test) -> (report: Report, error: runtime.Allocator_Error) {
  43. assert(len(internal_tests) > 0, "make_report called with no tests")
  44. packages: [dynamic]Package_Run
  45. report.all_tests = internal_tests
  46. report.all_test_states = make([]Test_State, len(internal_tests)) or_return
  47. // First, figure out what belongs where.
  48. #no_bounds_check cur_pkg := internal_tests[0].pkg
  49. pkg_start: int
  50. // This loop assumes the tests are sorted by package already.
  51. for it, index in internal_tests {
  52. if cur_pkg != it.pkg {
  53. #no_bounds_check {
  54. append(&packages, Package_Run {
  55. name = cur_pkg,
  56. tests = report.all_tests[pkg_start:index],
  57. test_states = report.all_test_states[pkg_start:index],
  58. }) or_return
  59. }
  60. when PROGRESS_WIDTH == 0 {
  61. report.progress_width = max(report.progress_width, index - pkg_start)
  62. }
  63. pkg_start = index
  64. report.pkg_column_len = max(report.pkg_column_len, len(cur_pkg))
  65. cur_pkg = it.pkg
  66. }
  67. report.test_column_len = max(report.test_column_len, len(it.name))
  68. }
  69. // Handle the last (or only) package.
  70. #no_bounds_check {
  71. append(&packages, Package_Run {
  72. name = cur_pkg,
  73. header = cur_pkg,
  74. tests = report.all_tests[pkg_start:],
  75. test_states = report.all_test_states[pkg_start:],
  76. }) or_return
  77. }
  78. when PROGRESS_WIDTH == 0 {
  79. report.progress_width = max(report.progress_width, len(internal_tests) - pkg_start)
  80. } else {
  81. report.progress_width = PROGRESS_WIDTH
  82. }
  83. report.progress_width = min(report.progress_width, MAX_PROGRESS_WIDTH)
  84. report.pkg_column_len = PROGRESS_COLUMN_SPACING + max(report.pkg_column_len, len(cur_pkg))
  85. shrink(&packages) or_return
  86. for &pkg in packages {
  87. pkg.header = fmt.aprintf("%- *[1]s[", pkg.name, report.pkg_column_len)
  88. assert(len(pkg.header) > 0, "Error allocating package header string.")
  89. // This is safe because the array is done resizing, and it has the same
  90. // lifetime as the map.
  91. report.packages_by_name[pkg.name] = &pkg
  92. }
  93. // It's okay to discard the dynamic array's allocator information here,
  94. // because its capacity has been shrunk to its length, it was allocated by
  95. // the caller's context allocator, and it will be deallocated by the same.
  96. //
  97. // `delete_slice` is equivalent to `delete_dynamic_array` in this case.
  98. report.packages = packages[:]
  99. return
  100. }
  101. destroy_report :: proc(report: ^Report) {
  102. for pkg in report.packages {
  103. delete(pkg.header)
  104. }
  105. delete(report.packages)
  106. delete(report.packages_by_name)
  107. delete(report.all_test_states)
  108. }
  109. redraw_package :: proc(w: io.Writer, report: Report, pkg: ^Package_Run) {
  110. if pkg.frame_ready {
  111. io.write_string(w, pkg.redraw_string)
  112. return
  113. }
  114. // Write the output line here so we can cache it.
  115. line_builder := strings.builder_from_bytes(pkg.redraw_buffer[:])
  116. line_writer := strings.to_writer(&line_builder)
  117. highest_run_index: int
  118. failed_count: int
  119. done_count: int
  120. #no_bounds_check for i := 0; i < len(pkg.test_states); i += 1 {
  121. switch pkg.test_states[i] {
  122. case .Ready:
  123. continue
  124. case .Running:
  125. highest_run_index = max(highest_run_index, i)
  126. case .Successful:
  127. done_count += 1
  128. case .Failed:
  129. failed_count += 1
  130. done_count += 1
  131. }
  132. }
  133. start := max(0, highest_run_index - (report.progress_width - 1))
  134. end := min(start + report.progress_width, len(pkg.test_states))
  135. // This variable is to keep track of the last ANSI code emitted, in
  136. // order to avoid repeating the same code over in a sequence.
  137. //
  138. // This should help reduce screen flicker.
  139. last_state := Test_State(-1)
  140. io.write_string(line_writer, pkg.header)
  141. #no_bounds_check for state in pkg.test_states[start:end] {
  142. switch state {
  143. case .Ready:
  144. if last_state != state {
  145. io.write_string(line_writer, SGR_READY)
  146. last_state = state
  147. }
  148. case .Running:
  149. if last_state != state {
  150. io.write_string(line_writer, SGR_RUNNING)
  151. last_state = state
  152. }
  153. case .Successful:
  154. if last_state != state {
  155. io.write_string(line_writer, SGR_SUCCESS)
  156. last_state = state
  157. }
  158. case .Failed:
  159. if last_state != state {
  160. io.write_string(line_writer, SGR_FAILED)
  161. last_state = state
  162. }
  163. }
  164. io.write_byte(line_writer, '|')
  165. }
  166. for _ in 0 ..< report.progress_width - (end - start) {
  167. io.write_byte(line_writer, ' ')
  168. }
  169. io.write_string(line_writer, SGR_RESET + "] ")
  170. ticker: string
  171. if done_count == len(pkg.test_states) {
  172. ticker = "[package done]"
  173. if failed_count > 0 {
  174. ticker = fmt.tprintf("%s (" + SGR_FAILED + "%i" + SGR_RESET + " failed)", ticker, failed_count)
  175. }
  176. } else {
  177. if len(pkg.last_change_name) == 0 {
  178. #no_bounds_check pkg.last_change_name = pkg.tests[0].name
  179. }
  180. switch pkg.last_change_state {
  181. case .Ready:
  182. ticker = fmt.tprintf(SGR_READY + "%s" + SGR_RESET, pkg.last_change_name)
  183. case .Running:
  184. ticker = fmt.tprintf(SGR_RUNNING + "%s" + SGR_RESET, pkg.last_change_name)
  185. case .Failed:
  186. ticker = fmt.tprintf(SGR_FAILED + "%s" + SGR_RESET, pkg.last_change_name)
  187. case .Successful:
  188. ticker = fmt.tprintf(SGR_SUCCESS + "%s" + SGR_RESET, pkg.last_change_name)
  189. }
  190. }
  191. if done_count == len(pkg.test_states) {
  192. fmt.wprintfln(line_writer, " % 4i :: %s",
  193. len(pkg.test_states),
  194. ticker,
  195. )
  196. } else {
  197. fmt.wprintfln(line_writer, "% 4i/% 4i :: %s",
  198. done_count,
  199. len(pkg.test_states),
  200. ticker,
  201. )
  202. }
  203. pkg.redraw_string = strings.to_string(line_builder)
  204. pkg.frame_ready = true
  205. io.write_string(w, pkg.redraw_string)
  206. }
  207. redraw_report :: proc(w: io.Writer, report: Report) {
  208. // If we print a line longer than the user's terminal can handle, it may
  209. // wrap around, shifting the progress report out of alignment.
  210. //
  211. // There are ways to get the current terminal width, and that would be the
  212. // ideal way to handle this, but it would require system-specific code such
  213. // as setting STDIN to be non-blocking in order to read the response from
  214. // the ANSI DSR escape code, or reading environment variables.
  215. //
  216. // The DECAWM escape codes control whether or not the terminal will wrap
  217. // long lines or overwrite the last visible character.
  218. // This should be fine for now.
  219. //
  220. // Note that we only do this for the animated summary; log messages are
  221. // still perfectly fine to wrap, as they're printed in their own batch,
  222. // whereas the animation depends on each package being only on one line.
  223. //
  224. // Of course, if you resize your terminal while it's printing, things can
  225. // still break...
  226. fmt.wprint(w, ansi.CSI + ansi.DECAWM_OFF)
  227. for &pkg in report.packages {
  228. redraw_package(w, report, &pkg)
  229. }
  230. fmt.wprint(w, ansi.CSI + ansi.DECAWM_ON)
  231. }
  232. needs_to_redraw :: proc(report: Report) -> bool {
  233. for pkg in report.packages {
  234. if !pkg.frame_ready {
  235. return true
  236. }
  237. }
  238. return false
  239. }
  240. draw_status_bar :: proc(w: io.Writer, threads_string: string, total_done_count, total_test_count: int) {
  241. if total_done_count == total_test_count {
  242. // All tests are done; print a blank line to maintain the same height
  243. // of the progress report.
  244. fmt.wprintln(w)
  245. } else {
  246. fmt.wprintfln(w,
  247. "%s % 4i/% 4i :: total",
  248. threads_string,
  249. total_done_count,
  250. total_test_count)
  251. }
  252. }
  253. write_memory_report :: proc(w: io.Writer, tracker: ^mem.Tracking_Allocator, pkg, name: string) {
  254. fmt.wprintf(w,
  255. "<% 10M/% 10M> <% 10M> (% 5i/% 5i) :: %s.%s",
  256. tracker.current_memory_allocated,
  257. tracker.total_memory_allocated,
  258. tracker.peak_memory_allocated,
  259. tracker.total_free_count,
  260. tracker.total_allocation_count,
  261. pkg,
  262. name)
  263. for ptr, entry in tracker.allocation_map {
  264. fmt.wprintf(w,
  265. "\n +++ leak % 10M @ %p [%s:%i:%s()]",
  266. entry.size,
  267. ptr,
  268. filepath.base(entry.location.file_path),
  269. entry.location.line,
  270. entry.location.procedure)
  271. }
  272. for entry in tracker.bad_free_array {
  273. fmt.wprintf(w,
  274. "\n +++ bad free @ %p [%s:%i:%s()]",
  275. entry.memory,
  276. filepath.base(entry.location.file_path),
  277. entry.location.line,
  278. entry.location.procedure)
  279. }
  280. }