1package cmd_test
2
3import (
4	"errors"
5
6	fakesys "github.com/cloudfoundry/bosh-utils/system/fakes"
7	"github.com/cppforlife/go-patch/patch"
8	. "github.com/onsi/ginkgo"
9	. "github.com/onsi/gomega"
10
11	. "github.com/cloudfoundry/bosh-cli/cmd"
12)
13
14var _ = Describe("OpsFileArg", func() {
15	Describe("UnmarshalFlag", func() {
16		var (
17			fs  *fakesys.FakeFileSystem
18			arg OpsFileArg
19		)
20
21		BeforeEach(func() {
22			fs = fakesys.NewFakeFileSystem()
23			arg = OpsFileArg{FS: fs}
24		})
25
26		It("sets read operations", func() {
27			fs.WriteFileString("/some/path", `
28- type: remove
29  path: /a
30- type: remove
31  path: /b
32`)
33
34			err := (&arg).UnmarshalFlag("/some/path")
35			Expect(err).ToNot(HaveOccurred())
36
37			Expect(arg.Ops).To(Equal(patch.Ops{
38				patch.RemoveOp{Path: patch.MustNewPointerFromString("/a")},
39				patch.RemoveOp{Path: patch.MustNewPointerFromString("/b")},
40			}))
41		})
42
43		It("returns an error if operations are not valid", func() {
44			fs.WriteFileString("/some/path", "- type: unknown")
45
46			err := (&arg).UnmarshalFlag("/some/path")
47			Expect(err).To(HaveOccurred())
48			Expect(err.Error()).To(Equal(`Building ops: Unknown operation [0] with type 'unknown' within
49{
50  "Type": "unknown"
51}`))
52		})
53
54		It("returns an error if reading file fails", func() {
55			fs.WriteFileString("/some/path", "content")
56			fs.ReadFileError = errors.New("fake-err")
57
58			err := (&arg).UnmarshalFlag("/some/path")
59			Expect(err).To(HaveOccurred())
60			Expect(err.Error()).To(ContainSubstring("fake-err"))
61		})
62
63		It("returns an error if parsing file fails", func() {
64			fs.WriteFileString("/some/path", "content")
65
66			err := (&arg).UnmarshalFlag("/some/path")
67			Expect(err).To(HaveOccurred())
68			Expect(err.Error()).To(ContainSubstring("Deserializing ops file '/some/path'"))
69		})
70
71		It("returns an error when it's empty", func() {
72			err := (&arg).UnmarshalFlag("")
73			Expect(err).To(HaveOccurred())
74			Expect(err.Error()).To(Equal("Expected file path to be non-empty"))
75		})
76	})
77})
78