github.com/jdhenke/godel@v0.0.0-20161213181855-abeb3861bf0d/layout/app_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 layout_test 16 17 import ( 18 "fmt" 19 "io/ioutil" 20 "os" 21 "path" 22 "runtime" 23 "testing" 24 25 "github.com/nmiyake/pkg/dirs" 26 "github.com/palantir/pkg/specdir" 27 "github.com/stretchr/testify/assert" 28 "github.com/stretchr/testify/require" 29 30 "github.com/palantir/godel/layout" 31 ) 32 33 func TestAppLayoutNoValidation(t *testing.T) { 34 specDir, err := specdir.New("testRoot", layout.AppSpec(), layout.AppSpecTemplate("0.0.1"), specdir.SpecOnly) 35 require.NoError(t, err) 36 37 for i, currCase := range []struct { 38 aliasName string 39 want string 40 }{ 41 { 42 aliasName: layout.AppExecutable, 43 want: fmt.Sprintf("godel-0.0.1/bin/%v-%v/godel", runtime.GOOS, runtime.GOARCH), 44 }, 45 } { 46 actual := specDir.Path(currCase.aliasName) 47 assert.Equal(t, currCase.want, actual, "Case %d", i) 48 } 49 } 50 51 func TestAppSpecLayoutValidationFail(t *testing.T) { 52 tmpDir, cleanup, err := dirs.TempDir("", "") 53 defer cleanup() 54 require.NoError(t, err) 55 56 gödelDir := path.Join(tmpDir, "godel-0.0.1") 57 err = os.Mkdir(gödelDir, 0755) 58 require.NoError(t, err) 59 60 for i, currCase := range []struct { 61 rootName string 62 wantError string 63 }{ 64 { 65 rootName: "testRoot", 66 wantError: `testRoot is not a path to godel-0.0.1`, 67 }, 68 { 69 rootName: gödelDir, 70 wantError: `godel-0.0.1/bin does not exist`, 71 }, 72 } { 73 spec := layout.AppSpec() 74 err = spec.Validate(currCase.rootName, layout.AppSpecTemplate("0.0.1")) 75 assert.Error(t, err, fmt.Sprintf("Case %d", i)) 76 77 if err != nil { 78 assert.EqualError(t, err, currCase.wantError, "Case %d", i) 79 } 80 } 81 } 82 83 func TestAppLayoutValidation(t *testing.T) { 84 tmpDir, cleanup, err := dirs.TempDir("", "") 85 defer cleanup() 86 require.NoError(t, err) 87 88 filesToCreate := map[string]string{ 89 path.Join("godel-0.0.1", "bin", "darwin-amd64", "godel"): "godel", 90 path.Join("godel-0.0.1", "bin", "linux-amd64", "godel"): "godel", 91 path.Join("godel-0.0.1", "wrapper", "godel", "bin", "godelw"): "godelw", 92 path.Join("godel-0.0.1", "wrapper", "godel", "config", "foo.yml"): "testconfig", 93 path.Join("godel-0.0.1", "wrapper", "godelw"): "godelw", 94 } 95 96 createdFilesTmpDir := createFiles(t, tmpDir, filesToCreate) 97 specDir, err := specdir.New(path.Join(createdFilesTmpDir, "godel-0.0.1"), layout.AppSpec(), layout.AppSpecTemplate("0.0.1"), specdir.Validate) 98 require.NoError(t, err) 99 100 for i, currCase := range []struct { 101 aliasName string 102 want string 103 }{ 104 { 105 aliasName: layout.AppExecutable, 106 want: fmt.Sprintf("godel-0.0.1/bin/%v-%v/godel", runtime.GOOS, runtime.GOARCH), 107 }, 108 } { 109 expected := path.Join(createdFilesTmpDir, currCase.want) 110 got := specDir.Path(currCase.aliasName) 111 assert.Equal(t, expected, got, "Case %d", i) 112 } 113 } 114 115 func createFiles(t *testing.T, tmpDir string, files map[string]string) string { 116 currCaseTmpDir, err := ioutil.TempDir(tmpDir, "") 117 require.NoError(t, err) 118 119 for currFile, currContent := range files { 120 err = os.MkdirAll(path.Join(currCaseTmpDir, path.Dir(currFile)), 0755) 121 require.NoError(t, err) 122 err = ioutil.WriteFile(path.Join(currCaseTmpDir, currFile), []byte(currContent), 0644) 123 require.NoError(t, err) 124 } 125 126 return currCaseTmpDir 127 }