push_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package main
  2. import (
  3. "io/ioutil"
  4. "os/exec"
  5. "path/filepath"
  6. "strings"
  7. "testing"
  8. )
  9. func TestPush(t *testing.T) {
  10. // $ git clone ipfs://ipfs/$hash/repo.git $tmpDir
  11. startURL := "ipfs://ipfs/QmZhuM4TxuhxbamPtWHyHYCUXfkqCkgBmWREKF2kqTLbvz/unpackedTest"
  12. tmpDir := cloneAndCheckout(t, startURL, expectedClone)
  13. // $ cd repo && make $stuff
  14. checkFatal(t, ioutil.WriteFile(filepath.Join(tmpDir, "newFile"), []byte("Hello From Test"), 0700))
  15. cmd := exec.Command(gitPath, "add", "newFile")
  16. cmd.Dir = tmpDir
  17. out, err := cmd.CombinedOutput()
  18. t.Log("git add out: ", string(out))
  19. checkFatal(t, err)
  20. // $ git commit -a -m 'done!'
  21. cmd = exec.Command(gitPath, "commit", "-m", "Test Add newFile Commit")
  22. cmd.Dir = tmpDir
  23. out, err = cmd.CombinedOutput()
  24. t.Log("git commit out: ", string(out))
  25. checkFatal(t, err)
  26. // $ git push origin
  27. cmd = exec.Command(gitPath, "push", "origin")
  28. cmd.Dir = tmpDir
  29. out, err = cmd.CombinedOutput()
  30. t.Log("git push out: ", string(out))
  31. checkFatal(t, err)
  32. cmd = exec.Command(gitPath, "config", "--get", "remote.origin.url")
  33. cmd.Dir = tmpDir
  34. out, err = cmd.CombinedOutput()
  35. checkFatal(t, err)
  36. newURL := strings.TrimSpace(string(out))
  37. if newURL == startURL {
  38. t.Fatalf("remote url wasn't updated. is:%q", newURL)
  39. }
  40. rmDir(t, tmpDir)
  41. var expectedClone = map[string]string{
  42. "testA": "9417d011822b875da72221c8d188089cbfcee806",
  43. "hello.txt": "e2839ad2e47386d342038958fba941fc78e3780e",
  44. "notes": "32ed91604b272860ec911fc2bf4ae631b7900aa8",
  45. "newFile": "cc7aae22f2d4301b6006e5f26e28b63579b61072",
  46. }
  47. rmDir(t, cloneAndCheckout(t, newURL, expectedClone))
  48. }
  49. func TestPush_twice(t *testing.T) {
  50. startURL := "ipfs://ipfs/QmZhuM4TxuhxbamPtWHyHYCUXfkqCkgBmWREKF2kqTLbvz/unpackedTest"
  51. tmpDir := cloneAndCheckout(t, startURL, expectedClone)
  52. checkFatal(t, ioutil.WriteFile(filepath.Join(tmpDir, "newFile"), []byte("Hello From Test"), 0700))
  53. cmd := exec.Command(gitPath, "add", "newFile")
  54. cmd.Dir = tmpDir
  55. out, err := cmd.CombinedOutput()
  56. t.Log("git add out: ", string(out))
  57. checkFatal(t, err)
  58. cmd = exec.Command(gitPath, "commit", "-m", "test: Add newFile Commit")
  59. cmd.Dir = tmpDir
  60. out, err = cmd.CombinedOutput()
  61. t.Log("git commit out: ", string(out))
  62. checkFatal(t, err)
  63. // $ git push origin
  64. cmd = exec.Command(gitPath, "push", "origin")
  65. cmd.Dir = tmpDir
  66. out, err = cmd.CombinedOutput()
  67. t.Log("git push out: ", string(out))
  68. checkFatal(t, err)
  69. cmd = exec.Command(gitPath, "config", "--get", "remote.origin.url")
  70. cmd.Dir = tmpDir
  71. out, err = cmd.CombinedOutput()
  72. checkFatal(t, err)
  73. newURL := strings.TrimSpace(string(out))
  74. if newURL == startURL {
  75. t.Fatalf("remote url wasn't updated. is:%q", newURL)
  76. }
  77. checkFatal(t, ioutil.WriteFile(filepath.Join(tmpDir, "2ndNewFile"), []byte("Hello From 2nd push Test"), 0700))
  78. cmd = exec.Command(gitPath, "add", "2ndNewFile")
  79. cmd.Dir = tmpDir
  80. out, err = cmd.CombinedOutput()
  81. t.Log("git add out: ", string(out))
  82. checkFatal(t, err)
  83. cmd = exec.Command(gitPath, "commit", "-m", "test: Add a 2nd file")
  84. cmd.Dir = tmpDir
  85. out, err = cmd.CombinedOutput()
  86. t.Log("git commit out: ", string(out))
  87. checkFatal(t, err)
  88. // $ git push origin
  89. cmd = exec.Command(gitPath, "push", "origin")
  90. cmd.Dir = tmpDir
  91. out, err = cmd.CombinedOutput()
  92. t.Log("git push out: ", string(out))
  93. checkFatal(t, err)
  94. cmd = exec.Command(gitPath, "config", "--get", "remote.origin.url")
  95. cmd.Dir = tmpDir
  96. out, err = cmd.CombinedOutput()
  97. checkFatal(t, err)
  98. nextURL := strings.TrimSpace(string(out))
  99. if nextURL == startURL || nextURL == newURL {
  100. t.Fatalf("remote url wasn't updated (2nd time). is:%q", nextURL)
  101. }
  102. var expectedClone = map[string]string{
  103. "testA": "9417d011822b875da72221c8d188089cbfcee806",
  104. "hello.txt": "e2839ad2e47386d342038958fba941fc78e3780e",
  105. "notes": "32ed91604b272860ec911fc2bf4ae631b7900aa8",
  106. "newFile": "cc7aae22f2d4301b6006e5f26e28b63579b61072",
  107. "2ndNewFile": "bacbe054a5fc6654bac497e36b474cd6839e3616",
  108. }
  109. rmDir(t, cloneAndCheckout(t, nextURL, expectedClone))
  110. }