github.com/wrgl/wrgl@v0.14.0/pkg/conf/refspec_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright © 2022 Wrangle Ltd
     3  
     4  package conf
     5  
     6  import (
     7  	"sort"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestRefSpec(t *testing.T) {
    15  	for i, c := range []struct {
    16  		Text   string
    17  		Src    string
    18  		Dst    string
    19  		Force  bool
    20  		Negate bool
    21  		IsGlob bool
    22  	}{
    23  		{"+refs/heads/*:refs/remotes/origin/*", "refs/heads/*", "refs/remotes/origin/*", true, false, true},
    24  		{"refs/heads/main:refs/remotes/origin/main", "refs/heads/main", "refs/remotes/origin/main", false, false, false},
    25  		{"refs/heads/main", "refs/heads/main", "", false, false, false},
    26  		{"^refs/heads/qa*", "refs/heads/qa*", "", false, true, true},
    27  		{"tag v1.0.*", "refs/tags/v1.0.*", "refs/tags/v1.0.*", false, false, true},
    28  		{"main~4:refs/heads/main", "main~4", "refs/heads/main", false, false, false},
    29  		{":refs/heads/main", "", "refs/heads/main", false, false, false},
    30  	} {
    31  		rs, err := ParseRefspec(c.Text)
    32  		require.NoError(t, err, "case %d", i)
    33  		assert.Equal(t, c.Src, rs.Src(), "case %d", i)
    34  		assert.Equal(t, c.Dst, rs.Dst(), "case %d", i)
    35  		assert.Equal(t, c.Force, rs.Force, "case %d", i)
    36  		assert.Equal(t, c.Negate, rs.Negate, "case %d", i)
    37  		b, err := rs.MarshalText()
    38  		require.NoError(t, err, "case %d", i)
    39  		assert.Equal(t, c.Text, string(b), "case %d", i)
    40  		assert.Equal(t, c.IsGlob, rs.IsGlob())
    41  	}
    42  }
    43  
    44  func TestSrcMatchRef(t *testing.T) {
    45  	for i, c := range []struct {
    46  		Text  string
    47  		Ref   string
    48  		Match bool
    49  	}{
    50  		{"+refs/heads/*:refs/remotes/origin/*", "refs/heads/abc", true},
    51  		{"+refs/heads/*:refs/remotes/origin/*", "refs/tags/pdw", false},
    52  		{"refs/heads/abc", "refs/heads/abc", true},
    53  		{"refs/heads/main:refs/remotes/origin/mymain", "refs/remotes/origin/mymain", false},
    54  	} {
    55  		rs, err := ParseRefspec(c.Text)
    56  		require.NoError(t, err, "case %d", i)
    57  		assert.Equal(t, c.Match, rs.SrcMatchRef(c.Ref), "case %d", i)
    58  	}
    59  }
    60  
    61  func TestDstMatchRef(t *testing.T) {
    62  	for i, c := range []struct {
    63  		Text  string
    64  		Ref   string
    65  		Match bool
    66  	}{
    67  		{"+refs/heads/*:refs/remotes/origin/*", "refs/remotes/origin/abc", true},
    68  		{"+refs/heads/*:refs/remotes/origin/*", "refs/tags/pdw", false},
    69  		{"refs/heads/abc", "refs/heads/abc", false},
    70  		{"refs/heads/main:refs/remotes/origin/mymain", "refs/remotes/origin/mymain", true},
    71  		{"refs/heads/main:refs/remotes/origin/mymain", "refs/heads/qwer", false},
    72  	} {
    73  		rs, err := ParseRefspec(c.Text)
    74  		require.NoError(t, err, "case %d", i)
    75  		assert.Equal(t, c.Match, rs.DstMatchRef(c.Ref), "case %d", i)
    76  	}
    77  }
    78  
    79  func TestDstForRef(t *testing.T) {
    80  	for i, c := range []struct {
    81  		Text string
    82  		Ref  string
    83  		Dst  string
    84  	}{
    85  		{"+refs/heads/*:refs/remotes/origin/*", "refs/heads/abc", "refs/remotes/origin/abc"},
    86  		{"+refs/heads/*:refs/remotes/origin/*", "refs/tags/pdw", ""},
    87  		{"refs/heads/abc", "refs/heads/abc", ""},
    88  		{"refs/heads/main:refs/remotes/origin/mymain", "refs/heads/main", "refs/remotes/origin/mymain"},
    89  		{"refs/heads/main:refs/remotes/origin/mymain", "refs/heads/qwer", ""},
    90  	} {
    91  		rs, err := ParseRefspec(c.Text)
    92  		require.NoError(t, err, "case %d", i)
    93  		assert.Equal(t, c.Dst, rs.DstForRef(c.Ref), "case %d", i)
    94  	}
    95  }
    96  
    97  func TestRefspecExclude(t *testing.T) {
    98  	for i, c := range []struct {
    99  		Text    string
   100  		Ref     string
   101  		Exclude bool
   102  	}{
   103  		{"^refs/heads/m*", "refs/heads/main", true},
   104  		{"^refs/heads/m*", "refs/heads/tickets", false},
   105  		{"^refs/heads/main", "refs/heads/main", true},
   106  		{"^refs/heads/main", "refs/heads/abc", false},
   107  		{"+refs/tags/*:refs/remotes/origin/tags/*", "refs/heads/main", false},
   108  	} {
   109  		rs, err := ParseRefspec(c.Text)
   110  		require.NoError(t, err, "case %d", i)
   111  		assert.Equal(t, c.Exclude, rs.Exclude(c.Ref), "case %d", i)
   112  	}
   113  }
   114  
   115  func TestNewRefspec(t *testing.T) {
   116  	for i, c := range []struct {
   117  		Src    string
   118  		Dst    string
   119  		Negate bool
   120  		Force  bool
   121  		Result string
   122  	}{
   123  		{"main~4", "refs/heads/main", false, false, "main~4:refs/heads/main"},
   124  		{"main^", "refs/tags/dec-2020", true, false, "^main^:refs/tags/dec-2020"},
   125  		{"refs/heads/tickets", "refs/remotes/origin/tickets", false, true, "+refs/heads/tickets:refs/remotes/origin/tickets"},
   126  		{"refs/heads/tickets", "", false, true, "+refs/heads/tickets"},
   127  	} {
   128  		rs, err := NewRefspec(c.Src, c.Dst, c.Negate, c.Force)
   129  		require.NoError(t, err, "case %d", i)
   130  		assert.Equal(t, c.Result, rs.String(), "case %d", i)
   131  	}
   132  }
   133  
   134  func TestRefspecSlice(t *testing.T) {
   135  	sl := RefspecSlice{
   136  		MustParseRefspec("refs/heads/b:refs/remotes/origin/b"),
   137  		MustParseRefspec("refs/heads/a:refs/remotes/origin/a"),
   138  		MustParseRefspec("+refs/heads/c:refs/remotes/origin/c"),
   139  		MustParseRefspec("!refs/heads/d:refs/remotes/origin/d"),
   140  		MustParseRefspec("+refs/heads/*:refs/remotes/origin/*"),
   141  		MustParseRefspec("tag abc"),
   142  	}
   143  	sort.Sort(sl)
   144  	assert.Equal(t, RefspecSlice{
   145  		MustParseRefspec("!refs/heads/d:refs/remotes/origin/d"),
   146  		MustParseRefspec("refs/heads/a:refs/remotes/origin/a"),
   147  		MustParseRefspec("refs/heads/b:refs/remotes/origin/b"),
   148  		MustParseRefspec("tag abc"),
   149  		MustParseRefspec("+refs/heads/*:refs/remotes/origin/*"),
   150  		MustParseRefspec("+refs/heads/c:refs/remotes/origin/c"),
   151  	}, sl)
   152  	assert.Equal(t, 3, sl.IndexOf(MustParseRefspec("tag abc")))
   153  	assert.Equal(t, -1, sl.IndexOf(MustParseRefspec("tag def")))
   154  	assert.Equal(t, -1, sl.IndexOf(MustParseRefspec("+refs/heads/e:refs/remotes/origin/e")))
   155  }