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

     1  // Copyright 2021 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 run
    16  
    17  import (
    18  	"strings"
    19  	"testing"
    20  
    21  	"go.chromium.org/luci/cv/internal/changelist"
    22  	"go.chromium.org/luci/cv/internal/gerrit/metadata"
    23  
    24  	. "github.com/smartystreets/goconvey/convey"
    25  	. "go.chromium.org/luci/common/testing/assertions"
    26  )
    27  
    28  func TestExtractOptions(t *testing.T) {
    29  	t.Parallel()
    30  
    31  	Convey("ExtractOptions works", t, func() {
    32  		extract := func(msg string) *Options {
    33  			lines := strings.Split(strings.TrimSpace(msg), "\n")
    34  			for i, l := range lines {
    35  				lines[i] = strings.TrimSpace(l)
    36  			}
    37  			msg = strings.Join(lines, "\n")
    38  			return ExtractOptions(&changelist.Snapshot{Metadata: metadata.Extract(msg)})
    39  		}
    40  		Convey("Default", func() {
    41  			So(extract(`
    42  				CL title.
    43  
    44  				SOME_COMMIT_TAG=ignored-by-CV
    45  				BUG=1
    46  				YES_BUG=is also a tag
    47  
    48  				Gerrit-Or-Git-Style-Footer: but its key not recognized by CV either.
    49  				Change-Id: Ideadbeef
    50  				Bug: 1
    51  				Yes-Bug-Above: is a Git/Gerrit footer
    52        `), ShouldResembleProto, &Options{})
    53  		})
    54  
    55  		Convey("No-Tree-Checks", func() {
    56  			So(extract(`
    57  				CL title.
    58  
    59  				No-Tree-Checks: true
    60        `), ShouldResembleProto, &Options{
    61  				SkipTreeChecks: true,
    62  			})
    63  			So(extract(`
    64  				CL title.
    65  
    66  				NOTREECHECKS=true
    67        `), ShouldResembleProto, &Options{
    68  				SkipTreeChecks: true,
    69  			})
    70  		})
    71  
    72  		Convey("No-Try / No-Presubmit", func() {
    73  			So(extract(`
    74  				CL title.
    75  
    76  				NOPRESUBMIT=true
    77  
    78  				No-Try: true
    79        `), ShouldResembleProto, &Options{
    80  				SkipTryjobs:   true,
    81  				SkipPresubmit: true,
    82  			})
    83  			So(extract(`
    84  				CL title.
    85  
    86  				NOTRY=true
    87  
    88  				No-Presubmit: true
    89        `), ShouldResembleProto, &Options{
    90  				SkipTryjobs:   true,
    91  				SkipPresubmit: true,
    92  			})
    93  		})
    94  
    95  		Convey("Cq-Do-Not-Cancel-Tryjobs", func() {
    96  			So(extract(`
    97  				CL title.
    98  
    99  				Cq-Do-Not-Cancel-Tryjobs: true
   100        `), ShouldResembleProto, &Options{
   101  				AvoidCancellingTryjobs: true,
   102  			})
   103  		})
   104  
   105  		Convey("No-Equivalent-Builders", func() {
   106  			So(extract(`
   107  				CL title.
   108  
   109  				No-Equivalent-Builders: true
   110        `), ShouldResembleProto, &Options{
   111  				SkipEquivalentBuilders: true,
   112  			})
   113  		})
   114  
   115  		Convey("Cq-Include-Trybots/CQ_INCLUDE_TRYBOTS", func() {
   116  			So(extract(`
   117  				CL title.
   118  
   119  				Cq-Include-Trybots: project/bucket:builder1,builder2;project2/bucket:builder3
   120  				CQ_INCLUDE_TRYBOTS=project/bucket:builder4
   121  			`), ShouldResembleProto,
   122  				&Options{
   123  					IncludedTryjobs: []string{
   124  						"project/bucket:builder1,builder2;project2/bucket:builder3",
   125  						"project/bucket:builder4",
   126  					},
   127  				})
   128  		})
   129  
   130  		Convey("Override-Tryjobs-For-Automation", func() {
   131  			So(extract(`
   132  				CL title.
   133  
   134  				Override-Tryjobs-For-Automation: project/bucket:builder1,builder2;project2/bucket:builder3
   135  				Override-Tryjobs-For-Automation: project/bucket:builder4
   136  			`), ShouldResembleProto,
   137  				&Options{
   138  					OverriddenTryjobs: []string{
   139  						"project/bucket:builder4",
   140  						"project/bucket:builder1,builder2;project2/bucket:builder3",
   141  					},
   142  				})
   143  		})
   144  
   145  		Convey("Cq-Cl-Tag", func() {
   146  			// legacy format (i.e. CQ_CL_TAG=XXX) is not supported.
   147  			So(extract(`
   148  				CL title.
   149  
   150  				Cq-Cl-Tag: foo:bar
   151  				Cq-Cl-Tag: foo:baz
   152  				CQ_CL_TAG=another_foo:another_bar
   153  			`), ShouldResembleProto,
   154  				&Options{
   155  					CustomTryjobTags: []string{
   156  						"foo:baz",
   157  						"foo:bar",
   158  					},
   159  				})
   160  		})
   161  
   162  		Convey("If keys are repeated, any true value means true", func() {
   163  			So(extract(`
   164  				CL title.
   165  
   166  				NOTRY=true
   167  				No-Try: false
   168        `), ShouldResembleProto, &Options{
   169  				SkipTryjobs: true,
   170  			})
   171  			So(extract(`
   172  				CL title.
   173  
   174  				NOTRY=false
   175  				No-Try: true
   176        `), ShouldResembleProto, &Options{
   177  				SkipTryjobs: true,
   178  			})
   179  		})
   180  
   181  	})
   182  }
   183  
   184  func TestMergeOptions(t *testing.T) {
   185  	t.Parallel()
   186  
   187  	Convey("MergeOptions works", t, func() {
   188  		o := &Options{}
   189  		So(MergeOptions(o, nil), ShouldResembleProto, o)
   190  
   191  		a := &Options{
   192  			SkipTreeChecks:         true,
   193  			AvoidCancellingTryjobs: true,
   194  		}
   195  		So(MergeOptions(a, o), ShouldResembleProto, a)
   196  
   197  		b := &Options{
   198  			SkipTreeChecks:         true,
   199  			SkipEquivalentBuilders: true,
   200  		}
   201  		So(MergeOptions(a, b), ShouldResembleProto, &Options{
   202  			SkipTreeChecks:         true,
   203  			SkipEquivalentBuilders: true,
   204  			AvoidCancellingTryjobs: true,
   205  		})
   206  	})
   207  }