github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/testing/testbase/patch_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package testbase_test 5 6 import ( 7 "errors" 8 "os" 9 10 gc "launchpad.net/gocheck" 11 12 "launchpad.net/juju-core/testing/testbase" 13 ) 14 15 type PatchValueSuite struct{} 16 17 var _ = gc.Suite(&PatchValueSuite{}) 18 19 func (*PatchValueSuite) TestSetInt(c *gc.C) { 20 i := 99 21 restore := testbase.PatchValue(&i, 88) 22 c.Assert(i, gc.Equals, 88) 23 restore() 24 c.Assert(i, gc.Equals, 99) 25 } 26 27 func (*PatchValueSuite) TestSetError(c *gc.C) { 28 oldErr := errors.New("foo") 29 newErr := errors.New("bar") 30 err := oldErr 31 restore := testbase.PatchValue(&err, newErr) 32 c.Assert(err, gc.Equals, newErr) 33 restore() 34 c.Assert(err, gc.Equals, oldErr) 35 } 36 37 func (*PatchValueSuite) TestSetErrorToNil(c *gc.C) { 38 oldErr := errors.New("foo") 39 err := oldErr 40 restore := testbase.PatchValue(&err, nil) 41 c.Assert(err, gc.Equals, nil) 42 restore() 43 c.Assert(err, gc.Equals, oldErr) 44 } 45 46 func (*PatchValueSuite) TestSetMapToNil(c *gc.C) { 47 oldMap := map[string]int{"foo": 1234} 48 m := oldMap 49 restore := testbase.PatchValue(&m, nil) 50 c.Assert(m, gc.IsNil) 51 restore() 52 c.Assert(m, gc.DeepEquals, oldMap) 53 } 54 55 func (*PatchValueSuite) TestSetPanicsWhenNotAssignable(c *gc.C) { 56 i := 99 57 type otherInt int 58 c.Assert(func() { testbase.PatchValue(&i, otherInt(88)) }, gc.PanicMatches, `reflect\.Set: value of type testbase_test\.otherInt is not assignable to type int`) 59 } 60 61 type PatchEnvironmentSuite struct{} 62 63 var _ = gc.Suite(&PatchEnvironmentSuite{}) 64 65 func (*PatchEnvironmentSuite) TestPatchEnvironment(c *gc.C) { 66 const envName = "TESTING_PATCH_ENVIRONMENT" 67 // remember the old value, and set it to something we can check 68 oldValue := os.Getenv(envName) 69 os.Setenv(envName, "initial") 70 restore := testbase.PatchEnvironment(envName, "new value") 71 // Using check to make sure the environment gets set back properly in the test. 72 c.Check(os.Getenv(envName), gc.Equals, "new value") 73 restore() 74 c.Check(os.Getenv(envName), gc.Equals, "initial") 75 os.Setenv(envName, oldValue) 76 } 77 78 func (*PatchEnvironmentSuite) TestRestorerAdd(c *gc.C) { 79 var order []string 80 first := testbase.Restorer(func() { order = append(order, "first") }) 81 second := testbase.Restorer(func() { order = append(order, "second") }) 82 restore := first.Add(second) 83 restore() 84 c.Assert(order, gc.DeepEquals, []string{"second", "first"}) 85 } 86 87 func (*PatchEnvironmentSuite) TestPatchEnvPathPrepend(c *gc.C) { 88 oldPath := os.Getenv("PATH") 89 dir := "/bin/bar" 90 91 // just in case something goes wrong 92 defer os.Setenv("PATH", oldPath) 93 94 restore := testbase.PatchEnvPathPrepend(dir) 95 96 expect := dir + string(os.PathListSeparator) + oldPath 97 c.Check(os.Getenv("PATH"), gc.Equals, expect) 98 restore() 99 c.Check(os.Getenv("PATH"), gc.Equals, oldPath) 100 }