forked from cihub/seelog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Address feedback and add archive_test
- Loading branch information
Showing
8 changed files
with
242 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
package archive_test | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"testing" | ||
|
||
"github.com/jonsyu1/seelog/archive" | ||
"github.com/jonsyu1/seelog/archive/gzip" | ||
"github.com/jonsyu1/seelog/archive/tar" | ||
"github.com/jonsyu1/seelog/archive/zip" | ||
"github.com/jonsyu1/seelog/io/iotest" | ||
) | ||
|
||
const ( | ||
gzipType = iota | ||
tarType | ||
zipType | ||
) | ||
|
||
type file struct { | ||
name string | ||
contents []byte | ||
} | ||
|
||
var ( | ||
oneFile = []file{ | ||
{ | ||
name: "file1", | ||
contents: []byte("This is a single log."), | ||
}, | ||
} | ||
twoFiles = []file{ | ||
{ | ||
name: "file1", | ||
contents: []byte("This is a log."), | ||
}, | ||
{ | ||
name: "file2", | ||
contents: []byte("This is another log."), | ||
}, | ||
} | ||
) | ||
|
||
var copyTests = map[string]struct { | ||
srcType, dstType int | ||
in []file | ||
}{ | ||
"zip to zip": { | ||
srcType: zipType, | ||
dstType: zipType, | ||
in: twoFiles, | ||
}, | ||
"zip to tar": { | ||
srcType: zipType, | ||
dstType: tarType, | ||
in: twoFiles, | ||
}, | ||
"zip to gzip": { | ||
srcType: zipType, | ||
dstType: gzipType, | ||
in: oneFile, | ||
}, | ||
"tar to tar": { | ||
srcType: zipType, | ||
dstType: tarType, | ||
in: twoFiles, | ||
}, | ||
"tar to gzip": { | ||
srcType: zipType, | ||
dstType: tarType, | ||
in: oneFile, | ||
}, | ||
"tar to zip": { | ||
srcType: zipType, | ||
dstType: tarType, | ||
in: twoFiles, | ||
}, | ||
"gzip to gzip": { | ||
srcType: zipType, | ||
dstType: tarType, | ||
in: oneFile, | ||
}, | ||
"gzip to tar": { | ||
srcType: zipType, | ||
dstType: tarType, | ||
in: oneFile, | ||
}, | ||
"gzip to zip": { | ||
srcType: zipType, | ||
dstType: tarType, | ||
in: oneFile, | ||
}, | ||
} | ||
|
||
func TestCopy(t *testing.T) { | ||
var srcb, dstb bytes.Buffer | ||
for tname, tt := range copyTests { | ||
// Reset buffers between tests | ||
srcb.Reset() | ||
dstb.Reset() | ||
|
||
// Last file name (needed for gzip.NewReader) | ||
var fname string | ||
|
||
// Seed the src | ||
srcw := writer(t, tname, &srcb, tt.srcType) | ||
for _, f := range tt.in { | ||
srcw.NextFile(f.name, iotest.FileInfo(t, f.contents)) | ||
mustCopy(t, tname, srcw, bytes.NewReader(f.contents)) | ||
fname = f.name | ||
} | ||
mustClose(t, tname, srcw) | ||
|
||
// Perform the copy | ||
srcr := reader(t, tname, &srcb, tt.srcType, fname) | ||
dstw := writer(t, tname, &dstb, tt.dstType) | ||
if err := archive.Copy(dstw, srcr); err != nil { | ||
t.Fatalf("%s: %v", tname, err) | ||
} | ||
srcr.Close() // Read-only | ||
mustClose(t, tname, dstw) | ||
|
||
// Read back dst to confirm our expectations | ||
dstr := reader(t, tname, &dstb, tt.dstType, fname) | ||
for _, want := range tt.in { | ||
var ( | ||
got file | ||
buf bytes.Buffer | ||
) | ||
got.name, _ = dstr.NextFile() | ||
mustCopy(t, tname, &buf, dstr) | ||
got.contents = buf.Bytes() | ||
|
||
switch { | ||
case got.name != want.name: | ||
t.Errorf("%s: got file %q but want file %q", | ||
tname, got.name, want.name) | ||
|
||
case !bytes.Equal(got.contents, want.contents): | ||
t.Errorf("%s: mismatched contents in %q: got %q but want %q", | ||
tname, got.name, got.contents, want.contents) | ||
} | ||
} | ||
dstr.Close() | ||
} | ||
} | ||
|
||
func writer(t *testing.T, tname string, w io.Writer, atype int) archive.WriteCloser { | ||
switch atype { | ||
case gzipType: | ||
return gzip.NewWriter(w) | ||
case tarType: | ||
return tar.NewWriter(w) | ||
case zipType: | ||
return zip.NewWriter(w) | ||
} | ||
t.Fatalf("%s: unrecognized archive type: %d", tname, atype) | ||
panic("execution continued after (*testing.T).Fatalf") | ||
} | ||
|
||
func reader(t *testing.T, tname string, buf *bytes.Buffer, atype int, fname string) archive.ReadCloser { | ||
switch atype { | ||
case gzipType: | ||
gr, err := gzip.NewReader(buf, fname) | ||
if err != nil { | ||
t.Fatalf("%s: %v", tname, err) | ||
} | ||
return gr | ||
case tarType: | ||
return archive.NopCloser(tar.NewReader(buf)) | ||
case zipType: | ||
zr, err := zip.NewReader( | ||
bytes.NewReader(buf.Bytes()), | ||
int64(buf.Len())) | ||
if err != nil { | ||
t.Fatalf("%s: new zip reader: %v", tname, err) | ||
} | ||
return archive.NopCloser(zr) | ||
} | ||
t.Fatalf("%s: unrecognized archive type: %d", tname, atype) | ||
panic("execution continued after (*testing.T).Fatalf") | ||
} | ||
|
||
func mustCopy(t *testing.T, tname string, dst io.Writer, src io.Reader) { | ||
if _, err := io.Copy(dst, src); err != nil { | ||
t.Fatalf("%s: copy: %v", tname, err) | ||
} | ||
} | ||
|
||
func mustClose(t *testing.T, tname string, c io.Closer) { | ||
if err := c.Close(); err != nil { | ||
t.Fatalf("%s: close: %v", tname, err) | ||
} | ||
} |
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
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
Oops, something went wrong.