You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

issue_test.go 2.0KB

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