CommandLineToolkit
626 строк · 24.6 Кб
1import Foundation
2import XCTest
3import InlineSnapshotTesting
4import Logging
5@testable import Console
6
7final class TraceHierarchyTests: XCTestCase {
8override func invokeTest() {
9withSnapshotTesting(record: .never) {
10super.invokeTest()
11}
12}
13
14func testTopLevelInProgressInfoTrace() {
15let component = TraceComponent.normal(level: .info)
16
17assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .default)) {
18"""
19╶ Test info trace, options: [] ⠋
20"""
21}
22
23assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .trace)) {
24"""
25╶ Test info trace, options: [] ⠋
26"""
27}
28
29assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .verbose)) {
30"""
31╶ Test info trace, options: [] ⠋
32"""
33}
34}
35
36func testTopLevelInProgressDebugTrace() {
37let component = TraceComponent.normal(level: .debug)
38
39assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .default)) {
40"""
41╶ Test debug trace, options: [] ⠋
42"""
43}
44
45assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .trace)) {
46"""
47╶ Test debug trace, options: [] ⠋
48"""
49}
50
51assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .verbose)) {
52"""
53╶ Test debug trace, options: [] ⠋
54"""
55}
56}
57
58func testTopLevelSuccessInfoTrace() {
59let component = TraceComponent.normal(level: .info, result: .success(()))
60
61assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .default)) {
62"""
63╶ Test info trace, options: [] ✔
64"""
65}
66
67assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .trace)) {
68"""
69╶ Test info trace, options: [] ✔
70"""
71}
72
73assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .verbose)) {
74"""
75╶ Test info trace, options: [] ✔
76"""
77}
78}
79
80func testTopLevelSuccessDebugTrace() {
81let component = TraceComponent.normal(level: .debug, result: .success(()))
82
83assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .default)) {
84"""
85╶ Test debug trace, options: [] ✔
86"""
87}
88
89assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .trace)) {
90"""
91╶ Test debug trace, options: [] ✔
92"""
93}
94
95assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .verbose)) {
96"""
97╶ Test debug trace, options: [] ✔
98"""
99}
100}
101
102func testTopLevelFailureDebugTrace() {
103let component = TraceComponent.normal(level: .debug, result: .failure("error"))
104
105assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .default)) {
106"""
107╶ Test debug trace, options: [] ✘
108"""
109}
110
111assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .trace)) {
112"""
113╶ Test debug trace, options: [] ✘
114"""
115}
116
117assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .verbose)) {
118"""
119╶ Test debug trace, options: [] ✘
120"""
121}
122}
123
124func testTopLevelFailureInfoTrace() {
125let component = TraceComponent.normal(level: .info, result: .failure("error"))
126
127assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .default)) {
128"""
129╶ Test info trace, options: [] ✘
130"""
131}
132
133assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .trace)) {
134"""
135╶ Test info trace, options: [] ✘
136"""
137}
138
139assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .verbose)) {
140"""
141╶ Test info trace, options: [] ✘
142"""
143}
144}
145
146func testExternalNormalNestedRunning() {
147let component = TraceComponent.normal(level: .info) {
148TraceComponent.normal(level: .info)
149TraceComponent.normal(level: .debug)
150
151TraceComponent.normal(level: .info) {
152TraceComponent.normal(level: .info)
153TraceComponent.normal(level: .debug)
154}
155
156TraceComponent.normal(level: .debug) {
157TraceComponent.normal(level: .info)
158TraceComponent.normal(level: .debug)
159}
160}
161
162assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .verbose)) {
163"""
164╭ Test info trace, options: [] ⠋
165│ ╶ Test info trace, options: [] ⠋
166│ ╶ Test debug trace, options: [] ⠋
167│ ╭ Test info trace, options: [] ⠋
168│ │ ╶ Test info trace, options: [] ⠋
169│ │ ╶ Test debug trace, options: [] ⠋
170│ ╰
171│ ╭ Test debug trace, options: [] ⠋
172│ │ ╶ Test info trace, options: [] ⠋
173│ │ ╶ Test debug trace, options: [] ⠋
174│ ╰
175╰
176"""
177}
178
179assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .trace)) {
180"""
181╭ Test info trace, options: [] ⠋
182│ ╶ Test info trace, options: [] ⠋
183│ ╶ Test debug trace, options: [] ⠋
184│ ╭ Test info trace, options: [] ⠋
185│ │ ╶ Test info trace, options: [] ⠋
186│ │ ╶ Test debug trace, options: [] ⠋
187│ ╰
188│ ╭ Test debug trace, options: [] ⠋
189│ │ ╶ Test info trace, options: [] ⠋
190│ │ ╶ Test debug trace, options: [] ⠋
191│ ╰
192╰
193"""
194}
195
196assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .default)) {
197"""
198╭ Test info trace, options: [] ⠋
199│ ╶ Test info trace, options: [] ⠋
200│ ╭ Test info trace, options: [] ⠋
201│ │ ╶ Test info trace, options: [] ⠋
202│ ╰
203│ ╭ Test debug trace, options: [] ⠋
204│ │ ╶ Test info trace, options: [] ⠋
205│ ╰
206╰
207"""
208}
209}
210
211func testExternalVerboseNestedSuccess() {
212let component = TraceComponent.normal(level: .info) {
213TraceComponent.normal(level: .info, result: .success(()))
214TraceComponent.normal(level: .debug, result: .success(()))
215
216TraceComponent.normal(level: .info) {
217TraceComponent.normal(level: .info, result: .success(()))
218TraceComponent.normal(level: .debug, result: .success(()))
219}
220
221TraceComponent.normal(level: .debug) {
222TraceComponent.normal(level: .info, result: .success(()))
223TraceComponent.normal(level: .debug, result: .success(()))
224}
225
226TraceComponent.normal(level: .debug) {
227TraceComponent.normal(level: .debug, result: .success(()))
228}
229}
230
231assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .verbose)) {
232"""
233╭ Test info trace, options: [] ⠋
234│ ╶ Test info trace, options: [] ✔
235│ ╶ Test debug trace, options: [] ✔
236│ ╭ Test info trace, options: [] ⠋
237│ │ ╶ Test info trace, options: [] ✔
238│ │ ╶ Test debug trace, options: [] ✔
239│ ╰
240│ ╭ Test debug trace, options: [] ⠋
241│ │ ╶ Test info trace, options: [] ✔
242│ │ ╶ Test debug trace, options: [] ✔
243│ ╰
244│ ╭ Test debug trace, options: [] ⠋
245│ │ ╶ Test debug trace, options: [] ✔
246│ ╰
247╰
248"""
249}
250
251assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .trace)) {
252"""
253╭ Test info trace, options: [] ⠋
254│ ╶ Test info trace, options: [] ✔
255│ ╶ Test debug trace, options: [] ✔
256│ ╭ Test info trace, options: [] ⠋
257│ │ ╶ Test info trace, options: [] ✔
258│ │ ╶ Test debug trace, options: [] ✔
259│ ╰
260│ ╭ Test debug trace, options: [] ⠋
261│ │ ╶ Test info trace, options: [] ✔
262│ │ ╶ Test debug trace, options: [] ✔
263│ ╰
264│ ╭ Test debug trace, options: [] ⠋
265│ │ ╶ Test debug trace, options: [] ✔
266│ ╰
267╰
268"""
269}
270
271assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .default)) {
272"""
273╭ Test info trace, options: [] ⠋
274│ ╶ Test info trace, options: [] ✔
275│ ╭ Test info trace, options: [] ⠋
276│ │ ╶ Test info trace, options: [] ✔
277│ ╰
278│ ╭ Test debug trace, options: [] ⠋
279│ │ ╶ Test info trace, options: [] ✔
280│ ╰
281╰
282"""
283}
284}
285
286func testExternalCollapseNestedRunning() {
287let component = TraceComponent.collapse(level: .info) {
288TraceComponent.normal(level: .info)
289TraceComponent.normal(level: .debug)
290
291TraceComponent.collapse(level: .info) {
292TraceComponent.normal(level: .info)
293TraceComponent.normal(level: .debug)
294}
295
296TraceComponent.collapse(level: .debug) {
297TraceComponent.normal(level: .info)
298TraceComponent.normal(level: .debug)
299}
300
301TraceComponent.collapse(level: .debug) {
302TraceComponent.normal(level: .debug)
303}
304}
305
306assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .verbose)) {
307"""
308╭ Test info trace, options: [collapseFinished] ⠋
309│ ╶ Test info trace, options: [] ⠋
310│ ╶ Test debug trace, options: [] ⠋
311│ ╭ Test info trace, options: [collapseFinished] ⠋
312│ │ ╶ Test info trace, options: [] ⠋
313│ │ ╶ Test debug trace, options: [] ⠋
314│ ╰
315│ ╭ Test debug trace, options: [collapseFinished] ⠋
316│ │ ╶ Test info trace, options: [] ⠋
317│ │ ╶ Test debug trace, options: [] ⠋
318│ ╰
319│ ╭ Test debug trace, options: [collapseFinished] ⠋
320│ │ ╶ Test debug trace, options: [] ⠋
321│ ╰
322╰
323"""
324}
325
326assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .trace)) {
327"""
328╭ Test info trace, options: [collapseFinished] ⠋
329│ ╶ Test info trace, options: [] ⠋
330│ ╶ Test debug trace, options: [] ⠋
331│ ╭ Test info trace, options: [collapseFinished] ⠋
332│ │ ╶ Test info trace, options: [] ⠋
333│ │ ╶ Test debug trace, options: [] ⠋
334│ ╰
335│ ╭ Test debug trace, options: [collapseFinished] ⠋
336│ │ ╶ Test info trace, options: [] ⠋
337│ │ ╶ Test debug trace, options: [] ⠋
338│ ╰
339│ ╭ Test debug trace, options: [collapseFinished] ⠋
340│ │ ╶ Test debug trace, options: [] ⠋
341│ ╰
342╰
343"""
344}
345
346assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .default)) {
347"""
348╭ Test info trace, options: [collapseFinished] ⠋
349│ ╶ Test info trace, options: [] ⠋
350│ ╭ Test info trace, options: [collapseFinished] ⠋
351│ │ ╶ Test info trace, options: [] ⠋
352│ ╰
353│ ╭ Test debug trace, options: [collapseFinished] ⠋
354│ │ ╶ Test info trace, options: [] ⠋
355│ ╰
356╰
357"""
358}
359}
360
361func testExternalCollapseNestedSuccess() {
362let component = TraceComponent.collapse(level: .info) {
363TraceComponent.normal(level: .info, result: .success(()))
364TraceComponent.normal(level: .trace, result: .success(()))
365
366TraceComponent.collapse(level: .info) {
367TraceComponent.normal(level: .info, result: .success(()))
368TraceComponent.normal(level: .trace, result: .success(()))
369}
370
371TraceComponent.collapse(level: .trace) {
372TraceComponent.normal(level: .info, result: .success(()))
373TraceComponent.normal(level: .trace, result: .success(()))
374}
375
376TraceComponent.collapse(level: .trace) {
377TraceComponent.normal(level: .trace, result: .success(()))
378}
379}
380
381assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .verbose)) {
382"""
383╭ Test info trace, options: [collapseFinished] ⠋
384│ ╶ Test info trace, options: [] ✔
385│ ╶ Test trace trace, options: [] ✔
386│ ╭ Test info trace, options: [collapseFinished] ⠋
387│ │ ╶ Test info trace, options: [] ✔
388│ │ ╶ Test trace trace, options: [] ✔
389│ ╰
390│ ╭ Test trace trace, options: [collapseFinished] ⠋
391│ │ ╶ Test info trace, options: [] ✔
392│ │ ╶ Test trace trace, options: [] ✔
393│ ╰
394│ ╭ Test trace trace, options: [collapseFinished] ⠋
395│ │ ╶ Test trace trace, options: [] ✔
396│ ╰
397╰
398"""
399}
400
401assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .trace)) {
402"""
403╭ Test info trace, options: [collapseFinished] ⠋
404│ ╶ Test info trace, options: [collapseFinished] ⠋
405│ ╭ Test trace trace, options: [collapseFinished] ⠋
406│ │ ╶ Test info trace, options: [] ✔
407│ ╰
408│ ╶ Test trace trace, options: [collapseFinished] ⠋
409╰
410"""
411}
412
413assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .default)) {
414"""
415╭ Test info trace, options: [collapseFinished] ⠋
416│ ╶ Test info trace, options: [collapseFinished] ⠋
417│ ╭ Test trace trace, options: [collapseFinished] ⠋
418│ │ ╶ Test info trace, options: [] ✔
419│ ╰
420╰
421"""
422}
423}
424
425func testExternalCollapseNestedFailure() {
426let component = TraceComponent.collapse(level: .info) {
427TraceComponent.normal(level: .info, result: .failure("error"))
428TraceComponent.normal(level: .debug, result: .failure("error"))
429
430TraceComponent.collapse(level: .info) {
431TraceComponent.normal(level: .info, result: .failure("error"))
432TraceComponent.normal(level: .debug, result: .failure("error"))
433}
434
435TraceComponent.collapse(level: .debug) {
436TraceComponent.normal(level: .info, result: .failure("error"))
437TraceComponent.normal(level: .debug, result: .failure("error"))
438}
439
440TraceComponent.collapse(level: .debug) {
441TraceComponent.normal(level: .debug, result: .failure("error"))
442}
443}
444
445assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .verbose)) {
446"""
447╭ Test info trace, options: [collapseFinished] ⠋
448│ ╶ Test info trace, options: [] ✘
449│ ╶ Test debug trace, options: [] ✘
450│ ╭ Test info trace, options: [collapseFinished] ⠋
451│ │ ╶ Test info trace, options: [] ✘
452│ │ ╶ Test debug trace, options: [] ✘
453│ ╰
454│ ╭ Test debug trace, options: [collapseFinished] ⠋
455│ │ ╶ Test info trace, options: [] ✘
456│ │ ╶ Test debug trace, options: [] ✘
457│ ╰
458│ ╭ Test debug trace, options: [collapseFinished] ⠋
459│ │ ╶ Test debug trace, options: [] ✘
460│ ╰
461╰
462"""
463}
464
465assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .trace)) {
466"""
467╭ Test info trace, options: [collapseFinished] ⠋
468│ ╶ Test info trace, options: [] ✘
469│ ╶ Test debug trace, options: [] ✘
470│ ╭ Test info trace, options: [collapseFinished] ⠋
471│ │ ╶ Test info trace, options: [] ✘
472│ │ ╶ Test debug trace, options: [] ✘
473│ ╰
474│ ╭ Test debug trace, options: [collapseFinished] ⠋
475│ │ ╶ Test info trace, options: [] ✘
476│ │ ╶ Test debug trace, options: [] ✘
477│ ╰
478│ ╭ Test debug trace, options: [collapseFinished] ⠋
479│ │ ╶ Test debug trace, options: [] ✘
480│ ╰
481╰
482"""
483}
484
485assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .default)) {
486"""
487╭ Test info trace, options: [collapseFinished] ⠋
488│ ╶ Test info trace, options: [] ✘
489│ ╶ Test debug trace, options: [] ✘
490│ ╭ Test info trace, options: [collapseFinished] ⠋
491│ │ ╶ Test info trace, options: [] ✘
492│ │ ╶ Test debug trace, options: [] ✘
493│ ╰
494│ ╭ Test debug trace, options: [collapseFinished] ⠋
495│ │ ╶ Test info trace, options: [] ✘
496│ │ ╶ Test debug trace, options: [] ✘
497│ ╰
498│ ╭ Test debug trace, options: [collapseFinished] ⠋
499│ │ ╶ Test debug trace, options: [] ✘
500│ ╰
501╰
502"""
503}
504}
505
506func testExternalCollapseNestedFailureShouldUnfoldOnlyErrorBranch() {
507let component = TraceComponent.collapse(level: .info, result: .failure("error")) {
508TraceComponent.collapse(level: .info, result: .success(())) {
509TraceComponent.normal(level: .info, result: .success(()))
510}
511TraceComponent.collapse(level: .trace, result: .success(())) {
512TraceComponent.normal(level: .trace, result: .success(()))
513}
514
515TraceComponent.collapse(level: .trace, result: .failure("error")) {
516TraceComponent.collapse(level: .trace, result: .success(())) {
517LogComponent(state: .init(level: .trace, message: "Trace message", metadata: [:], source: "", file: "", function: "", line: 0))
518LogComponent(state: .init(level: .info, message: "Info message", metadata: [:], source: "", file: "", function: "", line: 0))
519LogStreamComponent(state: .init(level: .trace, name: "Trace process", lines: ["message"], result: .failure(.init(statusCode: 1))))
520LogStreamComponent(state: .init(level: .info, name: "Info process", lines: ["message"]))
521}
522TraceComponent.collapse(level: .trace, result: .failure("error")) {
523LogComponent(state: .init(level: .trace, message: "Trace message", metadata: [:], source: "", file: "", function: "", line: 0))
524LogComponent(state: .init(level: .info, message: "Info message", metadata: [:], source: "", file: "", function: "", line: 0))
525LogStreamComponent(state: .init(level: .trace, name: "Trace process", lines: ["message"], result: .failure(.init(statusCode: 1))))
526LogStreamComponent(state: .init(level: .info, name: "Info process", lines: ["message"]))
527}
528}
529}
530
531assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .verbose)) {
532"""
533╭ Test info trace, options: [collapseFinished] ✘
534│ ╭ Test info trace, options: [collapseFinished] ✔
535│ │ ╶ Test info trace, options: [] ✔
536│ ╰
537│ ╭ Test trace trace, options: [collapseFinished] ✔
538│ │ ╶ Test trace trace, options: [] ✔
539│ ╰
540│ ╭ Test trace trace, options: [collapseFinished] ✘
541│ │ ╭ Test trace trace, options: [collapseFinished] ✔
542│ │ │ ╶ Trace message
543│ │ │ ╶ Info message
544│ │ │ ╭ Trace process ✘
545│ │ │ │ message
546│ │ │ ╰
547│ │ │ ╭ Info process ⠋
548│ │ │ │ message
549│ │ │ ╰
550│ │ ╰
551│ │ ╭ Test trace trace, options: [collapseFinished] ✘
552│ │ │ ╶ Trace message
553│ │ │ ╶ Info message
554│ │ │ ╭ Trace process ✘
555│ │ │ │ message
556│ │ │ ╰
557│ │ │ ╭ Info process ⠋
558│ │ │ │ message
559│ │ │ ╰
560│ │ ╰
561│ ╰
562╰
563"""
564}
565
566assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .trace)) {
567"""
568╭ Test info trace, options: [collapseFinished] ✘
569│ ╶ Test info trace, options: [collapseFinished] ✔
570│ ╶ Test trace trace, options: [collapseFinished] ✔
571│ ╭ Test trace trace, options: [collapseFinished] ✘
572│ │ ╭ Test trace trace, options: [collapseFinished] ✔
573│ │ │ ╶ Info message
574│ │ │ ╭ Trace process ✘
575│ │ │ │ message
576│ │ │ ╰
577│ │ │ ╭ Info process ⠋
578│ │ │ │ message
579│ │ │ ╰
580│ │ ╰
581│ │ ╭ Test trace trace, options: [collapseFinished] ✘
582│ │ │ ╶ Trace message
583│ │ │ ╶ Info message
584│ │ │ ╭ Trace process ✘
585│ │ │ │ message
586│ │ │ ╰
587│ │ │ ╭ Info process ⠋
588│ │ │ │ message
589│ │ │ ╰
590│ │ ╰
591│ ╰
592╰
593"""
594}
595
596assertInlineSnapshot(of: component, as: .consoleText(verbositySettings: .default)) {
597"""
598╭ Test info trace, options: [collapseFinished] ✘
599│ ╶ Test info trace, options: [collapseFinished] ✔
600│ ╶ Test trace trace, options: [collapseFinished] ✔
601│ ╭ Test trace trace, options: [collapseFinished] ✘
602│ │ ╭ Test trace trace, options: [collapseFinished] ✔
603│ │ │ ╶ Info message
604│ │ │ ╭ Trace process ✘
605│ │ │ │ message
606│ │ │ ╰
607│ │ │ ╭ Info process ⠋
608│ │ │ │ message
609│ │ │ ╰
610│ │ ╰
611│ │ ╭ Test trace trace, options: [collapseFinished] ✘
612│ │ │ ╶ Trace message
613│ │ │ ╶ Info message
614│ │ │ ╭ Trace process ✘
615│ │ │ │ message
616│ │ │ ╰
617│ │ │ ╭ Info process ⠋
618│ │ │ │ message
619│ │ │ ╰
620│ │ ╰
621│ ╰
622╰
623"""
624}
625}
626}
627