github.com/joshdk/godel@v0.0.0-20170529232908-862138a45aee/cmd/githooks/hooks_test.go (about) 1 // Copyright 2016 Palantir Technologies, 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 githooks_test 16 17 import ( 18 "fmt" 19 "io/ioutil" 20 "os/exec" 21 "path" 22 "regexp" 23 "testing" 24 25 "github.com/nmiyake/pkg/dirs" 26 "github.com/stretchr/testify/assert" 27 "github.com/stretchr/testify/require" 28 29 "github.com/palantir/godel/cmd/githooks" 30 ) 31 32 func TestInstallGitHooks(t *testing.T) { 33 tmpDir, cleanup, err := dirs.TempDir("", "") 34 defer cleanup() 35 require.NoError(t, err) 36 37 for i, currCase := range []struct { 38 setup func(projectDir string) 39 validate func(projectDir string, caseNum int, err error) 40 }{ 41 { 42 setup: func(projectDir string) { 43 cmd := exec.Command("git", "init") 44 cmd.Dir = projectDir 45 err := cmd.Run() 46 require.NoError(t, err) 47 }, 48 validate: func(projectDir string, caseNum int, err error) { 49 require.NoError(t, err, "Case %d", caseNum) 50 bytes, err := ioutil.ReadFile(path.Join(projectDir, ".git/hooks/pre-commit")) 51 require.NoError(t, err, "Case %d", caseNum) 52 assert.Regexp(t, regexp.MustCompile(`(?s).+\./godelw format -l runAll \$gofiles.+`), string(bytes), "Case %d", caseNum) 53 }, 54 }, 55 { 56 validate: func(projectDir string, caseNum int, err error) { 57 expectedErr := fmt.Sprintf(".git directory does not exist at %v", path.Join(projectDir, ".git")) 58 assert.EqualError(t, err, expectedErr, "Case %d", caseNum) 59 }, 60 }, 61 } { 62 projectDir, err := ioutil.TempDir(tmpDir, "") 63 require.NoError(t, err, "Case %d", i) 64 65 if currCase.setup != nil { 66 currCase.setup(projectDir) 67 } 68 currCase.validate(projectDir, i, githooks.InstallGitHooks(projectDir)) 69 } 70 }