tlog.app/go/tlog@v0.23.1/ext/tlflag/flag_test.go (about) 1 package tlflag 2 3 import ( 4 "os" 5 "testing" 6 7 "github.com/nikandfor/assert" 8 "tlog.app/go/eazy" 9 "tlog.app/go/errors" 10 11 "tlog.app/go/tlog" 12 "tlog.app/go/tlog/convert" 13 "tlog.app/go/tlog/rotating" 14 "tlog.app/go/tlog/tlio" 15 ) 16 17 type testFile string 18 19 func TestingFileOpener(n string, f int, m os.FileMode) (interface{}, error) { 20 return testFile(n), nil 21 } 22 23 func TestFileWriter(t *testing.T) { 24 OpenFileWriter = TestingFileOpener 25 26 w, err := OpenWriter("stderr") 27 assert.NoError(t, err) 28 assert.Equal(t, tlio.MultiWriter{ 29 tlog.NewConsoleWriter(tlog.Stderr, tlog.LstdFlags), 30 }, w) 31 32 w, err = OpenWriter("stderr?dm,stderr?dm") 33 assert.NoError(t, err) 34 assert.Equal(t, tlio.MultiWriter{ 35 tlog.NewConsoleWriter(tlog.Stderr, tlog.LdetFlags|tlog.Lmilliseconds), 36 tlog.NewConsoleWriter(tlog.Stderr, tlog.LdetFlags|tlog.Lmilliseconds), 37 }, w) 38 39 w, err = OpenWriter(".tl,-.tl") 40 assert.NoError(t, err) 41 assert.Equal(t, tlio.MultiWriter{ 42 tlio.NopCloser{Writer: tlog.Stderr}, 43 tlio.NopCloser{Writer: tlog.Stdout}, 44 }, w) 45 46 w, err = OpenWriter(".tlz") 47 assert.NoError(t, err) 48 assert.Equal(t, tlio.MultiWriter{ 49 eazy.NewWriter(tlog.Stderr, EazyBlockSize, EazyHTable), 50 }, w) 51 52 w, err = OpenWriter("file.tl") 53 assert.NoError(t, err) 54 assert.Equal(t, testFile("file.tl"), w) 55 56 w, err = OpenWriter("file.tlz") 57 assert.NoError(t, err) 58 assert.Equal(t, tlio.WriteCloser{ 59 Writer: eazy.NewWriter(testFile("file.tlz"), EazyBlockSize, EazyHTable), 60 Closer: testFile("file.tlz"), 61 }, w) 62 63 w, err = OpenWriter("file.tl.ez") 64 assert.NoError(t, err) 65 assert.Equal(t, tlio.WriteCloser{ 66 Writer: eazy.NewWriter(testFile("file.tl.ez"), EazyBlockSize, EazyHTable), 67 Closer: testFile("file.tl.ez"), 68 }, w) 69 70 w, err = OpenWriter("file.ezdump") 71 assert.NoError(t, err) 72 assert.Equal(t, tlio.WriteCloser{ 73 Writer: eazy.NewWriter(eazy.NewDumper(testFile("file.ezdump")), EazyBlockSize, EazyHTable), 74 Closer: testFile("file.ezdump"), 75 }, w) 76 77 w, err = OpenWriter("file.json") 78 assert.NoError(t, err) 79 assert.Equal(t, tlio.WriteCloser{ 80 Writer: convert.NewJSON(testFile("file.json")), 81 Closer: testFile("file.json"), 82 }, w) 83 84 w, err = OpenWriter("file.json.ez") 85 assert.NoError(t, err) 86 assert.Equal(t, tlio.WriteCloser{ 87 Writer: convert.NewJSON( 88 eazy.NewWriter( 89 testFile("file.json.ez"), 90 EazyBlockSize, EazyHTable)), 91 Closer: testFile("file.json.ez"), 92 }, w) 93 } 94 95 func TestURLWriter(t *testing.T) { //nolint:dupl 96 OpenFileWriter = func(n string, f int, m os.FileMode) (interface{}, error) { 97 return testFile(n), nil 98 } 99 100 w, err := OpenWriter("relative/path.tlog") 101 assert.NoError(t, err) 102 assert.Equal(t, testFile("relative/path.tlog"), w) 103 104 w, err = OpenWriter("file://relative/path.tlog") 105 assert.NoError(t, err) 106 assert.Equal(t, testFile("relative/path.tlog"), w) 107 108 w, err = OpenWriter("file:///absolute/path.tlog") 109 assert.NoError(t, err) 110 assert.Equal(t, testFile("/absolute/path.tlog"), w) 111 } 112 113 func TestRotatedWriter(t *testing.T) { 114 OpenFileWriter = TestingFileOpener 115 116 const CompressorBlockSize = 1 * eazy.MiB 117 118 with := func(f *rotating.File, wrap func(*rotating.File)) *rotating.File { 119 wrap(f) 120 121 return f 122 } 123 124 w, err := OpenWriter("file_XXXX.tl") 125 assert.NoError(t, err) 126 assert.Equal(t, with(rotating.Create("file_XXXX.tl"), func(f *rotating.File) { 127 f.OpenFile = openFileWriter 128 }), w) 129 130 w, err = OpenWriter("file.tl?rotating=1") 131 assert.NoError(t, err) 132 assert.Equal(t, with(rotating.Create("file.tl"), func(f *rotating.File) { 133 f.OpenFile = openFileWriter 134 }), w) 135 136 w, err = OpenWriter("file_XXXX.tlz") 137 assert.NoError(t, err) 138 assert.Equal(t, with(rotating.Create("file_XXXX.tlz"), func(f *rotating.File) { 139 f.OpenFile = RotatedTLZFileOpener(nil) 140 }), w) 141 142 w, err = OpenWriter("file_XXXX.tl.ez") 143 assert.NoError(t, err) 144 assert.Equal(t, with(rotating.Create("file_XXXX.tl.ez"), func(f *rotating.File) { 145 f.OpenFile = RotatedTLZFileOpener(nil) 146 }), w) 147 148 w, err = OpenWriter("file_XXXX.json.ez") 149 assert.NoError(t, err) 150 assert.Equal(t, tlio.WriteCloser{ 151 Writer: convert.NewJSON(with(rotating.Create("file_XXXX.json.ez"), func(f *rotating.File) { 152 f.OpenFile = RotatedTLZFileOpener(nil) 153 })), 154 Closer: with(rotating.Create("file_XXXX.json.ez"), func(f *rotating.File) { 155 f.OpenFile = RotatedTLZFileOpener(nil) 156 }), 157 }, w) 158 } 159 160 func TestFileReader(t *testing.T) { 161 OpenFileReader = TestingFileOpener 162 163 r, err := OpenReader("stdin") 164 assert.NoError(t, err) 165 assert.Equal(t, tlio.NopCloser{ 166 Reader: os.Stdin, 167 }, r) 168 169 r, err = OpenReader("./stdin") 170 assert.NoError(t, err) 171 assert.Equal(t, testFile("./stdin"), r) 172 173 r, err = OpenReader(".tlog.ez") 174 assert.NoError(t, err) 175 assert.Equal(t, tlio.NopCloser{Reader: eazy.NewReader(os.Stdin)}, r) 176 } 177 178 func TestURLReader(t *testing.T) { //nolint:dupl 179 OpenFileReader = func(n string, f int, m os.FileMode) (interface{}, error) { 180 return testFile(n), nil 181 } 182 183 w, err := OpenReader("relative/path.tlog") 184 assert.NoError(t, err) 185 assert.Equal(t, testFile("relative/path.tlog"), w) 186 187 w, err = OpenReader("file://relative/path.tlog") 188 assert.NoError(t, err) 189 assert.Equal(t, testFile("relative/path.tlog"), w) 190 191 w, err = OpenReader("file:///absolute/path.tlog") 192 assert.NoError(t, err) 193 assert.Equal(t, testFile("/absolute/path.tlog"), w) 194 } 195 196 func (testFile) Write(p []byte) (int, error) { return len(p), nil } 197 198 func (testFile) Read(p []byte) (int, error) { return 0, errors.New("test mock") } 199 200 func (testFile) Close() error { return nil }