launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/testing/testbase/cleanup_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 "os" 8 9 gc "launchpad.net/gocheck" 10 11 "launchpad.net/juju-core/testing/testbase" 12 ) 13 14 type cleanupSuite struct { 15 testbase.CleanupSuite 16 } 17 18 var _ = gc.Suite(&cleanupSuite{}) 19 20 func (s *cleanupSuite) TestTearDownSuiteEmpty(c *gc.C) { 21 // The suite stack is empty initially, check we can tear that down. 22 s.TearDownSuite(c) 23 s.SetUpSuite(c) 24 } 25 26 func (s *cleanupSuite) TestTearDownTestEmpty(c *gc.C) { 27 // The test stack is empty initially, check we can tear that down. 28 s.TearDownTest(c) 29 s.SetUpTest(c) 30 } 31 32 func (s *cleanupSuite) TestAddSuiteCleanup(c *gc.C) { 33 order := []string{} 34 s.AddSuiteCleanup(func(*gc.C) { 35 order = append(order, "first") 36 }) 37 s.AddSuiteCleanup(func(*gc.C) { 38 order = append(order, "second") 39 }) 40 41 s.TearDownSuite(c) 42 c.Assert(order, gc.DeepEquals, []string{"second", "first"}) 43 44 // SetUpSuite resets the cleanup stack, this stops the cleanup functions 45 // being called again. 46 s.SetUpSuite(c) 47 } 48 49 func (s *cleanupSuite) TestAddCleanup(c *gc.C) { 50 order := []string{} 51 s.AddCleanup(func(*gc.C) { 52 order = append(order, "first") 53 }) 54 s.AddCleanup(func(*gc.C) { 55 order = append(order, "second") 56 }) 57 58 s.TearDownTest(c) 59 c.Assert(order, gc.DeepEquals, []string{"second", "first"}) 60 61 // SetUpTest resets the cleanup stack, this stops the cleanup functions 62 // being called again. 63 s.SetUpTest(c) 64 } 65 66 func (s *cleanupSuite) TestPatchEnvironment(c *gc.C) { 67 const envName = "TESTING_PATCH_ENVIRONMENT" 68 // remember the old value, and set it to something we can check 69 oldValue := os.Getenv(envName) 70 os.Setenv(envName, "initial") 71 72 s.PatchEnvironment(envName, "new value") 73 // Using check to make sure the environment gets set back properly in the test. 74 c.Check(os.Getenv(envName), gc.Equals, "new value") 75 76 s.TearDownTest(c) 77 c.Check(os.Getenv(envName), gc.Equals, "initial") 78 79 // SetUpTest resets the cleanup stack, this stops the cleanup functions 80 // being called again. 81 s.SetUpTest(c) 82 // explicitly return the envName to the old value 83 os.Setenv(envName, oldValue) 84 } 85 86 func (s *cleanupSuite) TestPatchValueInt(c *gc.C) { 87 i := 42 88 s.PatchValue(&i, 0) 89 c.Assert(i, gc.Equals, 0) 90 91 s.TearDownTest(c) 92 c.Assert(i, gc.Equals, 42) 93 94 // SetUpTest resets the cleanup stack, this stops the cleanup functions 95 // being called again. 96 s.SetUpTest(c) 97 } 98 99 func (s *cleanupSuite) TestPatchValueFunction(c *gc.C) { 100 function := func() string { 101 return "original" 102 } 103 104 s.PatchValue(&function, func() string { 105 return "patched" 106 }) 107 c.Assert(function(), gc.Equals, "patched") 108 109 s.TearDownTest(c) 110 c.Assert(function(), gc.Equals, "original") 111 112 // SetUpTest resets the cleanup stack, this stops the cleanup functions 113 // being called again. 114 s.SetUpTest(c) 115 }