github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/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 "testing" 10 "time" 11 12 . "code.cloudfoundry.org/cli/actor/v7action" 13 "code.cloudfoundry.org/cli/actor/v7action/v7actionfakes" 14 "code.cloudfoundry.org/clock/fakeclock" 15 . "github.com/onsi/ginkgo" 16 . "github.com/onsi/gomega" 17 log "github.com/sirupsen/logrus" 18 ) 19 20 func TestV3Action(t *testing.T) { 21 RegisterFailHandler(Fail) 22 RunSpecs(t, "V7 Actions Suite") 23 } 24 25 var _ = BeforeEach(func() { 26 log.SetLevel(log.PanicLevel) 27 }) 28 29 func NewTestActor() (*Actor, *v7actionfakes.FakeCloudControllerClient, *v7actionfakes.FakeConfig, *v7actionfakes.FakeSharedActor, *v7actionfakes.FakeUAAClient, *v7actionfakes.FakeRoutingClient, *fakeclock.FakeClock) { 30 fakeCloudControllerClient := new(v7actionfakes.FakeCloudControllerClient) 31 fakeConfig := new(v7actionfakes.FakeConfig) 32 fakeSharedActor := new(v7actionfakes.FakeSharedActor) 33 fakeUAAClient := new(v7actionfakes.FakeUAAClient) 34 fakeRoutingClient := new(v7actionfakes.FakeRoutingClient) 35 fakeClock := fakeclock.NewFakeClock(time.Now()) 36 actor := NewActor(fakeCloudControllerClient, fakeConfig, fakeSharedActor, fakeUAAClient, fakeRoutingClient, fakeClock) 37 38 return actor, fakeCloudControllerClient, fakeConfig, fakeSharedActor, fakeUAAClient, fakeRoutingClient, fakeClock 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 }