github.com/cloudfoundry/cli@v7.1.0+incompatible/actor/v7action/v7action_suite_test.go (about) 1 package v7action_test 2 3 import ( 4 "archive/zip" 5 "io" 6 "os" 7 "path/filepath" 8 "strings" 9 "time" 10 11 . "code.cloudfoundry.org/cli/actor/v7action" 12 "code.cloudfoundry.org/cli/actor/v7action/v7actionfakes" 13 "code.cloudfoundry.org/clock/fakeclock" 14 . "github.com/onsi/ginkgo" 15 . "github.com/onsi/gomega" 16 log "github.com/sirupsen/logrus" 17 18 "testing" 19 ) 20 21 func TestV3Action(t *testing.T) { 22 RegisterFailHandler(Fail) 23 RunSpecs(t, "V7 Actions Suite") 24 } 25 26 var _ = BeforeEach(func() { 27 log.SetLevel(log.PanicLevel) 28 }) 29 30 func NewTestActor() (*Actor, *v7actionfakes.FakeCloudControllerClient, *v7actionfakes.FakeConfig, *v7actionfakes.FakeSharedActor, *v7actionfakes.FakeUAAClient, *v7actionfakes.FakeRoutingClient, *fakeclock.FakeClock) { 31 fakeCloudControllerClient := new(v7actionfakes.FakeCloudControllerClient) 32 fakeConfig := new(v7actionfakes.FakeConfig) 33 fakeSharedActor := new(v7actionfakes.FakeSharedActor) 34 fakeUAAClient := new(v7actionfakes.FakeUAAClient) 35 fakeRoutingClient := new(v7actionfakes.FakeRoutingClient) 36 fakeClock := fakeclock.NewFakeClock(time.Now()) 37 actor := NewActor(fakeCloudControllerClient, fakeConfig, fakeSharedActor, fakeUAAClient, fakeRoutingClient, fakeClock) 38 39 return actor, fakeCloudControllerClient, fakeConfig, fakeSharedActor, fakeUAAClient, fakeRoutingClient, fakeClock 40 } 41 42 // Thanks to Svett Ralchev 43 // http://blog.ralch.com/tutorial/golang-working-with-zip/ 44 func zipit(source, target, prefix string) error { 45 zipfile, err := os.Create(target) 46 if err != nil { 47 return err 48 } 49 defer zipfile.Close() 50 51 if prefix != "" { 52 _, err = io.WriteString(zipfile, prefix) 53 if err != nil { 54 return err 55 } 56 } 57 58 archive := zip.NewWriter(zipfile) 59 defer archive.Close() 60 61 err = filepath.Walk(source, func(path string, info os.FileInfo, err error) error { 62 if err != nil { 63 return err 64 } 65 66 header, err := zip.FileInfoHeader(info) 67 if err != nil { 68 return err 69 } 70 71 header.Name = strings.TrimPrefix(path, source) 72 73 if info.IsDir() { 74 header.Name += string(os.PathSeparator) 75 } else { 76 header.Method = zip.Deflate 77 } 78 79 writer, err := archive.CreateHeader(header) 80 if err != nil { 81 return err 82 } 83 84 if info.IsDir() { 85 return nil 86 } 87 88 file, err := os.Open(path) 89 if err != nil { 90 return err 91 } 92 defer file.Close() 93 94 _, err = io.Copy(writer, file) 95 return err 96 }) 97 98 return err 99 }