- Notifications
You must be signed in to change notification settings - Fork1k
chore(coderd): extract fileszip to its own package for reuse#15229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
4 commits Select commitHold shift + click to select a range
554281c
chore(coderd): extract fileszip to its own package for reuse
johnstcn804d9e8
fixup! chore(coderd): extract fileszip to its own package for reuse
johnstcn33860ae
address PR comments
johnstcn2c1d3d6
export HTTPFileMaxBytes, reference in cli test
johnstcnFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
25 changes: 14 additions & 11 deletionscoderd/fileszip.go → archive/archive.go
This file contains hidden or 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
109 changes: 12 additions & 97 deletionscoderd/fileszip_test.go → archive/archive_test.go
This file contains hidden or 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
113 changes: 113 additions & 0 deletionsarchive/archivetest/archivetest.go
This file contains hidden or 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,113 @@ | ||
package archivetest | ||
import ( | ||
"archive/tar" | ||
"archive/zip" | ||
"bytes" | ||
_ "embed" | ||
"io" | ||
"testing" | ||
"time" | ||
"github.com/stretchr/testify/require" | ||
"golang.org/x/xerrors" | ||
) | ||
//go:embed testdata/test.tar | ||
var testTarFileBytes []byte | ||
//go:embed testdata/test.zip | ||
var testZipFileBytes []byte | ||
// TestTarFileBytes returns the content of testdata/test.tar | ||
func TestTarFileBytes() []byte { | ||
return append([]byte{}, testTarFileBytes...) | ||
} | ||
// TestZipFileBytes returns the content of testdata/test.zip | ||
func TestZipFileBytes() []byte { | ||
return append([]byte{}, testZipFileBytes...) | ||
} | ||
// AssertSampleTarfile compares the content of tarBytes against testdata/test.tar. | ||
func AssertSampleTarFile(t *testing.T, tarBytes []byte) { | ||
t.Helper() | ||
tr := tar.NewReader(bytes.NewReader(tarBytes)) | ||
for { | ||
hdr, err := tr.Next() | ||
if err != nil { | ||
if err == io.EOF { | ||
return | ||
} | ||
require.NoError(t, err) | ||
} | ||
// Note: ignoring timezones here. | ||
require.Equal(t, ArchiveRefTime(t).UTC(), hdr.ModTime.UTC()) | ||
switch hdr.Name { | ||
case "test/": | ||
require.Equal(t, hdr.Typeflag, byte(tar.TypeDir)) | ||
case "test/hello.txt": | ||
require.Equal(t, hdr.Typeflag, byte(tar.TypeReg)) | ||
bs, err := io.ReadAll(tr) | ||
if err != nil && !xerrors.Is(err, io.EOF) { | ||
require.NoError(t, err) | ||
} | ||
require.Equal(t, "hello", string(bs)) | ||
case "test/dir/": | ||
require.Equal(t, hdr.Typeflag, byte(tar.TypeDir)) | ||
case "test/dir/world.txt": | ||
require.Equal(t, hdr.Typeflag, byte(tar.TypeReg)) | ||
bs, err := io.ReadAll(tr) | ||
if err != nil && !xerrors.Is(err, io.EOF) { | ||
require.NoError(t, err) | ||
} | ||
require.Equal(t, "world", string(bs)) | ||
default: | ||
require.Failf(t, "unexpected file in tar", hdr.Name) | ||
} | ||
} | ||
} | ||
// AssertSampleZipFile compares the content of zipBytes against testdata/test.zip. | ||
func AssertSampleZipFile(t *testing.T, zipBytes []byte) { | ||
t.Helper() | ||
zr, err := zip.NewReader(bytes.NewReader(zipBytes), int64(len(zipBytes))) | ||
require.NoError(t, err) | ||
for _, f := range zr.File { | ||
// Note: ignoring timezones here. | ||
require.Equal(t, ArchiveRefTime(t).UTC(), f.Modified.UTC()) | ||
switch f.Name { | ||
case "test/", "test/dir/": | ||
// directory | ||
case "test/hello.txt": | ||
rc, err := f.Open() | ||
require.NoError(t, err) | ||
bs, err := io.ReadAll(rc) | ||
_ = rc.Close() | ||
require.NoError(t, err) | ||
require.Equal(t, "hello", string(bs)) | ||
case "test/dir/world.txt": | ||
rc, err := f.Open() | ||
require.NoError(t, err) | ||
bs, err := io.ReadAll(rc) | ||
_ = rc.Close() | ||
require.NoError(t, err) | ||
require.Equal(t, "world", string(bs)) | ||
default: | ||
require.Failf(t, "unexpected file in zip", f.Name) | ||
} | ||
} | ||
} | ||
// archiveRefTime is the Go reference time. The contents of the sample tar and zip files | ||
// in testdata/ all have their modtimes set to the below in some timezone. | ||
func ArchiveRefTime(t *testing.T) time.Time { | ||
locMST, err := time.LoadLocation("MST") | ||
require.NoError(t, err, "failed to load MST timezone") | ||
return time.Date(2006, 1, 2, 3, 4, 5, 0, locMST) | ||
} |
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletioncli/templatepull_test.go
This file contains hidden or 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.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.