github.com/abayer/test-infra@v0.0.5/mungegithub/github/status_change_test.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package github
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  )
    23  
    24  func TestStatusChange(t *testing.T) {
    25  	sc := NewStatusChange()
    26  
    27  	// Initially we have nothing
    28  	want := []int{}
    29  	if got := sc.PopChangedPullRequests(); !reflect.DeepEqual(got, want) {
    30  		t.Errorf("Changed() = %+v, %+v", got, want)
    31  	}
    32  
    33  	// Commit 123456 has changed, but we don't know what PR it is, just discard
    34  	sc.CommitStatusChanged("123456")
    35  	want = []int{}
    36  	if got := sc.PopChangedPullRequests(); !reflect.DeepEqual(got, want) {
    37  		t.Errorf("Changed() = %+v, %+v", got, want)
    38  	}
    39  
    40  	// Let's add a ref for this
    41  	sc.UpdatePullRequestHead(1, "123456")
    42  	want = []int{}
    43  	if got := sc.PopChangedPullRequests(); !reflect.DeepEqual(got, want) {
    44  		t.Errorf("Changed() = %+v, %+v", got, want)
    45  	}
    46  
    47  	// Now we have an actual change
    48  	sc.CommitStatusChanged("123456")
    49  
    50  	want = []int{1}
    51  	if got := sc.PopChangedPullRequests(); !reflect.DeepEqual(got, want) {
    52  		t.Errorf("Changed() = %+v, %+v", got, want)
    53  	}
    54  
    55  	// We only get it once
    56  	want = []int{}
    57  	if got := sc.PopChangedPullRequests(); !reflect.DeepEqual(got, want) {
    58  		t.Errorf("Changed() = %+v, %+v", got, want)
    59  	}
    60  
    61  	// Let's have another change
    62  	sc.CommitStatusChanged("123456")
    63  	// But the PR changes to another head
    64  	sc.UpdatePullRequestHead(1, "654321")
    65  
    66  	// That change is not considered
    67  	want = []int{}
    68  	if got := sc.PopChangedPullRequests(); !reflect.DeepEqual(got, want) {
    69  		t.Errorf("Changed() = %+v, %+v", got, want)
    70  	}
    71  }