Files
Myasnikov DaniilandClaude Opus 4.8 2e7f14acb7 test(version): clear ambient COZYSTACK_VERSION in the unset case
Address review feedback from coderabbitai on pkg/version/version_test.go: the
env-unset case only skipped t.Setenv, so a COZYSTACK_VERSION inherited from the
test runner would make it skip the baked-fallback path it is meant to cover.
Unset the variable (restoring it on cleanup) for that case.

Assisted-By: Claude <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Myasnikov Daniil <myasnikovdaniil2001@gmail.com>
2026-07-06 10:03:56 +05:00

70 lines
1.8 KiB
Go

/*
Copyright 2025 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package version
import (
"os"
"testing"
)
// unsetenv clears key for the duration of the test and restores its previous
// value (or absence) afterwards, so the env-unset case exercises the fallback
// path even when COZYSTACK_VERSION leaks in from the test runner's environment.
func unsetenv(t *testing.T, key string) {
t.Helper()
old, ok := os.LookupEnv(key)
if err := os.Unsetenv(key); err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if ok {
_ = os.Setenv(key, old)
} else {
_ = os.Unsetenv(key)
}
})
}
func TestResolve(t *testing.T) {
const baked = "v1.4.0-rc.2"
tests := []struct {
name string
env string
set bool
want string
}{
{name: "env unset falls back to baked", set: false, want: baked},
{name: "empty env falls back to baked", env: "", set: true, want: baked},
{name: "env overrides baked", env: "v1.4.0", set: true, want: "v1.4.0"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.set {
t.Setenv("COZYSTACK_VERSION", tt.env)
} else {
unsetenv(t, "COZYSTACK_VERSION")
}
if got := resolve(baked); got != tt.want {
t.Errorf("resolve(%q) with env set=%v value=%q = %q, want %q",
baked, tt.set, tt.env, got, tt.want)
}
})
}
}