-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support for reading gzip,bzip2 and xz files
- Loading branch information
Showing
4 changed files
with
142 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package tui | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/ulikunitz/xz" | ||
) | ||
|
||
func TestGetScannerForPlainString(t *testing.T) { | ||
r := bytes.NewReader([]byte("hello")) | ||
s, err := getScanner(r) | ||
assert.Nil(t, err) | ||
|
||
assert.Equal(t, true, s.Scan()) | ||
assert.Equal(t, "hello", s.Text()) | ||
assert.Equal(t, nil, s.Err()) | ||
} | ||
|
||
func TestGetScannerForGzipped(t *testing.T) { | ||
b := bytes.NewBuffer([]byte{}) | ||
w := gzip.NewWriter(b) | ||
|
||
_, err := w.Write([]byte("hello world")) | ||
assert.Nil(t, err) | ||
|
||
err = w.Close() | ||
assert.Nil(t, err) | ||
|
||
r := bytes.NewReader(b.Bytes()) | ||
s, err := getScanner(r) | ||
assert.Nil(t, err) | ||
|
||
assert.Equal(t, true, s.Scan()) | ||
assert.Equal(t, "hello world", s.Text()) | ||
assert.Equal(t, nil, s.Err()) | ||
} | ||
|
||
func TestGetScannerForBzipped(t *testing.T) { | ||
r := bytes.NewReader([]byte{ | ||
// bzip2 header | ||
0x42, 0x5A, 0x68, 0x39, | ||
// bzip2 compressed data: "hello" | ||
0x31, 0x41, 0x59, 0x26, | ||
0x53, 0x59, 0xC1, 0xC0, | ||
0x80, 0xE2, 0x00, 0x00, | ||
0x01, 0x41, 0x00, 0x00, | ||
0x10, 0x02, 0x44, 0xA0, | ||
0x00, 0x30, 0xCD, 0x00, | ||
0xC3, 0x46, 0x29, 0x97, | ||
0x17, 0x72, 0x45, 0x38, | ||
0x50, 0x90, 0xC1, 0xC0, | ||
0x80, 0xE2, | ||
}) | ||
s, err := getScanner(r) | ||
assert.Nil(t, err) | ||
|
||
assert.Equal(t, true, s.Scan()) | ||
assert.Equal(t, "hello", s.Text()) | ||
assert.Equal(t, nil, s.Err()) | ||
} | ||
|
||
func TestGetScannerForXzipped(t *testing.T) { | ||
b := bytes.NewBuffer([]byte{}) | ||
w, err := xz.NewWriter(b) | ||
assert.Nil(t, err) | ||
|
||
_, err = w.Write([]byte("hello world")) | ||
assert.Nil(t, err) | ||
|
||
err = w.Close() | ||
assert.Nil(t, err) | ||
|
||
r := bytes.NewReader(b.Bytes()) | ||
s, err := getScanner(r) | ||
assert.Nil(t, err) | ||
|
||
assert.Equal(t, true, s.Scan()) | ||
assert.Equal(t, "hello world", s.Text()) | ||
assert.Equal(t, nil, s.Err()) | ||
} |