4
0

git_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package git_test
  2. // TODO: commenting out this test for now because it's flaky. Alternatives to consider to get this working:
  3. // -- use `git stash` instead of `restore()`
  4. // const repoBasePath = "../../testdata/repos/"
  5. // const expectPath = "../../testdata/expected/"
  6. // func TestGitLog(t *testing.T) {
  7. // tests := []struct {
  8. // source string
  9. // logOpts string
  10. // expected string
  11. // }{
  12. // {
  13. // source: filepath.Join(repoBasePath, "small"),
  14. // expected: filepath.Join(expectPath, "git", "small.txt"),
  15. // },
  16. // {
  17. // source: filepath.Join(repoBasePath, "small"),
  18. // expected: filepath.Join(expectPath, "git", "small-branch-foo.txt"),
  19. // logOpts: "--all foo...",
  20. // },
  21. // }
  22. // err := moveDotGit("dotGit", ".git")
  23. // if err != nil {
  24. // t.Fatal(err)
  25. // }
  26. // defer func() {
  27. // if err = moveDotGit(".git", "dotGit"); err != nil {
  28. // t.Fatal(err)
  29. // }
  30. // }()
  31. // for _, tt := range tests {
  32. // files, err := git.GitLog(tt.source, tt.logOpts)
  33. // if err != nil {
  34. // t.Error(err)
  35. // }
  36. // var diffSb strings.Builder
  37. // for f := range files {
  38. // for _, tf := range f.TextFragments {
  39. // diffSb.WriteString(tf.Raw(gitdiff.OpAdd))
  40. // }
  41. // }
  42. // expectedBytes, err := os.ReadFile(tt.expected)
  43. // if err != nil {
  44. // t.Error(err)
  45. // }
  46. // expected := string(expectedBytes)
  47. // if expected != diffSb.String() {
  48. // // write string builder to .got file using os.Create
  49. // err = os.WriteFile(strings.Replace(tt.expected, ".txt", ".got.txt", 1), []byte(diffSb.String()), 0644)
  50. // if err != nil {
  51. // t.Error(err)
  52. // }
  53. // t.Error("expected: ", expected, "got: ", diffSb.String())
  54. // }
  55. // }
  56. // }
  57. // func TestGitDiff(t *testing.T) {
  58. // tests := []struct {
  59. // source string
  60. // expected string
  61. // additions string
  62. // target string
  63. // }{
  64. // {
  65. // source: filepath.Join(repoBasePath, "small"),
  66. // expected: "this line is added\nand another one",
  67. // additions: "this line is added\nand another one",
  68. // target: filepath.Join(repoBasePath, "small", "main.go"),
  69. // },
  70. // }
  71. // err := moveDotGit("dotGit", ".git")
  72. // if err != nil {
  73. // t.Fatal(err)
  74. // }
  75. // defer func() {
  76. // if err = moveDotGit(".git", "dotGit"); err != nil {
  77. // t.Fatal(err)
  78. // }
  79. // }()
  80. // for _, tt := range tests {
  81. // noChanges, err := os.ReadFile(tt.target)
  82. // if err != nil {
  83. // t.Error(err)
  84. // }
  85. // err = os.WriteFile(tt.target, []byte(tt.additions), 0644)
  86. // if err != nil {
  87. // restore(tt.target, noChanges, t)
  88. // t.Error(err)
  89. // }
  90. // files, err := git.GitDiff(tt.source, false)
  91. // if err != nil {
  92. // restore(tt.target, noChanges, t)
  93. // t.Error(err)
  94. // }
  95. // for f := range files {
  96. // sb := strings.Builder{}
  97. // for _, tf := range f.TextFragments {
  98. // sb.WriteString(tf.Raw(gitdiff.OpAdd))
  99. // }
  100. // if sb.String() != tt.expected {
  101. // restore(tt.target, noChanges, t)
  102. // t.Error("expected: ", tt.expected, "got: ", sb.String())
  103. // }
  104. // }
  105. // restore(tt.target, noChanges, t)
  106. // }
  107. // }
  108. // func restore(path string, data []byte, t *testing.T) {
  109. // err := os.WriteFile(path, data, 0644)
  110. // if err != nil {
  111. // t.Fatal(err)
  112. // }
  113. // }
  114. // func moveDotGit(from, to string) error {
  115. // repoDirs, err := os.ReadDir("../../testdata/repos")
  116. // if err != nil {
  117. // return err
  118. // }
  119. // for _, dir := range repoDirs {
  120. // if to == ".git" {
  121. // _, err := os.Stat(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), "dotGit"))
  122. // if os.IsNotExist(err) {
  123. // // dont want to delete the only copy of .git accidentally
  124. // continue
  125. // }
  126. // os.RemoveAll(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), ".git"))
  127. // }
  128. // if !dir.IsDir() {
  129. // continue
  130. // }
  131. // _, err := os.Stat(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), from))
  132. // if os.IsNotExist(err) {
  133. // continue
  134. // }
  135. // err = os.Rename(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), from),
  136. // fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), to))
  137. // if err != nil {
  138. // return err
  139. // }
  140. // }
  141. // return nil
  142. // }