github.com/buildtool/build-tools@v0.2.29-0.20240322150259-6a1d0a553c23/pkg/cli/cli_test.go (about) 1 // MIT License 2 // 3 // Copyright (c) 2018 buildtool 4 // 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 // 12 // The above copyright notice and this permission notice shall be included in all 13 // copies or substantial portions of the Software. 14 // 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 // SOFTWARE. 22 23 package cli 24 25 import ( 26 "bytes" 27 "os" 28 "testing" 29 "time" 30 31 "github.com/apex/log" 32 "github.com/stretchr/testify/assert" 33 ) 34 35 func Test_New_Stdout(t *testing.T) { 36 handler := New(os.Stdout) 37 assert.Equal(t, os.Stdout, handler.Writer) 38 assert.IsType(t, &os.File{}, handler.Writer) 39 } 40 41 func Test_New_NoFileWriter(t *testing.T) { 42 w := &Writer{} 43 handler := New(w) 44 45 assert.Equal(t, w, handler.Writer) 46 assert.IsType(t, w, handler.Writer) 47 } 48 49 func Test_HandleLog(t *testing.T) { 50 mu := &checkLocker{} 51 buff := &bytes.Buffer{} 52 handler := Handler{ 53 mu: mu, 54 Writer: buff, 55 } 56 _ = handler.HandleLog(&log.Entry{ 57 Logger: nil, 58 Fields: nil, 59 Level: log.WarnLevel, 60 Timestamp: time.Time{}, 61 Message: "<green>msg</green>", 62 }) 63 assert.Equal(t, 1, mu.lockCalled) 64 assert.Equal(t, 1, mu.unlockCalled) 65 assert.Equal(t, "\x1b[0m\x1b[32mmsg\x1b[39m\x1b[0m", buff.String()) 66 } 67 68 func Test_Writer_New(t *testing.T) { 69 assert.NotNil(t, NewWriter(log.Log)) 70 assert.Nil(t, NewWriter(invalidLog{})) 71 } 72 73 func Test_Writer_Write(t *testing.T) { 74 w := NewWriter(log.Log) 75 logMsg := "test" 76 written, err := w.Write([]byte(logMsg)) 77 78 assert.NoError(t, err) 79 assert.Equal(t, len(logMsg), written) 80 } 81 82 func Test_Verbose(t *testing.T) { 83 log.SetLevel(log.DebugLevel) 84 assert.True(t, Verbose(log.Log)) 85 86 log.SetLevel(log.InfoLevel) 87 assert.False(t, Verbose(log.Log)) 88 assert.False(t, Verbose(invalidLog{})) 89 } 90 91 type checkLocker struct { 92 lockCalled int 93 unlockCalled int 94 } 95 96 func (c *checkLocker) Lock() { 97 c.lockCalled = c.lockCalled + 1 98 } 99 100 func (c *checkLocker) Unlock() { 101 c.unlockCalled = c.unlockCalled + 1 102 } 103 104 type invalidLog struct { 105 } 106 107 func (i invalidLog) WithFields(fielder log.Fielder) *log.Entry { 108 panic("implement me") 109 } 110 111 func (i invalidLog) WithField(s string, i2 interface{}) *log.Entry { 112 panic("implement me") 113 } 114 115 func (i invalidLog) WithDuration(duration time.Duration) *log.Entry { 116 panic("implement me") 117 } 118 119 func (i invalidLog) WithError(err error) *log.Entry { 120 panic("implement me") 121 } 122 123 func (i invalidLog) Debug(s string) { 124 panic("implement me") 125 } 126 127 func (i invalidLog) Info(s string) { 128 panic("implement me") 129 } 130 131 func (i invalidLog) Warn(s string) { 132 panic("implement me") 133 } 134 135 func (i invalidLog) Error(s string) { 136 panic("implement me") 137 } 138 139 func (i invalidLog) Fatal(s string) { 140 panic("implement me") 141 } 142 143 func (i invalidLog) Debugf(s string, i2 ...interface{}) { 144 panic("implement me") 145 } 146 147 func (i invalidLog) Infof(s string, i2 ...interface{}) { 148 panic("implement me") 149 } 150 151 func (i invalidLog) Warnf(s string, i2 ...interface{}) { 152 panic("implement me") 153 } 154 155 func (i invalidLog) Errorf(s string, i2 ...interface{}) { 156 panic("implement me") 157 } 158 159 func (i invalidLog) Fatalf(s string, i2 ...interface{}) { 160 panic("implement me") 161 } 162 163 func (i invalidLog) Trace(s string) *log.Entry { 164 panic("implement me") 165 }