github.com/coreos/mantle@v0.13.0/sdk/version_test.go (about) 1 // Copyright 2015 CoreOS, 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 sdk 16 17 import ( 18 "io/ioutil" 19 "os" 20 "path/filepath" 21 "testing" 22 23 "github.com/kylelemons/godebug/pretty" 24 ) 25 26 var versionTestData = []struct { 27 Versions 28 Text string 29 }{{ 30 Text: ` 31 OREOS_BUILD=968 32 COREOS_BRANCH=2 33 COREOS_PATCH=0 34 COREOS_VERSION=968.2.0 35 COREOS_VERSION_ID=968.2.0 36 COREOS_BUILD_ID="" 37 COREOS_SDK_VERSION=967.0.0 38 `, 39 Versions: Versions{ 40 Version: "968.2.0", 41 VersionID: "968.2.0", 42 BuildID: "", 43 SDKVersion: "967.0.0", 44 }}, { 45 Text: ` 46 COREOS_VERSION='968.2.0+build-foo' 47 COREOS_VERSION_ID=968.2.0 48 COREOS_BUILD_ID="build-foo" 49 COREOS_SDK_VERSION=967.0.0 50 `, 51 Versions: Versions{ 52 Version: "968.2.0+build-foo", 53 VersionID: "968.2.0", 54 BuildID: "build-foo", 55 SDKVersion: "967.0.0", 56 }}} 57 58 func TestVersionsFromDir(t *testing.T) { 59 dir, err := ioutil.TempDir("", "") 60 if err != nil { 61 t.Fatal(err) 62 } 63 defer os.RemoveAll(dir) 64 65 filename := filepath.Join(dir, "version.txt") 66 for _, data := range versionTestData { 67 err = ioutil.WriteFile(filename, []byte(data.Text), 0600) 68 if err != nil { 69 t.Fatal(err) 70 } 71 ver, err := VersionsFromDir(dir) 72 if err != nil { 73 t.Fatal(err) 74 } 75 if diff := pretty.Compare(data.Versions, ver); diff != "" { 76 t.Error(diff) 77 } 78 } 79 }