gitea源码

checker_test.go 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2025 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package attribute
  4. import (
  5. "path/filepath"
  6. "testing"
  7. "code.gitea.io/gitea/modules/git"
  8. "code.gitea.io/gitea/modules/setting"
  9. "code.gitea.io/gitea/modules/test"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. )
  13. func Test_Checker(t *testing.T) {
  14. setting.AppDataPath = t.TempDir()
  15. repoPath := "../tests/repos/language_stats_repo"
  16. gitRepo, err := git.OpenRepository(t.Context(), repoPath)
  17. require.NoError(t, err)
  18. defer gitRepo.Close()
  19. commitID := "8fee858da5796dfb37704761701bb8e800ad9ef3"
  20. t.Run("Create index file to run git check-attr", func(t *testing.T) {
  21. defer test.MockVariableValue(&git.DefaultFeatures().SupportCheckAttrOnBare, false)()
  22. attrs, err := CheckAttributes(t.Context(), gitRepo, commitID, CheckAttributeOpts{
  23. Filenames: []string{"i-am-a-python.p"},
  24. Attributes: LinguistAttributes,
  25. })
  26. assert.NoError(t, err)
  27. assert.Len(t, attrs, 1)
  28. assert.Equal(t, expectedAttrs(), attrs["i-am-a-python.p"])
  29. })
  30. // run git check-attr on work tree
  31. t.Run("Run git check-attr on git work tree", func(t *testing.T) {
  32. dir := filepath.Join(t.TempDir(), "test-repo")
  33. err := git.Clone(t.Context(), repoPath, dir, git.CloneRepoOptions{
  34. Shared: true,
  35. Branch: "master",
  36. })
  37. assert.NoError(t, err)
  38. tempRepo, err := git.OpenRepository(t.Context(), dir)
  39. assert.NoError(t, err)
  40. defer tempRepo.Close()
  41. attrs, err := CheckAttributes(t.Context(), tempRepo, "", CheckAttributeOpts{
  42. Filenames: []string{"i-am-a-python.p"},
  43. Attributes: LinguistAttributes,
  44. })
  45. assert.NoError(t, err)
  46. assert.Len(t, attrs, 1)
  47. assert.Equal(t, expectedAttrs(), attrs["i-am-a-python.p"])
  48. })
  49. t.Run("Run git check-attr in bare repository using index", func(t *testing.T) {
  50. attrs, err := CheckAttributes(t.Context(), gitRepo, "", CheckAttributeOpts{
  51. Filenames: []string{"i-am-a-python.p"},
  52. Attributes: LinguistAttributes,
  53. })
  54. assert.NoError(t, err)
  55. assert.Len(t, attrs, 1)
  56. assert.Equal(t, expectedAttrs(), attrs["i-am-a-python.p"])
  57. })
  58. if !git.DefaultFeatures().SupportCheckAttrOnBare {
  59. t.Skip("git version 2.40 is required to support run check-attr on bare repo without using index")
  60. return
  61. }
  62. t.Run("Run git check-attr in bare repository", func(t *testing.T) {
  63. attrs, err := CheckAttributes(t.Context(), gitRepo, commitID, CheckAttributeOpts{
  64. Filenames: []string{"i-am-a-python.p"},
  65. Attributes: LinguistAttributes,
  66. })
  67. assert.NoError(t, err)
  68. assert.Len(t, attrs, 1)
  69. assert.Equal(t, expectedAttrs(), attrs["i-am-a-python.p"])
  70. })
  71. }