diff --git a/pkg/app/app_apply_test.go b/pkg/app/app_apply_test.go index 5e9f6e81..1e6d5ea7 100644 --- a/pkg/app/app_apply_test.go +++ b/pkg/app/app_apply_test.go @@ -1,14 +1,12 @@ package app import ( - "bufio" - "bytes" - "io" "sync" "testing" "github.com/google/go-cmp/cmp" "github.com/variantdev/vals" + "go.uber.org/zap" "github.com/helmfile/helmfile/pkg/exectest" "github.com/helmfile/helmfile/pkg/filesystem" @@ -54,35 +52,9 @@ func TestApply_2(t *testing.T) { ReleasesMutex: &sync.Mutex{}, } - bs := &bytes.Buffer{} - - func() { + bs := runWithLogCapture(t, "debug", func(t *testing.T, logger *zap.SugaredLogger) { t.Helper() - logReader, logWriter := io.Pipe() - - logFlushed := &sync.WaitGroup{} - // Ensure all the log is consumed into `bs` by calling `logWriter.Close()` followed by `logFlushed.Wait()` - logFlushed.Add(1) - go func() { - scanner := bufio.NewScanner(logReader) - for scanner.Scan() { - bs.Write(scanner.Bytes()) - bs.WriteString("\n") - } - logFlushed.Done() - }() - - 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, "debug") - valsRuntime, err := vals.New(vals.Options{CacheSize: 32}) if err != nil { t.Errorf("unexpected error creating vals runtime: %v", err) @@ -156,7 +128,7 @@ func TestApply_2(t *testing.T) { } } } - }() + }) if tc.log != "" { actual := bs.String() diff --git a/pkg/app/app_diff_test.go b/pkg/app/app_diff_test.go index d9e06f70..4c3751db 100644 --- a/pkg/app/app_diff_test.go +++ b/pkg/app/app_diff_test.go @@ -1,15 +1,13 @@ package app import ( - "bufio" - "bytes" - "io" "sync" "testing" "github.com/google/go-cmp/cmp" "github.com/stretchr/testify/require" "github.com/variantdev/vals" + "go.uber.org/zap" "github.com/helmfile/helmfile/pkg/exectest" "github.com/helmfile/helmfile/pkg/filesystem" @@ -46,35 +44,9 @@ func TestDiffWithNeeds(t *testing.T) { ReleasesMutex: &sync.Mutex{}, } - bs := &bytes.Buffer{} - - func() { + bs := runWithLogCapture(t, "debug", func(t *testing.T, logger *zap.SugaredLogger) { t.Helper() - logReader, logWriter := io.Pipe() - - logFlushed := &sync.WaitGroup{} - // Ensure all the log is consumed into `bs` by calling `logWriter.Close()` followed by `logFlushed.Wait()` - logFlushed.Add(1) - go func() { - scanner := bufio.NewScanner(logReader) - for scanner.Scan() { - bs.Write(scanner.Bytes()) - bs.WriteString("\n") - } - logFlushed.Done() - }() - - 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, "debug") - valsRuntime, err := vals.New(vals.Options{CacheSize: 32}) if err != nil { t.Errorf("unexpected error creating vals runtime: %v", err) @@ -168,7 +140,7 @@ releases: } require.Equal(t, wantDiffs, helm.Diffed) - }() + }) testhelper.RequireLog(t, "app_diff_test", bs) } @@ -346,35 +318,9 @@ func TestDiffWithInstalled(t *testing.T) { ReleasesMutex: &sync.Mutex{}, } - bs := &bytes.Buffer{} - - func() { + bs := runWithLogCapture(t, "debug", func(t *testing.T, logger *zap.SugaredLogger) { t.Helper() - logReader, logWriter := io.Pipe() - - logFlushed := &sync.WaitGroup{} - // Ensure all the log is consumed into `bs` by calling `logWriter.Close()` followed by `logFlushed.Wait()` - logFlushed.Add(1) - go func() { - scanner := bufio.NewScanner(logReader) - for scanner.Scan() { - bs.Write(scanner.Bytes()) - bs.WriteString("\n") - } - logFlushed.Done() - }() - - 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, "debug") - valsRuntime, err := vals.New(vals.Options{CacheSize: 32}) if err != nil { t.Errorf("unexpected error creating vals runtime: %v", err) @@ -421,7 +367,7 @@ func TestDiffWithInstalled(t *testing.T) { } require.Equal(t, wantDiffs, helm.Diffed) - }() + }) testhelper.RequireLog(t, "app_diff_test", bs) } diff --git a/pkg/app/app_lint_test.go b/pkg/app/app_lint_test.go index 20ca6844..127e17ec 100644 --- a/pkg/app/app_lint_test.go +++ b/pkg/app/app_lint_test.go @@ -1,9 +1,6 @@ package app import ( - "bufio" - "bytes" - "io" "os" "path/filepath" "strings" @@ -13,6 +10,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/stretchr/testify/require" "github.com/variantdev/vals" + "go.uber.org/zap" "github.com/helmfile/helmfile/pkg/exectest" ffs "github.com/helmfile/helmfile/pkg/filesystem" @@ -48,35 +46,9 @@ func TestLint(t *testing.T) { ReleasesMutex: &sync.Mutex{}, } - bs := &bytes.Buffer{} - - func() { + bs := runWithLogCapture(t, "debug", func(t *testing.T, logger *zap.SugaredLogger) { t.Helper() - logReader, logWriter := io.Pipe() - - logFlushed := &sync.WaitGroup{} - // Ensure all the log is consumed into `bs` by calling `logWriter.Close()` followed by `logFlushed.Wait()` - logFlushed.Add(1) - go func() { - scanner := bufio.NewScanner(logReader) - for scanner.Scan() { - bs.Write(scanner.Bytes()) - bs.WriteString("\n") - } - logFlushed.Done() - }() - - 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, "debug") - valsRuntime, err := vals.New(vals.Options{CacheSize: 32}) if err != nil { t.Errorf("unexpected error creating vals runtime: %v", err) @@ -169,7 +141,7 @@ releases: } require.Equal(t, wantLints, helm.Linted) - }() + }) testNameComponents := strings.Split(t.Name(), "/") testBaseName := strings.ToLower( diff --git a/pkg/app/app_list_test.go b/pkg/app/app_list_test.go index 79602aab..5fc5ebaf 100644 --- a/pkg/app/app_list_test.go +++ b/pkg/app/app_list_test.go @@ -1,16 +1,14 @@ package app import ( - "bufio" "bytes" - "io" "os" - "sync" "testing" "github.com/google/go-cmp/cmp" "github.com/stretchr/testify/assert" "github.com/variantdev/vals" + "go.uber.org/zap" ffs "github.com/helmfile/helmfile/pkg/filesystem" "github.com/helmfile/helmfile/pkg/helmexec" @@ -30,35 +28,9 @@ func testListWithEnvironment(t *testing.T, cfg configImpl) { check := func(t *testing.T, tc testcase, cfg configImpl) { t.Helper() - bs := &bytes.Buffer{} - - func() { + bs := runWithLogCapture(t, "debug", func(t *testing.T, logger *zap.SugaredLogger) { t.Helper() - logReader, logWriter := io.Pipe() - - logFlushed := &sync.WaitGroup{} - // Ensure all the log is consumed into `bs` by calling `logWriter.Close()` followed by `logFlushed.Wait()` - logFlushed.Add(1) - go func() { - scanner := bufio.NewScanner(logReader) - for scanner.Scan() { - bs.Write(scanner.Bytes()) - bs.WriteString("\n") - } - logFlushed.Done() - }() - - 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, "debug") - valsRuntime, err := vals.New(vals.Options{CacheSize: 32}) if err != nil { t.Errorf("unexpected error creating vals runtime: %v", err) @@ -178,7 +150,7 @@ releases: } assert.Equal(t, tc.expected, out) - }() + }) testhelper.RequireLog(t, "app_list_test", bs) } diff --git a/pkg/app/app_sync_test.go b/pkg/app/app_sync_test.go index 5920c19c..3c522320 100644 --- a/pkg/app/app_sync_test.go +++ b/pkg/app/app_sync_test.go @@ -1,14 +1,12 @@ package app import ( - "bufio" - "bytes" - "io" "sync" "testing" "github.com/google/go-cmp/cmp" "github.com/variantdev/vals" + "go.uber.org/zap" "github.com/helmfile/helmfile/pkg/exectest" ffs "github.com/helmfile/helmfile/pkg/filesystem" @@ -52,35 +50,9 @@ func TestSync(t *testing.T) { ReleasesMutex: &sync.Mutex{}, } - bs := &bytes.Buffer{} - - func() { + bs := runWithLogCapture(t, "debug", func(t *testing.T, logger *zap.SugaredLogger) { t.Helper() - logReader, logWriter := io.Pipe() - - logFlushed := &sync.WaitGroup{} - // Ensure all the log is consumed into `bs` by calling `logWriter.Close()` followed by `logFlushed.Wait()` - logFlushed.Add(1) - go func() { - scanner := bufio.NewScanner(logReader) - for scanner.Scan() { - bs.Write(scanner.Bytes()) - bs.WriteString("\n") - } - logFlushed.Done() - }() - - 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, "debug") - valsRuntime, err := vals.New(vals.Options{CacheSize: 32}) if err != nil { t.Errorf("unexpected error creating vals runtime: %v", err) @@ -154,7 +126,7 @@ func TestSync(t *testing.T) { } } } - }() + }) if tc.log != "" { actual := bs.String() diff --git a/pkg/app/app_template_test.go b/pkg/app/app_template_test.go index f9d7acc6..83a7a3a4 100644 --- a/pkg/app/app_template_test.go +++ b/pkg/app/app_template_test.go @@ -1,9 +1,6 @@ package app import ( - "bufio" - "bytes" - "io" "os" "path/filepath" "strings" @@ -13,6 +10,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/stretchr/testify/require" "github.com/variantdev/vals" + "go.uber.org/zap" "github.com/helmfile/helmfile/pkg/exectest" ffs "github.com/helmfile/helmfile/pkg/filesystem" @@ -48,35 +46,9 @@ func TestTemplate(t *testing.T) { ReleasesMutex: &sync.Mutex{}, } - bs := &bytes.Buffer{} - - func() { + bs := runWithLogCapture(t, "debug", func(t *testing.T, logger *zap.SugaredLogger) { t.Helper() - logReader, logWriter := io.Pipe() - - logFlushed := &sync.WaitGroup{} - // Ensure all the log is consumed into `bs` by calling `logWriter.Close()` followed by `logFlushed.Wait()` - logFlushed.Add(1) - go func() { - scanner := bufio.NewScanner(logReader) - for scanner.Scan() { - bs.Write(scanner.Bytes()) - bs.WriteString("\n") - } - logFlushed.Done() - }() - - 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, "debug") - valsRuntime, err := vals.New(vals.Options{CacheSize: 32}) if err != nil { t.Errorf("unexpected error creating vals runtime: %v", err) @@ -169,7 +141,7 @@ releases: } require.Equal(t, wantTemplates, helm.Templated) - }() + }) testNameComponents := strings.Split(t.Name(), "/") testBaseName := strings.ToLower( diff --git a/pkg/app/app_test.go b/pkg/app/app_test.go index 317902ea..f561ad15 100644 --- a/pkg/app/app_test.go +++ b/pkg/app/app_test.go @@ -1,7 +1,6 @@ package app import ( - "bufio" "bytes" "errors" "fmt" @@ -3970,32 +3969,8 @@ changing working directory back to "/path/to" ReleasesMutex: &sync.Mutex{}, } - bs := &bytes.Buffer{} - - func() { - logReader, logWriter := io.Pipe() - - logFlushed := &sync.WaitGroup{} - // Ensure all the log is consumed into `bs` by calling `logWriter.Close()` followed by `logFlushed.Wait()` - logFlushed.Add(1) - go func() { - scanner := bufio.NewScanner(logReader) - for scanner.Scan() { - bs.Write(scanner.Bytes()) - bs.WriteString("\n") - } - logFlushed.Done() - }() - - 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, "debug") + bs := runWithLogCapture(t, "debug", func(t *testing.T, logger *zap.SugaredLogger) { + t.Helper() valsRuntime, err := vals.New(vals.Options{CacheSize: 32}) if err != nil { @@ -4067,7 +4042,7 @@ changing working directory back to "/path/to" } } } - }() + }) if tc.log != "" { actual := bs.String() @@ -4157,32 +4132,8 @@ changing working directory back to "/path/to" ReleasesMutex: &sync.Mutex{}, } - bs := &bytes.Buffer{} - - func() { - logReader, logWriter := io.Pipe() - - logFlushed := &sync.WaitGroup{} - // Ensure all the log is consumed into `bs` by calling `logWriter.Close()` followed by `logFlushed.Wait()` - logFlushed.Add(1) - go func() { - scanner := bufio.NewScanner(logReader) - for scanner.Scan() { - bs.Write(scanner.Bytes()) - bs.WriteString("\n") - } - logFlushed.Done() - }() - - 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, "debug") + bs := runWithLogCapture(t, "debug", func(t *testing.T, logger *zap.SugaredLogger) { + t.Helper() app := appWithFs(&App{ OverrideHelmBinary: DefaultHelmBinary, @@ -4211,7 +4162,7 @@ changing working directory back to "/path/to" if !reflect.DeepEqual(helm.Charts, tc.charts) { t.Fatalf("expected charts %v, got %v", helm.Charts, tc.charts) } - }() + }) if tc.log != "" { actual := bs.String() diff --git a/pkg/app/destroy_nokubectx_test.go b/pkg/app/destroy_nokubectx_test.go index 41bfb742..8e1fb2e0 100644 --- a/pkg/app/destroy_nokubectx_test.go +++ b/pkg/app/destroy_nokubectx_test.go @@ -1,13 +1,11 @@ package app import ( - "bufio" - "bytes" - "io" "sync" "testing" "github.com/variantdev/vals" + "go.uber.org/zap" "github.com/helmfile/helmfile/pkg/exectest" ffs "github.com/helmfile/helmfile/pkg/filesystem" @@ -47,35 +45,9 @@ func TestDestroy_2(t *testing.T) { ReleasesMutex: &sync.Mutex{}, } - bs := &bytes.Buffer{} - - func() { + bs := runWithLogCapture(t, "debug", func(t *testing.T, logger *zap.SugaredLogger) { t.Helper() - logReader, logWriter := io.Pipe() - - logFlushed := &sync.WaitGroup{} - // Ensure all the log is consumed into `bs` by calling `logWriter.Close()` followed by `logFlushed.Wait()` - logFlushed.Add(1) - go func() { - scanner := bufio.NewScanner(logReader) - for scanner.Scan() { - bs.Write(scanner.Bytes()) - bs.WriteString("\n") - } - logFlushed.Done() - }() - - 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, "debug") - valsRuntime, err := vals.New(vals.Options{CacheSize: 32}) if err != nil { t.Errorf("unexpected error creating vals runtime: %v", err) @@ -146,7 +118,7 @@ func TestDestroy_2(t *testing.T) { } } } - }() + }) if tc.log != "" { actual := bs.String() diff --git a/pkg/app/destroy_test.go b/pkg/app/destroy_test.go index 8a7a5234..da5faa78 100644 --- a/pkg/app/destroy_test.go +++ b/pkg/app/destroy_test.go @@ -1,9 +1,6 @@ package app import ( - "bufio" - "bytes" - "io" "sync" "testing" @@ -88,35 +85,9 @@ func TestDestroy(t *testing.T) { ReleasesMutex: &sync.Mutex{}, } - bs := &bytes.Buffer{} - - func() { + bs := runWithLogCapture(t, "debug", func(t *testing.T, logger *zap.SugaredLogger) { t.Helper() - logReader, logWriter := io.Pipe() - - logFlushed := &sync.WaitGroup{} - // Ensure all the log is consumed into `bs` by calling `logWriter.Close()` followed by `logFlushed.Wait()` - logFlushed.Add(1) - go func() { - scanner := bufio.NewScanner(logReader) - for scanner.Scan() { - bs.Write(scanner.Bytes()) - bs.WriteString("\n") - } - logFlushed.Done() - }() - - 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, "debug") - valsRuntime, err := vals.New(vals.Options{CacheSize: 32}) if err != nil { t.Errorf("unexpected error creating vals runtime: %v", err) @@ -186,7 +157,7 @@ func TestDestroy(t *testing.T) { } } } - }() + }) if tc.log != "" { actual := bs.String() diff --git a/pkg/app/diff_nokubectx_test.go b/pkg/app/diff_nokubectx_test.go index e48dfad4..41534864 100644 --- a/pkg/app/diff_nokubectx_test.go +++ b/pkg/app/diff_nokubectx_test.go @@ -1,14 +1,12 @@ package app import ( - "bufio" - "bytes" - "io" "sync" "testing" "github.com/google/go-cmp/cmp" "github.com/variantdev/vals" + "go.uber.org/zap" "github.com/helmfile/helmfile/pkg/exectest" ffs "github.com/helmfile/helmfile/pkg/filesystem" @@ -1026,32 +1024,8 @@ changing working directory back to "/path/to" ReleasesMutex: &sync.Mutex{}, } - bs := &bytes.Buffer{} - - func() { - logReader, logWriter := io.Pipe() - - logFlushed := &sync.WaitGroup{} - // Ensure all the log is consumed into `bs` by calling `logWriter.Close()` followed by `logFlushed.Wait()` - logFlushed.Add(1) - go func() { - scanner := bufio.NewScanner(logReader) - for scanner.Scan() { - bs.Write(scanner.Bytes()) - bs.WriteString("\n") - } - logFlushed.Done() - }() - - 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, "debug") + bs := runWithLogCapture(t, "debug", func(t *testing.T, logger *zap.SugaredLogger) { + t.Helper() valsRuntime, err := vals.New(vals.Options{CacheSize: 32}) if err != nil { @@ -1125,7 +1099,7 @@ changing working directory back to "/path/to" } } } - }() + }) if tc.log != "" { actual := bs.String() diff --git a/pkg/app/diff_test.go b/pkg/app/diff_test.go index bded5afd..f2649129 100644 --- a/pkg/app/diff_test.go +++ b/pkg/app/diff_test.go @@ -1,9 +1,6 @@ package app import ( - "bufio" - "bytes" - "io" "sync" "testing" @@ -1324,32 +1321,8 @@ changing working directory back to "/path/to" ReleasesMutex: &sync.Mutex{}, } - bs := &bytes.Buffer{} - - func() { - logReader, logWriter := io.Pipe() - - logFlushed := &sync.WaitGroup{} - // Ensure all the log is consumed into `bs` by calling `logWriter.Close()` followed by `logFlushed.Wait()` - logFlushed.Add(1) - go func() { - scanner := bufio.NewScanner(logReader) - for scanner.Scan() { - bs.Write(scanner.Bytes()) - bs.WriteString("\n") - } - logFlushed.Done() - }() - - 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, "debug") + bs := runWithLogCapture(t, "debug", func(t *testing.T, logger *zap.SugaredLogger) { + t.Helper() valsRuntime, err := vals.New(vals.Options{CacheSize: 32}) if err != nil { @@ -1429,7 +1402,7 @@ changing working directory back to "/path/to" } } } - }() + }) if tc.log != "" { actual := bs.String()