gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package attribute
  4. import (
  5. "path/filepath"
  6. "testing"
  7. "time"
  8. "code.gitea.io/gitea/modules/git"
  9. "code.gitea.io/gitea/modules/setting"
  10. "code.gitea.io/gitea/modules/test"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. )
  14. func Test_nulSeparatedAttributeWriter_ReadAttribute(t *testing.T) {
  15. wr := &nulSeparatedAttributeWriter{
  16. attributes: make(chan attributeTriple, 5),
  17. }
  18. testStr := ".gitignore\"\n\x00linguist-vendored\x00unspecified\x00"
  19. n, err := wr.Write([]byte(testStr))
  20. assert.Len(t, testStr, n)
  21. assert.NoError(t, err)
  22. select {
  23. case attr := <-wr.ReadAttribute():
  24. assert.Equal(t, ".gitignore\"\n", attr.Filename)
  25. assert.Equal(t, LinguistVendored, attr.Attribute)
  26. assert.Equal(t, "unspecified", attr.Value)
  27. case <-time.After(100 * time.Millisecond):
  28. assert.FailNow(t, "took too long to read an attribute from the list")
  29. }
  30. // Write a second attribute again
  31. n, err = wr.Write([]byte(testStr))
  32. assert.Len(t, testStr, n)
  33. assert.NoError(t, err)
  34. select {
  35. case attr := <-wr.ReadAttribute():
  36. assert.Equal(t, ".gitignore\"\n", attr.Filename)
  37. assert.Equal(t, LinguistVendored, attr.Attribute)
  38. assert.Equal(t, "unspecified", attr.Value)
  39. case <-time.After(100 * time.Millisecond):
  40. assert.FailNow(t, "took too long to read an attribute from the list")
  41. }
  42. // Write a partial attribute
  43. _, err = wr.Write([]byte("incomplete-file"))
  44. assert.NoError(t, err)
  45. _, err = wr.Write([]byte("name\x00"))
  46. assert.NoError(t, err)
  47. select {
  48. case <-wr.ReadAttribute():
  49. assert.FailNow(t, "There should not be an attribute ready to read")
  50. case <-time.After(100 * time.Millisecond):
  51. }
  52. _, err = wr.Write([]byte("attribute\x00"))
  53. assert.NoError(t, err)
  54. select {
  55. case <-wr.ReadAttribute():
  56. assert.FailNow(t, "There should not be an attribute ready to read")
  57. case <-time.After(100 * time.Millisecond):
  58. }
  59. _, err = wr.Write([]byte("value\x00"))
  60. assert.NoError(t, err)
  61. attr := <-wr.ReadAttribute()
  62. assert.Equal(t, "incomplete-filename", attr.Filename)
  63. assert.Equal(t, "attribute", attr.Attribute)
  64. assert.Equal(t, "value", attr.Value)
  65. _, err = wr.Write([]byte("shouldbe.vendor\x00linguist-vendored\x00set\x00shouldbe.vendor\x00linguist-generated\x00unspecified\x00shouldbe.vendor\x00linguist-language\x00unspecified\x00"))
  66. assert.NoError(t, err)
  67. attr = <-wr.ReadAttribute()
  68. assert.NoError(t, err)
  69. assert.Equal(t, attributeTriple{
  70. Filename: "shouldbe.vendor",
  71. Attribute: LinguistVendored,
  72. Value: "set",
  73. }, attr)
  74. attr = <-wr.ReadAttribute()
  75. assert.NoError(t, err)
  76. assert.Equal(t, attributeTriple{
  77. Filename: "shouldbe.vendor",
  78. Attribute: LinguistGenerated,
  79. Value: "unspecified",
  80. }, attr)
  81. attr = <-wr.ReadAttribute()
  82. assert.NoError(t, err)
  83. assert.Equal(t, attributeTriple{
  84. Filename: "shouldbe.vendor",
  85. Attribute: LinguistLanguage,
  86. Value: "unspecified",
  87. }, attr)
  88. }
  89. func expectedAttrs() *Attributes {
  90. return &Attributes{
  91. m: map[string]Attribute{
  92. LinguistGenerated: "unspecified",
  93. LinguistDetectable: "unspecified",
  94. LinguistDocumentation: "unspecified",
  95. LinguistVendored: "unspecified",
  96. LinguistLanguage: "Python",
  97. GitlabLanguage: "unspecified",
  98. },
  99. }
  100. }
  101. func Test_BatchChecker(t *testing.T) {
  102. setting.AppDataPath = t.TempDir()
  103. repoPath := "../tests/repos/language_stats_repo"
  104. gitRepo, err := git.OpenRepository(t.Context(), repoPath)
  105. require.NoError(t, err)
  106. defer gitRepo.Close()
  107. commitID := "8fee858da5796dfb37704761701bb8e800ad9ef3"
  108. t.Run("Create index file to run git check-attr", func(t *testing.T) {
  109. defer test.MockVariableValue(&git.DefaultFeatures().SupportCheckAttrOnBare, false)()
  110. checker, err := NewBatchChecker(gitRepo, commitID, LinguistAttributes)
  111. assert.NoError(t, err)
  112. defer checker.Close()
  113. attributes, err := checker.CheckPath("i-am-a-python.p")
  114. assert.NoError(t, err)
  115. assert.Equal(t, expectedAttrs(), attributes)
  116. })
  117. // run git check-attr on work tree
  118. t.Run("Run git check-attr on git work tree", func(t *testing.T) {
  119. dir := filepath.Join(t.TempDir(), "test-repo")
  120. err := git.Clone(t.Context(), repoPath, dir, git.CloneRepoOptions{
  121. Shared: true,
  122. Branch: "master",
  123. })
  124. assert.NoError(t, err)
  125. tempRepo, err := git.OpenRepository(t.Context(), dir)
  126. assert.NoError(t, err)
  127. defer tempRepo.Close()
  128. checker, err := NewBatchChecker(tempRepo, "", LinguistAttributes)
  129. assert.NoError(t, err)
  130. defer checker.Close()
  131. attributes, err := checker.CheckPath("i-am-a-python.p")
  132. assert.NoError(t, err)
  133. assert.Equal(t, expectedAttrs(), attributes)
  134. })
  135. if !git.DefaultFeatures().SupportCheckAttrOnBare {
  136. t.Skip("git version 2.40 is required to support run check-attr on bare repo")
  137. return
  138. }
  139. t.Run("Run git check-attr in bare repository", func(t *testing.T) {
  140. checker, err := NewBatchChecker(gitRepo, commitID, LinguistAttributes)
  141. assert.NoError(t, err)
  142. defer checker.Close()
  143. attributes, err := checker.CheckPath("i-am-a-python.p")
  144. assert.NoError(t, err)
  145. assert.Equal(t, expectedAttrs(), attributes)
  146. })
  147. }