github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/cmd/git-dolt/utils/find_git_config_dir_test.go (about) 1 // Copyright 2019 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package utils 16 17 import ( 18 "io/ioutil" 19 "os" 20 "path/filepath" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 ) 25 26 func TestFindGitConfigDir(t *testing.T) { 27 // Setup 28 tmpDir, err := ioutil.TempDir("", "git-dolt-test") 29 if err != nil { 30 t.Errorf("Error creating temp directory: %v", err) 31 } 32 defer func() { 33 if err := os.RemoveAll(tmpDir); err != nil { 34 t.Errorf("Error removing test directories: %v", err) 35 } 36 }() 37 38 nestedExamplePath := filepath.Join(tmpDir, "deeply", "nested", "example") 39 topLevelExamplePath := filepath.Join(tmpDir, "top", "level", "example") 40 noGitExamplePath := filepath.Join(tmpDir, "no", "git", "example") 41 42 nestedGitPath := filepath.Join(nestedExamplePath, ".git") 43 topLevelGitPath := filepath.Join(tmpDir, "top", ".git") 44 45 if err := os.MkdirAll(nestedGitPath, os.ModePerm); err != nil { 46 t.Errorf("Error creating test directories: %v", err) 47 } 48 49 if err := os.MkdirAll(topLevelExamplePath, os.ModePerm); err != nil { 50 t.Errorf("Error creating test directories: %v", err) 51 } 52 if err := os.MkdirAll(topLevelGitPath, os.ModePerm); err != nil { 53 t.Errorf("Error creating test directories: %v", err) 54 } 55 56 if err := os.MkdirAll(noGitExamplePath, os.ModePerm); err != nil { 57 t.Errorf("Error creating test directories: %v", err) 58 } 59 60 // Tests 61 type args struct { 62 startingPath string 63 terminalPath string 64 } 65 tests := []struct { 66 name string 67 args args 68 want string 69 wantErr bool 70 }{ 71 {"finds a deeply-nested git directory", args{nestedExamplePath, tmpDir}, nestedGitPath, false}, 72 {"finds a top-level git directory", args{topLevelExamplePath, tmpDir}, topLevelGitPath, false}, 73 {"returns an error when there is no git directory", args{noGitExamplePath, tmpDir}, "", true}, 74 {"returns an error (and does not hang) when startingPath is not a descendent of terminalPath", args{noGitExamplePath, nestedExamplePath}, "", true}, 75 } 76 77 for _, tt := range tests { 78 t.Run(tt.name, func(t *testing.T) { 79 got, err := FindGitConfigDir(tt.args.startingPath, tt.args.terminalPath) 80 if tt.wantErr { 81 assert.Error(t, err) 82 } else if err != nil { 83 t.Errorf("wanted %v, got error %v", tt.want, err) 84 } 85 86 assert.Equal(t, tt.want, got) 87 }) 88 } 89 }