Unverified Commit 84b31908 authored by Tomasz Maczukin's avatar Tomasz Maczukin
Browse files

Move searching logic to helpers/archives package

parent ef27396c
......@@ -61,12 +61,6 @@ func (c *ArtifactsDownloaderCommand) Execute(context *cli.Context) {
logrus.Fatalln(err)
}
paths, err := archives.ListZipFile(file.Name())
if err != nil {
logrus.Fatalln(err)
}
warnIfTryingToExtractGitDirectory(paths)
// Extract artifacts file
err = archives.ExtractZipFile(file.Name())
if err != nil {
......
......@@ -30,8 +30,6 @@ func (c *ArtifactsUploaderCommand) createAndUpload() (bool, error) {
pr, pw := io.Pipe()
defer pr.Close()
warnIfTryingToArchiveGitDirectory(c.sortedFiles())
// Create the archive
go func() {
err := archives.CreateZipArchive(pw, c.sortedFiles())
......
......@@ -70,8 +70,6 @@ func (c *CacheArchiverCommand) Execute(*cli.Context) {
logrus.Fatalln(err)
}
warnIfTryingToArchiveGitDirectory(c.sortedFiles())
// Check if list of files changed
if !c.isFileChanged(c.File) {
logrus.Infoln("Archive is up to date!")
......
......@@ -83,13 +83,7 @@ func (c *CacheExtractorCommand) Execute(context *cli.Context) {
}
}
paths, err := archives.ListZipFile(c.File)
if err != nil {
logrus.Fatalln(err)
}
warnIfTryingToExtractGitDirectory(paths)
err = archives.ExtractZipFile(c.File)
err := archives.ExtractZipFile(c.File)
if err != nil && !os.IsNotExist(err) {
logrus.Fatalln(err)
}
......
......@@ -83,6 +83,8 @@ func createZipEntry(archive *zip.Writer, fileName string) error {
}
func CreateZipArchive(w io.Writer, fileNames []string) error {
warnIfTryingToArchiveGitDirectory(fileNames)
archive := zip.NewWriter(w)
defer archive.Close()
......
......@@ -83,13 +83,12 @@ func extractZipFile(file *zip.File) (err error) {
func ExtractZipArchive(archive *zip.Reader) error {
tracker := newPathErrorTracker()
paths := []string{}
for _, file := range archive.File {
if err := extractZipFile(file); tracker.actionable(err) {
logrus.Warningf("%s: %s (suppressing repeats)", file.Name, err)
}
}
for _, file := range archive.File {
// Update file permissions
if err := os.Chmod(file.Name, file.Mode().Perm()); tracker.actionable(err) {
logrus.Warningf("%s: %s (suppressing repeats)", file.Name, err)
......@@ -99,42 +98,21 @@ func ExtractZipArchive(archive *zip.Reader) error {
if err := processZipExtra(&file.FileHeader); tracker.actionable(err) {
logrus.Warningf("%s: %s (suppressing repeats)", file.Name, err)
}
}
return nil
}
type ZipArchiveLister struct {
paths []string
}
func (a *ZipArchiveLister) list(archive *zip.Reader) error {
for _, file := range archive.File {
a.paths = append(a.paths, file.Name)
paths = append(paths, file.Name)
}
warnIfTryingToExtractGitDirectory(paths)
return nil
}
func workOnZipArchive(fileName string, handler func(archive *zip.Reader) error) error {
func ExtractZipFile(fileName string) error {
archive, err := zip.OpenReader(fileName)
if err != nil {
return err
}
defer archive.Close()
return handler(&archive.Reader)
}
func ExtractZipFile(fileName string) error {
return workOnZipArchive(fileName, ExtractZipArchive)
}
func ListZipFile(fileName string) ([]string, error) {
al := &ZipArchiveLister{}
err := workOnZipArchive(fileName, al.list)
if err != nil {
return []string{}, err
}
return al.paths, nil
return ExtractZipArchive(&archive.Reader)
}
......@@ -21,7 +21,7 @@ func writeArchive(t *testing.T, w io.Writer) {
io.WriteString(testFile, "test file")
}
func onTemporaryArchive(t *testing.T, handler func(t *testing.T, tempFile *os.File)) {
func TestExtractZipFile(t *testing.T) {
tempFile, err := ioutil.TempFile("", "archive")
if !assert.NoError(t, err) {
return
......@@ -31,39 +31,24 @@ func onTemporaryArchive(t *testing.T, handler func(t *testing.T, tempFile *os.Fi
writeArchive(t, tempFile)
tempFile.Close()
handler(t, tempFile)
}
func TestExtractZipFile(t *testing.T) {
onTemporaryArchive(t, func(t *testing.T, tempFile *os.File) {
err := ExtractZipFile(tempFile.Name())
if !assert.NoError(t, err) {
return
}
err = ExtractZipFile(tempFile.Name())
if !assert.NoError(t, err) {
return
}
stat, err := os.Stat("temporary_file.txt")
assert.False(t, os.IsNotExist(err), "Expected temporary_file.txt to exist")
if !os.IsNotExist(err) {
assert.NoError(t, err)
}
stat, err := os.Stat("temporary_file.txt")
assert.False(t, os.IsNotExist(err), "Expected temporary_file.txt to exist")
if !os.IsNotExist(err) {
assert.NoError(t, err)
}
if stat != nil {
defer os.Remove("temporary_file.txt")
assert.Equal(t, int64(9), stat.Size())
}
})
if stat != nil {
defer os.Remove("temporary_file.txt")
assert.Equal(t, int64(9), stat.Size())
}
}
func TestExtractZipFileNotFound(t *testing.T) {
err := ExtractZipFile("non_existing_zip_file.zip")
assert.Error(t, err)
}
func TestListZipFile(t *testing.T) {
onTemporaryArchive(t, func(t *testing.T, tempFile *os.File) {
paths, err := ListZipFile(tempFile.Name())
assert.NoError(t, err)
assert.NotEmpty(t, paths)
assert.Contains(t, paths, "temporary_file.txt")
})
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment