|
| 1 | +// Copyright 2025 Google LLC |
| 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 | +// https://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 sideflip |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "runtime/debug" |
| 20 | + "strings" |
| 21 | + "testing" |
| 22 | +) |
| 23 | + |
| 24 | +func TestVersion(t *testing.T) { |
| 25 | + baseVersion := strings.TrimSpace(versionString) |
| 26 | + for _, test := range []struct { |
| 27 | + name string |
| 28 | + want string |
| 29 | + buildinfo *debug.BuildInfo |
| 30 | + }{ |
| 31 | + { |
| 32 | + name: "tagged version", |
| 33 | + want: "1.2.3", |
| 34 | + buildinfo: &debug.BuildInfo{ |
| 35 | + Main: debug.Module{ |
| 36 | + Version: "1.2.3", |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "pseudoversion", |
| 42 | + want: fmt.Sprintf("%s-123456789000-20230125195754", baseVersion), |
| 43 | + buildinfo: &debug.BuildInfo{ |
| 44 | + Settings: []debug.BuildSetting{ |
| 45 | + {Key: "vcs.revision", Value: "1234567890001234"}, |
| 46 | + {Key: "vcs.time", Value: "2023-01-25T19:57:54Z"}, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "pseudoversion only revision", |
| 52 | + want: fmt.Sprintf("%s-123456789000", baseVersion), |
| 53 | + buildinfo: &debug.BuildInfo{ |
| 54 | + Settings: []debug.BuildSetting{ |
| 55 | + {Key: "vcs.revision", Value: "1234567890001234"}, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "pseudoversion only time", |
| 61 | + want: fmt.Sprintf("%s-20230102150405", baseVersion), |
| 62 | + buildinfo: &debug.BuildInfo{ |
| 63 | + Settings: []debug.BuildSetting{ |
| 64 | + {Key: "vcs.time", Value: "2023-01-02T15:04:05Z"}, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "pseudoversion invalid time", |
| 70 | + want: fmt.Sprintf("%s-123456789000", baseVersion), |
| 71 | + buildinfo: &debug.BuildInfo{ |
| 72 | + Settings: []debug.BuildSetting{ |
| 73 | + {Key: "vcs.revision", Value: "123456789000"}, |
| 74 | + {Key: "vcs.time", Value: "invalid-time"}, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "revision less than 12 chars", |
| 80 | + want: fmt.Sprintf("%s-shortrev-20230125195754", baseVersion), |
| 81 | + buildinfo: &debug.BuildInfo{ |
| 82 | + Settings: []debug.BuildSetting{ |
| 83 | + {Key: "vcs.revision", Value: "shortrev"}, |
| 84 | + {Key: "vcs.time", Value: "2023-01-25T19:57:54Z"}, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "local development", |
| 90 | + want: "not available", |
| 91 | + buildinfo: &debug.BuildInfo{}, |
| 92 | + }, |
| 93 | + } { |
| 94 | + t.Run(test.name, func(t *testing.T) { |
| 95 | + if got := version(test.buildinfo); got != test.want { |
| 96 | + t.Errorf("got %s; want %s", got, test.want) |
| 97 | + } |
| 98 | + }) |
| 99 | + } |
| 100 | +} |
0 commit comments