github.com/xenophex/i18n4go@v0.2.7-0.20160907212557-40256cda157a/integration/rewrite_package/f_option_test.go (about) 1 package rewrite_package_test 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "strings" 8 9 . "github.com/XenoPhex/i18n4go/integration/test_helpers" 10 . "github.com/onsi/ginkgo" 11 . "github.com/onsi/gomega" 12 ) 13 14 var _ = Describe("rewrite-package -f filename", func() { 15 var ( 16 outputDir string 17 rootPath string 18 fixturesPath string 19 inputFilesPath string 20 expectedFilesPath string 21 ) 22 23 AfterEach(func() { 24 err := os.RemoveAll(outputDir) 25 Ω(err).ShouldNot(HaveOccurred()) 26 }) 27 28 Context("no -o option passed, so input file is rewritten", func() { 29 BeforeEach(func() { 30 dir, err := os.Getwd() 31 Ω(err).ShouldNot(HaveOccurred()) 32 rootPath = filepath.Join(dir, "..", "..") 33 34 outputDir, err = ioutil.TempDir(rootPath, "i18n4go_integration") 35 Ω(err).ShouldNot(HaveOccurred()) 36 37 fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package") 38 inputFilesPath = filepath.Join(fixturesPath, "f_option", "input_files") 39 expectedFilesPath = filepath.Join(fixturesPath, "f_option", "expected_output") 40 41 CopyFile(filepath.Join(inputFilesPath, "test.go"), filepath.Join(outputDir, "test.go")) 42 43 session := Runi18n("-c", "rewrite-package", 44 "-f", filepath.Join(outputDir, "test.go"), 45 "--root-path", outputDir, 46 "-v", 47 ) 48 49 Ω(session.ExitCode()).Should(Equal(0)) 50 }) 51 52 It("overwrites the input file with T() wrappers around strings", func() { 53 expectedOutputFile := filepath.Join(expectedFilesPath, "test.go") 54 bytes, err := ioutil.ReadFile(expectedOutputFile) 55 Ω(err).ShouldNot(HaveOccurred()) 56 57 expectedOutput := string(bytes) 58 59 generatedOutputFile := filepath.Join(outputDir, "test.go") 60 bytes, err = ioutil.ReadFile(generatedOutputFile) 61 Ω(err).ShouldNot(HaveOccurred()) 62 63 actualOutput := string(bytes) 64 65 Ω(actualOutput).Should(Equal(expectedOutput)) 66 }) 67 68 It("adds T func declaration and i18n init() func in the same directory as input file", func() { 69 initFile := filepath.Join(outputDir, "i18n_init.go") 70 expectedBytes, err := ioutil.ReadFile(initFile) 71 Ω(err).ShouldNot(HaveOccurred()) 72 Ω(expectedBytes).ShouldNot(Equal("")) 73 }) 74 }) 75 76 Context("all strings to rewrite are simple strings", func() { 77 BeforeEach(func() { 78 dir, err := os.Getwd() 79 Ω(err).ShouldNot(HaveOccurred()) 80 rootPath = filepath.Join(dir, "..", "..") 81 82 outputDir, err = ioutil.TempDir(rootPath, "i18n4go_integration") 83 Ω(err).ShouldNot(HaveOccurred()) 84 85 fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package") 86 inputFilesPath = filepath.Join(fixturesPath, "f_option", "input_files") 87 expectedFilesPath = filepath.Join(fixturesPath, "f_option", "expected_output") 88 89 session := Runi18n("-c", 90 "rewrite-package", 91 "-f", filepath.Join(inputFilesPath, "test.go"), 92 "-o", outputDir, 93 "--root-path", rootPath, 94 "-v", 95 ) 96 97 Ω(session.ExitCode()).Should(Equal(0)) 98 }) 99 100 It("rewrites the input file with T() wrappers around strings", func() { 101 expectedOutputFile := filepath.Join(expectedFilesPath, "test.go") 102 bytes, err := ioutil.ReadFile(expectedOutputFile) 103 Ω(err).ShouldNot(HaveOccurred()) 104 105 expectedOutput := string(bytes) 106 107 generatedOutputFile := filepath.Join(outputDir, "test.go") 108 bytes, err = ioutil.ReadFile(generatedOutputFile) 109 Ω(err).ShouldNot(HaveOccurred()) 110 111 actualOutput := string(bytes) 112 113 Ω(actualOutput).Should(Equal(expectedOutput)) 114 }) 115 116 It("adds T func declaration and i18n init() func", func() { 117 initFile := filepath.Join(outputDir, "i18n_init.go") 118 expectedBytes, err := ioutil.ReadFile(initFile) 119 Ω(err).ShouldNot(HaveOccurred()) 120 expected := strings.TrimSpace(string(expectedBytes)) 121 122 expectedInitFile := filepath.Join(expectedFilesPath, "i18n_init.go") 123 actualBytes, err := ioutil.ReadFile(expectedInitFile) 124 Ω(err).ShouldNot(HaveOccurred()) 125 actual := strings.TrimSpace(string(actualBytes)) 126 127 Ω(actual).Should(Equal(expected)) 128 }) 129 }) 130 131 Context("strings to rewrite contain complex templated strings", func() { 132 BeforeEach(func() { 133 dir, err := os.Getwd() 134 Ω(err).ShouldNot(HaveOccurred()) 135 rootPath = filepath.Join(dir, "..", "..") 136 137 outputDir, err = ioutil.TempDir(rootPath, "i18n4go_integration") 138 Ω(err).ShouldNot(HaveOccurred()) 139 140 fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package") 141 inputFilesPath = filepath.Join(fixturesPath, "f_option", "input_files") 142 expectedFilesPath = filepath.Join(fixturesPath, "f_option", "expected_output") 143 144 session := Runi18n("-c", 145 "rewrite-package", 146 "-f", filepath.Join(inputFilesPath, "test_templated_strings.go"), 147 "-o", filepath.Join(outputDir), 148 "-v", 149 ) 150 151 Ω(session.ExitCode()).Should(Equal(0)) 152 }) 153 154 It("rewrites the input file with T() wrappers around all (simple and templated) strings", func() { 155 expectedOutputFile := filepath.Join(expectedFilesPath, "test_templated_strings.go") 156 bytes, err := ioutil.ReadFile(expectedOutputFile) 157 Ω(err).ShouldNot(HaveOccurred()) 158 159 expectedOutput := string(bytes) 160 161 generatedOutputFile := filepath.Join(outputDir, "test_templated_strings.go") 162 bytes, err = ioutil.ReadFile(generatedOutputFile) 163 Ω(err).ShouldNot(HaveOccurred()) 164 165 actualOutput := string(bytes) 166 167 Ω(actualOutput).Should(Equal(expectedOutput)) 168 }) 169 }) 170 171 Context("strings to rewrite contain interpolated strings", func() { 172 BeforeEach(func() { 173 dir, err := os.Getwd() 174 Ω(err).ShouldNot(HaveOccurred()) 175 rootPath = filepath.Join(dir, "..", "..") 176 177 outputDir, err = ioutil.TempDir(rootPath, "i18n4go_integration") 178 Ω(err).ShouldNot(HaveOccurred()) 179 180 fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package") 181 inputFilesPath = filepath.Join(fixturesPath, "f_option", "input_files") 182 expectedFilesPath = filepath.Join(fixturesPath, "f_option", "expected_output") 183 184 session := Runi18n("-c", 185 "rewrite-package", 186 "-f", filepath.Join(inputFilesPath, "test_interpolated_strings.go"), 187 "-o", filepath.Join(outputDir), 188 "-v", 189 ) 190 191 Ω(session.ExitCode()).Should(Equal(0)) 192 }) 193 194 It("converts interpolated strings to templated and rewrites the input file with T() wrappers around all (simple and templated) strings", func() { 195 expectedOutputFile := filepath.Join(expectedFilesPath, "test_interpolated_strings.go") 196 bytes, err := ioutil.ReadFile(expectedOutputFile) 197 Ω(err).ShouldNot(HaveOccurred()) 198 199 expectedOutput := string(bytes) 200 201 generatedOutputFile := filepath.Join(outputDir, "test_interpolated_strings.go") 202 bytes, err = ioutil.ReadFile(generatedOutputFile) 203 Ω(err).ShouldNot(HaveOccurred()) 204 205 actualOutput := string(bytes) 206 207 Ω(actualOutput).Should(Equal(expectedOutput)) 208 }) 209 }) 210 })