Skip to content

Commit ce3795d

Browse files
committed
Keep version information short and readable
1 parent ef41212 commit ce3795d

File tree

2 files changed

+66
-7
lines changed

2 files changed

+66
-7
lines changed

main.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
)
2020

2121
var (
22-
commit = "000000"
22+
commit = "0000000"
2323
date = ""
2424
)
2525

@@ -67,12 +67,7 @@ func parseFlags() execOptions {
6767
pflag.Parse()
6868

6969
if *showVersion {
70-
fullVersion := strings.TrimSpace(version)
71-
if date != "" {
72-
fullVersion += fmt.Sprintf(" (%s, %s)", date, commit)
73-
}
74-
fmt.Printf("qbench %s", fullVersion)
75-
fmt.Println()
70+
printVersion()
7671
os.Exit(0)
7772
}
7873

@@ -119,6 +114,29 @@ func parseFlags() execOptions {
119114
}
120115
}
121116

117+
func shortCommit(hash string) string {
118+
if len(hash) > 7 {
119+
return hash[:7]
120+
}
121+
return hash
122+
}
123+
124+
func shortDate(d string) string {
125+
if t, err := time.Parse(time.RFC3339, d); err == nil {
126+
return t.Format("2006-01-02")
127+
}
128+
return d
129+
}
130+
131+
func printVersion() {
132+
fullVersion := strings.TrimSpace(version)
133+
if date != "" {
134+
fullVersion += fmt.Sprintf(" (%s, %s)", shortDate(date), shortCommit(commit))
135+
}
136+
fmt.Printf("qbench %s", fullVersion)
137+
fmt.Println()
138+
}
139+
122140
func openDB(options execOptions) *sql.DB {
123141
db, err := sql.Open(options.driver, options.dsn)
124142
if err != nil {

main_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,47 @@ func makeRange(min, max int) []float64 {
7171
return r
7272
}
7373

74+
func TestShortCommit(t *testing.T) {
75+
tests := []struct {
76+
name string
77+
hash string
78+
want string
79+
}{
80+
{"full 40-char hash", "abc1234567890def1234567890abcdef12345678", "abc1234"},
81+
{"empty string", "", ""},
82+
{"default commit", "0000000", "0000000"},
83+
}
84+
85+
for _, tt := range tests {
86+
t.Run(tt.name, func(t *testing.T) {
87+
got := shortCommit(tt.hash)
88+
if got != tt.want {
89+
t.Errorf("shortCommit(%q) = %q, want %q", tt.hash, got, tt.want)
90+
}
91+
})
92+
}
93+
}
94+
95+
func TestShortDate(t *testing.T) {
96+
tests := []struct {
97+
name string
98+
date string
99+
want string
100+
}{
101+
{"RFC3339 full", "2026-03-08T22:08:22Z", "2026-03-08"},
102+
{"empty", "", ""},
103+
}
104+
105+
for _, tt := range tests {
106+
t.Run(tt.name, func(t *testing.T) {
107+
got := shortDate(tt.date)
108+
if got != tt.want {
109+
t.Errorf("shortDate(%q) = %q, want %q", tt.date, got, tt.want)
110+
}
111+
})
112+
}
113+
}
114+
74115
func setupTestDB(t *testing.T) (*sql.DB, string) {
75116
t.Helper()
76117
tmpFile, err := os.CreateTemp("", "qbench-test-*.db")

0 commit comments

Comments
 (0)