Use the new log capturing helper in all tests

Signed-off-by: Yusuke Kuoka <ykuoka@gmail.com>
This commit is contained in:
Yusuke Kuoka
2022-11-13 08:53:53 +00:00
parent 235917c11c
commit 8d2aec249d
11 changed files with 38 additions and 391 deletions
+5 -59
View File
@@ -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)
}