Skip to content

Commit 3664809

Browse files
fix: use git commit timestamp for BuildDate to ensure reproducible builds
- Replace wall-clock date with git log -1 --format=%cI HEAD so that rebuilding the same commit always produces the same binary hash - Assert buildDate appears in JSON output when populated - Add exact-output subtest to guard against blank lines between fields
1 parent 1f46bda commit 3664809

2 files changed

Lines changed: 36 additions & 5 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ BIN_GOIMPORTS ?= "$(PWD)/bin/goimports"
2525
# If the current commit does not have a semver tag, 'tip' is used, unless there
2626
# is a TAG environment variable. Precedence is git tag, environment variable, 'tip'
2727
HASH := $(shell git rev-parse --short HEAD 2>/dev/null)
28-
DATE := $(shell date -u +%Y-%m-%dT%H:%M:%SZ)
28+
DATE := $(shell git log -1 --format=%cI HEAD 2>/dev/null)
2929
VTAG := $(shell git tag --points-at HEAD | head -1)
3030
VTAG := $(shell [ -z $(VTAG) ] && echo $(ETAG) || echo $(VTAG))
3131
VERS ?= $(shell git describe --tags --match 'v*')

cmd/root_test.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,16 +256,25 @@ func TestVerbose(t *testing.T) {
256256
viper.Reset()
257257
var out bytes.Buffer
258258
cmd := NewRootCmd(RootCommandConfig{
259-
Name: "func",
260-
Version: Version{Vers: "v0.42.0"},
259+
Name: "func",
260+
Version: Version{
261+
Vers: "v0.42.0",
262+
BuildDate: "2024-01-01T00:00:00Z",
263+
},
261264
})
262265
cmd.SetArgs([]string{"version", "--output", "json"})
263266
cmd.SetOut(&out)
264267
if err := cmd.Execute(); err != nil {
265268
t.Fatal(err)
266269
}
267-
if !strings.Contains(out.String(), `"version": "v0.42.0"`) {
268-
t.Errorf("expected JSON to contain version field, got:\n%s", out.String())
270+
output := out.String()
271+
for _, want := range []string{
272+
`"version": "v0.42.0"`,
273+
`"buildDate": "2024-01-01T00:00:00Z"`,
274+
} {
275+
if !strings.Contains(output, want) {
276+
t.Errorf("expected JSON to contain %q, got:\n%s", want, output)
277+
}
269278
}
270279
})
271280

@@ -294,6 +303,28 @@ func TestVerbose(t *testing.T) {
294303
}
295304
})
296305

306+
// exact output guards against extra blank lines or whitespace being
307+
// reintroduced between populated fields.
308+
t.Run("verbose exact output", func(t *testing.T) {
309+
v := Version{
310+
Vers: "v0.42.0",
311+
Kver: "knative-v1.10.0",
312+
Hash: "cafe",
313+
BuildDate: "2024-01-01T00:00:00Z",
314+
SocatImage: "ghcr.io/knative/func-utils:v2",
315+
TarImage: "ghcr.io/knative/func-utils:v2",
316+
}
317+
want := "Version: v0.42.0\n" +
318+
"Knative: v1.10.0\n" +
319+
"Commit: cafe\n" +
320+
"BuildDate: 2024-01-01T00:00:00Z\n" +
321+
"SocatImage: ghcr.io/knative/func-utils:v2\n" +
322+
"TarImage: ghcr.io/knative/func-utils:v2\n"
323+
if got := v.StringVerbose(); got != want {
324+
t.Errorf("unexpected verbose output:\nwant:\n%s\ngot:\n%s", want, got)
325+
}
326+
})
327+
297328
t.Run("middleware versions omitted when empty", func(t *testing.T) {
298329
v := Version{
299330
Vers: "v0.42.0",

0 commit comments

Comments
 (0)