github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/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 "testing"
    18  
    19  func TestRefSpec(t *testing.T) {
    20  	tests := []struct {
    21  		remote     string
    22  		refSpecStr string
    23  		isValid    bool
    24  		inToExpOut map[string]string
    25  	}{
    26  		{
    27  			"origin",
    28  			"refs/heads/*:refs/remotes/origin/*",
    29  			true,
    30  			map[string]string{
    31  				"refs/heads/master":          "refs/remotes/origin/master",
    32  				"refs/heads/feature":         "refs/remotes/origin/feature",
    33  				"refs/remotes/origin/master": "refs/nil/",
    34  			},
    35  		}, {
    36  			"borigin",
    37  			"refs/heads/master:refs/remotes/borigin/mymaster",
    38  			true,
    39  			map[string]string{
    40  				"refs/heads/master":  "refs/remotes/borigin/mymaster",
    41  				"refs/heads/feature": "refs/nil/",
    42  			},
    43  		}, {
    44  			"",
    45  			"refs/heads/*/master:refs/remotes/borigin/*/mymaster",
    46  			true,
    47  			map[string]string{
    48  				"refs/heads/master":    "refs/nil/",
    49  				"refs/heads/bh/master": "refs/remotes/borigin/bh/mymaster",
    50  				"refs/heads/as/master": "refs/remotes/borigin/as/mymaster",
    51  			},
    52  		}, {
    53  			"",
    54  			"master",
    55  			true,
    56  			map[string]string{
    57  				"refs/heads/master":  "refs/heads/master",
    58  				"refs/heads/feature": "refs/nil/",
    59  			},
    60  		}, {
    61  			"",
    62  			"master:master",
    63  			true,
    64  			map[string]string{
    65  				"refs/heads/master":  "refs/heads/master",
    66  				"refs/heads/feature": "refs/nil/",
    67  			},
    68  		}, {
    69  			"origin",
    70  			"refs/heads/master:refs/remotes/not_borigin/mymaster",
    71  			false,
    72  			nil,
    73  		}, {
    74  			"origin",
    75  			"refs/heads/*:refs/remotes/origin/branchname",
    76  			false,
    77  			nil,
    78  		}, {
    79  			"origin",
    80  			"refs/heads/branchname:refs/remotes/origin/*",
    81  			false,
    82  			nil,
    83  		}, {
    84  			"origin",
    85  			"refs/heads/*/*:refs/remotes/origin/*/*",
    86  			false,
    87  			nil,
    88  		},
    89  	}
    90  
    91  	for _, test := range tests {
    92  		var refSpec RefSpec
    93  		var err error
    94  
    95  		if test.remote == "" {
    96  			refSpec, err = ParseRefSpec(test.refSpecStr)
    97  		} else {
    98  			refSpec, err = ParseRefSpecForRemote(test.remote, test.refSpecStr)
    99  		}
   100  
   101  		if (err == nil) != test.isValid {
   102  			t.Error(test.refSpecStr, "is valid:", err == nil)
   103  		} else if err == nil {
   104  			for in, out := range test.inToExpOut {
   105  				inRef, _ := Parse(in)
   106  				outRef, _ := Parse(out)
   107  
   108  				actual := refSpec.DestRef(inRef)
   109  
   110  				if !Equals(actual, outRef) {
   111  					t.Error(test.refSpecStr, "mapped", in, "to", actual.String(), "expected", outRef.String())
   112  				}
   113  			}
   114  		}
   115  	}
   116  }