grim/convey

674d936a6785
ChangeLog the fixes from pr #25. Fixes #125, #126
/*
* Convey
* 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/>.
*/
package state
import (
"testing"
"github.com/aphistic/sweet"
junit "github.com/aphistic/sweet-junit"
. "github.com/onsi/gomega"
)
type stateSuite struct{}
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) TestMap(t sweet.T) {
mapEnv := func(st *State, val string) []string {
mapped, err := st.MapSlice([]string{val}, st.Environment)
Expect(err).To(BeNil())
return mapped
}
st1 := &State{}
st1.Environment = []string{"FOO=BAR"}
Expect(mapEnv(st1, "$X")).To(Equal([]string{"$X"}))
Expect(mapEnv(st1, "$FOO")).To(Equal([]string{"BAR"}))
st2 := st1.WrapWithExpandableEnv([]string{"X=A;B;C"}, []string{"X"}, ";")
Expect(mapEnv(st2, "$X")).To(Equal([]string{"A", "B", "C"}))
Expect(mapEnv(st2, "$FOO")).To(Equal([]string{"BAR"}))
st3 := st2.WrapWithExpandableEnv([]string{"BAR=B;A;R::B;A;Z", "FOO=SAME"}, []string{"BAR"}, "::")
Expect(mapEnv(st3, "$X")).To(Equal([]string{"A", "B", "C"}))
Expect(mapEnv(st3, "$FOO")).To(Equal([]string{"BAR"}))
Expect(mapEnv(st3, "$BAR")).To(Equal([]string{"B;A;R", "B;A;Z"}))
}
func (s *stateSuite) TestWrapParent(t sweet.T) {
st1 := &State{}
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 := &State{}
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) TestGetName(t sweet.T) {
Expect(getName("foo")).To(Equal(""))
Expect(getName("$FOO")).To(Equal("FOO"))
Expect(getName("${FOO}")).To(Equal("FOO"))
}
func (s *stateSuite) TestDetached(t sweet.T) {
st1 := New()
Expect(st1.GetDetached()).To(BeEmpty())
st1.MarkDetached("foo")
Expect(st1.GetDetached()).To(HaveLen(1))
Expect(st1.GetDetached()).To(ConsistOf([]string{"foo"}))
st2 := st1.WrapWithExpandableEnv(nil, nil, "")
st2.MarkDetached("bar")
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"}))
}