go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/gerrit/cqdepend/cqdepend_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 cqdepend
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/smartystreets/goconvey/convey"
    21  	. "go.chromium.org/luci/common/testing/assertions"
    22  )
    23  
    24  func TestParser(t *testing.T) {
    25  	t.Parallel()
    26  
    27  	Convey("Parse works", t, func() {
    28  		Convey("Basic", func() {
    29  			So(Parse("Nothing\n\ninteresting."), ShouldBeNil)
    30  			So(Parse("Title.\n\nCq-Depend: 456,123"), ShouldResemble, []Dep{
    31  				{Subdomain: "", Change: 123},
    32  				{Subdomain: "", Change: 456},
    33  			})
    34  		})
    35  		Convey("Case and space incensitive", func() {
    36  			So(Parse("Title.\n\nCQ-dePend: Any-case:456 , Zx:23 "), ShouldResemble, []Dep{
    37  				{Subdomain: "any-case", Change: 456},
    38  				{Subdomain: "zx", Change: 23},
    39  			})
    40  		})
    41  		Convey("Dedup and multiline", func() {
    42  			So(Parse("Title.\n\nCq-Depend: 456,123\nCq-Depend: 123,y:456"), ShouldResemble, []Dep{
    43  				{Subdomain: "", Change: 123},
    44  				{Subdomain: "", Change: 456},
    45  				{Subdomain: "y", Change: 456},
    46  			})
    47  		})
    48  		Convey("Ignores errors", func() {
    49  			So(Parse("Title.\n\nCq-Depend: 2, x;3\nCq-Depend: y-review:4,z:5"), ShouldResemble, []Dep{
    50  				{Subdomain: "", Change: 2},
    51  				{Subdomain: "z", Change: 5},
    52  			})
    53  		})
    54  		Convey("Ignores non-footers", func() {
    55  			So(Parse("Cq-Depend: 1\n\nCq-Depend: i:2\n\nChange-Id: Ideadbeef"), ShouldBeNil)
    56  		})
    57  	})
    58  }
    59  
    60  func TestParseSingleDep(t *testing.T) {
    61  	t.Parallel()
    62  
    63  	Convey("parseSingleDep works", t, func() {
    64  		Convey("OK", func() {
    65  			d, err := parseSingleDep(" x:123 ")
    66  			So(err, ShouldBeNil)
    67  			So(d, ShouldResemble, Dep{Change: 123, Subdomain: "x"})
    68  
    69  			d, err = parseSingleDep("123")
    70  			So(err, ShouldBeNil)
    71  			So(d, ShouldResemble, Dep{Change: 123, Subdomain: ""})
    72  		})
    73  		Convey("Invalid format", func() {
    74  			_, err := parseSingleDep("weird/value:here")
    75  			So(err, ShouldErrLike, "must match")
    76  			_, err = parseSingleDep("https://abc.example.com:123")
    77  			So(err, ShouldErrLike, "must match")
    78  			_, err = parseSingleDep("abc-review.example.com:1")
    79  			So(err, ShouldErrLike, "must match")
    80  			_, err = parseSingleDep("no-spaces-around-colon :1")
    81  			So(err, ShouldErrLike, "must match")
    82  			_, err = parseSingleDep("no-spaces-around-colon: 2")
    83  			So(err, ShouldErrLike, "must match")
    84  		})
    85  		Convey("Too large", func() {
    86  			_, err := parseSingleDep("12312123123123123123")
    87  			So(err, ShouldErrLike, "change number too large")
    88  		})
    89  		Convey("Disallow -Review", func() {
    90  			_, err := parseSingleDep("x-review:1")
    91  			So(err, ShouldErrLike, "must not include '-review'")
    92  		})
    93  	})
    94  }