code.gitea.io/gitea@v1.19.3/modules/git/foreachref/parser_test.go (about)

     1  // Copyright 2022 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package foreachref_test
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  	"io"
    10  	"strings"
    11  	"testing"
    12  
    13  	"code.gitea.io/gitea/modules/git/foreachref"
    14  	"code.gitea.io/gitea/modules/json"
    15  
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  type refSlice = []map[string]string
    20  
    21  func TestParser(t *testing.T) {
    22  	tests := []struct {
    23  		name string
    24  
    25  		givenFormat foreachref.Format
    26  		givenInput  io.Reader
    27  
    28  		wantRefs    refSlice
    29  		wantErr     bool
    30  		expectedErr error
    31  	}{
    32  		// this would, for example, be the result when running `git
    33  		// for-each-ref refs/tags` on a repo without tags.
    34  		{
    35  			name: "no references on empty input",
    36  
    37  			givenFormat: foreachref.NewFormat("refname:short"),
    38  			givenInput:  strings.NewReader(``),
    39  
    40  			wantRefs: []map[string]string{},
    41  		},
    42  
    43  		// note: `git for-each-ref` will add a newline between every
    44  		// reference (in addition to the ref-delimiter we've chosen)
    45  		{
    46  			name: "single field requested, single reference in output",
    47  
    48  			givenFormat: foreachref.NewFormat("refname:short"),
    49  			givenInput:  strings.NewReader("refname:short v0.0.1\x00\x00" + "\n"),
    50  
    51  			wantRefs: []map[string]string{
    52  				{"refname:short": "v0.0.1"},
    53  			},
    54  		},
    55  		{
    56  			name: "single field requested, multiple references in output",
    57  
    58  			givenFormat: foreachref.NewFormat("refname:short"),
    59  			givenInput: strings.NewReader(
    60  				"refname:short v0.0.1\x00\x00" + "\n" +
    61  					"refname:short v0.0.2\x00\x00" + "\n" +
    62  					"refname:short v0.0.3\x00\x00" + "\n"),
    63  
    64  			wantRefs: []map[string]string{
    65  				{"refname:short": "v0.0.1"},
    66  				{"refname:short": "v0.0.2"},
    67  				{"refname:short": "v0.0.3"},
    68  			},
    69  		},
    70  
    71  		{
    72  			name: "multiple fields requested for each reference",
    73  
    74  			givenFormat: foreachref.NewFormat("refname:short", "objecttype", "objectname"),
    75  			givenInput: strings.NewReader(
    76  
    77  				"refname:short v0.0.1\x00objecttype commit\x00objectname 7b2c5ac9fc04fc5efafb60700713d4fa609b777b\x00\x00" + "\n" +
    78  					"refname:short v0.0.2\x00objecttype commit\x00objectname a1f051bc3eba734da4772d60e2d677f47cf93ef4\x00\x00" + "\n" +
    79  					"refname:short v0.0.3\x00objecttype commit\x00objectname ef82de70bb3f60c65fb8eebacbb2d122ef517385\x00\x00" + "\n",
    80  			),
    81  
    82  			wantRefs: []map[string]string{
    83  				{
    84  					"refname:short": "v0.0.1",
    85  					"objecttype":    "commit",
    86  					"objectname":    "7b2c5ac9fc04fc5efafb60700713d4fa609b777b",
    87  				},
    88  				{
    89  					"refname:short": "v0.0.2",
    90  					"objecttype":    "commit",
    91  					"objectname":    "a1f051bc3eba734da4772d60e2d677f47cf93ef4",
    92  				},
    93  				{
    94  					"refname:short": "v0.0.3",
    95  					"objecttype":    "commit",
    96  					"objectname":    "ef82de70bb3f60c65fb8eebacbb2d122ef517385",
    97  				},
    98  			},
    99  		},
   100  
   101  		{
   102  			name: "must handle multi-line fields such as 'content'",
   103  
   104  			givenFormat: foreachref.NewFormat("refname:short", "contents", "author"),
   105  			givenInput: strings.NewReader(
   106  				"refname:short v0.0.1\x00contents Create new buffer if not present yet (#549)\n\nFixes a nil dereference when ProcessFoo is used\nwith multiple commands.\x00author Foo Bar <foo@bar.com> 1507832733 +0200\x00\x00" + "\n" +
   107  					"refname:short v0.0.2\x00contents Update CI config (#651)\n\n\x00author John Doe <john.doe@foo.com> 1521643174 +0000\x00\x00" + "\n" +
   108  					"refname:short v0.0.3\x00contents Fixed code sample for bash completion (#687)\n\n\x00author Foo Baz <foo@baz.com> 1524836750 +0200\x00\x00" + "\n",
   109  			),
   110  
   111  			wantRefs: []map[string]string{
   112  				{
   113  					"refname:short": "v0.0.1",
   114  					"contents":      "Create new buffer if not present yet (#549)\n\nFixes a nil dereference when ProcessFoo is used\nwith multiple commands.",
   115  					"author":        "Foo Bar <foo@bar.com> 1507832733 +0200",
   116  				},
   117  				{
   118  					"refname:short": "v0.0.2",
   119  					"contents":      "Update CI config (#651)",
   120  					"author":        "John Doe <john.doe@foo.com> 1521643174 +0000",
   121  				},
   122  				{
   123  					"refname:short": "v0.0.3",
   124  					"contents":      "Fixed code sample for bash completion (#687)",
   125  					"author":        "Foo Baz <foo@baz.com> 1524836750 +0200",
   126  				},
   127  			},
   128  		},
   129  
   130  		{
   131  			name: "must handle fields without values",
   132  
   133  			givenFormat: foreachref.NewFormat("refname:short", "object", "objecttype"),
   134  			givenInput: strings.NewReader(
   135  				"refname:short v0.0.1\x00object \x00objecttype commit\x00\x00" + "\n" +
   136  					"refname:short v0.0.2\x00object \x00objecttype commit\x00\x00" + "\n" +
   137  					"refname:short v0.0.3\x00object \x00objecttype commit\x00\x00" + "\n",
   138  			),
   139  
   140  			wantRefs: []map[string]string{
   141  				{
   142  					"refname:short": "v0.0.1",
   143  					"object":        "",
   144  					"objecttype":    "commit",
   145  				},
   146  				{
   147  					"refname:short": "v0.0.2",
   148  					"object":        "",
   149  					"objecttype":    "commit",
   150  				},
   151  				{
   152  					"refname:short": "v0.0.3",
   153  					"object":        "",
   154  					"objecttype":    "commit",
   155  				},
   156  			},
   157  		},
   158  
   159  		{
   160  			name: "must fail when the number of fields in the input doesn't match expected format",
   161  
   162  			givenFormat: foreachref.NewFormat("refname:short", "objecttype", "objectname"),
   163  			givenInput: strings.NewReader(
   164  				"refname:short v0.0.1\x00objecttype commit\x00\x00" + "\n" +
   165  					"refname:short v0.0.2\x00objecttype commit\x00\x00" + "\n" +
   166  					"refname:short v0.0.3\x00objecttype commit\x00\x00" + "\n",
   167  			),
   168  
   169  			wantErr:     true,
   170  			expectedErr: errors.New("unexpected number of reference fields: wanted 2, was 3"),
   171  		},
   172  
   173  		{
   174  			name: "must fail input fields don't match expected format",
   175  
   176  			givenFormat: foreachref.NewFormat("refname:short", "objectname"),
   177  			givenInput: strings.NewReader(
   178  				"refname:short v0.0.1\x00objecttype commit\x00\x00" + "\n" +
   179  					"refname:short v0.0.2\x00objecttype commit\x00\x00" + "\n" +
   180  					"refname:short v0.0.3\x00objecttype commit\x00\x00" + "\n",
   181  			),
   182  
   183  			wantErr:     true,
   184  			expectedErr: errors.New("unexpected field name at position 1: wanted: 'objectname', was: 'objecttype'"),
   185  		},
   186  	}
   187  
   188  	for _, test := range tests {
   189  		tc := test // don't close over loop variable
   190  		t.Run(tc.name, func(t *testing.T) {
   191  			parser := tc.givenFormat.Parser(tc.givenInput)
   192  
   193  			//
   194  			// parse references from input
   195  			//
   196  			gotRefs := make([]map[string]string, 0)
   197  			for {
   198  				ref := parser.Next()
   199  				if ref == nil {
   200  					break
   201  				}
   202  				gotRefs = append(gotRefs, ref)
   203  			}
   204  			err := parser.Err()
   205  
   206  			//
   207  			// verify expectations
   208  			//
   209  			if tc.wantErr {
   210  				require.Error(t, err)
   211  				require.EqualError(t, err, tc.expectedErr.Error())
   212  			} else {
   213  				require.NoError(t, err, "for-each-ref parser unexpectedly failed with: %v", err)
   214  				require.Equal(t, tc.wantRefs, gotRefs, "for-each-ref parser produced unexpected reference set. wanted: %v, got: %v", pretty(tc.wantRefs), pretty(gotRefs))
   215  			}
   216  		})
   217  	}
   218  }
   219  
   220  func pretty(v interface{}) string {
   221  	data, err := json.MarshalIndent(v, "", "  ")
   222  	if err != nil {
   223  		// shouldn't happen
   224  		panic(fmt.Sprintf("json-marshalling failed: %v", err))
   225  	}
   226  	return string(data)
   227  }