| 260 | } |
| 261 | |
| 262 | func TestExtractName(t *testing.T) { |
| 263 | tmpfile, err := os.CreateTemp(t.TempDir(), "gh-cli") |
| 264 | if err != nil { |
| 265 | t.Fatal(err) |
| 266 | } |
| 267 | defer tmpfile.Close() |
| 268 | |
| 269 | type args struct { |
| 270 | filePath string |
| 271 | } |
| 272 | tests := []struct { |
| 273 | name string |
| 274 | prepare string |
| 275 | args args |
| 276 | want string |
| 277 | }{ |
| 278 | { |
| 279 | name: "Complete front-matter", |
| 280 | prepare: `--- |
| 281 | name: Bug Report |
| 282 | about: This is how you report bugs |
| 283 | --- |
| 284 | |
| 285 | **Template contents** |
| 286 | `, |
| 287 | args: args{ |
| 288 | filePath: tmpfile.Name(), |
| 289 | }, |
| 290 | want: "Bug Report", |
| 291 | }, |
| 292 | { |
| 293 | name: "Incomplete front-matter", |
| 294 | prepare: `--- |
| 295 | about: This is how you report bugs |
| 296 | --- |
| 297 | `, |
| 298 | args: args{ |
| 299 | filePath: tmpfile.Name(), |
| 300 | }, |
| 301 | want: path.Base(tmpfile.Name()), |
| 302 | }, |
| 303 | { |
| 304 | name: "No front-matter", |
| 305 | prepare: `name: This is not yaml!`, |
| 306 | args: args{ |
| 307 | filePath: tmpfile.Name(), |
| 308 | }, |
| 309 | want: path.Base(tmpfile.Name()), |
| 310 | }, |
| 311 | } |
| 312 | for _, tt := range tests { |
| 313 | t.Run(tt.name, func(t *testing.T) { |
| 314 | _ = os.WriteFile(tmpfile.Name(), []byte(tt.prepare), 0600) |
| 315 | if got := ExtractName(tt.args.filePath); got != tt.want { |
| 316 | t.Errorf("ExtractName() = %v, want %v", got, tt.want) |
| 317 | } |
| 318 | }) |
| 319 | } |