go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/gerrit/equi_test.go (about)

     1  // Copyright 2020 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package gerrit
    16  
    17  import (
    18  	"encoding/hex"
    19  	"testing"
    20  
    21  	gerritpb "go.chromium.org/luci/common/proto/gerrit"
    22  
    23  	. "github.com/smartystreets/goconvey/convey"
    24  	. "go.chromium.org/luci/common/testing/assertions"
    25  )
    26  
    27  func TestEquivalentPatchsetRange(t *testing.T) {
    28  	t.Parallel()
    29  
    30  	Convey("EquivalentPatchsetRange", t, func() {
    31  
    32  		Convey("No revisions", func() {
    33  			_, _, err := EquivalentPatchsetRange(makeCI())
    34  			So(err, ShouldErrLike, "must have all revisions populated")
    35  		})
    36  
    37  		Convey("Wrong CurrentRevision", func() {
    38  			ci := makeCI(gerritpb.RevisionInfo_REWORK)
    39  			ci.CurrentRevision = ""
    40  			_, _, err := EquivalentPatchsetRange(ci)
    41  			So(err, ShouldErrLike, "must have current_revision populated")
    42  		})
    43  
    44  		Convey("Wrong Kind", func() {
    45  			_, _, err := EquivalentPatchsetRange(makeCI(
    46  				gerritpb.RevisionInfo_TRIVIAL_REBASE,
    47  				gerritpb.RevisionInfo_Kind(199)))
    48  			So(err, ShouldErrLike, "Unknown revision kind 199")
    49  		})
    50  
    51  		Convey("works", func() {
    52  			m, p, err := EquivalentPatchsetRange(makeCI(gerritpb.RevisionInfo_REWORK))
    53  			So(err, ShouldBeNil)
    54  			So(m, ShouldEqual, 1)
    55  			So(p, ShouldEqual, 1)
    56  
    57  			m, p, err = EquivalentPatchsetRange(makeCI(
    58  				gerritpb.RevisionInfo_REWORK,
    59  				gerritpb.RevisionInfo_TRIVIAL_REBASE,
    60  				gerritpb.RevisionInfo_REWORK,
    61  				gerritpb.RevisionInfo_NO_CHANGE,
    62  				gerritpb.RevisionInfo_NO_CODE_CHANGE,
    63  				gerritpb.RevisionInfo_MERGE_FIRST_PARENT_UPDATE,
    64  				gerritpb.RevisionInfo_TRIVIAL_REBASE,
    65  			))
    66  			So(err, ShouldBeNil)
    67  			So(m, ShouldEqual, 3)
    68  			So(p, ShouldEqual, 7)
    69  		})
    70  	})
    71  }
    72  
    73  func makeCI(kinds ...gerritpb.RevisionInfo_Kind) *gerritpb.ChangeInfo {
    74  	ci := &gerritpb.ChangeInfo{
    75  		Revisions: make(map[string]*gerritpb.RevisionInfo, len(kinds)),
    76  	}
    77  	var rev string
    78  	for i, k := range kinds {
    79  		rev = hex.EncodeToString([]byte{byte(i ^ 37), byte(k ^ 7)})
    80  		ci.Revisions[rev] = &gerritpb.RevisionInfo{Number: int32(i + 1), Kind: k}
    81  	}
    82  	ci.CurrentRevision = rev
    83  	return ci
    84  }