go.fuchsia.dev/infra@v0.0.0-20240507153436-9b593402251b/buildbucket/builder_test.go (about)

     1  // Copyright 2019 The Fuchsia Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style license that can be
     3  // found in the LICENSE file.
     4  
     5  package buildbucket
     6  
     7  import (
     8  	"flag"
     9  	"reflect"
    10  	"testing"
    11  
    12  	buildbucketpb "go.chromium.org/luci/buildbucket/proto"
    13  )
    14  
    15  func TestBuilderIDGetter(t *testing.T) {
    16  	tests := []struct {
    17  		// The name of this test case
    18  		name string
    19  
    20  		// The string to parse into a BuilderID
    21  		input string
    22  
    23  		// The expected result.
    24  		output *buildbucketpb.BuilderID
    25  
    26  		// Whether to expect an error
    27  		expectErr bool
    28  	}{
    29  		{
    30  			name:  "should parse an input string into a BuilderID",
    31  			input: "project/bucket/builder",
    32  			output: &buildbucketpb.BuilderID{
    33  				Project: "project",
    34  				Bucket:  "bucket",
    35  				Builder: "builder",
    36  			},
    37  		}, {
    38  			name:      "should err when the input contains < 2 fields",
    39  			expectErr: true,
    40  			output:    &buildbucketpb.BuilderID{},
    41  		}, {
    42  			name:      "should err when the input is empty",
    43  			expectErr: true,
    44  			output:    &buildbucketpb.BuilderID{},
    45  		},
    46  	}
    47  
    48  	for _, tt := range tests {
    49  		t.Run(tt.name, func(t *testing.T) {
    50  			value := BuilderID()
    51  			err := value.Set(tt.input)
    52  			if err != nil != tt.expectErr {
    53  				if tt.expectErr {
    54  					t.Error("expected an err but got nil")
    55  				} else {
    56  					t.Errorf("wanted %s but got an err: %s", tt.output, err)
    57  				}
    58  			}
    59  
    60  			// Compare flag values directly rather than using reflect.DeepEqual to prove
    61  			// that `Get` returns a valid object.
    62  			builderID := value.Get().(*buildbucketpb.BuilderID)
    63  			if !reflect.DeepEqual(builderID, tt.output) {
    64  				t.Errorf("got\n%s\nbut wanted:\n%s", builderID, tt.output)
    65  			}
    66  		})
    67  	}
    68  }
    69  
    70  func TestBuilderIDString(t *testing.T) {
    71  	tests := []struct {
    72  		// The name of this test case
    73  		name string
    74  
    75  		// The input BuilderID
    76  		input flag.Value
    77  
    78  		// The expected string.
    79  		output string
    80  
    81  		// Whether to expect an error
    82  		expectErr bool
    83  	}{
    84  		{
    85  			name: "should format the ID as a string",
    86  			input: &BuilderIDFlag{
    87  				project: "project",
    88  				bucket:  "bucket",
    89  				builder: "builder",
    90  			},
    91  			output: "project/bucket/builder",
    92  		}, {
    93  			name: "when the ID is empty",
    94  			input: &BuilderIDFlag{
    95  				project: "",
    96  				bucket:  "",
    97  				builder: "",
    98  			},
    99  			output: "//",
   100  		}, {
   101  			name: "when Project is empty",
   102  			input: &BuilderIDFlag{
   103  				project: "",
   104  				bucket:  "bucket",
   105  				builder: "builder",
   106  			},
   107  			output: "/bucket/builder",
   108  		}, {
   109  			name: "when Bucket is empty",
   110  			input: &BuilderIDFlag{
   111  				project: "project",
   112  				bucket:  "",
   113  				builder: "builder",
   114  			},
   115  			output: "project//builder",
   116  		}, {
   117  			name: "when Builder is empty",
   118  			input: &BuilderIDFlag{
   119  				project: "project",
   120  				bucket:  "bucket",
   121  				builder: "",
   122  			},
   123  			output: "project/bucket/",
   124  		},
   125  	}
   126  
   127  	for _, tt := range tests {
   128  		t.Run(tt.name, func(t *testing.T) {
   129  			output := tt.input.String()
   130  			if output != tt.output {
   131  				t.Errorf("got %q but wanted %q", output, tt.output)
   132  			}
   133  		})
   134  	}
   135  }