go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/led/ledcli/edit_cl_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 ledcli
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	. "github.com/smartystreets/goconvey/convey"
    22  	. "go.chromium.org/luci/common/testing/assertions"
    23  
    24  	bbpb "go.chromium.org/luci/buildbucket/proto"
    25  	"go.chromium.org/luci/common/errors"
    26  )
    27  
    28  func TestParseCLURL(t *testing.T) {
    29  	t.Parallel()
    30  
    31  	cases := []struct {
    32  		url             string
    33  		err             string
    34  		cl              *bbpb.GerritChange
    35  		resolvePatchset int64
    36  	}{
    37  		{
    38  			url: "",
    39  			err: "only *-review.googlesource.com URLs are supported",
    40  		},
    41  
    42  		{
    43  			url: "%20://",
    44  			err: "URL_TO_CHANGELIST: parse",
    45  		},
    46  
    47  		{
    48  			url: "https://other.domain.example.com/stuff/things",
    49  			err: "only *-review.googlesource.com URLs are supported",
    50  		},
    51  
    52  		{
    53  			url: "https://thing-review.googlesource.com/",
    54  			err: "old/empty",
    55  		},
    56  
    57  		{
    58  			url: "https://thing-review.googlesource.com/#/c/oldstyle",
    59  			err: "old/empty",
    60  		},
    61  
    62  		{
    63  			url: "https://thing-review.googlesource.com/wat",
    64  			err: "gerrit URL parsing change",
    65  		},
    66  
    67  		{
    68  			url: "https://thing-review.googlesource.com/c/+/1235",
    69  			err: "missing project",
    70  		},
    71  
    72  		{
    73  			url: "https://thing-review.googlesource.com/c/project/+",
    74  			err: "missing change/patchset",
    75  		},
    76  
    77  		{
    78  			url: "https://thing-review.googlesource.com/c/project/+/nan",
    79  			err: "parsing change",
    80  		},
    81  
    82  		{
    83  			url: "https://thing-review.googlesource.com/c/project/+/123/nan",
    84  			err: "parsing patchset",
    85  		},
    86  
    87  		{
    88  			url: "https://thing-review.googlesource.com/c/project/+/1111",
    89  			err: "TEST: resolvePatchset not set",
    90  		},
    91  
    92  		{
    93  			url: "https://thing-review.googlesource.com/c/project/+/123",
    94  
    95  			resolvePatchset: 1024,
    96  			cl: &bbpb.GerritChange{
    97  				Host:     "thing-review.googlesource.com",
    98  				Project:  "project",
    99  				Change:   123,
   100  				Patchset: 1024,
   101  			},
   102  		},
   103  
   104  		{
   105  			url: "https://thing-review.googlesource.com/c/project/+/123/1337",
   106  			cl: &bbpb.GerritChange{
   107  				Host:     "thing-review.googlesource.com",
   108  				Project:  "project",
   109  				Change:   123,
   110  				Patchset: 1337,
   111  			},
   112  		},
   113  
   114  		{
   115  			url: "https://thing-review.git.corp.google.com/c/project/+/123/1337",
   116  			cl: &bbpb.GerritChange{
   117  				Host:     "thing-review.googlesource.com",
   118  				Project:  "project",
   119  				Change:   123,
   120  				Patchset: 1337,
   121  			},
   122  		},
   123  	}
   124  
   125  	Convey(`parseCrChangeListURL`, t, func() {
   126  		for _, tc := range cases {
   127  			tc := tc
   128  			Convey(fmt.Sprintf("%q", tc.url), func() {
   129  				cl, err := parseCrChangeListURL(tc.url, func(string, int64) (string, int64, error) {
   130  					if tc.resolvePatchset != 0 {
   131  						return "project", tc.resolvePatchset, nil
   132  					}
   133  					return "", 0, errors.New("TEST: resolvePatchset not set")
   134  				})
   135  				if tc.err != "" {
   136  					So(err, ShouldErrLike, tc.err)
   137  					So(cl, ShouldBeNil)
   138  				} else {
   139  					So(err, ShouldBeNil)
   140  					So(cl, ShouldResembleProto, tc.cl)
   141  				}
   142  			})
   143  		}
   144  
   145  	})
   146  }