github.com/craigmonson/colonize@v0.1.1-alpha.0.20170808202020-04bf903fb1ea/generate/branch/branch_test.go (about) 1 package branch_test 2 3 import ( 4 . "github.com/craigmonson/colonize/generate/branch" 5 . "github.com/onsi/ginkgo" 6 . "github.com/onsi/gomega" 7 8 "io/ioutil" 9 "os" 10 "path" 11 12 "github.com/craigmonson/colonize/config" 13 "github.com/craigmonson/colonize/log_mock" 14 "github.com/craigmonson/colonize/util" 15 ) 16 17 var test_dir string 18 var cwd string 19 var build_order *os.File 20 var err error 21 22 // Before we run tests, we create a temp dir and req files to work with 23 var _ = BeforeSuite(func() { 24 25 cwd, err = os.Getwd() 26 if err != nil { 27 panic(err.Error()) 28 } 29 30 test_dir, err = ioutil.TempDir("", "branch_test") 31 if err != nil { 32 panic(err.Error()) 33 } 34 35 err = os.Chdir(test_dir) 36 if err != nil { 37 panic(err.Error()) 38 } 39 40 os.Mkdir("env", 0777) 41 util.Touch(test_dir, "build_order.txt") 42 util.Touch(path.Join(test_dir, "env"), "dev.tfvars") 43 util.Touch(path.Join(test_dir, "env"), "prod.tfvars") 44 45 Run(&config.Config{ 46 RootPath: test_dir, 47 TmplPath: test_dir, 48 49 ConfigFile: config.ConfigFile{ 50 Environments_Dir: "env", 51 Branch_Order_File: "build_order.txt", 52 }, 53 }, &log_mock.MockLog{}, RunArgs{ 54 Name: "testbranch", 55 Leafs: []string{"leaf1", "leaf2"}, 56 }) 57 58 }) 59 60 // After all tests, cleanup 61 var _ = AfterSuite(func() { 62 os.Chdir(cwd) 63 build_order.Close() 64 os.RemoveAll(test_dir) 65 }) 66 67 var _ = Describe("generate/branch/branch", func() { 68 69 It("should have created a directory for the branch", func() { 70 _, err := os.Stat("../testbranch") 71 Ω(err).ShouldNot(HaveOccurred()) 72 }) 73 74 It("should have appended the branch name to the parent build order", func() { 75 contents, err := ioutil.ReadFile("../build_order.txt") 76 Ω(err).ShouldNot(HaveOccurred()) 77 Ω(string(contents)).Should(Equal("testbranch\n")) 78 }) 79 80 It("should have created an env directory in the branch", func() { 81 _, err := os.Stat("env") 82 Ω(err).ShouldNot(HaveOccurred()) 83 }) 84 85 It("should have created environment tfvars inside the env directory", func() { 86 _, err := os.Stat("env/dev.tfvars") 87 Ω(err).ShouldNot(HaveOccurred()) 88 _, err = os.Stat("env/prod.tfvars") 89 Ω(err).ShouldNot(HaveOccurred()) 90 }) 91 92 It("should have created leaf directories (leaf1,leaf2)", func() { 93 _, err := os.Stat("leaf1") 94 Ω(err).ShouldNot(HaveOccurred()) 95 _, err = os.Stat("leaf2") 96 Ω(err).ShouldNot(HaveOccurred()) 97 }) 98 99 It("should have listed leafs in build_order.txt", func() { 100 contents, err := ioutil.ReadFile("build_order.txt") 101 Ω(err).ShouldNot(HaveOccurred()) 102 Ω(string(contents)).Should(Equal("leaf1\nleaf2\n")) 103 }) 104 105 })