go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/config_service/internal/common/gitiles_test.go (about)

     1  // Copyright 2023 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 common
    16  
    17  import (
    18  	"testing"
    19  
    20  	cfgcommonpb "go.chromium.org/luci/common/proto/config"
    21  
    22  	. "github.com/smartystreets/goconvey/convey"
    23  	. "go.chromium.org/luci/common/testing/assertions"
    24  )
    25  
    26  func TestGitiles(t *testing.T) {
    27  	t.Parallel()
    28  
    29  	Convey("ValidateGitilesLocation", t, func() {
    30  		Convey("valid", func() {
    31  			loc := &cfgcommonpb.GitilesLocation{
    32  				Repo: "https://a.googlesource.com/ok",
    33  				Ref:  "refs/heads/main",
    34  				Path: "infra/config/generated",
    35  			}
    36  			So(ValidateGitilesLocation(loc), ShouldBeNil)
    37  		})
    38  
    39  		Convey("invalid", func() {
    40  			Convey("ref", func() {
    41  				So(ValidateGitilesLocation(&cfgcommonpb.GitilesLocation{}), ShouldErrLike, `ref must start with 'refs/'`)
    42  			})
    43  
    44  			Convey("path", func() {
    45  				So(ValidateGitilesLocation(&cfgcommonpb.GitilesLocation{Ref: "refs/heads/infra", Path: "/abc"}), ShouldErrLike, `path must not start with '/'`)
    46  			})
    47  
    48  			Convey("repo", func() {
    49  				So(ValidateGitilesLocation(&cfgcommonpb.GitilesLocation{Ref: "refs/heads/infra", Path: "abc"}), ShouldErrLike, "repo: not specified")
    50  				So(ValidateGitilesLocation(&cfgcommonpb.GitilesLocation{
    51  					Repo: "hostname",
    52  					Ref:  "refs/heads/infra",
    53  					Path: "dir/abc",
    54  				}), ShouldErrLike, "repo: only https scheme is supported")
    55  
    56  				So(ValidateGitilesLocation(&cfgcommonpb.GitilesLocation{
    57  					Repo: "https://a.googlesource.com/project/.git",
    58  					Ref:  "refs/heads/infra",
    59  					Path: "dir/abc",
    60  				}), ShouldErrLike, "repo: must not end with '.git'")
    61  
    62  				So(ValidateGitilesLocation(&cfgcommonpb.GitilesLocation{
    63  					Repo: "https://a.googlesource.com/a/project/",
    64  					Ref:  "refs/heads/infra",
    65  					Path: "dir/abc",
    66  				}), ShouldErrLike, "repo: must not have '/a/' prefix of a path component")
    67  
    68  				So(ValidateGitilesLocation(&cfgcommonpb.GitilesLocation{
    69  					Repo: "https://a.googlesource.com/project/",
    70  					Ref:  "refs/heads/infra",
    71  					Path: "dir/abc",
    72  				}), ShouldErrLike, "repo: must not end with '/'")
    73  			})
    74  		})
    75  	})
    76  }