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