* Copyright 2016-2017 Gary Kramlich <grim@reaperworld.com> * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. "github.com/aphistic/sweet" junit "github.com/aphistic/sweet-junit" . "github.com/onsi/gomega" func TestMain(m *testing.M) { RegisterFailHandler(sweet.GomegaFail) sweet.Run(m, func(s *sweet.S) { s.RegisterPlugin(junit.NewPlugin()) s.AddSuite(&stateSuite{}) func (s *stateSuite) TestMapSlice(t sweet.T) { st1.Environment = []string{"FOO=BAR"} Expect(mapEnv(st1, "$X")).To(ConsistOf([]string{"$X"})) Expect(mapEnv(st1, "$FOO")).To(ConsistOf([]string{"BAR"})) st2 := st1.WrapWithExpandableEnv([]string{"X=A;B;C"}, []string{"X"}, ";") Expect(mapEnv(st2, "$X")).To(ConsistOf([]string{"A", "B", "C"})) Expect(mapEnv(st2, "$FOO")).To(ConsistOf([]string{"BAR"})) st3 := st2.WrapWithExpandableEnv([]string{"BAR=B;A;R::B;A;Z", "FOO=SAME"}, []string{"BAR"}, "::") Expect(mapEnv(st3, "$X")).To(ConsistOf([]string{"A", "B", "C"})) Expect(mapEnv(st3, "$FOO")).To(ConsistOf([]string{"BAR"})) Expect(mapEnv(st3, "$BAR")).To(ConsistOf([]string{"B;A;R", "B;A;Z"})) func (s *stateSuite) TestMapSliceComplex(t sweet.T) { st.Environment = []string{"FOO=BAR"} st = st.WrapWithExpandableEnv([]string{"X=A;B;C", "Y=D;E;F"}, []string{"X", "Y"}, ";") Expect(mapEnv(st, "BONK")).To(ConsistOf([]string{"BONK"})) Expect(mapEnv(st, "$FOO")).To(ConsistOf([]string{"BAR"})) Expect(mapEnv(st, "$X")).To(ConsistOf([]string{"A", "B", "C"})) Expect(mapEnv(st, "$Y")).To(ConsistOf([]string{"D", "E", "F"})) Expect(mapEnv(st, "x${X}x")).To(ConsistOf([]string{"xAx", "xBx", "xCx"})) Expect(mapEnv(st, "x${Y}x")).To(ConsistOf([]string{"xDx", "xEx", "xFx"})) Expect(mapEnv(st, "$X/$Y")).To(ConsistOf([]string{ func (s *stateSuite) TestMapSliceIterative(t sweet.T) { st.Environment = []string{"FOO=BAR"} st = st.WrapWithExpandableEnv([]string{"X=A;x$Y", "Y=B;y$Z", "Z=C;D;E"}, []string{"X", "Y", "Z"}, ";") Expect(mapEnv(st, "$X")).To(ConsistOf([]string{"A", "xB", "xyC", "xyD", "xyE"})) func (s *stateSuite) TestMapSliceIndirect(t sweet.T) { st.Environment = []string{"FOO=BAR"} st = st.WrapWithExpandableEnv([]string{"X=$Y", "Y=A;$Z;C", "Z=B;$W", "W=D"}, []string{"X", "Y", "Z"}, ";") Expect(mapEnv(st, "$X")).To(ConsistOf([]string{"A", "B", "C", "D"})) func (s *stateSuite) TestMapSliceRecursive(t sweet.T) { st.Environment = []string{"FOO=BAR"} st = st.WrapWithExpandableEnv([]string{"X=A;x$Y", "Y=B;y$Z", "Z=C;D;$X"}, []string{"X", "Y", "Z"}, ";") _, err := st.MapSlice([]string{"$X"}, st.Environment) Expect(err).NotTo(BeNil()) Expect(err.Error()).To(ContainSubstring("hit limit")) func (e *stateSuite) TestGetNames(t sweet.T) { names := getNames("foo $bar ${baz} ${bar} $bonk_quux honk") Expect(names).To(HaveLen(4)) Expect(names).To(ConsistOf([]string{"bar", "baz", "bar", "bonk_quux"})) func (e *stateSuite) TestProduct(t sweet.T) { p := product(map[string][]string{ "a": []string{"1", "2", "3"}, Expect(p).To(ConsistOf([]map[string]string{ map[string]string{"a": "1", "b": "4", "c": "6"}, map[string]string{"a": "1", "b": "4", "c": "7"}, map[string]string{"a": "1", "b": "5", "c": "6"}, map[string]string{"a": "1", "b": "5", "c": "7"}, map[string]string{"a": "2", "b": "4", "c": "6"}, map[string]string{"a": "2", "b": "4", "c": "7"}, map[string]string{"a": "2", "b": "5", "c": "6"}, map[string]string{"a": "2", "b": "5", "c": "7"}, map[string]string{"a": "3", "b": "4", "c": "6"}, map[string]string{"a": "3", "b": "4", "c": "7"}, map[string]string{"a": "3", "b": "5", "c": "6"}, map[string]string{"a": "3", "b": "5", "c": "7"}, func (s *stateSuite) TestWrapParent(t sweet.T) { st1.WrapWithExpandableEnv(nil, nil, "") Expect(st1.parent).To(BeNil()) st2 := st1.WrapWithExpandableEnv(nil, nil, "") Expect(st2.parent).To(BeIdenticalTo(st1)) func (s *stateSuite) TestWrapWithExpandableEnvMap(t sweet.T) { st1.Environment = []string{"FOO=BAR", "BAR=BAZ"} Expect(st1.Environment).To(HaveLen(2)) Expect(st1.Environment).To(ConsistOf([]string{"FOO=BAR", "BAR=BAZ"})) st2 := st1.WrapWithExpandableEnv([]string{"FOO=BONK", "BAZ=BONK"}, nil, "") Expect(st2.Environment).To(HaveLen(3)) Expect(st2.Environment).To(ConsistOf([]string{"FOO=BAR", "BAR=BAZ", "BAZ=BONK"})) func (s *stateSuite) TestDetached(t sweet.T) { Expect(st1.GetDetached()).To(BeEmpty()) Expect(st1.GetDetached()).To(HaveLen(1)) Expect(st1.GetDetached()).To(ConsistOf([]string{"foo"})) st2 := st1.WrapWithExpandableEnv(nil, nil, "") Expect(st2.GetDetached()).To(HaveLen(2)) Expect(st2.GetDetached()).To(ConsistOf([]string{"foo", "bar"})) Expect(st1.GetDetached()).To(HaveLen(2)) Expect(st1.GetDetached()).To(ConsistOf([]string{"foo", "bar"})) func mapEnv(st *State, val string) []string { mapped, err := st.MapSlice([]string{val}, st.Environment)