github.com/joshdk/godel@v0.0.0-20170529232908-862138a45aee/properties/reader_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 properties 16 17 import ( 18 "io/ioutil" 19 "testing" 20 21 "github.com/nmiyake/pkg/dirs" 22 "github.com/pkg/errors" 23 "github.com/stretchr/testify/assert" 24 "github.com/stretchr/testify/require" 25 ) 26 27 const ( 28 propertiesFileContent = `# Lines starting with '#' should be ignored 29 distributionURL=https://github.com/palantir/godel/godel-0.0.1.tgz 30 distributionSHA256=871ee79691aee47301214ab0ae10bd851e5bc0d48042ba8d33ac85cfcc7eb6cc 31 ` 32 urlValue = "https://github.com/palantir/godel/godel-0.0.1.tgz" 33 checksumValue = "871ee79691aee47301214ab0ae10bd851e5bc0d48042ba8d33ac85cfcc7eb6cc" 34 ) 35 36 func TestReadProperties(t *testing.T) { 37 tmpDir, cleanup, err := dirs.TempDir("", "") 38 defer cleanup() 39 require.NoError(t, err) 40 41 for i, currCase := range []struct { 42 propertiesFileWriter func(dir string) string 43 want map[string]string 44 }{ 45 { 46 propertiesFileWriter: func(dir string) string { 47 dest, err := ioutil.TempFile(dir, "") 48 require.NoError(t, err) 49 err = dest.Close() 50 require.NoError(t, err) 51 err = writePropertiesFile(dest.Name()) 52 require.NoError(t, err) 53 return dest.Name() 54 }, 55 want: map[string]string{ 56 URL: urlValue, 57 Checksum: checksumValue, 58 }, 59 }, 60 } { 61 propertiesFile := currCase.propertiesFileWriter(tmpDir) 62 got, err := Read(propertiesFile) 63 require.NoError(t, err) 64 65 assert.Equal(t, currCase.want, got, "Case %d", i) 66 } 67 } 68 69 func writePropertiesFile(filePath string) error { 70 err := ioutil.WriteFile(filePath, []byte(propertiesFileContent), 0644) 71 if err != nil { 72 return errors.Wrapf(err, "failed to write file to path %s", filePath) 73 } 74 return nil 75 }