This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 390
Add Read and Write methods to Archiver interface that accept io.Reader/io.Writer. #43
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,10 +13,14 @@ import ( | |
type Archiver interface { | ||
// Match checks supported files | ||
Match(filename string) bool | ||
// Make makes an archive. | ||
// Make makes an archive file on disk. | ||
Make(destination string, sources []string) error | ||
// Open extracts an archive. | ||
// Open extracts an archive file on disk. | ||
Open(source, destination string) error | ||
// Write writes an archive to a Writer. | ||
Write(output io.Writer, sources []string) error | ||
// Read reads an archive from a Reader. | ||
Read(input io.Reader, destination string) error | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. apicompat: breaking change members added There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good bot 🐕 |
||
} | ||
|
||
// SupportedFormats contains all supported archive formats | ||
|
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 |
---|---|---|
|
@@ -17,49 +17,83 @@ func TestArchiver(t *testing.T) { | |
if _, ok := ar.(rarFormat); ok { | ||
t.Skip("not supported") | ||
} | ||
symmetricTest(t, name, ar) | ||
testWriteRead(t, name, ar) | ||
testMakeOpen(t, name, ar) | ||
}) | ||
} | ||
} | ||
|
||
// symmetricTest performs a symmetric test by using ar.Make to make an archive | ||
// testWriteRead performs a symmetric test by using ar.Write to generate an archive | ||
// from the test corpus, then using ar.Read to extract the archive and comparing | ||
// the contents to ensure they are equal. | ||
func testWriteRead(t *testing.T, name string, ar Archiver) { | ||
buf := new(bytes.Buffer) | ||
tmp, err := ioutil.TempDir("", "archiver") | ||
if err != nil { | ||
t.Fatalf("[%s] %v", name, err) | ||
} | ||
defer os.RemoveAll(tmp) | ||
|
||
// Test creating archive | ||
err = ar.Write(buf, []string{"testdata"}) | ||
if err != nil { | ||
t.Fatalf("[%s] writing archive: didn't expect an error, but got: %v", name, err) | ||
} | ||
|
||
// Test extracting archive | ||
err = ar.Read(buf, tmp) | ||
if err != nil { | ||
t.Fatalf("[%s] reading archive: didn't expect an error, but got: %v", name, err) | ||
} | ||
|
||
// Check that what was extracted is what was compressed | ||
symmetricTest(t, name, tmp) | ||
} | ||
|
||
// testMakeOpen performs a symmetric test by using ar.Make to make an archive | ||
// from the test corpus, then using ar.Open to open the archive and comparing | ||
// the contents to ensure they are equal. | ||
func symmetricTest(t *testing.T, name string, ar Archiver) { | ||
func testMakeOpen(t *testing.T, name string, ar Archiver) { | ||
tmp, err := ioutil.TempDir("", "archiver") | ||
if err != nil { | ||
t.Fatal(err) | ||
t.Fatalf("[%s] %v", name, err) | ||
} | ||
defer os.RemoveAll(tmp) | ||
|
||
// Test creating archive | ||
outfile := filepath.Join(tmp, "test-"+name) | ||
err = ar.Make(outfile, []string{"testdata"}) | ||
if err != nil { | ||
t.Fatalf("making archive: didn't expect an error, but got: %v", err) | ||
t.Fatalf("[%s] making archive: didn't expect an error, but got: %v", name, err) | ||
} | ||
|
||
if !ar.Match(outfile) { | ||
t.Fatalf("identifying format should be 'true', but got 'false'") | ||
t.Fatalf("[%s] identifying format should be 'true', but got 'false'", name) | ||
} | ||
|
||
var expectedFileCount int | ||
filepath.Walk("testdata", func(fpath string, info os.FileInfo, err error) error { | ||
expectedFileCount++ | ||
return nil | ||
}) | ||
|
||
// Test extracting archive | ||
dest := filepath.Join(tmp, "extraction_test") | ||
os.Mkdir(dest, 0755) | ||
err = ar.Open(outfile, dest) | ||
if err != nil { | ||
t.Fatalf("extracting archive [%s -> %s]: didn't expect an error, but got: %v", outfile, dest, err) | ||
t.Fatalf("[%s] extracting archive [%s -> %s]: didn't expect an error, but got: %v", name, outfile, dest, err) | ||
} | ||
|
||
// Check that what was extracted is what was compressed | ||
symmetricTest(t, name, dest) | ||
} | ||
|
||
// symmetricTest compares the contents of a destination directory to the contents | ||
// of the test corpus and tests that they are equal. | ||
func symmetricTest(t *testing.T, name, dest string) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unparam: name is unused There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed by next commit. |
||
var expectedFileCount int | ||
filepath.Walk("testdata", func(fpath string, info os.FileInfo, err error) error { | ||
expectedFileCount++ | ||
return nil | ||
}) | ||
|
||
// If outputs equals inputs, we're good; traverse output files | ||
// and compare file names, file contents, and file count. | ||
|
||
var actualFileCount int | ||
filepath.Walk(dest, func(fpath string, info os.FileInfo, err error) error { | ||
if fpath == dest { | ||
|
@@ -69,51 +103,51 @@ func symmetricTest(t *testing.T, name string, ar Archiver) { | |
|
||
origPath, err := filepath.Rel(dest, fpath) | ||
if err != nil { | ||
t.Fatalf("%s: Error inducing original file path: %v", fpath, err) | ||
t.Fatalf("[%s] %s: Error inducing original file path: %v", name, fpath, err) | ||
} | ||
|
||
if info.IsDir() { | ||
// stat dir instead of read file | ||
_, err = os.Stat(origPath) | ||
if err != nil { | ||
t.Fatalf("%s: Couldn't stat original directory (%s): %v", | ||
t.Fatalf("[%s] %s: Couldn't stat original directory (%s): %v", name, | ||
fpath, origPath, err) | ||
} | ||
return nil | ||
} | ||
|
||
expectedFileInfo, err := os.Stat(origPath) | ||
if err != nil { | ||
t.Fatalf("%s: Error obtaining original file info: %v", fpath, err) | ||
t.Fatalf("[%s] %s: Error obtaining original file info: %v", name, fpath, err) | ||
} | ||
expected, err := ioutil.ReadFile(origPath) | ||
if err != nil { | ||
t.Fatalf("%s: Couldn't open original file (%s) from disk: %v", | ||
t.Fatalf("[%s] %s: Couldn't open original file (%s) from disk: %v", name, | ||
fpath, origPath, err) | ||
} | ||
|
||
actualFileInfo, err := os.Stat(fpath) | ||
if err != nil { | ||
t.Fatalf("%s: Error obtaining actual file info: %v", fpath, err) | ||
t.Fatalf("[%s] %s: Error obtaining actual file info: %v", name, fpath, err) | ||
} | ||
actual, err := ioutil.ReadFile(fpath) | ||
if err != nil { | ||
t.Fatalf("%s: Couldn't open new file from disk: %v", fpath, err) | ||
t.Fatalf("[%s] %s: Couldn't open new file from disk: %v", name, fpath, err) | ||
} | ||
|
||
if actualFileInfo.Mode() != expectedFileInfo.Mode() { | ||
t.Fatalf("%s: File mode differed between on disk and compressed", | ||
t.Fatalf("[%s] %s: File mode differed between on disk and compressed", name, | ||
expectedFileInfo.Mode().String()+" : "+actualFileInfo.Mode().String()) | ||
} | ||
if !bytes.Equal(expected, actual) { | ||
t.Fatalf("%s: File contents differed between on disk and compressed", origPath) | ||
t.Fatalf("[%s] %s: File contents differed between on disk and compressed", name, origPath) | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
if got, want := actualFileCount, expectedFileCount; got != want { | ||
t.Fatalf("Expected %d resulting files, got %d", want, got) | ||
t.Fatalf("[%s] Expected %d resulting files, got %d", name, want, got) | ||
} | ||
} | ||
|
||
|
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
apicompat: breaking change members added