github.com/joshdk/godel@v0.0.0-20170529232908-862138a45aee/apps/gonform/integration_test/integration_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 integration_test 16 17 import ( 18 "io/ioutil" 19 "os" 20 "os/exec" 21 "path" 22 "strings" 23 "testing" 24 25 "github.com/nmiyake/pkg/dirs" 26 "github.com/nmiyake/pkg/gofiles" 27 "github.com/stretchr/testify/assert" 28 "github.com/stretchr/testify/require" 29 30 "github.com/palantir/godel/pkg/products" 31 ) 32 33 func TestRun(t *testing.T) { 34 cli, err := products.Bin("gonform") 35 require.NoError(t, err) 36 37 wd, err := os.Getwd() 38 require.NoError(t, err) 39 40 tmpDir, cleanup, err := dirs.TempDir(wd, "") 41 defer cleanup() 42 require.NoError(t, err) 43 44 for i, currCase := range []struct { 45 filesToCreate []gofiles.GoFileSpec 46 filesToCheck []string 47 config string 48 want []gofiles.GoFileSpec 49 wantStdout string 50 }{ 51 { 52 filesToCreate: []gofiles.GoFileSpec{ 53 { 54 RelPath: "foo.go", 55 Src: unindent(`package foo 56 57 import "fmt" 58 59 func main() { 60 fmt.Println("indented way too far") 61 } 62 `), 63 }, 64 { 65 RelPath: "vendor/foo/foo.go", 66 Src: unindent(`package foo 67 68 import "fmt" 69 70 func main() { 71 fmt.Println("indented way too far") 72 } 73 `), 74 }, 75 }, 76 config: unindent(` 77 exclude: 78 paths: 79 - "vendor" 80 `), 81 want: []gofiles.GoFileSpec{ 82 { 83 RelPath: "foo.go", 84 Src: unindent(`package foo 85 86 import ( 87 "fmt" 88 ) 89 90 func main() { 91 fmt.Println("indented way too far") 92 } 93 `), 94 }, 95 { 96 RelPath: "vendor/foo/foo.go", 97 Src: unindent(`package foo 98 99 import "fmt" 100 101 func main() { 102 fmt.Println("indented way too far") 103 } 104 `), 105 }, 106 }, 107 wantStdout: "Running ptimports...\n", 108 }, 109 { 110 filesToCreate: []gofiles.GoFileSpec{ 111 { 112 RelPath: "vendor/foo/foo.go", 113 Src: unindent(`package foo 114 115 import "fmt" 116 117 func main() { 118 fmt.Println("indented way too far") 119 } 120 `), 121 }, 122 }, 123 config: unindent(` 124 exclude: 125 paths: 126 - "vendor" 127 `), 128 want: []gofiles.GoFileSpec{ 129 { 130 RelPath: "vendor/foo/foo.go", 131 Src: unindent(`package foo 132 133 import "fmt" 134 135 func main() { 136 fmt.Println("indented way too far") 137 } 138 `), 139 }, 140 }, 141 }, 142 } { 143 currCaseTmpDir, err := ioutil.TempDir(tmpDir, "") 144 require.NoError(t, err) 145 146 _, err = gofiles.Write(currCaseTmpDir, currCase.filesToCreate) 147 require.NoError(t, err, "Case %d", i) 148 149 configFile := path.Join(currCaseTmpDir, "config.yml") 150 err = ioutil.WriteFile(configFile, []byte(currCase.config), 0644) 151 require.NoError(t, err) 152 153 err = os.Chdir(currCaseTmpDir) 154 require.NoError(t, err) 155 156 cmd := exec.Command(cli, "--config", configFile) 157 err = cmd.Run() 158 require.NoError(t, err, "Case %d", i) 159 160 verifyFileContent(t, i, currCaseTmpDir, currCase.want) 161 } 162 } 163 164 func verifyFileContent(t *testing.T, caseNum int, rootDir string, expected []gofiles.GoFileSpec) { 165 for _, spec := range expected { 166 bytes, err := ioutil.ReadFile(path.Join(rootDir, spec.RelPath)) 167 require.NoError(t, err, "Case %d", caseNum) 168 169 actualContent := string(bytes) 170 assert.Equal(t, spec.Src, actualContent, "Case %d", caseNum) 171 } 172 } 173 174 func unindent(input string) string { 175 return strings.Replace(input, "\n\t\t\t\t\t", "\n", -1) 176 }