|
| 1 | +/* |
| 2 | +Copyright 2026 The cert-manager 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 cmd |
| 18 | + |
| 19 | +import ( |
| 20 | + "runtime/debug" |
| 21 | + "testing" |
| 22 | +) |
| 23 | + |
| 24 | +func TestReleaseRepoRefFromBuildInfo(t *testing.T) { |
| 25 | + const commit = "f6da9c76877551ef32503b17189bb178501f59a7" |
| 26 | + |
| 27 | + tests := []struct { |
| 28 | + name string |
| 29 | + info *debug.BuildInfo |
| 30 | + ok bool |
| 31 | + want string |
| 32 | + wantErr bool |
| 33 | + }{ |
| 34 | + { |
| 35 | + name: "no build info", |
| 36 | + info: nil, |
| 37 | + ok: false, |
| 38 | + wantErr: true, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "clean working tree pins to the commit", |
| 42 | + ok: true, |
| 43 | + info: &debug.BuildInfo{Settings: []debug.BuildSetting{ |
| 44 | + {Key: "vcs.revision", Value: commit}, |
| 45 | + {Key: "vcs.modified", Value: "false"}, |
| 46 | + }}, |
| 47 | + want: commit, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "modified working tree fails closed", |
| 51 | + ok: true, |
| 52 | + info: &debug.BuildInfo{Settings: []debug.BuildSetting{ |
| 53 | + {Key: "vcs.revision", Value: commit}, |
| 54 | + {Key: "vcs.modified", Value: "true"}, |
| 55 | + }}, |
| 56 | + wantErr: true, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "module install pins to the version", |
| 60 | + ok: true, |
| 61 | + info: &debug.BuildInfo{Main: debug.Module{Version: "v0.0.0-20230101000000-" + commit[:12]}}, |
| 62 | + want: "v0.0.0-20230101000000-" + commit[:12], |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "devel build without vcs stamping fails closed", |
| 66 | + ok: true, |
| 67 | + info: &debug.BuildInfo{Main: debug.Module{Version: "(devel)"}}, |
| 68 | + wantErr: true, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "no revision and no version fails closed", |
| 72 | + ok: true, |
| 73 | + info: &debug.BuildInfo{}, |
| 74 | + wantErr: true, |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + for _, tt := range tests { |
| 79 | + t.Run(tt.name, func(t *testing.T) { |
| 80 | + got, err := releaseRepoRefFromBuildInfo(tt.info, tt.ok) |
| 81 | + if (err != nil) != tt.wantErr { |
| 82 | + t.Fatalf("releaseRepoRefFromBuildInfo() error = %v, wantErr %v", err, tt.wantErr) |
| 83 | + } |
| 84 | + if got != tt.want { |
| 85 | + t.Errorf("releaseRepoRefFromBuildInfo() = %q, want %q", got, tt.want) |
| 86 | + } |
| 87 | + }) |
| 88 | + } |
| 89 | +} |
0 commit comments