|
|
@ -6,18 +6,21 @@ package template |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"net/url" |
|
|
|
"net/url" |
|
|
|
"reflect" |
|
|
|
|
|
|
|
"testing" |
|
|
|
"testing" |
|
|
|
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/json" |
|
|
|
"code.gitea.io/gitea/modules/json" |
|
|
|
api "code.gitea.io/gitea/modules/structs" |
|
|
|
api "code.gitea.io/gitea/modules/structs" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func TestValidate(t *testing.T) { |
|
|
|
func TestValidate(t *testing.T) { |
|
|
|
tests := []struct { |
|
|
|
tests := []struct { |
|
|
|
name string |
|
|
|
name string |
|
|
|
content string |
|
|
|
filename string |
|
|
|
wantErr string |
|
|
|
content string |
|
|
|
|
|
|
|
want *api.IssueTemplate |
|
|
|
|
|
|
|
wantErr string |
|
|
|
}{ |
|
|
|
}{ |
|
|
|
{ |
|
|
|
{ |
|
|
|
name: "miss name", |
|
|
|
name: "miss name", |
|
|
@ -316,21 +319,9 @@ body: |
|
|
|
`, |
|
|
|
`, |
|
|
|
wantErr: "body[0](checkboxes), option[0]: 'required' should be a bool", |
|
|
|
wantErr: "body[0](checkboxes), option[0]: 'required' should be a bool", |
|
|
|
}, |
|
|
|
}, |
|
|
|
} |
|
|
|
{ |
|
|
|
for _, tt := range tests { |
|
|
|
name: "valid", |
|
|
|
t.Run(tt.name, func(t *testing.T) { |
|
|
|
content: ` |
|
|
|
tmpl, err := unmarshal("test.yaml", []byte(tt.content)) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
t.Fatal(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if err := Validate(tmpl); (err == nil) != (tt.wantErr == "") || err != nil && err.Error() != tt.wantErr { |
|
|
|
|
|
|
|
t.Errorf("Validate() error = %v, wantErr %q", err, tt.wantErr) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("valid", func(t *testing.T) { |
|
|
|
|
|
|
|
content := ` |
|
|
|
|
|
|
|
name: Name |
|
|
|
name: Name |
|
|
|
title: Title |
|
|
|
title: Title |
|
|
|
about: About |
|
|
|
about: About |
|
|
@ -386,96 +377,227 @@ body: |
|
|
|
required: false |
|
|
|
required: false |
|
|
|
- label: Option 3 of checkboxes |
|
|
|
- label: Option 3 of checkboxes |
|
|
|
required: true |
|
|
|
required: true |
|
|
|
` |
|
|
|
`, |
|
|
|
want := &api.IssueTemplate{ |
|
|
|
want: &api.IssueTemplate{ |
|
|
|
Name: "Name", |
|
|
|
Name: "Name", |
|
|
|
Title: "Title", |
|
|
|
Title: "Title", |
|
|
|
About: "About", |
|
|
|
About: "About", |
|
|
|
Labels: []string{"label1", "label2"}, |
|
|
|
Labels: []string{"label1", "label2"}, |
|
|
|
Ref: "Ref", |
|
|
|
Ref: "Ref", |
|
|
|
Fields: []*api.IssueFormField{ |
|
|
|
Fields: []*api.IssueFormField{ |
|
|
|
{ |
|
|
|
{ |
|
|
|
Type: "markdown", |
|
|
|
Type: "markdown", |
|
|
|
ID: "id1", |
|
|
|
ID: "id1", |
|
|
|
Attributes: map[string]interface{}{ |
|
|
|
Attributes: map[string]interface{}{ |
|
|
|
"value": "Value of the markdown", |
|
|
|
"value": "Value of the markdown", |
|
|
|
|
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
{ |
|
|
|
{ |
|
|
|
Type: "textarea", |
|
|
|
Type: "textarea", |
|
|
|
ID: "id2", |
|
|
|
ID: "id2", |
|
|
|
Attributes: map[string]interface{}{ |
|
|
|
Attributes: map[string]interface{}{ |
|
|
|
"label": "Label of textarea", |
|
|
|
"label": "Label of textarea", |
|
|
|
"description": "Description of textarea", |
|
|
|
"description": "Description of textarea", |
|
|
|
"placeholder": "Placeholder of textarea", |
|
|
|
"placeholder": "Placeholder of textarea", |
|
|
|
"value": "Value of textarea", |
|
|
|
"value": "Value of textarea", |
|
|
|
"render": "bash", |
|
|
|
"render": "bash", |
|
|
|
}, |
|
|
|
|
|
|
|
Validations: map[string]interface{}{ |
|
|
|
|
|
|
|
"required": true, |
|
|
|
|
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
Validations: map[string]interface{}{ |
|
|
|
{ |
|
|
|
"required": true, |
|
|
|
Type: "input", |
|
|
|
|
|
|
|
ID: "id3", |
|
|
|
|
|
|
|
Attributes: map[string]interface{}{ |
|
|
|
|
|
|
|
"label": "Label of input", |
|
|
|
|
|
|
|
"description": "Description of input", |
|
|
|
|
|
|
|
"placeholder": "Placeholder of input", |
|
|
|
|
|
|
|
"value": "Value of input", |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
Validations: map[string]interface{}{ |
|
|
|
|
|
|
|
"required": true, |
|
|
|
|
|
|
|
"is_number": true, |
|
|
|
|
|
|
|
"regex": "[a-zA-Z0-9]+", |
|
|
|
|
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
{ |
|
|
|
{ |
|
|
|
Type: "dropdown", |
|
|
|
Type: "input", |
|
|
|
ID: "id4", |
|
|
|
ID: "id3", |
|
|
|
Attributes: map[string]interface{}{ |
|
|
|
Attributes: map[string]interface{}{ |
|
|
|
"label": "Label of dropdown", |
|
|
|
"label": "Label of input", |
|
|
|
"description": "Description of dropdown", |
|
|
|
"description": "Description of input", |
|
|
|
"multiple": true, |
|
|
|
"placeholder": "Placeholder of input", |
|
|
|
"options": []interface{}{ |
|
|
|
"value": "Value of input", |
|
|
|
"Option 1 of dropdown", |
|
|
|
|
|
|
|
"Option 2 of dropdown", |
|
|
|
|
|
|
|
"Option 3 of dropdown", |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
Validations: map[string]interface{}{ |
|
|
|
|
|
|
|
"required": true, |
|
|
|
|
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
Validations: map[string]interface{}{ |
|
|
|
{ |
|
|
|
"required": true, |
|
|
|
Type: "checkboxes", |
|
|
|
"is_number": true, |
|
|
|
ID: "id5", |
|
|
|
"regex": "[a-zA-Z0-9]+", |
|
|
|
Attributes: map[string]interface{}{ |
|
|
|
|
|
|
|
"label": "Label of checkboxes", |
|
|
|
|
|
|
|
"description": "Description of checkboxes", |
|
|
|
|
|
|
|
"options": []interface{}{ |
|
|
|
|
|
|
|
map[string]interface{}{"label": "Option 1 of checkboxes", "required": true}, |
|
|
|
|
|
|
|
map[string]interface{}{"label": "Option 2 of checkboxes", "required": false}, |
|
|
|
|
|
|
|
map[string]interface{}{"label": "Option 3 of checkboxes", "required": true}, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
{ |
|
|
|
FileName: "test.yaml", |
|
|
|
Type: "dropdown", |
|
|
|
}, |
|
|
|
ID: "id4", |
|
|
|
wantErr: "", |
|
|
|
Attributes: map[string]interface{}{ |
|
|
|
}, |
|
|
|
"label": "Label of dropdown", |
|
|
|
{ |
|
|
|
"description": "Description of dropdown", |
|
|
|
name: "single label", |
|
|
|
"multiple": true, |
|
|
|
content: ` |
|
|
|
"options": []interface{}{ |
|
|
|
name: Name |
|
|
|
"Option 1 of dropdown", |
|
|
|
title: Title |
|
|
|
"Option 2 of dropdown", |
|
|
|
about: About |
|
|
|
"Option 3 of dropdown", |
|
|
|
labels: label1 |
|
|
|
|
|
|
|
ref: Ref |
|
|
|
|
|
|
|
body: |
|
|
|
|
|
|
|
- type: markdown |
|
|
|
|
|
|
|
id: id1 |
|
|
|
|
|
|
|
attributes: |
|
|
|
|
|
|
|
value: Value of the markdown |
|
|
|
|
|
|
|
`, |
|
|
|
|
|
|
|
want: &api.IssueTemplate{ |
|
|
|
|
|
|
|
Name: "Name", |
|
|
|
|
|
|
|
Title: "Title", |
|
|
|
|
|
|
|
About: "About", |
|
|
|
|
|
|
|
Labels: []string{"label1"}, |
|
|
|
|
|
|
|
Ref: "Ref", |
|
|
|
|
|
|
|
Fields: []*api.IssueFormField{ |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Type: "markdown", |
|
|
|
|
|
|
|
ID: "id1", |
|
|
|
|
|
|
|
Attributes: map[string]interface{}{ |
|
|
|
|
|
|
|
"value": "Value of the markdown", |
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
Validations: map[string]interface{}{ |
|
|
|
}, |
|
|
|
"required": true, |
|
|
|
FileName: "test.yaml", |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
wantErr: "", |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
name: "comma-delimited labels", |
|
|
|
|
|
|
|
content: ` |
|
|
|
|
|
|
|
name: Name |
|
|
|
|
|
|
|
title: Title |
|
|
|
|
|
|
|
about: About |
|
|
|
|
|
|
|
labels: label1,label2,,label3 ,, |
|
|
|
|
|
|
|
ref: Ref |
|
|
|
|
|
|
|
body: |
|
|
|
|
|
|
|
- type: markdown |
|
|
|
|
|
|
|
id: id1 |
|
|
|
|
|
|
|
attributes: |
|
|
|
|
|
|
|
value: Value of the markdown |
|
|
|
|
|
|
|
`, |
|
|
|
|
|
|
|
want: &api.IssueTemplate{ |
|
|
|
|
|
|
|
Name: "Name", |
|
|
|
|
|
|
|
Title: "Title", |
|
|
|
|
|
|
|
About: "About", |
|
|
|
|
|
|
|
Labels: []string{"label1", "label2", "label3"}, |
|
|
|
|
|
|
|
Ref: "Ref", |
|
|
|
|
|
|
|
Fields: []*api.IssueFormField{ |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Type: "markdown", |
|
|
|
|
|
|
|
ID: "id1", |
|
|
|
|
|
|
|
Attributes: map[string]interface{}{ |
|
|
|
|
|
|
|
"value": "Value of the markdown", |
|
|
|
|
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
{ |
|
|
|
FileName: "test.yaml", |
|
|
|
Type: "checkboxes", |
|
|
|
}, |
|
|
|
ID: "id5", |
|
|
|
wantErr: "", |
|
|
|
Attributes: map[string]interface{}{ |
|
|
|
}, |
|
|
|
"label": "Label of checkboxes", |
|
|
|
{ |
|
|
|
"description": "Description of checkboxes", |
|
|
|
name: "empty string as labels", |
|
|
|
"options": []interface{}{ |
|
|
|
content: ` |
|
|
|
map[interface{}]interface{}{"label": "Option 1 of checkboxes", "required": true}, |
|
|
|
name: Name |
|
|
|
map[interface{}]interface{}{"label": "Option 2 of checkboxes", "required": false}, |
|
|
|
title: Title |
|
|
|
map[interface{}]interface{}{"label": "Option 3 of checkboxes", "required": true}, |
|
|
|
about: About |
|
|
|
|
|
|
|
labels: '' |
|
|
|
|
|
|
|
ref: Ref |
|
|
|
|
|
|
|
body: |
|
|
|
|
|
|
|
- type: markdown |
|
|
|
|
|
|
|
id: id1 |
|
|
|
|
|
|
|
attributes: |
|
|
|
|
|
|
|
value: Value of the markdown |
|
|
|
|
|
|
|
`, |
|
|
|
|
|
|
|
want: &api.IssueTemplate{ |
|
|
|
|
|
|
|
Name: "Name", |
|
|
|
|
|
|
|
Title: "Title", |
|
|
|
|
|
|
|
About: "About", |
|
|
|
|
|
|
|
Labels: nil, |
|
|
|
|
|
|
|
Ref: "Ref", |
|
|
|
|
|
|
|
Fields: []*api.IssueFormField{ |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Type: "markdown", |
|
|
|
|
|
|
|
ID: "id1", |
|
|
|
|
|
|
|
Attributes: map[string]interface{}{ |
|
|
|
|
|
|
|
"value": "Value of the markdown", |
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
|
|
|
|
FileName: "test.yaml", |
|
|
|
}, |
|
|
|
}, |
|
|
|
FileName: "test.yaml", |
|
|
|
wantErr: "", |
|
|
|
} |
|
|
|
}, |
|
|
|
got, err := unmarshal("test.yaml", []byte(content)) |
|
|
|
{ |
|
|
|
if err != nil { |
|
|
|
name: "comma delimited labels in markdown", |
|
|
|
t.Fatal(err) |
|
|
|
filename: "test.md", |
|
|
|
} |
|
|
|
content: `--- |
|
|
|
if err := Validate(got); err != nil { |
|
|
|
name: Name |
|
|
|
t.Errorf("Validate() error = %v", err) |
|
|
|
title: Title |
|
|
|
} |
|
|
|
about: About |
|
|
|
if !reflect.DeepEqual(want, got) { |
|
|
|
labels: label1,label2,,label3 ,, |
|
|
|
jsonWant, _ := json.Marshal(want) |
|
|
|
ref: Ref |
|
|
|
jsonGot, _ := json.Marshal(got) |
|
|
|
--- |
|
|
|
t.Errorf("want:\n%s\ngot:\n%s", jsonWant, jsonGot) |
|
|
|
Content |
|
|
|
} |
|
|
|
`, |
|
|
|
}) |
|
|
|
want: &api.IssueTemplate{ |
|
|
|
|
|
|
|
Name: "Name", |
|
|
|
|
|
|
|
Title: "Title", |
|
|
|
|
|
|
|
About: "About", |
|
|
|
|
|
|
|
Labels: []string{"label1", "label2", "label3"}, |
|
|
|
|
|
|
|
Ref: "Ref", |
|
|
|
|
|
|
|
Fields: nil, |
|
|
|
|
|
|
|
Content: "Content\n", |
|
|
|
|
|
|
|
FileName: "test.md", |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
wantErr: "", |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
for _, tt := range tests { |
|
|
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) { |
|
|
|
|
|
|
|
filename := "test.yaml" |
|
|
|
|
|
|
|
if tt.filename != "" { |
|
|
|
|
|
|
|
filename = tt.filename |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
tmpl, err := unmarshal(filename, []byte(tt.content)) |
|
|
|
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
if tt.wantErr != "" { |
|
|
|
|
|
|
|
require.EqualError(t, Validate(tmpl), tt.wantErr) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
require.NoError(t, Validate(tmpl)) |
|
|
|
|
|
|
|
want, _ := json.Marshal(tt.want) |
|
|
|
|
|
|
|
got, _ := json.Marshal(tmpl) |
|
|
|
|
|
|
|
require.JSONEq(t, string(want), string(got)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestRenderToMarkdown(t *testing.T) { |
|
|
|
func TestRenderToMarkdown(t *testing.T) { |
|
|
|