gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repository
  4. import (
  5. "fmt"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func Test_getLicense(t *testing.T) {
  10. type args struct {
  11. name string
  12. values *LicenseValues
  13. }
  14. tests := []struct {
  15. name string
  16. args args
  17. want string
  18. wantErr assert.ErrorAssertionFunc
  19. }{
  20. {
  21. name: "regular",
  22. args: args{
  23. name: "MIT",
  24. values: &LicenseValues{Owner: "Gitea", Year: "2023"},
  25. },
  26. want: `MIT License
  27. Copyright (c) 2023 Gitea
  28. Permission is hereby granted`,
  29. wantErr: assert.NoError,
  30. },
  31. {
  32. name: "license not found",
  33. args: args{
  34. name: "notfound",
  35. },
  36. wantErr: assert.Error,
  37. },
  38. }
  39. for _, tt := range tests {
  40. t.Run(tt.name, func(t *testing.T) {
  41. got, err := GetLicense(tt.args.name, tt.args.values)
  42. if !tt.wantErr(t, err, fmt.Sprintf("GetLicense(%v, %v)", tt.args.name, tt.args.values)) {
  43. return
  44. }
  45. assert.Contains(t, string(got), tt.want, "GetLicense(%v, %v)", tt.args.name, tt.args.values)
  46. })
  47. }
  48. }
  49. func Test_fillLicensePlaceholder(t *testing.T) {
  50. type args struct {
  51. name string
  52. values *LicenseValues
  53. origin string
  54. }
  55. tests := []struct {
  56. name string
  57. args args
  58. want string
  59. }{
  60. {
  61. name: "owner",
  62. args: args{
  63. name: "regular",
  64. values: &LicenseValues{Year: "2023", Owner: "Gitea", Email: "teabot@gitea.io", Repo: "gitea"},
  65. origin: `
  66. <name of author>
  67. <owner>
  68. [NAME]
  69. [name of copyright owner]
  70. [name of copyright holder]
  71. <COPYRIGHT HOLDERS>
  72. <copyright holders>
  73. <AUTHOR>
  74. <author's name or designee>
  75. [one or more legally recognised persons or entities offering the Work under the terms and conditions of this Licence]
  76. `,
  77. },
  78. want: `
  79. Gitea
  80. Gitea
  81. Gitea
  82. Gitea
  83. Gitea
  84. Gitea
  85. Gitea
  86. Gitea
  87. Gitea
  88. Gitea
  89. `,
  90. },
  91. {
  92. name: "email",
  93. args: args{
  94. name: "regular",
  95. values: &LicenseValues{Year: "2023", Owner: "Gitea", Email: "teabot@gitea.io", Repo: "gitea"},
  96. origin: `
  97. [EMAIL]
  98. `,
  99. },
  100. want: `
  101. teabot@gitea.io
  102. `,
  103. },
  104. {
  105. name: "repo",
  106. args: args{
  107. name: "regular",
  108. values: &LicenseValues{Year: "2023", Owner: "Gitea", Email: "teabot@gitea.io", Repo: "gitea"},
  109. origin: `
  110. <program>
  111. <one line to give the program's name and a brief idea of what it does.>
  112. `,
  113. },
  114. want: `
  115. gitea
  116. gitea
  117. `,
  118. },
  119. {
  120. name: "year",
  121. args: args{
  122. name: "regular",
  123. values: &LicenseValues{Year: "2023", Owner: "Gitea", Email: "teabot@gitea.io", Repo: "gitea"},
  124. origin: `
  125. <year>
  126. [YEAR]
  127. {YEAR}
  128. [yyyy]
  129. [Year]
  130. [year]
  131. `,
  132. },
  133. want: `
  134. 2023
  135. 2023
  136. 2023
  137. 2023
  138. 2023
  139. 2023
  140. `,
  141. },
  142. {
  143. name: "0BSD",
  144. args: args{
  145. name: "0BSD",
  146. values: &LicenseValues{Year: "2023", Owner: "Gitea", Email: "teabot@gitea.io", Repo: "gitea"},
  147. origin: `
  148. Copyright (C) YEAR by AUTHOR EMAIL
  149. ...
  150. ... THE AUTHOR BE LIABLE FOR ...
  151. `,
  152. },
  153. want: `
  154. Copyright (C) 2023 by Gitea teabot@gitea.io
  155. ...
  156. ... THE AUTHOR BE LIABLE FOR ...
  157. `,
  158. },
  159. }
  160. for _, tt := range tests {
  161. t.Run(tt.name, func(t *testing.T) {
  162. assert.Equalf(t, tt.want, string(fillLicensePlaceholder(tt.args.name, tt.args.values, []byte(tt.args.origin))), "fillLicensePlaceholder(%v, %v, %v)", tt.args.name, tt.args.values, tt.args.origin)
  163. })
  164. }
  165. }