github.com/komosa/bug@v0.3.1/scm/TestHelpers_test.go (about) 1 package scm 2 3 import ( 4 "fmt" 5 "github.com/driusan/bug/bugs" 6 "io/ioutil" 7 "os" 8 "os/exec" 9 "testing" 10 ) 11 12 type Commit interface { 13 CommitID() string 14 LogMsg() string 15 Diff() (string, error) 16 } 17 18 type ManagerTester interface { 19 GetLogs() ([]Commit, error) 20 Setup() error 21 GetWorkDir() string 22 TearDown() 23 StageFile(string) error 24 AssertCleanTree(t *testing.T) 25 AssertStagingIndex(*testing.T, []FileStatus) 26 GetManager() SCMHandler 27 } 28 29 func runCmd(cmd string, options ...string) (string, error) { 30 runcmd := exec.Command(cmd, options...) 31 out, err := runcmd.CombinedOutput() 32 33 return string(out), err 34 } 35 36 func assertLogs(tester ManagerTester, t *testing.T, titles []string, diffs []string) { 37 logs, err := tester.GetLogs() 38 if err != nil { 39 t.Error("Could not get scm logs" + err.Error()) 40 return 41 } 42 43 if len(diffs) != len(titles) { 44 t.Error("Different number of diffs from titles") 45 return 46 } 47 if len(logs) != len(titles) || len(logs) != len(diffs) { 48 t.Error("Unexpected number of log messages") 49 return 50 } 51 52 for i, _ := range titles { 53 if titles[i] != logs[i].LogMsg() { 54 t.Error("Unexpected commit message:" + logs[i].LogMsg()) 55 } 56 57 if diff, err := logs[i].Diff(); err != nil { 58 t.Error("Could not get diff of commit") 59 } else { 60 if diff != diffs[i] { 61 t.Error("Incorrect diff for " + titles[i]) 62 fmt.Fprintf(os.Stderr, "Got %s, expected %s", diff, diffs[i]) 63 } 64 } 65 } 66 } 67 68 func runtestRenameCommitsHelper(tester ManagerTester, t *testing.T, expectedDiffs []string) { 69 err := tester.Setup() 70 defer tester.TearDown() 71 if err != nil { 72 t.Error("Could not initialize repo") 73 return 74 } 75 76 m := tester.GetManager() 77 if m == nil { 78 t.Error("Could not get manager") 79 return 80 } 81 os.MkdirAll("issues/Test-bug", 0755) 82 ioutil.WriteFile("issues/Test-bug/Description", []byte(""), 0644) 83 m.Commit(bugs.Directory(tester.GetWorkDir()), "Initial commit") 84 runCmd("bug", "relabel", "1", "Renamed", "bug") 85 m.Commit(bugs.Directory(tester.GetWorkDir()), "This is a test rename") 86 87 tester.AssertCleanTree(t) 88 89 assertLogs(tester, t, []string{"Initial commit", "This is a test rename"}, expectedDiffs) 90 91 } 92 func runtestCommitDirtyTree(tester ManagerTester, t *testing.T) { 93 err := tester.Setup() 94 if err != nil { 95 panic("Something went wrong trying to initialize git:" + err.Error()) 96 } 97 defer tester.TearDown() 98 m := tester.GetManager() 99 if m == nil { 100 t.Error("Could not get manager") 101 return 102 } 103 os.Mkdir("issues", 0755) 104 runCmd("bug", "create", "-n", "Test", "bug") 105 if err = ioutil.WriteFile("donotcommit.txt", []byte(""), 0644); err != nil { 106 t.Error("Could not write file") 107 return 108 } 109 tester.AssertStagingIndex(t, []FileStatus{ 110 FileStatus{"donotcommit.txt", "?", "?"}, 111 }) 112 113 m.Commit(bugs.Directory(tester.GetWorkDir()+"/issues"), "Initial commit") 114 tester.AssertStagingIndex(t, []FileStatus{ 115 FileStatus{"donotcommit.txt", "?", "?"}, 116 }) 117 tester.StageFile("donotcommit.txt") 118 tester.AssertStagingIndex(t, []FileStatus{ 119 FileStatus{"donotcommit.txt", "A", " "}, 120 }) 121 m.Commit(bugs.Directory(tester.GetWorkDir()+"/issues"), "Initial commit") 122 tester.AssertStagingIndex(t, []FileStatus{ 123 FileStatus{"donotcommit.txt", "A", " "}, 124 }) 125 }