github.com/kolanos/fargate@v0.2.3/git/main_test.go (about) 1 package git 2 3 import ( 4 "io/ioutil" 5 "os" 6 "os/exec" 7 "strings" 8 "testing" 9 ) 10 11 func TestGetShortSha(t *testing.T) { 12 cwd, err := os.Getwd() 13 14 if err != nil { 15 t.Error("Could not read current working directory", err) 16 return 17 } 18 19 dir, err := ioutil.TempDir("", "fargate-tests") 20 21 if err != nil { 22 t.Error("Could not create temporary directory", err) 23 return 24 } 25 defer os.RemoveAll(dir) 26 27 os.Chdir(dir) 28 defer os.Chdir(cwd) 29 30 exec.Command("git", "init").Run() 31 32 gitCommit := exec.Command("git", "commit", "--allow-empty", "--message", "dummy commit") 33 commitOutput, err := gitCommit.CombinedOutput() 34 35 if err != nil { 36 t.Error("Could not create dummy git commit", err) 37 return 38 } 39 40 if shortSha := GetShortSha(); !strings.Contains(string(commitOutput), GetShortSha()) { 41 t.Errorf("expected %s to contain %s", commitOutput, shortSha) 42 } 43 } 44 45 func TestIsCwdGitRepoAgainstADir(t *testing.T) { 46 cwd, err := os.Getwd() 47 48 if err != nil { 49 t.Error("Could not read current working directory", err) 50 return 51 } 52 53 dir, err := ioutil.TempDir("", "fargate-tests") 54 55 if err != nil { 56 t.Error("Could not create temporary directory", err) 57 return 58 } 59 defer os.RemoveAll(dir) 60 61 os.Chdir(dir) 62 defer os.Chdir(cwd) 63 64 if isCwdGitRepo := IsCwdGitRepo(); isCwdGitRepo { 65 t.Errorf("wanted false, got %+v", isCwdGitRepo) 66 } 67 } 68 69 func TestIsCwdGitRepoAgainstARepo(t *testing.T) { 70 cwd, err := os.Getwd() 71 72 if err != nil { 73 t.Error("Could not read current working directory", err) 74 return 75 } 76 77 dir, err := ioutil.TempDir("", "fargate-tests") 78 79 if err != nil { 80 t.Error("Could not create temporary directory", err) 81 return 82 } 83 defer os.RemoveAll(dir) 84 85 os.Chdir(dir) 86 defer os.Chdir(cwd) 87 88 cmd := exec.Command("git", "init") 89 cmd.Run() 90 91 if isCwdGitRepo := IsCwdGitRepo(); !isCwdGitRepo { 92 t.Errorf("wanted true, got %+v", isCwdGitRepo) 93 } 94 }