code.gitea.io/gitea@v1.19.3/modules/structs/issue_test.go (about)

     1  // Copyright 2022 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package structs
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"gopkg.in/yaml.v3"
    11  )
    12  
    13  func TestIssueTemplate_Type(t *testing.T) {
    14  	tests := []struct {
    15  		fileName string
    16  		want     IssueTemplateType
    17  	}{
    18  		{
    19  			fileName: ".gitea/ISSUE_TEMPLATE/bug_report.yaml",
    20  			want:     IssueTemplateTypeYaml,
    21  		},
    22  		{
    23  			fileName: ".gitea/ISSUE_TEMPLATE/bug_report.md",
    24  			want:     IssueTemplateTypeMarkdown,
    25  		},
    26  		{
    27  			fileName: ".gitea/ISSUE_TEMPLATE/bug_report.txt",
    28  			want:     "",
    29  		},
    30  		{
    31  			fileName: ".gitea/ISSUE_TEMPLATE/config.yaml",
    32  			want:     "",
    33  		},
    34  	}
    35  	for _, tt := range tests {
    36  		t.Run(tt.fileName, func(t *testing.T) {
    37  			it := IssueTemplate{
    38  				FileName: tt.fileName,
    39  			}
    40  			assert.Equal(t, tt.want, it.Type())
    41  		})
    42  	}
    43  }
    44  
    45  func TestIssueTemplateLabels_UnmarshalYAML(t *testing.T) {
    46  	tests := []struct {
    47  		name    string
    48  		content string
    49  		tmpl    *IssueTemplate
    50  		want    *IssueTemplate
    51  		wantErr string
    52  	}{
    53  		{
    54  			name:    "array",
    55  			content: `labels: ["a", "b", "c"]`,
    56  			tmpl: &IssueTemplate{
    57  				Labels: []string{"should_be_overwrote"},
    58  			},
    59  			want: &IssueTemplate{
    60  				Labels: []string{"a", "b", "c"},
    61  			},
    62  		},
    63  		{
    64  			name:    "string",
    65  			content: `labels: "a,b,c"`,
    66  			tmpl: &IssueTemplate{
    67  				Labels: []string{"should_be_overwrote"},
    68  			},
    69  			want: &IssueTemplate{
    70  				Labels: []string{"a", "b", "c"},
    71  			},
    72  		},
    73  		{
    74  			name:    "empty",
    75  			content: `labels:`,
    76  			tmpl: &IssueTemplate{
    77  				Labels: []string{"should_be_overwrote"},
    78  			},
    79  			want: &IssueTemplate{
    80  				Labels: nil,
    81  			},
    82  		},
    83  		{
    84  			name: "error",
    85  			content: `
    86  labels:
    87    a: aa
    88    b: bb
    89  `,
    90  			tmpl:    &IssueTemplate{},
    91  			wantErr: "line 3: cannot unmarshal !!map into IssueTemplateLabels",
    92  		},
    93  	}
    94  	for _, tt := range tests {
    95  		t.Run(tt.name, func(t *testing.T) {
    96  			err := yaml.Unmarshal([]byte(tt.content), tt.tmpl)
    97  			if tt.wantErr != "" {
    98  				assert.EqualError(t, err, tt.wantErr)
    99  			} else {
   100  				assert.NoError(t, err)
   101  				assert.Equal(t, tt.want, tt.tmpl)
   102  			}
   103  		})
   104  	}
   105  }