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