|
| 1 | +package watch |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + buildkite "github.com/buildkite/go-buildkite/v4" |
| 7 | +) |
| 8 | + |
| 9 | +func TestTestTracker_Update(t *testing.T) { |
| 10 | + t.Run("reports new failures", func(t *testing.T) { |
| 11 | + tracker := NewTestTracker() |
| 12 | + tests := []buildkite.BuildTest{ |
| 13 | + { |
| 14 | + ID: "test-1", |
| 15 | + Name: "flaky test", |
| 16 | + LatestFail: &buildkite.BuildTestLatestFail{ |
| 17 | + ID: "exec-1", |
| 18 | + FailureReason: "expected 3, got 2", |
| 19 | + }, |
| 20 | + }, |
| 21 | + } |
| 22 | + |
| 23 | + newFailures := tracker.Update(tests) |
| 24 | + if len(newFailures) != 1 { |
| 25 | + t.Fatalf("expected 1 new failure, got %d", len(newFailures)) |
| 26 | + } |
| 27 | + if newFailures[0].Name != "flaky test" { |
| 28 | + t.Errorf("expected 'flaky test', got %q", newFailures[0].Name) |
| 29 | + } |
| 30 | + }) |
| 31 | + |
| 32 | + t.Run("does not re-report same execution", func(t *testing.T) { |
| 33 | + tracker := NewTestTracker() |
| 34 | + tests := []buildkite.BuildTest{ |
| 35 | + { |
| 36 | + ID: "test-1", |
| 37 | + Name: "flaky test", |
| 38 | + LatestFail: &buildkite.BuildTestLatestFail{ |
| 39 | + ID: "exec-1", |
| 40 | + FailureReason: "expected 3, got 2", |
| 41 | + }, |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + tracker.Update(tests) |
| 46 | + newFailures := tracker.Update(tests) |
| 47 | + if len(newFailures) != 0 { |
| 48 | + t.Errorf("expected 0 new failures on second poll, got %d", len(newFailures)) |
| 49 | + } |
| 50 | + }) |
| 51 | + |
| 52 | + t.Run("reports new execution for same test", func(t *testing.T) { |
| 53 | + tracker := NewTestTracker() |
| 54 | + tracker.Update([]buildkite.BuildTest{ |
| 55 | + { |
| 56 | + ID: "test-1", |
| 57 | + Name: "flaky test", |
| 58 | + LatestFail: &buildkite.BuildTestLatestFail{ |
| 59 | + ID: "exec-1", |
| 60 | + FailureReason: "first failure", |
| 61 | + }, |
| 62 | + }, |
| 63 | + }) |
| 64 | + |
| 65 | + newFailures := tracker.Update([]buildkite.BuildTest{ |
| 66 | + { |
| 67 | + ID: "test-1", |
| 68 | + Name: "flaky test", |
| 69 | + LatestFail: &buildkite.BuildTestLatestFail{ |
| 70 | + ID: "exec-2", |
| 71 | + FailureReason: "second failure", |
| 72 | + }, |
| 73 | + }, |
| 74 | + }) |
| 75 | + |
| 76 | + if len(newFailures) != 1 { |
| 77 | + t.Fatalf("expected 1 new failure, got %d", len(newFailures)) |
| 78 | + } |
| 79 | + if newFailures[0].LatestFail.ID != "exec-2" { |
| 80 | + t.Errorf("expected exec-2, got %s", newFailures[0].LatestFail.ID) |
| 81 | + } |
| 82 | + }) |
| 83 | + |
| 84 | + t.Run("skips tests without latest_fail", func(t *testing.T) { |
| 85 | + tracker := NewTestTracker() |
| 86 | + tests := []buildkite.BuildTest{ |
| 87 | + {ID: "test-1", Name: "passing test"}, |
| 88 | + { |
| 89 | + ID: "test-2", |
| 90 | + Name: "failing test", |
| 91 | + LatestFail: &buildkite.BuildTestLatestFail{ |
| 92 | + ID: "exec-1", |
| 93 | + FailureReason: "boom", |
| 94 | + }, |
| 95 | + }, |
| 96 | + } |
| 97 | + |
| 98 | + newFailures := tracker.Update(tests) |
| 99 | + if len(newFailures) != 1 { |
| 100 | + t.Fatalf("expected 1 new failure, got %d", len(newFailures)) |
| 101 | + } |
| 102 | + if newFailures[0].ID != "test-2" { |
| 103 | + t.Errorf("expected test-2, got %s", newFailures[0].ID) |
| 104 | + } |
| 105 | + }) |
| 106 | + |
| 107 | + t.Run("handles multiple new failures at once", func(t *testing.T) { |
| 108 | + tracker := NewTestTracker() |
| 109 | + tests := []buildkite.BuildTest{ |
| 110 | + { |
| 111 | + ID: "test-1", |
| 112 | + LatestFail: &buildkite.BuildTestLatestFail{ID: "exec-1"}, |
| 113 | + }, |
| 114 | + { |
| 115 | + ID: "test-2", |
| 116 | + LatestFail: &buildkite.BuildTestLatestFail{ID: "exec-2"}, |
| 117 | + }, |
| 118 | + { |
| 119 | + ID: "test-3", |
| 120 | + LatestFail: &buildkite.BuildTestLatestFail{ID: "exec-3"}, |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + newFailures := tracker.Update(tests) |
| 125 | + if len(newFailures) != 3 { |
| 126 | + t.Fatalf("expected 3 new failures, got %d", len(newFailures)) |
| 127 | + } |
| 128 | + }) |
| 129 | +} |
0 commit comments