|
| 1 | +package printer |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/mszostok/codeowners-validator/internal/check" |
| 10 | + "github.com/mszostok/codeowners-validator/internal/ptr" |
| 11 | + |
| 12 | + "github.com/sebdah/goldie/v2" |
| 13 | +) |
| 14 | + |
| 15 | +func TestTTYPrinterPrintCheckResult(t *testing.T) { |
| 16 | + t.Run("Should print all reported issues", func(t *testing.T) { |
| 17 | + // given |
| 18 | + tty := TTYPrinter{} |
| 19 | + |
| 20 | + buff := &bytes.Buffer{} |
| 21 | + restore := overrideWriter(buff) |
| 22 | + defer restore() |
| 23 | + |
| 24 | + // when |
| 25 | + tty.PrintCheckResult("Foo Checker", time.Second, check.Output{ |
| 26 | + Issues: []check.Issue{ |
| 27 | + { |
| 28 | + Severity: check.Error, |
| 29 | + LineNo: ptr.Uint64Ptr(42), |
| 30 | + Message: "Simulate error in line 42", |
| 31 | + }, |
| 32 | + { |
| 33 | + Severity: check.Warning, |
| 34 | + LineNo: ptr.Uint64Ptr(2020), |
| 35 | + Message: "Simulate warning in line 2020", |
| 36 | + }, |
| 37 | + { |
| 38 | + Severity: check.Error, |
| 39 | + Message: "Error without line number", |
| 40 | + }, |
| 41 | + { |
| 42 | + Severity: check.Warning, |
| 43 | + Message: "Warning without line number", |
| 44 | + }, |
| 45 | + }, |
| 46 | + }) |
| 47 | + |
| 48 | + // then |
| 49 | + g := goldie.New(t, goldie.WithNameSuffix(".golden.txt")) |
| 50 | + g.Assert(t, t.Name(), buff.Bytes()) |
| 51 | + }) |
| 52 | + |
| 53 | + t.Run("Should print OK status on empty issues list", func(t *testing.T) { |
| 54 | + // given |
| 55 | + tty := TTYPrinter{} |
| 56 | + |
| 57 | + buff := &bytes.Buffer{} |
| 58 | + restore := overrideWriter(buff) |
| 59 | + defer restore() |
| 60 | + |
| 61 | + // when |
| 62 | + tty.PrintCheckResult("Foo Checker", time.Second, check.Output{ |
| 63 | + Issues: nil, |
| 64 | + }) |
| 65 | + |
| 66 | + // then |
| 67 | + g := goldie.New(t, goldie.WithNameSuffix(".golden.txt")) |
| 68 | + g.Assert(t, t.Name(), buff.Bytes()) |
| 69 | + }) |
| 70 | +} |
| 71 | + |
| 72 | +func TestTTYPrinterPrintSummary(t *testing.T) { |
| 73 | + t.Run("Should print number of failures", func(t *testing.T) { |
| 74 | + // given |
| 75 | + tty := TTYPrinter{} |
| 76 | + |
| 77 | + buff := &bytes.Buffer{} |
| 78 | + restore := overrideWriter(buff) |
| 79 | + defer restore() |
| 80 | + |
| 81 | + // when |
| 82 | + tty.PrintSummary(20, 10) |
| 83 | + |
| 84 | + // then |
| 85 | + g := goldie.New(t, goldie.WithNameSuffix(".golden.txt")) |
| 86 | + g.Assert(t, t.Name(), buff.Bytes()) |
| 87 | + }) |
| 88 | + |
| 89 | + t.Run("Should print 'no' when there is no failures", func(t *testing.T) { |
| 90 | + // given |
| 91 | + tty := TTYPrinter{} |
| 92 | + |
| 93 | + buff := &bytes.Buffer{} |
| 94 | + restore := overrideWriter(buff) |
| 95 | + defer restore() |
| 96 | + |
| 97 | + // when |
| 98 | + tty.PrintSummary(20, 0) |
| 99 | + |
| 100 | + // then |
| 101 | + g := goldie.New(t, goldie.WithNameSuffix(".golden.txt")) |
| 102 | + g.Assert(t, t.Name(), buff.Bytes()) |
| 103 | + }) |
| 104 | +} |
| 105 | + |
| 106 | +func overrideWriter(in io.Writer) func() { |
| 107 | + old := writer |
| 108 | + writer = in |
| 109 | + return func() { writer = old } |
| 110 | +} |
0 commit comments