Files
helmfile/pkg/app/snapshot_test.go
T
yxxheroandClaude ad81e4231e build: update Go to 1.27.1 and modernize the codebase (#2798)
Toolchain and CI:
- Bump go directive from 1.26.8 to 1.27.1 (go.mod)
- Use golang:1.27-alpine builder images in all Dockerfiles
- Bump golangci-lint to v2.13.2 (first release with go1.27 support)
- Add CI gate: `go fix -diff` fails when outdated Go patterns are
  detected (locally: `make check-modernize`)

Note: darwin binaries now require macOS 13 or later.

Lint fixes required by golangci-lint v2.13.2:
- goconst: ignore tests (all 436 findings were test-only; goconst
  got stricter since v2.12 and this option was added for it)
- openai.go: keep deprecated MaxTokens deliberately with a nolint
  rationale (max_tokens is the only form universally supported by
  OpenAI-compatible backends like One-API, LiteLLM, Ollama shim)
- state.go: drop always-nil flags param from appendChartVersionFlags
  (renamed to chartVersionFlags, unparam)

Modernization (go fix ./..., 62 files):
- interface{} -> any, maps.Copy, strings.SplitSeq, range-over-int,
  builtin min/max, slices.Contains/ContainsFunc/Sort, WaitGroup.Go,
  reflect.Type.Fields(), new(expr)
- exit_error.go: strings.Builder + fmt.Fprintf instead of string
  concatenation and WriteString(fmt.Sprintf(...)) (QF1012)
- chart_dependency.go: strings.CutLast for OCI dependency helpers

Signed-off-by: yxxhero <aiopsclub@163.com>
Co-authored-by: Claude <noreply@anthropic.com>
2026-09-16 13:29:02 +08:00

110 lines
2.4 KiB
Go

package app
import (
"bufio"
"bytes"
"io"
"os"
"path/filepath"
"reflect"
"strings"
"sync"
"testing"
"github.com/google/go-cmp/cmp"
"go.uber.org/zap"
"github.com/helmfile/helmfile/pkg/helmexec"
)
func runWithLogCapture(t *testing.T, logLevel string, f func(*testing.T, *zap.SugaredLogger)) *bytes.Buffer {
t.Helper()
bs := &bytes.Buffer{}
logReader, logWriter := io.Pipe()
logFlushed := &sync.WaitGroup{}
// Ensure all the log is consumed into `bs` by calling `logWriter.Close()` followed by `logFlushed.Wait()`
logFlushed.Go(func() {
scanner := bufio.NewScanner(logReader)
for scanner.Scan() {
bs.Write(scanner.Bytes())
bs.WriteString("\n")
}
})
defer func() {
// This is here to avoid data-trace on bytes buffer `bs` to capture logs
if err := logWriter.Close(); err != nil {
panic(err)
}
logFlushed.Wait()
}()
logger := helmexec.NewLogger(logWriter, logLevel)
f(t, logger)
return bs
}
func assertLogEqualsToSnapshot(t *testing.T, data string) {
t.Helper()
assertEqualsToSnapshot(t, "log", data)
}
func assertEqualsToSnapshot(t *testing.T, name string, data string) {
type thisPkgLocator struct{}
t.Helper()
snapshotFileName := snapshotFileName(t, name)
if os.Getenv("HELMFILE_UPDATE_SNAPSHOT") != "" {
update(t, snapshotFileName, []byte(data))
return
}
wantData, err := os.ReadFile(snapshotFileName)
if err != nil {
t.Fatalf(
"Snapshot file %q does not exist. Rerun this test with `HELMFILE_UPDATE_SNAPSHOT=1 go test -v -run %s %s` to create the snapshot",
snapshotFileName,
t.Name(),
reflect.TypeFor[thisPkgLocator]().PkgPath(),
)
}
want := string(wantData)
if d := cmp.Diff(want, data); d != "" {
t.Errorf("unexpected %s: want (-), got (+): %s", name, d)
t.Errorf(
"If you think this is due to the snapshot file being outdated, rerun this test with `HELMFILE_UPDATE_SNAPSHOT=1 go test -v -run %s %s` to update the snapshot",
t.Name(),
reflect.TypeFor[thisPkgLocator]().PkgPath(),
)
}
}
func update(t *testing.T, snapshotFileName string, data []byte) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(snapshotFileName), 0755); err != nil {
t.Fatalf("%v", err)
}
if err := os.WriteFile(snapshotFileName, data, 0644); err != nil {
t.Fatalf("%v", err)
}
}
func snapshotFileName(t *testing.T, name string) string {
dir := filepath.Join(strings.Split(strings.ToLower(t.Name()), "/")...)
return filepath.Join("testdata", dir, name)
}