github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/actor/v3action/v3action_suite_test.go (about)

     1  package v3action_test
     2  
     3  import (
     4  	"archive/zip"
     5  	"io"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  
    13  	"testing"
    14  )
    15  
    16  func TestV3Action(t *testing.T) {
    17  	RegisterFailHandler(Fail)
    18  	RunSpecs(t, "V3 Actions Suite")
    19  }
    20  
    21  // Thanks to Svett Ralchev
    22  // http://blog.ralch.com/tutorial/golang-working-with-zip/
    23  func zipit(source, target, prefix string) error {
    24  	zipfile, err := os.Create(target)
    25  	if err != nil {
    26  		return err
    27  	}
    28  	defer zipfile.Close()
    29  
    30  	if prefix != "" {
    31  		_, err = io.WriteString(zipfile, prefix)
    32  		if err != nil {
    33  			return err
    34  		}
    35  	}
    36  
    37  	archive := zip.NewWriter(zipfile)
    38  	defer archive.Close()
    39  
    40  	err = filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
    41  		if err != nil {
    42  			return err
    43  		}
    44  
    45  		header, err := zip.FileInfoHeader(info)
    46  		if err != nil {
    47  			return err
    48  		}
    49  
    50  		header.Name = strings.TrimPrefix(path, source)
    51  
    52  		if info.IsDir() {
    53  			header.Name += string(os.PathSeparator)
    54  		} else {
    55  			header.Method = zip.Deflate
    56  		}
    57  
    58  		writer, err := archive.CreateHeader(header)
    59  		if err != nil {
    60  			return err
    61  		}
    62  
    63  		if info.IsDir() {
    64  			return nil
    65  		}
    66  
    67  		file, err := os.Open(path)
    68  		if err != nil {
    69  			return err
    70  		}
    71  		defer file.Close()
    72  
    73  		_, err = io.Copy(writer, file)
    74  		return err
    75  	})
    76  
    77  	return err
    78  }