github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+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  	. "code.cloudfoundry.org/cli/actor/v2action"
    19  	"code.cloudfoundry.org/cli/actor/v2action/v2actionfakes"
    20  )
    21  
    22  func TestV2Action(t *testing.T) {
    23  	RegisterFailHandler(Fail)
    24  	RunSpecs(t, "V2 Actions Suite")
    25  }
    26  
    27  var _ = BeforeEach(func() {
    28  	SetDefaultEventuallyTimeout(3 * time.Second)
    29  	log.SetLevel(log.PanicLevel)
    30  })
    31  
    32  func NewTestActor() (*Actor, *v2actionfakes.FakeCloudControllerClient, *v2actionfakes.FakeUAAClient, *v2actionfakes.FakeConfig) {
    33  	fakeCloudControllerClient := new(v2actionfakes.FakeCloudControllerClient)
    34  	fakeUAAClient := new(v2actionfakes.FakeUAAClient)
    35  	fakeConfig := new(v2actionfakes.FakeConfig)
    36  	actor := NewActor(fakeCloudControllerClient, fakeUAAClient, fakeConfig)
    37  
    38  	return actor, fakeCloudControllerClient, fakeUAAClient, fakeConfig
    39  }
    40  
    41  // Thanks to Svett Ralchev
    42  // http://blog.ralch.com/tutorial/golang-working-with-zip/
    43  func zipit(source, target, prefix string) error {
    44  	zipfile, err := os.Create(target)
    45  	if err != nil {
    46  		return err
    47  	}
    48  	defer zipfile.Close()
    49  
    50  	if prefix != "" {
    51  		_, err = io.WriteString(zipfile, prefix)
    52  		if err != nil {
    53  			return err
    54  		}
    55  	}
    56  
    57  	archive := zip.NewWriter(zipfile)
    58  	defer archive.Close()
    59  
    60  	err = filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
    61  		if err != nil {
    62  			return err
    63  		}
    64  
    65  		header, err := zip.FileInfoHeader(info)
    66  		if err != nil {
    67  			return err
    68  		}
    69  
    70  		header.Name = strings.TrimPrefix(path, source)
    71  
    72  		if info.IsDir() {
    73  			header.Name += string(os.PathSeparator)
    74  		} else {
    75  			header.Method = zip.Deflate
    76  		}
    77  
    78  		writer, err := archive.CreateHeader(header)
    79  		if err != nil {
    80  			return err
    81  		}
    82  
    83  		if info.IsDir() {
    84  			return nil
    85  		}
    86  
    87  		file, err := os.Open(path)
    88  		if err != nil {
    89  			return err
    90  		}
    91  		defer file.Close()
    92  
    93  		_, err = io.Copy(writer, file)
    94  		return err
    95  	})
    96  
    97  	return err
    98  }