github.com/keikoproj/manny@v0.0.0-20210726112440-8571e4c99ced/utils/git_test.go (about) 1 package utils 2 3 import ( 4 "strings" 5 "testing" 6 ) 7 8 func Test_GitRepoRemote(t *testing.T) { 9 // test table 10 tt := []struct { 11 Identifier string 12 Path string 13 Output string 14 WantErr bool 15 ErrPrefix string 16 }{ 17 { 18 "Valid", 19 "../", 20 "manny.git", 21 false, 22 "", 23 }, 24 { 25 "Not a git directory", 26 "/tmp", 27 "", 28 true, 29 "repository does not exist", 30 }, 31 } 32 33 // testing loop 34 for _, tc := range tt { 35 t.Run(tc.Identifier, func(t *testing.T) { 36 out, err := GitRepoRemote(tc.Path) 37 haveErr := err != nil 38 39 // evaluate output 40 if !strings.Contains(out, tc.Output) { 41 t.Errorf("Error with output: got: %s, want: %s", out, tc.Output) 42 } 43 44 // err prefix is wrong 45 if haveErr && tc.WantErr && !strings.HasPrefix(err.Error(), tc.ErrPrefix) { 46 t.Errorf("Error not prefixed as expected. got: %s, want: %s", err.Error(), tc.ErrPrefix) 47 } 48 49 // didn't expect an error but got one 50 if haveErr && !tc.WantErr { 51 t.Errorf("Got: %s, want: nil", err) 52 } 53 54 // expected an error but didn't get one 55 if haveErr && err == nil { 56 t.Errorf("Got: nil,") 57 } 58 }) 59 } 60 }