history_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package git
  2. import (
  3. "testing"
  4. )
  5. func TestLogReturnsNewestFirst(t *testing.T) {
  6. manager := newTestManager(t)
  7. repoPath := newTestRepo(t, manager)
  8. commitFile(t, manager, repoPath, "a.txt", "one\n", "first commit")
  9. commitFile(t, manager, repoPath, "b.txt", "two\n", "second commit")
  10. third := commitFile(t, manager, repoPath, "c.txt", "three\n", "third commit")
  11. commits, err := manager.Log(repoPath, 10)
  12. if err != nil {
  13. t.Fatalf("Log() returned error: %v", err)
  14. }
  15. if len(commits) != 3 {
  16. t.Fatalf("Log() = %d commits, want 3", len(commits))
  17. }
  18. if commits[0].Hash != third {
  19. t.Errorf("Log()[0].Hash = %q, want the newest commit %q", commits[0].Hash, third)
  20. }
  21. if commits[0].Subject != "third commit" {
  22. t.Errorf("Log()[0].Subject = %q, want %q", commits[0].Subject, "third commit")
  23. }
  24. if len(commits[0].ShortHash) != 7 {
  25. t.Errorf("ShortHash = %q, want 7 characters", commits[0].ShortHash)
  26. }
  27. if commits[0].AuthorName != "Test User" {
  28. t.Errorf("AuthorName = %q, want %q", commits[0].AuthorName, "Test User")
  29. }
  30. if commits[0].Timestamp == 0 {
  31. t.Errorf("Timestamp = 0, want the author time")
  32. }
  33. }
  34. func TestLogRespectsLimit(t *testing.T) {
  35. manager := newTestManager(t)
  36. repoPath := newTestRepo(t, manager)
  37. for _, name := range []string{"a", "b", "c", "d"} {
  38. commitFile(t, manager, repoPath, name+".txt", name+"\n", "commit "+name)
  39. }
  40. tests := []struct {
  41. name string
  42. limit int
  43. want int
  44. }{
  45. {name: "limit below total", limit: 2, want: 2},
  46. {name: "limit above total", limit: 100, want: 4},
  47. {name: "zero falls back to default", limit: 0, want: 4},
  48. {name: "negative falls back to default", limit: -5, want: 4},
  49. }
  50. for _, test := range tests {
  51. t.Run(test.name, func(t *testing.T) {
  52. commits, err := manager.Log(repoPath, test.limit)
  53. if err != nil {
  54. t.Fatalf("Log() returned error: %v", err)
  55. }
  56. if len(commits) != test.want {
  57. t.Errorf("Log(limit=%d) = %d commits, want %d", test.limit, len(commits), test.want)
  58. }
  59. })
  60. }
  61. }
  62. func TestLogOnUnbornBranchIsEmpty(t *testing.T) {
  63. manager := newTestManager(t)
  64. repoPath := newTestRepo(t, manager)
  65. commits, err := manager.Log(repoPath, 10)
  66. if err != nil {
  67. t.Fatalf("Log() on an unborn branch returned error: %v", err)
  68. }
  69. if len(commits) != 0 {
  70. t.Errorf("Log() = %d commits, want 0", len(commits))
  71. }
  72. }
  73. func TestBranchesAndCheckout(t *testing.T) {
  74. manager := newTestManager(t)
  75. repoPath := newTestRepo(t, manager)
  76. commitFile(t, manager, repoPath, "a.txt", "one\n", "first")
  77. branches, err := manager.Branches(repoPath)
  78. if err != nil {
  79. t.Fatalf("Branches() returned error: %v", err)
  80. }
  81. if len(branches) != 1 {
  82. t.Fatalf("Branches() = %d entries, want 1", len(branches))
  83. }
  84. if !branches[0].IsCurrent {
  85. t.Errorf("the only branch is not flagged as current")
  86. }
  87. defaultBranch := branches[0].Name
  88. if err := manager.Checkout(repoPath, "feature", true); err != nil {
  89. t.Fatalf("Checkout(create) returned error: %v", err)
  90. }
  91. branches, err = manager.Branches(repoPath)
  92. if err != nil {
  93. t.Fatalf("Branches() returned error: %v", err)
  94. }
  95. if len(branches) != 2 {
  96. t.Fatalf("Branches() after create = %d entries, want 2", len(branches))
  97. }
  98. current := ""
  99. for _, branch := range branches {
  100. if branch.IsCurrent {
  101. current = branch.Name
  102. }
  103. }
  104. if current != "feature" {
  105. t.Errorf("current branch = %q, want %q", current, "feature")
  106. }
  107. if err := manager.Checkout(repoPath, defaultBranch, false); err != nil {
  108. t.Fatalf("Checkout(existing) returned error: %v", err)
  109. }
  110. }
  111. func TestCheckoutValidation(t *testing.T) {
  112. manager := newTestManager(t)
  113. repoPath := newTestRepo(t, manager)
  114. commitFile(t, manager, repoPath, "a.txt", "one\n", "first")
  115. tests := []struct {
  116. name string
  117. branch string
  118. }{
  119. {name: "empty name", branch: ""},
  120. {name: "whitespace only", branch: " "},
  121. {name: "space inside", branch: "my branch"},
  122. {name: "leading dash", branch: "-force"},
  123. {name: "double dot", branch: "a..b"},
  124. {name: "reflog syntax", branch: "main@{1}"},
  125. {name: "caret", branch: "main^"},
  126. {name: "tilde", branch: "main~1"},
  127. {name: "colon", branch: "refs:heads"},
  128. }
  129. for _, test := range tests {
  130. t.Run(test.name, func(t *testing.T) {
  131. if err := manager.Checkout(repoPath, test.branch, true); err == nil {
  132. t.Errorf("Checkout(%q) = nil error, want an error", test.branch)
  133. }
  134. })
  135. }
  136. }
  137. func TestValidateBranchName(t *testing.T) {
  138. tests := []struct {
  139. name string
  140. branch string
  141. wantError bool
  142. }{
  143. {name: "simple", branch: "master"},
  144. {name: "with slash", branch: "feature/login"},
  145. {name: "with dash", branch: "fix-123"},
  146. {name: "with dot", branch: "v3.0.2"},
  147. //An empty name would build the malformed ref "refs/heads/"
  148. {name: "empty", branch: "", wantError: true},
  149. {name: "whitespace only", branch: " ", wantError: true},
  150. {name: "leading dash", branch: "-x", wantError: true},
  151. {name: "leading slash", branch: "/x", wantError: true},
  152. {name: "trailing slash", branch: "x/", wantError: true},
  153. {name: "double dot", branch: "a..b", wantError: true},
  154. {name: "reflog", branch: "a@{0}", wantError: true},
  155. {name: "space", branch: "a b", wantError: true},
  156. {name: "asterisk", branch: "a*", wantError: true},
  157. {name: "backslash", branch: "a\\b", wantError: true},
  158. {name: "control character", branch: "a\tb", wantError: true},
  159. }
  160. for _, test := range tests {
  161. t.Run(test.name, func(t *testing.T) {
  162. err := validateBranchName(test.branch)
  163. if test.wantError && err == nil {
  164. t.Errorf("validateBranchName(%q) = nil, want an error", test.branch)
  165. }
  166. if !test.wantError && err != nil {
  167. t.Errorf("validateBranchName(%q) = %v, want nil", test.branch, err)
  168. }
  169. })
  170. }
  171. }
  172. func TestShortLocalName(t *testing.T) {
  173. tests := []struct {
  174. name string
  175. input string
  176. want string
  177. }{
  178. {name: "remote branch", input: "origin/master", want: "master"},
  179. {name: "nested remote branch", input: "origin/feature/login", want: "feature/login"},
  180. {name: "local branch", input: "master", want: "master"},
  181. }
  182. for _, test := range tests {
  183. t.Run(test.name, func(t *testing.T) {
  184. if got := shortLocalName(test.input); got != test.want {
  185. t.Errorf("shortLocalName(%q) = %q, want %q", test.input, got, test.want)
  186. }
  187. })
  188. }
  189. }
  190. func TestCommitToInfoSplitsSubject(t *testing.T) {
  191. manager := newTestManager(t)
  192. repoPath := newTestRepo(t, manager)
  193. commitFile(t, manager, repoPath, "a.txt", "one\n", "short subject\n\nA longer body explaining why.\n")
  194. commits, err := manager.Log(repoPath, 1)
  195. if err != nil {
  196. t.Fatalf("Log() returned error: %v", err)
  197. }
  198. if len(commits) != 1 {
  199. t.Fatalf("Log() = %d commits, want 1", len(commits))
  200. }
  201. if commits[0].Subject != "short subject" {
  202. t.Errorf("Subject = %q, want %q", commits[0].Subject, "short subject")
  203. }
  204. if commits[0].Message == commits[0].Subject {
  205. t.Errorf("Message should keep the full body, got %q", commits[0].Message)
  206. }
  207. }