// Copyright 2019 Drone.IO Inc. All rights reserved. // Use of this source code is governed by the Drone Non-Commercial License // that can be found in the LICENSE file. package canceler import ( "testing" "github.com/drone/drone/core" ) func TestMatch(t *testing.T) { tests := []struct { build *core.Build repo *core.Repository want bool }{ // does not match repository id { build: &core.Build{RepoID: 2}, repo: &core.Repository{ID: 1}, want: false, }, // does not match build number requirement that // must be older than current build { build: &core.Build{RepoID: 1, Number: 2}, repo: &core.Repository{ID: 1, Build: &core.Build{Number: 3}}, want: false, }, { build: &core.Build{RepoID: 1, Number: 2}, repo: &core.Repository{ID: 1, Build: &core.Build{Number: 2}}, want: false, }, // does not match required status { build: &core.Build{RepoID: 1, Number: 2}, repo: &core.Repository{ID: 1, Build: &core.Build{Number: 1, Status: core.StatusPassing}}, want: false, }, // does not match (one of) required event types { build: &core.Build{RepoID: 1, Number: 2, Event: core.EventPullRequest}, repo: &core.Repository{ID: 1, Build: &core.Build{ Number: 1, Status: core.StatusPending, Event: core.EventPush, }}, want: false, }, // does not match ref { build: &core.Build{RepoID: 1, Number: 2, Event: core.EventPush, Ref: "refs/heads/master"}, repo: &core.Repository{ID: 1, Build: &core.Build{ Number: 1, Status: core.StatusPending, Event: core.EventPush, Ref: "refs/heads/develop", }}, want: false, }, // // successful matches // { build: &core.Build{RepoID: 1, Number: 2, Event: core.EventPush, Ref: "refs/heads/master"}, repo: &core.Repository{ID: 1, Build: &core.Build{ Number: 1, Status: core.StatusPending, Event: core.EventPush, Ref: "refs/heads/master", }}, want: true, }, { build: &core.Build{RepoID: 1, Number: 2, Event: core.EventPullRequest, Ref: "refs/heads/master"}, repo: &core.Repository{ID: 1, Build: &core.Build{ Number: 1, Status: core.StatusPending, Event: core.EventPullRequest, Ref: "refs/heads/master", }}, want: true, }, } for i, test := range tests { if got, want := match(test.build, test.repo), test.want; got != want { t.Errorf("Want match %v at index %d, got %v", want, i, got) } } }