github.com/ivan4th/gm@v0.0.0-20221103162645-61171e27e81e/git_test.go (about) 1 /* 2 Copyright 2017 Mirantis 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package gm 18 19 import ( 20 "io/ioutil" 21 "os" 22 "os/exec" 23 "path/filepath" 24 "strings" 25 "testing" 26 ) 27 28 const ( 29 gitSetupCmd = ` 30 git config --global user.email 'foo@example.com' && 31 git config --global user.name 'foo' && 32 git init && 33 git add . && 34 git commit -m 'ok'` 35 ) 36 37 func TestGitDiff(t *testing.T) { 38 tmpDir, err := ioutil.TempDir("", "git-test") 39 if err != nil { 40 t.Fatalf("TempDir(): %v", err) 41 } 42 defer os.RemoveAll(tmpDir) 43 oldHome := os.Getenv("HOME") 44 os.Setenv("HOME", tmpDir) 45 defer os.Setenv("HOME", oldHome) 46 47 origWd, err := os.Getwd() 48 if err != nil { 49 t.Fatalf("os.Getwd(): %v", err) 50 } 51 defer os.Chdir(origWd) 52 if err := os.Chdir(tmpDir); err != nil { 53 t.Fatalf("os.Chdir(): %v", err) 54 } 55 if err := ioutil.WriteFile("samplefile", []byte("foobar"), 0777); err != nil { 56 t.Fatalf("ioutil.WriteFile(): %v", err) 57 } 58 59 out, err := exec.Command("/bin/sh", "-c", gitSetupCmd).CombinedOutput() 60 if err != nil { 61 t.Fatalf("git init failed: %v:\n%s", err, out) 62 } 63 64 diffOut, err := GitDiff(filepath.Join(tmpDir, "samplefile")) 65 switch { 66 case err != nil: 67 t.Errorf("GitDiff (unchanged): %v", err) 68 case diffOut != "": 69 t.Errorf("GitDiff (unchanged): unexpected diff:\n%s", diffOut) 70 } 71 72 if err := ioutil.WriteFile("samplefile", []byte("baz"), 0777); err != nil { 73 t.Fatalf("ioutil.WriteFile(): %v", err) 74 } 75 76 diffOut, err = GitDiff(filepath.Join(tmpDir, "samplefile")) 77 switch { 78 case err != nil: 79 t.Errorf("GitDiff (changed): %v", err) 80 case diffOut == "": 81 t.Errorf("GitDiff (changed): no diff") 82 case !strings.Contains(diffOut, "-foobar") || !strings.Contains(diffOut, "+baz"): 83 t.Errorf("GitDiff (changed): bad diff output:\n%s", diffOut) 84 } 85 86 out, err = exec.Command("git", "add", "samplefile").CombinedOutput() 87 if err != nil { 88 t.Fatalf("git add failed: %v:\n%s", err, out) 89 } 90 91 diffOut, err = GitDiff(filepath.Join(tmpDir, "samplefile")) 92 switch { 93 case err != nil: 94 t.Errorf("GitDiff (staged): %v", err) 95 case diffOut != "": 96 t.Errorf("GitDiff (staged): unexpected diff:\n%s", diffOut) 97 } 98 99 diffOut, err = GitDiff(filepath.Join(tmpDir, "samplefile")) 100 switch { 101 case err != nil: 102 t.Errorf("GitDiff (staged, other work dir): %v", err) 103 case diffOut != "": 104 t.Errorf("GitDiff (staged, other work dir): unexpected diff:\n%s", diffOut) 105 } 106 107 if err := ioutil.WriteFile("newfile", []byte("newcontent"), 0777); err != nil { 108 t.Fatalf("ioutil.WriteFile(): %v", err) 109 } 110 111 diffOut, err = GitDiff(filepath.Join(tmpDir, "newfile")) 112 switch { 113 case err != nil: 114 t.Errorf("GitDiff (new): %v", err) 115 case diffOut == "": 116 t.Errorf("GitDiff (new): no diff") 117 case !strings.Contains(diffOut, "<NEW FILE>") || !strings.Contains(diffOut, "newcontent"): 118 t.Errorf("GitDiff (new): bad diff output:\n%s", diffOut) 119 } 120 }