本站源代码
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

125 行
4.0KB

  1. package git
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. const testReposDir = "tests/repos/"
  10. const benchmarkReposDir = "benchmark/repos/"
  11. func cloneRepo(url, dir, name string) (string, error) {
  12. repoDir := filepath.Join(dir, name)
  13. if _, err := os.Stat(repoDir); err == nil {
  14. return repoDir, nil
  15. }
  16. return repoDir, Clone(url, repoDir, CloneRepoOptions{
  17. Mirror: false,
  18. Bare: false,
  19. Quiet: true,
  20. Timeout: 5 * time.Minute,
  21. })
  22. }
  23. func testGetCommitsInfo(t *testing.T, repo1 *Repository) {
  24. // these test case are specific to the repo1 test repo
  25. testCases := []struct {
  26. CommitID string
  27. Path string
  28. ExpectedIDs map[string]string
  29. ExpectedTreeCommit string
  30. }{
  31. {"8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2", "", map[string]string{
  32. "file1.txt": "95bb4d39648ee7e325106df01a621c530863a653",
  33. "file2.txt": "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2",
  34. }, "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2"},
  35. {"2839944139e0de9737a044f78b0e4b40d989a9e3", "", map[string]string{
  36. "file1.txt": "2839944139e0de9737a044f78b0e4b40d989a9e3",
  37. "branch1.txt": "9c9aef8dd84e02bc7ec12641deb4c930a7c30185",
  38. }, "2839944139e0de9737a044f78b0e4b40d989a9e3"},
  39. {"5c80b0245c1c6f8343fa418ec374b13b5d4ee658", "branch2", map[string]string{
  40. "branch2.txt": "5c80b0245c1c6f8343fa418ec374b13b5d4ee658",
  41. }, "5c80b0245c1c6f8343fa418ec374b13b5d4ee658"},
  42. {"feaf4ba6bc635fec442f46ddd4512416ec43c2c2", "", map[string]string{
  43. "file1.txt": "95bb4d39648ee7e325106df01a621c530863a653",
  44. "file2.txt": "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2",
  45. "foo": "37991dec2c8e592043f47155ce4808d4580f9123",
  46. }, "feaf4ba6bc635fec442f46ddd4512416ec43c2c2"},
  47. }
  48. for _, testCase := range testCases {
  49. commit, err := repo1.GetCommit(testCase.CommitID)
  50. assert.NoError(t, err)
  51. tree, err := commit.Tree.SubTree(testCase.Path)
  52. assert.NoError(t, err)
  53. entries, err := tree.ListEntries()
  54. assert.NoError(t, err)
  55. commitsInfo, treeCommit, err := entries.GetCommitsInfo(commit, testCase.Path, nil)
  56. assert.Equal(t, testCase.ExpectedTreeCommit, treeCommit.ID.String())
  57. assert.NoError(t, err)
  58. assert.Len(t, commitsInfo, len(testCase.ExpectedIDs))
  59. for _, commitInfo := range commitsInfo {
  60. entry := commitInfo[0].(*TreeEntry)
  61. commit := commitInfo[1].(*Commit)
  62. expectedID, ok := testCase.ExpectedIDs[entry.Name()]
  63. if !assert.True(t, ok) {
  64. continue
  65. }
  66. assert.Equal(t, expectedID, commit.ID.String())
  67. }
  68. }
  69. }
  70. func TestEntries_GetCommitsInfo(t *testing.T) {
  71. bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
  72. bareRepo1, err := OpenRepository(bareRepo1Path)
  73. assert.NoError(t, err)
  74. testGetCommitsInfo(t, bareRepo1)
  75. clonedPath, err := cloneRepo(bareRepo1Path, testReposDir, "repo1_TestEntries_GetCommitsInfo")
  76. assert.NoError(t, err)
  77. defer os.RemoveAll(clonedPath)
  78. clonedRepo1, err := OpenRepository(clonedPath)
  79. assert.NoError(t, err)
  80. testGetCommitsInfo(t, clonedRepo1)
  81. }
  82. func BenchmarkEntries_GetCommitsInfo(b *testing.B) {
  83. benchmarks := []struct {
  84. url string
  85. name string
  86. }{
  87. {url: "https://github.com/go-gitea/gitea.git", name: "gitea"},
  88. {url: "https://github.com/ethantkoenig/manyfiles.git", name: "manyfiles"},
  89. {url: "https://github.com/moby/moby.git", name: "moby"},
  90. {url: "https://github.com/golang/go.git", name: "go"},
  91. {url: "https://github.com/torvalds/linux.git", name: "linux"},
  92. }
  93. for _, benchmark := range benchmarks {
  94. var commit *Commit
  95. var entries Entries
  96. if repoPath, err := cloneRepo(benchmark.url, benchmarkReposDir, benchmark.name); err != nil {
  97. b.Fatal(err)
  98. } else if repo, err := OpenRepository(repoPath); err != nil {
  99. b.Fatal(err)
  100. } else if commit, err = repo.GetBranchCommit("master"); err != nil {
  101. b.Fatal(err)
  102. } else if entries, err = commit.Tree.ListEntries(); err != nil {
  103. b.Fatal(err)
  104. }
  105. entries.Sort()
  106. b.ResetTimer()
  107. b.Run(benchmark.name, func(b *testing.B) {
  108. for i := 0; i < b.N; i++ {
  109. _, _, err := entries.GetCommitsInfo(commit, "", nil)
  110. if err != nil {
  111. b.Fatal(err)
  112. }
  113. }
  114. })
  115. }
  116. }
上海开阖软件有限公司 沪ICP备12045867号-1