-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
buildstamp: Offer version string via function call
Currently, version info is available as part of the version information returned pre-formatted for printing to a console/log. However, we want to get version info to share with an external server, so this change creates such a function. If the binary has no stamped version info, the function will error (which implies that during development, testing against a real server will require version stamping to be enabled). If the released binary is a real release, it will return a common version string (`vX.Y.Z`). If the binary is not a real release, it will return a pseudo-version similar to the format used for [Go modules](https://go.dev/ref/mod#pseudo-versions) - namely, `v0.0.0-yyyymmddhhmmss-abcdefabcdef`, where the date is the build date, and the commit is the commit the tool's build was based on. No component of the version string will indicate if the code tree was dirty. Tested: unit tests only Bug: linear/CUS-385
- Loading branch information
1 parent
d0a943e
commit bc7afcf
Showing
3 changed files
with
133 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package buildstamp | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetVersion(t *testing.T) { | ||
testCases := []struct { | ||
desc string | ||
vars Vars | ||
wantVersion string | ||
wantErr string | ||
}{ | ||
{ | ||
desc: "stamped official build", | ||
vars: Vars{ | ||
ReleaseVersion: "v1.0.2", | ||
SourceBranch: "main", | ||
SourceRevision: "abcdefbutnotg", | ||
IsClean: true, | ||
IsOfficial: true, | ||
BuildTimestamp: time.Date(2024, 1, 2, 3, 4, 5, 0, time.UTC), | ||
}, | ||
wantVersion: "v1.0.2", | ||
}, | ||
{ | ||
desc: "unstamped build", | ||
vars: emptyValues, | ||
wantErr: ErrStampingDisabled.Error(), | ||
}, | ||
{ | ||
desc: "unofficial due to dirty repo", | ||
vars: Vars{ | ||
ReleaseVersion: unknown, | ||
SourceBranch: "main", | ||
SourceRevision: "abcdefbutnotg", | ||
IsClean: false, | ||
IsOfficial: false, | ||
BuildTimestamp: time.Date(2024, 1, 2, 3, 4, 5, 0, time.UTC), | ||
}, | ||
wantVersion: "v0.0.0-20240102030405-abcdefbutno", | ||
}, | ||
{ | ||
desc: "unofficial due to wrong branch", | ||
vars: Vars{ | ||
ReleaseVersion: unknown, | ||
SourceBranch: "some_dev_branch", | ||
SourceRevision: "abcdefbutnotg", | ||
IsClean: true, | ||
IsOfficial: false, | ||
BuildTimestamp: time.Date(2024, 1, 2, 3, 4, 5, 0, time.UTC), | ||
}, | ||
wantVersion: "v0.0.0-20240102030405-abcdefbutno", | ||
}, | ||
{ | ||
desc: "unofficial due to unknown reason", | ||
vars: Vars{ | ||
ReleaseVersion: "v1.0.2", | ||
SourceBranch: "main", | ||
SourceRevision: "abcdefbutnotg", | ||
IsClean: true, | ||
IsOfficial: false, | ||
BuildTimestamp: time.Date(2024, 1, 2, 3, 4, 5, 0, time.UTC), | ||
}, | ||
wantVersion: "v0.0.0-20240102030405-abcdefbutno", | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
got, gotErr := tc.vars.GetVersion() | ||
t.Logf("got: %v; gotErr: %v", got, gotErr) | ||
|
||
if tc.wantErr != "" { | ||
assert.ErrorContains(t, gotErr, tc.wantErr) | ||
} else { | ||
assert.NoError(t, gotErr) | ||
} | ||
|
||
if gotErr != nil { | ||
return | ||
} | ||
|
||
assert.Equal(t, tc.wantVersion, got) | ||
}) | ||
} | ||
} |