go.fuchsia.dev/infra@v0.0.0-20240507153436-9b593402251b/cmd/recipe_wrapper/git/git_test.go (about)

     1  // Copyright 2024 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 git
     6  
     7  import (
     8  	"testing"
     9  
    10  	buildbucketpb "go.chromium.org/luci/buildbucket/proto"
    11  	"google.golang.org/protobuf/types/known/structpb"
    12  )
    13  
    14  func TestChooseRef(t *testing.T) {
    15  	tests := []struct {
    16  		name  string
    17  		ref   string
    18  		props map[string]any
    19  		want  string
    20  	}{
    21  		{
    22  			name: "no ref set",
    23  			ref:  "",
    24  			want: FallbackRef,
    25  		},
    26  		{
    27  			name: "ref from commit",
    28  			ref:  "refs/heads/foo",
    29  			want: "refs/heads/foo",
    30  		},
    31  		{
    32  			name: "ref overridden",
    33  			ref:  "refs/heads/overridden",
    34  			props: map[string]any{
    35  				"recipes_integration_ref_override": "refs/heads/foo",
    36  			},
    37  			want: "refs/heads/foo",
    38  		},
    39  		{
    40  			name: "mapped ref",
    41  			ref:  "refs/heads/original",
    42  			props: map[string]any{
    43  				"recipes_integration_ref_mapping": map[string]any{
    44  					"refs/heads/original": "refs/heads/mapped",
    45  				},
    46  			},
    47  			want: "refs/heads/mapped",
    48  		},
    49  	}
    50  	for _, tc := range tests {
    51  		t.Run(tc.name, func(t *testing.T) {
    52  			props, err := structpb.NewStruct(tc.props)
    53  			if err != nil {
    54  				t.Fatal(err)
    55  			}
    56  			build := &buildbucketpb.Build{
    57  				Input: &buildbucketpb.Build_Input{
    58  					Properties:    props,
    59  					GitilesCommit: &buildbucketpb.GitilesCommit{Ref: tc.ref},
    60  				},
    61  			}
    62  			got, err := chooseRef(build)
    63  			if err != nil {
    64  				t.Fatal(err)
    65  			}
    66  			if tc.want != got {
    67  				t.Errorf("Expected ref %q, got %q", tc.want, got)
    68  			}
    69  		})
    70  	}
    71  }