|
1 | 1 | package automerge |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/bivas/rivi/bot" |
| 5 | + "github.com/bivas/rivi/bot/mock" |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +type mockMergableEventData struct { |
| 11 | + mock.MockEventData |
| 12 | + merged bool |
| 13 | + method string |
| 14 | +} |
| 15 | + |
| 16 | +func (m *mockMergableEventData) Merge(mergeMethod string) { |
| 17 | + m.merged = true |
| 18 | + m.method = mergeMethod |
| 19 | +} |
| 20 | + |
| 21 | +func TestNotCapableToMerge(t *testing.T) { |
| 22 | + action := action{rule: &rule{}} |
| 23 | + action.rule.Defaults() |
| 24 | + config := &mock.MockConfiguration{} |
| 25 | + meta := &mock.MockEventData{Assignees: []string{"user1"}, Comments: []bot.Comment{ |
| 26 | + {Commenter: "user1", Comment: "approved"}, |
| 27 | + }} |
| 28 | + action.Apply(config, meta) |
| 29 | + assert.NotNil(t, action.err, "should be unable to merge") |
| 30 | +} |
| 31 | + |
| 32 | +func TestCapableToMerge(t *testing.T) { |
| 33 | + action := action{rule: &rule{}} |
| 34 | + action.rule.Defaults() |
| 35 | + config := &mock.MockConfiguration{} |
| 36 | + mockEventData := mock.MockEventData{Assignees: []string{"user1"}, Comments: []bot.Comment{ |
| 37 | + {Commenter: "user1", Comment: "approved"}, |
| 38 | + }} |
| 39 | + meta := &mockMergableEventData{MockEventData: mockEventData} |
| 40 | + action.Apply(config, meta) |
| 41 | + assert.True(t, meta.merged, "should be merged") |
| 42 | + assert.Equal(t, "merge", meta.method, "default should be merge") |
| 43 | +} |
| 44 | + |
| 45 | +func TestOriginComment(t *testing.T) { |
| 46 | + action := action{rule: &rule{}} |
| 47 | + action.rule.Defaults() |
| 48 | + config := &mock.MockConfiguration{} |
| 49 | + mockEventData := mock.MockEventData{Assignees: []string{"user1"}, Origin: "user2", Comments: []bot.Comment{ |
| 50 | + {Commenter: "user2", Comment: "approved"}, |
| 51 | + }} |
| 52 | + meta := &mockMergableEventData{MockEventData: mockEventData} |
| 53 | + action.Apply(config, meta) |
| 54 | + assert.False(t, meta.merged, "should not be merged") |
| 55 | +} |
| 56 | + |
| 57 | +func TestNotApprovedComment(t *testing.T) { |
| 58 | + action := action{rule: &rule{}} |
| 59 | + action.rule.Defaults() |
| 60 | + config := &mock.MockConfiguration{} |
| 61 | + mockEventData := mock.MockEventData{Assignees: []string{"user1"}, Origin: "user2", Comments: []bot.Comment{ |
| 62 | + {Commenter: "user1", Comment: "not approved"}, |
| 63 | + }} |
| 64 | + meta := &mockMergableEventData{MockEventData: mockEventData} |
| 65 | + action.Apply(config, meta) |
| 66 | + assert.False(t, meta.merged, "should not be merged") |
| 67 | +} |
| 68 | + |
| 69 | +func TestSameApprovedComment(t *testing.T) { |
| 70 | + action := action{rule: &rule{Require: 2}} |
| 71 | + action.rule.Defaults() |
| 72 | + config := &mock.MockConfiguration{} |
| 73 | + mockEventData := mock.MockEventData{Assignees: []string{"user1"}, Origin: "user2", Comments: []bot.Comment{ |
| 74 | + {Commenter: "user1", Comment: "approved"}, |
| 75 | + {Commenter: "user1", Comment: "approved"}, |
| 76 | + }} |
| 77 | + meta := &mockMergableEventData{MockEventData: mockEventData} |
| 78 | + action.Apply(config, meta) |
| 79 | + assert.False(t, meta.merged, "should not be merged") |
| 80 | +} |
0 commit comments