feat(version): add Unix epoch support for buildTime
Support both Unix epoch timestamps and RFC3339 format for build time injection via ldflags. Unix epoch format provides simpler build commands: $(date +%s) vs $(date -u +%Y-%m-%dT%H:%M:%SZ). - Add parseBuildTime() to convert epoch to RFC3339 - Maintain backward compatibility with existing RFC3339 format - Ensure consistent RFC3339 output regardless of input format - Fix build date priority over git commit time
This commit is contained in:
@@ -309,3 +309,106 @@ func BenchmarkCheckVersionDevSnapshot(b *testing.B) {
|
||||
_ = CheckVersion(version, minimum)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseBuildTime(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "Unix epoch timestamp",
|
||||
input: "1672531200", // 2023-01-01T00:00:00Z
|
||||
expected: "2023-01-01T00:00:00Z",
|
||||
},
|
||||
{
|
||||
name: "Unix epoch zero",
|
||||
input: "0",
|
||||
expected: "1970-01-01T00:00:00Z",
|
||||
},
|
||||
{
|
||||
name: "Valid RFC3339 format",
|
||||
input: "2023-12-25T15:30:45Z",
|
||||
expected: "2023-12-25T15:30:45Z",
|
||||
},
|
||||
{
|
||||
name: "RFC3339 with timezone",
|
||||
input: "2023-12-25T10:30:45-05:00",
|
||||
expected: "2023-12-25T10:30:45-05:00",
|
||||
},
|
||||
{
|
||||
name: "Empty string",
|
||||
input: "",
|
||||
expected: "",
|
||||
},
|
||||
{
|
||||
name: "Invalid format - return unchanged",
|
||||
input: "not-a-date",
|
||||
expected: "not-a-date",
|
||||
},
|
||||
{
|
||||
name: "Invalid timestamp - return unchanged",
|
||||
input: "invalid-timestamp",
|
||||
expected: "invalid-timestamp",
|
||||
},
|
||||
{
|
||||
name: "Partial date - return unchanged",
|
||||
input: "2023-01-01",
|
||||
expected: "2023-01-01",
|
||||
},
|
||||
{
|
||||
name: "Negative epoch - should work",
|
||||
input: "-1",
|
||||
expected: "1969-12-31T23:59:59Z",
|
||||
},
|
||||
{
|
||||
name: "Large epoch timestamp",
|
||||
input: "4102444800", // 2100-01-01T00:00:00Z
|
||||
expected: "2100-01-01T00:00:00Z",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := parseBuildTime(tt.input)
|
||||
if result != tt.expected {
|
||||
t.Errorf("parseBuildTime(%q) = %q, expected %q", tt.input, result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseBuildTimeConsistency(t *testing.T) {
|
||||
// Test that calling parseBuildTime multiple times with the same input returns the same result
|
||||
testInputs := []string{
|
||||
"1672531200",
|
||||
"2023-01-01T00:00:00Z",
|
||||
"invalid-date",
|
||||
"",
|
||||
}
|
||||
|
||||
for _, input := range testInputs {
|
||||
result1 := parseBuildTime(input)
|
||||
result2 := parseBuildTime(input)
|
||||
if result1 != result2 {
|
||||
t.Errorf("parseBuildTime(%q) not consistent: %q != %q", input, result1, result2)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkParseBuildTime(b *testing.B) {
|
||||
inputs := []string{
|
||||
"1672531200", // Unix epoch
|
||||
"2023-01-01T00:00:00Z", // RFC3339
|
||||
"invalid-timestamp", // Invalid
|
||||
"", // Empty
|
||||
}
|
||||
|
||||
for _, input := range inputs {
|
||||
b.Run(input, func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = parseBuildTime(input)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user