github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/filteredwriter/filtered_writer_test.go (about)

     1  package filteredwriter
     2  
     3  import (
     4  	"bytes"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestFilteredWriter(t *testing.T) {
    12  	for _, tc := range []struct {
    13  		name           string
    14  		input          []string
    15  		expectedOutput string
    16  	}{
    17  		{
    18  			name:           "normal",
    19  			input:          []string{"abc\n", "foobar\n", "def\n"},
    20  			expectedOutput: "abc\ndef\n",
    21  		},
    22  		{
    23  			name:           "all one line",
    24  			input:          []string{"abc\nfoobar\ndef\n"},
    25  			expectedOutput: "abc\ndef\n",
    26  		},
    27  		{
    28  			name:           "lines split across writes",
    29  			input:          []string{"ab", "c\n", "foo", "ba", "r\n", "de", "f", "\n"},
    30  			expectedOutput: "abc\ndef\n",
    31  		},
    32  		{
    33  			name: "actual warning we want to suppress",
    34  			input: []string{
    35  				"hello\n",
    36  				"W1021 14:53:11.799222 68992 reflector.go:299] github.com/tilt-dev/tilt/internal/k8s/watch.go:172: " +
    37  					"watch of *v1.Pod ended with: too old resource version: 191906663 (191912819)\n",
    38  				"goodbye\n"},
    39  			expectedOutput: "hello\ngoodbye\n",
    40  		},
    41  	} {
    42  		t.Run(tc.name, func(t *testing.T) {
    43  			out := bytes.NewBuffer(nil)
    44  			fw := New(out, func(s string) bool {
    45  				return strings.Contains(s, "foobar") || strings.Contains(s, "too old resource version")
    46  			})
    47  			for _, s := range tc.input {
    48  				_, err := fw.Write([]byte(s))
    49  				require.NoError(t, err)
    50  			}
    51  
    52  			require.Equal(t, tc.expectedOutput, out.String())
    53  		})
    54  	}
    55  }