gitea源码

linguist_test.go 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "net/url"
  6. "strconv"
  7. "strings"
  8. "testing"
  9. "time"
  10. repo_model "code.gitea.io/gitea/models/repo"
  11. "code.gitea.io/gitea/models/unittest"
  12. user_model "code.gitea.io/gitea/models/user"
  13. "code.gitea.io/gitea/modules/indexer/stats"
  14. "code.gitea.io/gitea/modules/queue"
  15. repo_service "code.gitea.io/gitea/services/repository"
  16. files_service "code.gitea.io/gitea/services/repository/files"
  17. "code.gitea.io/gitea/tests"
  18. "github.com/stretchr/testify/assert"
  19. )
  20. func TestLinguist(t *testing.T) {
  21. onGiteaRun(t, func(t *testing.T, _ *url.URL) {
  22. user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
  23. cppContent := "#include <iostream>\nint main() {\nstd::cout << \"Hello Gitea!\";\nreturn 0;\n}"
  24. pyContent := "print(\"Hello Gitea!\")"
  25. phpContent := "<?php\necho 'Hallo Welt';\n?>"
  26. lockContent := "# This file is automatically @generated by Poetry 1.7.1 and should not be changed by hand."
  27. mdContent := "markdown"
  28. cases := []struct {
  29. GitAttributesContent string
  30. FilesToAdd []*files_service.ChangeRepoFile
  31. ExpectedLanguageOrder []string
  32. }{
  33. // case 0
  34. {
  35. ExpectedLanguageOrder: []string{},
  36. },
  37. // case 1
  38. {
  39. FilesToAdd: []*files_service.ChangeRepoFile{
  40. {
  41. TreePath: "cplusplus.cpp",
  42. ContentReader: strings.NewReader(cppContent),
  43. },
  44. {
  45. TreePath: "python.py",
  46. ContentReader: strings.NewReader(pyContent),
  47. },
  48. {
  49. TreePath: "php.php",
  50. ContentReader: strings.NewReader(phpContent),
  51. },
  52. },
  53. ExpectedLanguageOrder: []string{"C++", "PHP", "Python"},
  54. },
  55. // case 2
  56. {
  57. FilesToAdd: []*files_service.ChangeRepoFile{
  58. {
  59. TreePath: ".cplusplus.cpp",
  60. ContentReader: strings.NewReader(cppContent),
  61. },
  62. {
  63. TreePath: "python.py",
  64. ContentReader: strings.NewReader(pyContent),
  65. },
  66. {
  67. TreePath: "vendor/php.php",
  68. ContentReader: strings.NewReader(phpContent),
  69. },
  70. },
  71. ExpectedLanguageOrder: []string{"Python"},
  72. },
  73. // case 3
  74. {
  75. GitAttributesContent: "*.cpp linguist-language=Go",
  76. FilesToAdd: []*files_service.ChangeRepoFile{
  77. {
  78. TreePath: "cplusplus.cpp",
  79. ContentReader: strings.NewReader(cppContent),
  80. },
  81. },
  82. ExpectedLanguageOrder: []string{"Go"},
  83. },
  84. // case 4
  85. {
  86. GitAttributesContent: "*.cpp gitlab-language=Go?parent=json",
  87. FilesToAdd: []*files_service.ChangeRepoFile{
  88. {
  89. TreePath: "cplusplus.cpp",
  90. ContentReader: strings.NewReader(cppContent),
  91. },
  92. },
  93. ExpectedLanguageOrder: []string{"Go"},
  94. },
  95. // case 5
  96. {
  97. GitAttributesContent: "*.cpp linguist-language=HTML gitlab-language=Go?parent=json",
  98. FilesToAdd: []*files_service.ChangeRepoFile{
  99. {
  100. TreePath: "cplusplus.cpp",
  101. ContentReader: strings.NewReader(cppContent),
  102. },
  103. },
  104. ExpectedLanguageOrder: []string{"HTML"},
  105. },
  106. // case 6
  107. {
  108. GitAttributesContent: "vendor/** linguist-vendored=false",
  109. FilesToAdd: []*files_service.ChangeRepoFile{
  110. {
  111. TreePath: "vendor/php.php",
  112. ContentReader: strings.NewReader(phpContent),
  113. },
  114. },
  115. ExpectedLanguageOrder: []string{"PHP"},
  116. },
  117. // case 7
  118. {
  119. GitAttributesContent: "*.cpp linguist-vendored=true\n*.py linguist-vendored\nvendor/** -linguist-vendored",
  120. FilesToAdd: []*files_service.ChangeRepoFile{
  121. {
  122. TreePath: "cplusplus.cpp",
  123. ContentReader: strings.NewReader(cppContent),
  124. },
  125. {
  126. TreePath: "python.py",
  127. ContentReader: strings.NewReader(pyContent),
  128. },
  129. {
  130. TreePath: "vendor/php.php",
  131. ContentReader: strings.NewReader(phpContent),
  132. },
  133. },
  134. ExpectedLanguageOrder: []string{"PHP"},
  135. },
  136. // case 8
  137. {
  138. GitAttributesContent: "poetry.lock linguist-language=Go",
  139. FilesToAdd: []*files_service.ChangeRepoFile{
  140. {
  141. TreePath: "poetry.lock",
  142. ContentReader: strings.NewReader(lockContent),
  143. },
  144. },
  145. ExpectedLanguageOrder: []string{"Go"},
  146. },
  147. // case 9
  148. {
  149. GitAttributesContent: "poetry.lock linguist-generated=false",
  150. FilesToAdd: []*files_service.ChangeRepoFile{
  151. {
  152. TreePath: "poetry.lock",
  153. ContentReader: strings.NewReader(lockContent),
  154. },
  155. },
  156. ExpectedLanguageOrder: []string{"TOML"},
  157. },
  158. // case 10
  159. {
  160. GitAttributesContent: "*.cpp -linguist-detectable",
  161. FilesToAdd: []*files_service.ChangeRepoFile{
  162. {
  163. TreePath: "cplusplus.cpp",
  164. ContentReader: strings.NewReader(cppContent),
  165. },
  166. },
  167. ExpectedLanguageOrder: []string{},
  168. },
  169. // case 11
  170. {
  171. GitAttributesContent: "*.md linguist-detectable",
  172. FilesToAdd: []*files_service.ChangeRepoFile{
  173. {
  174. TreePath: "test.md",
  175. ContentReader: strings.NewReader(mdContent),
  176. },
  177. },
  178. ExpectedLanguageOrder: []string{"Markdown"},
  179. },
  180. // case 12
  181. {
  182. GitAttributesContent: "test2.md linguist-detectable",
  183. FilesToAdd: []*files_service.ChangeRepoFile{
  184. {
  185. TreePath: "cplusplus.cpp",
  186. ContentReader: strings.NewReader(cppContent),
  187. },
  188. {
  189. TreePath: "test.md",
  190. ContentReader: strings.NewReader(mdContent),
  191. },
  192. {
  193. TreePath: "test2.md",
  194. ContentReader: strings.NewReader(mdContent),
  195. },
  196. },
  197. ExpectedLanguageOrder: []string{"C++", "Markdown"},
  198. },
  199. // case 13
  200. {
  201. GitAttributesContent: "README.md linguist-documentation=false",
  202. FilesToAdd: []*files_service.ChangeRepoFile{
  203. {
  204. TreePath: "README.md",
  205. ContentReader: strings.NewReader(mdContent),
  206. },
  207. },
  208. ExpectedLanguageOrder: []string{"Markdown"},
  209. },
  210. }
  211. for i, c := range cases {
  212. t.Run("Case-"+strconv.Itoa(i), func(t *testing.T) {
  213. defer tests.PrintCurrentTest(t)()
  214. repo, err := repo_service.CreateRepository(t.Context(), user, user, repo_service.CreateRepoOptions{
  215. Name: "linguist-test-" + strconv.Itoa(i),
  216. })
  217. assert.NoError(t, err)
  218. files := []*files_service.ChangeRepoFile{
  219. {
  220. TreePath: ".gitattributes",
  221. ContentReader: strings.NewReader(c.GitAttributesContent),
  222. },
  223. }
  224. files = append(files, c.FilesToAdd...)
  225. for _, f := range files {
  226. f.Operation = "create"
  227. }
  228. _, err = files_service.ChangeRepoFiles(t.Context(), repo, user, &files_service.ChangeRepoFilesOptions{
  229. Files: files,
  230. OldBranch: repo.DefaultBranch,
  231. NewBranch: repo.DefaultBranch,
  232. })
  233. assert.NoError(t, err)
  234. assert.NoError(t, stats.UpdateRepoIndexer(repo))
  235. assert.NoError(t, queue.GetManager().FlushAll(t.Context(), 10*time.Second))
  236. stats, err := repo_model.GetTopLanguageStats(t.Context(), repo, len(c.FilesToAdd))
  237. assert.NoError(t, err)
  238. languages := make([]string, 0, len(stats))
  239. for _, s := range stats {
  240. languages = append(languages, s.Language)
  241. }
  242. assert.Equal(t, c.ExpectedLanguageOrder, languages, "case %d: unexpected language stats", i)
  243. })
  244. }
  245. })
  246. }