github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/ref/ref_spec_test.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     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 ref
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  func TestRefSpec(t *testing.T) {
    25  	tests := []struct {
    26  		remote     string
    27  		refSpecStr string
    28  		isValid    bool
    29  		inToExpOut map[string]string
    30  		skip       bool
    31  	}{
    32  		{
    33  			remote:     "origin",
    34  			refSpecStr: "refs/heads/*:refs/remotes/origin/*",
    35  			isValid:    true,
    36  			inToExpOut: map[string]string{
    37  				"refs/heads/main":          "refs/remotes/origin/main",
    38  				"refs/heads/feature":       "refs/remotes/origin/feature",
    39  				"refs/remotes/origin/main": "refs/nil/",
    40  			},
    41  		}, {
    42  			remote:     "borigin",
    43  			refSpecStr: "refs/heads/main:refs/remotes/borigin/mymain",
    44  			isValid:    true,
    45  			inToExpOut: map[string]string{
    46  				"refs/heads/main":    "refs/remotes/borigin/mymain",
    47  				"refs/heads/feature": "refs/nil/",
    48  			},
    49  		}, {
    50  			refSpecStr: "refs/heads/*/main:refs/remotes/borigin/*/mymain",
    51  			isValid:    true,
    52  			inToExpOut: map[string]string{
    53  				"refs/heads/main":    "refs/nil/",
    54  				"refs/heads/bh/main": "refs/remotes/borigin/bh/mymain",
    55  				"refs/heads/as/main": "refs/remotes/borigin/as/mymain",
    56  			},
    57  		}, {
    58  			refSpecStr: "main",
    59  			isValid:    true,
    60  			inToExpOut: map[string]string{
    61  				"refs/heads/main":    "refs/heads/main",
    62  				"refs/heads/feature": "refs/nil/",
    63  			},
    64  		}, {
    65  			refSpecStr: "main:main",
    66  			isValid:    true,
    67  			inToExpOut: map[string]string{
    68  				"refs/heads/main":    "refs/heads/main",
    69  				"refs/heads/feature": "refs/nil/",
    70  			},
    71  		}, {
    72  			remote:     "origin",
    73  			refSpecStr: "refs/heads/main:refs/remotes/not_borigin/mymain",
    74  		}, {
    75  			remote:     "origin",
    76  			refSpecStr: "refs/heads/*:refs/remotes/origin/branchname",
    77  		}, {
    78  			remote:     "origin",
    79  			refSpecStr: "refs/heads/branchname:refs/remotes/origin/*",
    80  		}, {
    81  			remote:     "origin",
    82  			refSpecStr: "refs/heads/*/*:refs/remotes/origin/*/*",
    83  		}, {
    84  			refSpecStr: "refs/tags/*:refs/tags/*",
    85  			isValid:    true,
    86  			inToExpOut: map[string]string{
    87  				"refs/tags/v1": "refs/tags/v1",
    88  			},
    89  			skip: true,
    90  		},
    91  	}
    92  
    93  	for _, test := range tests {
    94  		t.Run(test.refSpecStr, func(t *testing.T) {
    95  			if test.skip {
    96  				t.Skip()
    97  			}
    98  
    99  			var refSpec RefSpec
   100  			var err error
   101  
   102  			if test.remote == "" {
   103  				refSpec, err = ParseRefSpec(test.refSpecStr)
   104  			} else {
   105  				refSpec, err = ParseRefSpecForRemote(test.remote, test.refSpecStr)
   106  			}
   107  
   108  			if test.isValid {
   109  				require.NoError(t, err)
   110  			} else {
   111  				require.Error(t, err)
   112  			}
   113  
   114  			for in, out := range test.inToExpOut {
   115  				inRef, err := Parse(in)
   116  				require.NoError(t, err)
   117  				// outRef could be nil because of test construction, which is valid
   118  				expectedOutRef, _ := Parse(out)
   119  
   120  				outRef := refSpec.DestRef(inRef)
   121  				assert.Equal(t, expectedOutRef, outRef)
   122  			}
   123  		})
   124  	}
   125  }