github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/actor/v2action/v2action_suite_test.go (about)

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