github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/flagcontext/flag_content_helper_test.go (about) 1 package flagcontext_test 2 3 import ( 4 "fmt" 5 "os" 6 7 "code.cloudfoundry.org/cli/cf/flagcontext" 8 9 "code.cloudfoundry.org/gofileutils/fileutils" 10 . "github.com/onsi/ginkgo" 11 . "github.com/onsi/gomega" 12 ) 13 14 var _ = Describe("Flag Content Helpers", func() { 15 Describe("GetContentsFromOptionalFlagValue", func() { 16 It("returns an empty byte slice when given an empty string", func() { 17 bs, err := flagcontext.GetContentsFromOptionalFlagValue("") 18 Expect(err).NotTo(HaveOccurred()) 19 Expect(bs).To(Equal([]byte{})) 20 }) 21 22 It("returns bytes when given a file name prefixed with @", func() { 23 fileutils.TempFile("get-data-test", func(tmpFile *os.File, err error) { 24 fileData := `{"foo": "bar"}` 25 tmpFile.WriteString(fileData) 26 27 bs, err := flagcontext.GetContentsFromOptionalFlagValue("@" + tmpFile.Name()) 28 Expect(err).NotTo(HaveOccurred()) 29 Expect(bs).To(Equal([]byte(fileData))) 30 }) 31 }) 32 33 It("returns bytes when given a file name not prefixed with @", func() { 34 fileutils.TempFile("get-data-test", func(tmpFile *os.File, err error) { 35 fileData := `{"foo": "bar"}` 36 tmpFile.WriteString(fileData) 37 38 bs, err := flagcontext.GetContentsFromOptionalFlagValue(tmpFile.Name()) 39 Expect(err).NotTo(HaveOccurred()) 40 Expect(bs).To(Equal([]byte(fileData))) 41 }) 42 }) 43 44 It("returns bytes when given a file name not prefixed with @ and wrapped in double quotes", func() { 45 fileutils.TempFile("get-data-test", func(tmpFile *os.File, err error) { 46 Expect(err).NotTo(HaveOccurred()) 47 fileData := `{"foo": "bar"}` 48 tmpFile.WriteString(fileData) 49 50 bs, err := flagcontext.GetContentsFromOptionalFlagValue(fmt.Sprintf(`"%s"`, tmpFile.Name())) 51 Expect(err).NotTo(HaveOccurred()) 52 Expect(bs).To(Equal([]byte(fileData))) 53 }) 54 }) 55 56 It("returns bytes when given a file name prefixed with @ and wrapped in double quotes after the @", func() { 57 fileutils.TempFile("get-data-test", func(tmpFile *os.File, err error) { 58 Expect(err).NotTo(HaveOccurred()) 59 fileData := `{"foo": "bar"}` 60 tmpFile.WriteString(fileData) 61 62 bs, err := flagcontext.GetContentsFromOptionalFlagValue(fmt.Sprintf(`@"%s"`, tmpFile.Name())) 63 Expect(err).NotTo(HaveOccurred()) 64 Expect(bs).To(Equal([]byte(fileData))) 65 }) 66 }) 67 68 It("returns bytes when given something that isn't a file wrapped with single quotes", func() { 69 bs, err := flagcontext.GetContentsFromOptionalFlagValue(`'param1=value1¶m2=value2'`) 70 Expect(err).NotTo(HaveOccurred()) 71 Expect(bs).To(Equal([]byte("param1=value1¶m2=value2"))) 72 }) 73 74 It("returns bytes when given something that isn't a file wrapped with double quotes", func() { 75 bs, err := flagcontext.GetContentsFromOptionalFlagValue(`"param1=value1¶m2=value2"`) 76 Expect(err).NotTo(HaveOccurred()) 77 Expect(bs).To(Equal([]byte("param1=value1¶m2=value2"))) 78 }) 79 80 It("returns an error when it cannot read the file prefixed with @", func() { 81 _, err := flagcontext.GetContentsFromOptionalFlagValue("@nonexistent-file") 82 Expect(err).To(HaveOccurred()) 83 }) 84 }) 85 86 Describe("GetContentsFromFlagValue", func() { 87 It("returns an error when given an empty string", func() { 88 _, err := flagcontext.GetContentsFromFlagValue("") 89 Expect(err).To(HaveOccurred()) 90 }) 91 92 It("returns bytes when given a file name prefixed with @", func() { 93 fileutils.TempFile("get-data-test", func(tmpFile *os.File, err error) { 94 fileData := `{"foo": "bar"}` 95 tmpFile.WriteString(fileData) 96 97 bs, err := flagcontext.GetContentsFromFlagValue("@" + tmpFile.Name()) 98 Expect(err).NotTo(HaveOccurred()) 99 Expect(bs).To(Equal([]byte(fileData))) 100 }) 101 }) 102 103 It("returns bytes when given a file name not prefixed with @", func() { 104 fileutils.TempFile("get-data-test", func(tmpFile *os.File, err error) { 105 fileData := `{"foo": "bar"}` 106 tmpFile.WriteString(fileData) 107 108 bs, err := flagcontext.GetContentsFromFlagValue(tmpFile.Name()) 109 Expect(err).NotTo(HaveOccurred()) 110 Expect(bs).To(Equal([]byte(fileData))) 111 }) 112 }) 113 114 It("returns bytes when given a file name not prefixed with @ and wrapped in double quotes", func() { 115 fileutils.TempFile("get-data-test", func(tmpFile *os.File, err error) { 116 Expect(err).NotTo(HaveOccurred()) 117 fileData := `{"foo": "bar"}` 118 tmpFile.WriteString(fileData) 119 120 bs, err := flagcontext.GetContentsFromFlagValue(fmt.Sprintf(`"%s"`, tmpFile.Name())) 121 Expect(err).NotTo(HaveOccurred()) 122 Expect(bs).To(Equal([]byte(fileData))) 123 }) 124 }) 125 126 It("returns bytes when given a file name prefixed with @ and wrapped in double quotes after the @", func() { 127 fileutils.TempFile("get-data-test", func(tmpFile *os.File, err error) { 128 Expect(err).NotTo(HaveOccurred()) 129 fileData := `{"foo": "bar"}` 130 tmpFile.WriteString(fileData) 131 132 bs, err := flagcontext.GetContentsFromFlagValue(fmt.Sprintf(`@"%s"`, tmpFile.Name())) 133 Expect(err).NotTo(HaveOccurred()) 134 Expect(bs).To(Equal([]byte(fileData))) 135 }) 136 }) 137 138 It("returns bytes when given something that isn't a file wrapped with single quotes", func() { 139 bs, err := flagcontext.GetContentsFromFlagValue(`'param1=value1¶m2=value2'`) 140 Expect(err).NotTo(HaveOccurred()) 141 Expect(bs).To(Equal([]byte("param1=value1¶m2=value2"))) 142 }) 143 144 It("returns bytes when given something that isn't a file wrapped with double quotes", func() { 145 bs, err := flagcontext.GetContentsFromFlagValue(`"param1=value1¶m2=value2"`) 146 Expect(err).NotTo(HaveOccurred()) 147 Expect(bs).To(Equal([]byte("param1=value1¶m2=value2"))) 148 }) 149 150 It("returns an error when it cannot read the file prefixed with @", func() { 151 _, err := flagcontext.GetContentsFromFlagValue("@nonexistent-file") 152 Expect(err).To(HaveOccurred()) 153 }) 154 }) 155 })