github.com/CharukaK/i18n4go@v0.6.0/integration/rewrite_package/i18n_strings_filename_test.go (about) 1 // Copyright © 2015-2023 The Knative Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package rewrite_package_test 16 17 import ( 18 "io/ioutil" 19 "os" 20 "path/filepath" 21 22 . "github.com/CharukaK/i18n4go/integration/test_helpers" 23 . "github.com/onsi/ginkgo" 24 . "github.com/onsi/gomega" 25 ) 26 27 var _ = Describe("rewrite-package --i18n-strings-filename some-file", func() { 28 var ( 29 outputDir string 30 rootPath string 31 fixturesPath string 32 inputFilesPath string 33 expectedFilesPath string 34 ) 35 36 AfterEach(func() { 37 err := os.RemoveAll(outputDir) 38 Ω(err).ShouldNot(HaveOccurred()) 39 }) 40 41 Context("Using legacy commands", func() { 42 43 Context("input file only contains simple strings", func() { 44 BeforeEach(func() { 45 dir, err := os.Getwd() 46 Ω(err).ShouldNot(HaveOccurred()) 47 rootPath = filepath.Join(dir, "..", "..") 48 49 outputDir, err = ioutil.TempDir(rootPath, "i18n4go_integration") 50 Ω(err).ShouldNot(HaveOccurred()) 51 52 fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package") 53 inputFilesPath = filepath.Join(fixturesPath, "i18n_strings_filename_option", "input_files") 54 expectedFilesPath = filepath.Join(fixturesPath, "i18n_strings_filename_option", "expected_output") 55 56 session := Runi18n("-c", 57 "rewrite-package", 58 "-f", filepath.Join(inputFilesPath, "test.go"), 59 "-o", outputDir, 60 "--i18n-strings-filename", filepath.Join(inputFilesPath, "strings.json"), 61 "-v", 62 ) 63 64 Ω(session.ExitCode()).Should(Equal(0)) 65 }) 66 67 It("rewrites the input file with T() wrappers around the strings specified in the --i18n-strings-filename flag", func() { 68 expectedOutputFile := filepath.Join(expectedFilesPath, "test.go") 69 bytes, err := ioutil.ReadFile(expectedOutputFile) 70 Ω(err).ShouldNot(HaveOccurred()) 71 72 expectedOutput := string(bytes) 73 74 generatedOutputFile := filepath.Join(outputDir, "test.go") 75 bytes, err = ioutil.ReadFile(generatedOutputFile) 76 Ω(err).ShouldNot(HaveOccurred()) 77 78 actualOutput := string(bytes) 79 80 Ω(actualOutput).Should(Equal(expectedOutput)) 81 }) 82 }) 83 84 Context("input file contains some templated strings", func() { 85 BeforeEach(func() { 86 dir, err := os.Getwd() 87 Ω(err).ShouldNot(HaveOccurred()) 88 rootPath = filepath.Join(dir, "..", "..") 89 90 outputDir, err = ioutil.TempDir(rootPath, "i18n4go_integration") 91 Ω(err).ShouldNot(HaveOccurred()) 92 93 fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package") 94 inputFilesPath = filepath.Join(fixturesPath, "i18n_strings_filename_option", "input_files") 95 expectedFilesPath = filepath.Join(fixturesPath, "i18n_strings_filename_option", "expected_output") 96 97 session := Runi18n("-c", 98 "rewrite-package", 99 "-f", filepath.Join(inputFilesPath, "test_templated_strings.go"), 100 "-o", outputDir, 101 "--i18n-strings-filename", filepath.Join(inputFilesPath, "test_templated_strings.go.en.json"), 102 "-v", 103 ) 104 105 Ω(session.ExitCode()).Should(Equal(0)) 106 }) 107 108 It("rewrites the input file with T() wrappers around the strings (templated and not) specified in the --i18n-strings-filename flag", func() { 109 expectedOutputFile := filepath.Join(expectedFilesPath, "test_templated_strings.go") 110 bytes, err := ioutil.ReadFile(expectedOutputFile) 111 Ω(err).ShouldNot(HaveOccurred()) 112 113 expectedOutput := string(bytes) 114 115 generatedOutputFile := filepath.Join(outputDir, "test_templated_strings.go") 116 bytes, err = ioutil.ReadFile(generatedOutputFile) 117 Ω(err).ShouldNot(HaveOccurred()) 118 119 actualOutput := string(bytes) 120 121 Ω(actualOutput).Should(Equal(expectedOutput)) 122 }) 123 }) 124 125 Context("input file contains some interpolated strings", func() { 126 BeforeEach(func() { 127 dir, err := os.Getwd() 128 Ω(err).ShouldNot(HaveOccurred()) 129 rootPath = filepath.Join(dir, "..", "..") 130 131 outputDir, err = ioutil.TempDir(rootPath, "i18n4go_integration") 132 Ω(err).ShouldNot(HaveOccurred()) 133 134 fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package") 135 inputFilesPath = filepath.Join(fixturesPath, "i18n_strings_filename_option", "input_files") 136 expectedFilesPath = filepath.Join(fixturesPath, "i18n_strings_filename_option", "expected_output") 137 138 CopyFile(filepath.Join(inputFilesPath, "_test_interpolated_strings.go.en.json"), filepath.Join(inputFilesPath, "test_interpolated_strings.go.en.json")) 139 140 session := Runi18n("-c", 141 "rewrite-package", 142 "-f", filepath.Join(inputFilesPath, "test_interpolated_strings.go"), 143 "-o", outputDir, 144 "--i18n-strings-filename", filepath.Join(inputFilesPath, "test_interpolated_strings.go.en.json"), 145 "-v", 146 ) 147 148 Ω(session.ExitCode()).Should(Equal(0)) 149 }) 150 151 AfterEach(func() { 152 CopyFile(filepath.Join(inputFilesPath, "_test_interpolated_strings.go.en.json"), filepath.Join(inputFilesPath, "test_interpolated_strings.go.en.json")) 153 }) 154 155 It("converts interpolated strings to templated and rewrites the input file with T() wrappers around the strings (templated and not) specified in the --i18n-strings-filename flag", func() { 156 expectedOutputFile := filepath.Join(expectedFilesPath, "test_interpolated_strings.go") 157 bytes, err := ioutil.ReadFile(expectedOutputFile) 158 Ω(err).ShouldNot(HaveOccurred()) 159 160 expectedOutput := string(bytes) 161 162 generatedOutputFile := filepath.Join(outputDir, "test_interpolated_strings.go") 163 bytes, err = ioutil.ReadFile(generatedOutputFile) 164 Ω(err).ShouldNot(HaveOccurred()) 165 166 actualOutput := string(bytes) 167 168 Ω(actualOutput).Should(Equal(expectedOutput)) 169 }) 170 171 It("updates the i18n strings JSON file with the converted interpolated JSON strings", func() { 172 expectedOutputFile := filepath.Join(expectedFilesPath, "test_interpolated_strings.go.en.json") 173 generatedOutputFile := filepath.Join(inputFilesPath, "test_interpolated_strings.go.en.json") 174 CompareExpectedToGeneratedTraslationJson(expectedOutputFile, generatedOutputFile) 175 }) 176 }) 177 178 }) 179 180 Context("Using cobra commands", func() { 181 182 Context("input file only contains simple strings", func() { 183 BeforeEach(func() { 184 dir, err := os.Getwd() 185 Ω(err).ShouldNot(HaveOccurred()) 186 rootPath = filepath.Join(dir, "..", "..") 187 188 outputDir, err = ioutil.TempDir(rootPath, "i18n4go_integration") 189 Ω(err).ShouldNot(HaveOccurred()) 190 191 fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package") 192 inputFilesPath = filepath.Join(fixturesPath, "i18n_strings_filename_option", "input_files") 193 expectedFilesPath = filepath.Join(fixturesPath, "i18n_strings_filename_option", "expected_output") 194 195 session := Runi18n("rewrite-package", 196 "-f", filepath.Join(inputFilesPath, "test.go"), 197 "-o", outputDir, 198 "--i18n-strings-filename", filepath.Join(inputFilesPath, "strings.json"), 199 "-v", 200 ) 201 202 Ω(session.ExitCode()).Should(Equal(0)) 203 }) 204 205 It("rewrites the input file with T() wrappers around the strings specified in the --i18n-strings-filename flag", func() { 206 expectedOutputFile := filepath.Join(expectedFilesPath, "test.go") 207 bytes, err := ioutil.ReadFile(expectedOutputFile) 208 Ω(err).ShouldNot(HaveOccurred()) 209 210 expectedOutput := string(bytes) 211 212 generatedOutputFile := filepath.Join(outputDir, "test.go") 213 bytes, err = ioutil.ReadFile(generatedOutputFile) 214 Ω(err).ShouldNot(HaveOccurred()) 215 216 actualOutput := string(bytes) 217 218 Ω(actualOutput).Should(Equal(expectedOutput)) 219 }) 220 }) 221 222 Context("input file contains some templated strings", func() { 223 BeforeEach(func() { 224 dir, err := os.Getwd() 225 Ω(err).ShouldNot(HaveOccurred()) 226 rootPath = filepath.Join(dir, "..", "..") 227 228 outputDir, err = ioutil.TempDir(rootPath, "i18n4go_integration") 229 Ω(err).ShouldNot(HaveOccurred()) 230 231 fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package") 232 inputFilesPath = filepath.Join(fixturesPath, "i18n_strings_filename_option", "input_files") 233 expectedFilesPath = filepath.Join(fixturesPath, "i18n_strings_filename_option", "expected_output") 234 235 session := Runi18n("-c", 236 "rewrite-package", 237 "-f", filepath.Join(inputFilesPath, "test_templated_strings.go"), 238 "-o", outputDir, 239 "--i18n-strings-filename", filepath.Join(inputFilesPath, "test_templated_strings.go.en.json"), 240 "-v", 241 ) 242 243 Ω(session.ExitCode()).Should(Equal(0)) 244 }) 245 246 It("rewrites the input file with T() wrappers around the strings (templated and not) specified in the --i18n-strings-filename flag", func() { 247 expectedOutputFile := filepath.Join(expectedFilesPath, "test_templated_strings.go") 248 bytes, err := ioutil.ReadFile(expectedOutputFile) 249 Ω(err).ShouldNot(HaveOccurred()) 250 251 expectedOutput := string(bytes) 252 253 generatedOutputFile := filepath.Join(outputDir, "test_templated_strings.go") 254 bytes, err = ioutil.ReadFile(generatedOutputFile) 255 Ω(err).ShouldNot(HaveOccurred()) 256 257 actualOutput := string(bytes) 258 259 Ω(actualOutput).Should(Equal(expectedOutput)) 260 }) 261 }) 262 263 Context("input file contains some interpolated strings", func() { 264 BeforeEach(func() { 265 dir, err := os.Getwd() 266 Ω(err).ShouldNot(HaveOccurred()) 267 rootPath = filepath.Join(dir, "..", "..") 268 269 outputDir, err = ioutil.TempDir(rootPath, "i18n4go_integration") 270 Ω(err).ShouldNot(HaveOccurred()) 271 272 fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package") 273 inputFilesPath = filepath.Join(fixturesPath, "i18n_strings_filename_option", "input_files") 274 expectedFilesPath = filepath.Join(fixturesPath, "i18n_strings_filename_option", "expected_output") 275 276 CopyFile(filepath.Join(inputFilesPath, "_test_interpolated_strings.go.en.json"), filepath.Join(inputFilesPath, "test_interpolated_strings.go.en.json")) 277 278 session := Runi18n("rewrite-package", 279 "-f", filepath.Join(inputFilesPath, "test_interpolated_strings.go"), 280 "-o", outputDir, 281 "--i18n-strings-filename", filepath.Join(inputFilesPath, "test_interpolated_strings.go.en.json"), 282 "-v", 283 ) 284 285 Ω(session.ExitCode()).Should(Equal(0)) 286 }) 287 288 AfterEach(func() { 289 CopyFile(filepath.Join(inputFilesPath, "_test_interpolated_strings.go.en.json"), filepath.Join(inputFilesPath, "test_interpolated_strings.go.en.json")) 290 }) 291 292 It("converts interpolated strings to templated and rewrites the input file with T() wrappers around the strings (templated and not) specified in the --i18n-strings-filename flag", func() { 293 expectedOutputFile := filepath.Join(expectedFilesPath, "test_interpolated_strings.go") 294 bytes, err := ioutil.ReadFile(expectedOutputFile) 295 Ω(err).ShouldNot(HaveOccurred()) 296 297 expectedOutput := string(bytes) 298 299 generatedOutputFile := filepath.Join(outputDir, "test_interpolated_strings.go") 300 bytes, err = ioutil.ReadFile(generatedOutputFile) 301 Ω(err).ShouldNot(HaveOccurred()) 302 303 actualOutput := string(bytes) 304 305 Ω(actualOutput).Should(Equal(expectedOutput)) 306 }) 307 308 It("updates the i18n strings JSON file with the converted interpolated JSON strings", func() { 309 expectedOutputFile := filepath.Join(expectedFilesPath, "test_interpolated_strings.go.en.json") 310 generatedOutputFile := filepath.Join(inputFilesPath, "test_interpolated_strings.go.en.json") 311 CompareExpectedToGeneratedTraslationJson(expectedOutputFile, generatedOutputFile) 312 }) 313 }) 314 315 }) 316 })