package globals_test import ( "testing" "sneak.berlin/go/vaultik/internal/globals" ) // TestGlobalsNew ensures the globals package initializes correctly func TestGlobalsNew(t *testing.T) { t.Parallel() g, err := globals.New() if err != nil { t.Fatalf("Failed to create Globals: %v", err) } if g == nil { t.Fatal("Globals instance is nil") } if g.Appname != "vaultik" { t.Errorf("Expected Appname to be 'vaultik', got '%s'", g.Appname) } // Version and Commit will be "dev" and "unknown" by default if g.Version == "" { t.Error("Version should not be empty") } if g.Commit == "" { t.Error("Commit should not be empty") } } // TestIsDevVersion covers the boundary that matters: everything // script/version and goreleaser's snapshot template can emit for an // untagged build must be recognised as a development build, and a real // tag must not be. A plain equality check against "dev" used to decide // this, which classified every commit-stamped dev build as a release. func TestIsDevVersion(t *testing.T) { t.Parallel() cases := []struct { version string want bool }{ // What an untagged build produces. {"dev", true}, {"dev-b6e4a218a39e", true}, {"dev-b6e4a218a39e-dirty", true}, // What a tagged build produces (script/version strips the // leading "v", matching goreleaser's .Version). {"1.0.0", false}, {"0.1.0", false}, {"1.0.0-rc.1", false}, {"v1.0.0", false}, // A release must not be mistaken for a dev build just because // the string happens to contain "dev". {"1.0.0-dev", false}, {"developer", false}, // A binary with no version string at all did not get stamped, // which is a build failure, not a release. It must never print // as one. The Makefile refuses to build when script/version // yields nothing; this covers a binary linked some other way. {"", true}, } for _, tc := range cases { if got := globals.IsDevVersion(tc.version); got != tc.want { t.Errorf("IsDevVersion(%q) = %v, want %v", tc.version, got, tc.want) } } } // TestDefaultVersionIsDev pins the linker-flag contract: an unstamped // binary (no -ldflags at all, which is what `go build ./...` and `go // install` produce) must report itself as a development build rather // than as some default release number. func TestDefaultVersionIsDev(t *testing.T) { t.Parallel() if !globals.IsDevVersion(globals.DevVersion) { t.Errorf("DevVersion %q is not recognised as a dev version", globals.DevVersion) } }