package buildinfo import ( "encoding/json" "runtime/debug" "testing" ) func TestBuildIdentity(t *testing.T) { build := &debug.BuildInfo{Main: debug.Module{Version: "(devel)"}, Settings: []debug.BuildSetting{ {Key: "vcs.revision", Value: "0123456789abcdef"}, {Key: "vcs.modified", Value: "true"}, }} for _, tc := range []struct { name string build *debug.BuildInfo version, commit, dirty, wantVersion, wantCommit string wantDirty *bool }{ {"native", build, "", "", "", "0123456789ab-dirty", "0123456789abcdef", boolPtr(true)}, {"docker", build, "v1.2", "fedcba", "false", "v1.2", "fedcba", boolPtr(false)}, {"dirty docker", nil, "0123456789ab-dirty", "0123456789abcdef", "true", "0123456789ab-dirty", "0123456789abcdef", boolPtr(true)}, {"unknown", nil, "", "", "", "dev", "unknown", nil}, {"unstamped docker", nil, "dev", "unknown", "unknown", "dev", "unknown", nil}, {"module version", &debug.BuildInfo{Main: debug.Module{Version: "v1.2.3"}}, "", "", "", "v1.2.3", "unknown", nil}, {"Go dirty version", build, "v0.0.0-20261010195542-0123456789ab+dirty", "", "", "v0.0.0-20261010195542-0123456789ab-dirty", "0123456789abcdef", boolPtr(true)}, } { t.Run(tc.name, func(t *testing.T) { got := resolve(tc.build, tc.version, tc.commit, tc.dirty) if got.Version != tc.wantVersion || got.Commit != tc.wantCommit || (got.Dirty == nil) != (tc.wantDirty == nil) || (got.Dirty != nil && *got.Dirty != *tc.wantDirty) { t.Fatalf("unexpected identity: %+v", got) } data, err := json.Marshal(got) if err != nil { t.Fatal(err) } var roundtrip Info if err := json.Unmarshal(data, &roundtrip); err != nil { t.Fatal(err) } if (roundtrip.Dirty == nil) != (got.Dirty == nil) { t.Fatal("lost unknown dirty state") } }) } } func boolPtr(value bool) *bool { return &value }