1 /* 2 Copyright 2023 The Kubernetes 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 genericiooptions 18 19 import ( 20 "bytes" 21 "io" 22 ) 23 24 // IOStreams provides the standard names for iostreams. This is useful for embedding and for unit testing. 25 // Inconsistent and different names make it hard to read and review code 26 type IOStreams struct { 27 // In think, os.Stdin 28 In io.Reader 29 // Out think, os.Stdout 30 Out io.Writer 31 // ErrOut think, os.Stderr 32 ErrOut io.Writer 33 } 34 35 // NewTestIOStreams returns a valid IOStreams and in, out, errout buffers for unit tests 36 func NewTestIOStreams() (IOStreams, *bytes.Buffer, *bytes.Buffer, *bytes.Buffer) { 37 in := &bytes.Buffer{} 38 out := &bytes.Buffer{} 39 errOut := &bytes.Buffer{} 40 41 return IOStreams{ 42 In: in, 43 Out: out, 44 ErrOut: errOut, 45 }, in, out, errOut 46 } 47 48 // NewTestIOStreamsDiscard returns a valid IOStreams that just discards 49 func NewTestIOStreamsDiscard() IOStreams { 50 in := &bytes.Buffer{} 51 return IOStreams{ 52 In: in, 53 Out: io.Discard, 54 ErrOut: io.Discard, 55 } 56 } 57