1package template_test
2
3import (
4	"errors"
5
6	fakesys "github.com/cloudfoundry/bosh-utils/system/fakes"
7	. "github.com/onsi/ginkgo"
8	. "github.com/onsi/gomega"
9
10	. "github.com/cloudfoundry/bosh-cli/director/template"
11)
12
13var _ = Describe("VarFileArg", func() {
14	Describe("UnmarshalFlag", func() {
15		var (
16			fs  *fakesys.FakeFileSystem
17			arg VarFileArg
18		)
19
20		BeforeEach(func() {
21			fs = fakesys.NewFakeFileSystem()
22			arg = VarFileArg{FS: fs}
23		})
24
25		It("sets name and value from a file", func() {
26			fs.WriteFileString("/some/path", "val\nval")
27
28			err := (&arg).UnmarshalFlag("name=/some/path")
29			Expect(err).ToNot(HaveOccurred())
30			Expect(arg.Vars).To(Equal(StaticVariables{"name": "val\nval"}))
31		})
32
33		It("sets name and value when value contains a `=`", func() {
34			fs.WriteFileString("/some/path=", "val")
35
36			err := (&arg).UnmarshalFlag("name=/some/path=")
37			Expect(err).ToNot(HaveOccurred())
38			Expect(arg.Vars).To(Equal(StaticVariables{"name": "val"}))
39		})
40
41		It("returns error if string does not have 2 pieces", func() {
42			err := (&arg).UnmarshalFlag("val")
43			Expect(err).To(HaveOccurred())
44			Expect(err.Error()).To(Equal("Expected var 'val' to be in format 'name=path'"))
45		})
46
47		It("returns error if name is empty", func() {
48			err := (&arg).UnmarshalFlag("=val")
49			Expect(err).To(HaveOccurred())
50			Expect(err.Error()).To(Equal("Expected var '=val' to specify non-empty name"))
51		})
52
53		It("returns error if value is empty", func() {
54			err := (&arg).UnmarshalFlag("name=")
55			Expect(err).To(HaveOccurred())
56			Expect(err.Error()).To(Equal("Expected var 'name=' to specify non-empty path"))
57		})
58
59		It("returns an error if reading file fails", func() {
60			fs.WriteFileString("/some/path", "content")
61			fs.ReadFileError = errors.New("fake-err")
62
63			err := (&arg).UnmarshalFlag("var=/some/path")
64			Expect(err).To(HaveOccurred())
65			Expect(err.Error()).To(ContainSubstring("fake-err"))
66		})
67
68		It("returns an error if expanding path fails", func() {
69			fs.ExpandPathErr = errors.New("fake-err")
70
71			err := (&arg).UnmarshalFlag("var=/some/path")
72			Expect(err).To(HaveOccurred())
73			Expect(err.Error()).To(ContainSubstring("fake-err"))
74		})
75
76		It("returns an error when it's empty", func() {
77			err := (&arg).UnmarshalFlag("")
78			Expect(err).To(HaveOccurred())
79			Expect(err.Error()).To(Equal("Expected var '' to be in format 'name=path'"))
80		})
81	})
82})
83