github.com/jdhenke/godel@v0.0.0-20161213181855-abeb3861bf0d/integration_test/helpers_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 "fmt" 19 "io/ioutil" 20 "net/http" 21 "net/http/httptest" 22 "os" 23 "os/exec" 24 "path" 25 "strings" 26 "testing" 27 28 "github.com/stretchr/testify/require" 29 30 "github.com/palantir/godel/layout" 31 ) 32 33 func setUpGödelTestAndDownload(t *testing.T, testRootDir, gödelTGZ string, version string) string { 34 testProjectDir, server := setUpGödelTest(t, testRootDir, gödelTGZ, version) 35 defer server.Close() 36 37 cmd := exec.Command("./godelw", "--version") 38 cmd.Dir = testProjectDir 39 output, err := cmd.CombinedOutput() 40 require.NoError(t, err, "Command %v failed. Output:\n%v", cmd.Args, string(output)) 41 42 return testProjectDir 43 } 44 45 func setUpGödelTest(t *testing.T, testRootDir, gödelTGZ, version string) (string, *httptest.Server) { 46 testProjectDir, err := ioutil.TempDir(testRootDir, "") 47 require.NoError(t, err) 48 49 installGödel(t, testProjectDir, gödelTGZ, version) 50 server := createTGZServer(t, gödelTGZ) 51 updateGödelProperties(t, testProjectDir, server.URL) 52 53 return testProjectDir, server 54 } 55 56 func createTGZServer(t *testing.T, gödelTGZ string) *httptest.Server { 57 _, err := os.Stat(gödelTGZ) 58 require.NoError(t, err) 59 60 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 61 bytes, err := ioutil.ReadFile(gödelTGZ) 62 require.NoError(t, err) 63 _, err = w.Write(bytes) 64 require.NoError(t, err) 65 })) 66 return ts 67 } 68 69 func installGödel(t *testing.T, testProjectDir, gödelTGZ, version string) { 70 specDir, err := layout.AppSpecDir(strings.TrimSuffix(gödelTGZ, ".tgz"), version) 71 require.NoError(t, err) 72 73 err = layout.CopyFile(specDir.Path(layout.WrapperScriptFile), path.Join(testProjectDir, "godelw")) 74 require.NoError(t, err) 75 err = layout.CopyDir(specDir.Path(layout.WrapperAppDir), path.Join(testProjectDir, "godel")) 76 require.NoError(t, err) 77 } 78 79 func updateGödelProperties(t *testing.T, testProjectDir, url string) { 80 contents := fmt.Sprintf("distributionURL=%v\n", url) 81 err := ioutil.WriteFile(path.Join(testProjectDir, "godel", "config", "godel.properties"), []byte(contents), 0644) 82 require.NoError(t, err) 83 }