github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/output/output_test.go (about)

     1  /*
     2  Copyright 2021 The Skaffold Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package output
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"io"
    23  	"io/ioutil"
    24  	"os"
    25  	"testing"
    26  
    27  	"github.com/google/go-cmp/cmp/cmpopts"
    28  
    29  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
    30  	eventV2 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/event/v2"
    31  	"github.com/GoogleContainerTools/skaffold/testutil"
    32  )
    33  
    34  func TestIsStdOut(t *testing.T) {
    35  	tests := []struct {
    36  		description string
    37  		out         io.Writer
    38  		expected    bool
    39  	}{
    40  		{
    41  			description: "std out passed",
    42  			out:         os.Stdout,
    43  			expected:    true,
    44  		},
    45  		{
    46  			description: "out nil",
    47  			out:         nil,
    48  		},
    49  		{
    50  			description: "out bytes buffer",
    51  			out:         new(bytes.Buffer),
    52  		},
    53  		{
    54  			description: "colorable std out passed",
    55  			out: skaffoldWriter{
    56  				MainWriter: NewColorWriter(os.Stdout),
    57  			},
    58  			expected: true,
    59  		},
    60  		{
    61  			description: "colorableWriter passed",
    62  			out:         NewColorWriter(os.Stdout),
    63  			expected:    true,
    64  		},
    65  		{
    66  			description: "invalid colorableWriter passed",
    67  			out: skaffoldWriter{
    68  				MainWriter: NewColorWriter(ioutil.Discard),
    69  			},
    70  			expected: false,
    71  		},
    72  	}
    73  	for _, test := range tests {
    74  		testutil.Run(t, test.description, func(t *testutil.T) {
    75  			t.CheckDeepEqual(test.expected, IsStdout(test.out))
    76  		})
    77  	}
    78  }
    79  
    80  func TestGetUnderlyingWriter(t *testing.T) {
    81  	tests := []struct {
    82  		description string
    83  		out         io.Writer
    84  		expected    io.Writer
    85  	}{
    86  		{
    87  			description: "colorable os.Stdout returns os.Stdout",
    88  			out: skaffoldWriter{
    89  				MainWriter: colorableWriter{os.Stdout},
    90  			},
    91  			expected: os.Stdout,
    92  		},
    93  		{
    94  			description: "skaffold writer returns os.Stdout without colorableWriter",
    95  			out: skaffoldWriter{
    96  				MainWriter: os.Stdout,
    97  			},
    98  			expected: os.Stdout,
    99  		},
   100  		{
   101  			description: "return ioutil.Discard from skaffoldWriter",
   102  			out: skaffoldWriter{
   103  				MainWriter: NewColorWriter(ioutil.Discard),
   104  			},
   105  			expected: ioutil.Discard,
   106  		},
   107  		{
   108  			description: "os.Stdout returned from colorableWriter",
   109  			out:         NewColorWriter(os.Stdout),
   110  			expected:    os.Stdout,
   111  		},
   112  		{
   113  			description: "GetWriter returns original writer if not colorable",
   114  			out:         os.Stdout,
   115  			expected:    os.Stdout,
   116  		},
   117  	}
   118  	for _, test := range tests {
   119  		testutil.Run(t, test.description, func(t *testutil.T) {
   120  			t.CheckDeepEqual(true, test.expected == GetUnderlyingWriter(test.out))
   121  		})
   122  	}
   123  }
   124  
   125  func TestWithEventContext(t *testing.T) {
   126  	tests := []struct {
   127  		name      string
   128  		writer    io.Writer
   129  		phase     constants.Phase
   130  		subtaskID string
   131  
   132  		expected io.Writer
   133  	}{
   134  		{
   135  			name: "skaffoldWriter update info",
   136  			writer: skaffoldWriter{
   137  				MainWriter:  ioutil.Discard,
   138  				EventWriter: eventV2.NewLogger(constants.Build, "1"),
   139  			},
   140  			phase:     constants.Test,
   141  			subtaskID: "2",
   142  			expected: skaffoldWriter{
   143  				MainWriter:  ioutil.Discard,
   144  				EventWriter: eventV2.NewLogger(constants.Test, "2"),
   145  			},
   146  		},
   147  		{
   148  			name:     "non skaffoldWriter returns same",
   149  			writer:   ioutil.Discard,
   150  			expected: ioutil.Discard,
   151  		},
   152  	}
   153  
   154  	for _, test := range tests {
   155  		testutil.Run(t, test.name, func(t *testutil.T) {
   156  			got, _ := WithEventContext(context.Background(), test.writer, test.phase, test.subtaskID)
   157  			t.CheckDeepEqual(test.expected, got, cmpopts.IgnoreTypes(false, "", constants.DevLoop))
   158  		})
   159  	}
   160  }
   161  
   162  func TestWriteWithTimeStamps(t *testing.T) {
   163  	tests := []struct {
   164  		name        string
   165  		writer      func(io.Writer) io.Writer
   166  		expectedLen int
   167  	}{
   168  		{
   169  			name: "skaffold writer with color and timestamps",
   170  			writer: func(out io.Writer) io.Writer {
   171  				return skaffoldWriter{
   172  					MainWriter:  colorableWriter{out},
   173  					EventWriter: ioutil.Discard,
   174  					timestamps:  true,
   175  				}
   176  			},
   177  			expectedLen: len(timestampFormat) + len(" \u001B[32mtesting!\u001B[0m"),
   178  		},
   179  		{
   180  			name: "skaffold writer with color and no timestamps",
   181  			writer: func(out io.Writer) io.Writer {
   182  				return skaffoldWriter{
   183  					MainWriter:  colorableWriter{out},
   184  					EventWriter: ioutil.Discard,
   185  				}
   186  			},
   187  			expectedLen: len("\u001B[32mtesting!\u001B[0m"),
   188  		},
   189  		{
   190  			name: "skaffold writer with timestamps and no color",
   191  			writer: func(out io.Writer) io.Writer {
   192  				return skaffoldWriter{
   193  					MainWriter:  out,
   194  					EventWriter: ioutil.Discard,
   195  					timestamps:  true,
   196  				}
   197  			},
   198  			expectedLen: len(timestampFormat) + len(" testing!"),
   199  		},
   200  		{
   201  			name: "skaffold writer with no color and no timestamps",
   202  			writer: func(out io.Writer) io.Writer {
   203  				return skaffoldWriter{
   204  					MainWriter:  out,
   205  					EventWriter: ioutil.Discard,
   206  				}
   207  			},
   208  			expectedLen: len("testing!"),
   209  		},
   210  	}
   211  
   212  	for _, test := range tests {
   213  		t.Run(test.name, func(t *testing.T) {
   214  			var buf bytes.Buffer
   215  			out := test.writer(&buf)
   216  			Default.Fprintf(out, "testing!")
   217  			testutil.CheckDeepEqual(t, test.expectedLen, len(buf.String()))
   218  		})
   219  	}
   220  }