knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/changeset/commit_test.go (about) 1 /* 2 Copyright 2022 The Knative Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package changeset 18 19 import ( 20 "runtime/debug" 21 "sync" 22 "testing" 23 24 "github.com/google/go-cmp/cmp" 25 ) 26 27 func TestGet(t *testing.T) { 28 cases := []struct { 29 name string 30 info *debug.BuildInfo 31 ok bool 32 result string 33 }{{ 34 name: "info fails", 35 ok: false, 36 result: Unknown, 37 }, { 38 name: "missing revision", 39 ok: true, 40 info: &debug.BuildInfo{}, 41 result: Unknown, 42 }, { 43 name: "SHA1 revision is truncated", 44 ok: true, 45 info: &debug.BuildInfo{ 46 Settings: []debug.BuildSetting{{ 47 Key: "vcs.revision", Value: "3666ce749d32abe7be0528380c8c05a4282cb733", 48 }}, 49 }, 50 result: "3666ce7", 51 }, { 52 name: "SHA256 revision is truncated", 53 ok: true, 54 info: &debug.BuildInfo{ 55 Settings: []debug.BuildSetting{{ 56 Key: "vcs.revision", Value: "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824", 57 }}, 58 }, 59 result: "2cf24db", 60 }, { 61 name: "modified workspace results in -dirty suffix", 62 ok: true, 63 info: &debug.BuildInfo{ 64 Settings: []debug.BuildSetting{{ 65 Key: "vcs.revision", Value: "3666ce749d32abe7be0528380c8c05a4282cb733", 66 }, { 67 Key: "vcs.modified", Value: "true", 68 }}, 69 }, 70 result: "3666ce7-dirty", 71 }} 72 73 for _, c := range cases { 74 t.Run(c.name, func(t *testing.T) { 75 rev = "" 76 once = sync.Once{} 77 readBuildInfo = func() (info *debug.BuildInfo, ok bool) { 78 return c.info, c.ok 79 } 80 81 got := Get() 82 want := c.result 83 84 if diff := cmp.Diff(want, got); diff != "" { 85 t.Errorf("result doesn't match expected: %s", diff) 86 } 87 }) 88 } 89 }