github.com/CharukaK/i18n4go@v0.6.0/integration/merge_strings/d_option_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 merge_strings_test 16 17 import ( 18 "encoding/json" 19 "io/ioutil" 20 "os" 21 "path/filepath" 22 23 "github.com/CharukaK/i18n4go/i18n4go/common" 24 . "github.com/CharukaK/i18n4go/integration/test_helpers" 25 . "github.com/onsi/ginkgo" 26 . "github.com/onsi/gomega" 27 ) 28 29 var _ = Describe("merge-strings -d dirName", func() { 30 var ( 31 fixturesPath string 32 inputFilesPath string 33 expectedFilesPath string 34 ) 35 36 BeforeEach(func() { 37 _, err := os.Getwd() 38 Ω(err).ShouldNot(HaveOccurred()) 39 40 fixturesPath = filepath.Join("..", "..", "test_fixtures", "merge_strings") 41 inputFilesPath = filepath.Join(fixturesPath, "d_option", "input_files") 42 expectedFilesPath = filepath.Join(fixturesPath, "d_option", "expected_output") 43 }) 44 45 Context("Using legacy commands", func() { 46 Context("can combine multiple language files", func() { 47 Context("merging en files in input_files path", func() { 48 BeforeEach(func() { 49 session := Runi18n("-c", "merge-strings", "-v", "-d", filepath.Join(inputFilesPath), "--source-language", "en") 50 Ω(session.ExitCode()).Should(Equal(0)) 51 }) 52 53 AfterEach(func() { 54 RemoveAllFiles( 55 GetFilePath(inputFilesPath, "all.en.json"), 56 ) 57 }) 58 59 It("creates an all.en.json that contains translations from both files", func() { 60 CompareExpectedToGeneratedTraslationJson( 61 GetFilePath(expectedFilesPath, "all.en.json"), 62 GetFilePath(inputFilesPath, "all.en.json"), 63 ) 64 }) 65 66 It("creates an all.en.json for which the translation strings order are stable", func() { 67 expectedFilePath := GetFilePath(expectedFilesPath, "all.en.json") 68 actualFilePath := GetFilePath(inputFilesPath, "all.en.json") 69 70 expectedBytes, err := ioutil.ReadFile(expectedFilePath) 71 Ω(err).Should(BeNil()) 72 Ω(expectedBytes).ShouldNot(BeNil()) 73 74 actualBytes, err := ioutil.ReadFile(actualFilePath) 75 Ω(err).Should(BeNil()) 76 Ω(actualBytes).ShouldNot(BeNil()) 77 78 expectedTranslations := []common.I18nStringInfo{} 79 err = json.Unmarshal(expectedBytes, &expectedTranslations) 80 Ω(err).Should(BeNil()) 81 82 actualTranslations := []common.I18nStringInfo{} 83 err = json.Unmarshal(actualBytes, &actualTranslations) 84 Ω(err).Should(BeNil()) 85 86 Ω(len(actualTranslations)).Should(Equal(len(expectedTranslations))) 87 for idx := range actualTranslations { 88 Ω(actualTranslations[idx].ID).Should(Equal(expectedTranslations[idx].ID)) 89 Ω(actualTranslations[idx].Translation).Should(Equal(expectedTranslations[idx].Translation)) 90 } 91 92 }) 93 }) 94 95 Context("merging en files in input_files/reordered path", func() { 96 BeforeEach(func() { 97 session := Runi18n("-c", "merge-strings", "-v", "-d", filepath.Join(inputFilesPath, "reordered"), "--source-language", "en") 98 Ω(session.ExitCode()).Should(Equal(0)) 99 }) 100 101 AfterEach(func() { 102 RemoveAllFiles( 103 GetFilePath(filepath.Join(inputFilesPath, "reordered"), "all.en.json"), 104 ) 105 }) 106 107 It("creates an all.en.json keeping the stable order", func() { 108 expectedFilePath := GetFilePath(expectedFilesPath, "all.en.json") 109 actualFilePath := GetFilePath(filepath.Join(inputFilesPath, "reordered"), "all.en.json") 110 111 expectedBytes, err := ioutil.ReadFile(expectedFilePath) 112 Ω(err).Should(BeNil()) 113 Ω(expectedBytes).ShouldNot(BeNil()) 114 115 actualBytes, err := ioutil.ReadFile(actualFilePath) 116 Ω(err).Should(BeNil()) 117 Ω(actualBytes).ShouldNot(BeNil()) 118 119 expectedTranslations := []common.I18nStringInfo{} 120 err = json.Unmarshal(expectedBytes, &expectedTranslations) 121 Ω(err).Should(BeNil()) 122 123 actualTranslations := []common.I18nStringInfo{} 124 err = json.Unmarshal(actualBytes, &actualTranslations) 125 Ω(err).Should(BeNil()) 126 127 Ω(len(actualTranslations)).Should(Equal(len(expectedTranslations))) 128 for idx := range actualTranslations { 129 Ω(actualTranslations[idx].ID).Should(Equal(expectedTranslations[idx].ID)) 130 Ω(actualTranslations[idx].Translation).Should(Equal(expectedTranslations[idx].Translation)) 131 } 132 }) 133 }) 134 }) 135 }) 136 137 Context("Using cobra commands", func() { 138 Context("can combine multiple language files", func() { 139 Context("merging en files in input_files path", func() { 140 BeforeEach(func() { 141 session := Runi18n("merge-strings", "-v", "-d", filepath.Join(inputFilesPath), "--source-language", "en") 142 Ω(session.ExitCode()).Should(Equal(0)) 143 }) 144 145 AfterEach(func() { 146 RemoveAllFiles( 147 GetFilePath(inputFilesPath, "all.en.json"), 148 ) 149 }) 150 151 It("creates an all.en.json that contains translations from both files", func() { 152 CompareExpectedToGeneratedTraslationJson( 153 GetFilePath(expectedFilesPath, "all.en.json"), 154 GetFilePath(inputFilesPath, "all.en.json"), 155 ) 156 }) 157 158 It("creates an all.en.json for which the translation strings order are stable", func() { 159 expectedFilePath := GetFilePath(expectedFilesPath, "all.en.json") 160 actualFilePath := GetFilePath(inputFilesPath, "all.en.json") 161 162 expectedBytes, err := ioutil.ReadFile(expectedFilePath) 163 Ω(err).Should(BeNil()) 164 Ω(expectedBytes).ShouldNot(BeNil()) 165 166 actualBytes, err := ioutil.ReadFile(actualFilePath) 167 Ω(err).Should(BeNil()) 168 Ω(actualBytes).ShouldNot(BeNil()) 169 170 expectedTranslations := []common.I18nStringInfo{} 171 err = json.Unmarshal(expectedBytes, &expectedTranslations) 172 Ω(err).Should(BeNil()) 173 174 actualTranslations := []common.I18nStringInfo{} 175 err = json.Unmarshal(actualBytes, &actualTranslations) 176 Ω(err).Should(BeNil()) 177 178 Ω(len(actualTranslations)).Should(Equal(len(expectedTranslations))) 179 for idx := range actualTranslations { 180 Ω(actualTranslations[idx].ID).Should(Equal(expectedTranslations[idx].ID)) 181 Ω(actualTranslations[idx].Translation).Should(Equal(expectedTranslations[idx].Translation)) 182 } 183 }) 184 }) 185 186 Context("merging en files in input_files/reordered path", func() { 187 BeforeEach(func() { 188 session := Runi18n("merge-strings", "-v", "-d", filepath.Join(inputFilesPath, "reordered"), "--source-language", "en") 189 Ω(session.ExitCode()).Should(Equal(0)) 190 }) 191 192 AfterEach(func() { 193 RemoveAllFiles( 194 GetFilePath(filepath.Join(inputFilesPath, "reordered"), "all.en.json"), 195 ) 196 }) 197 198 It("creates an all.en.json keeping the stable order", func() { 199 expectedFilePath := GetFilePath(expectedFilesPath, "all.en.json") 200 actualFilePath := GetFilePath(filepath.Join(inputFilesPath, "reordered"), "all.en.json") 201 202 expectedBytes, err := ioutil.ReadFile(expectedFilePath) 203 Ω(err).Should(BeNil()) 204 Ω(expectedBytes).ShouldNot(BeNil()) 205 206 actualBytes, err := ioutil.ReadFile(actualFilePath) 207 Ω(err).Should(BeNil()) 208 Ω(actualBytes).ShouldNot(BeNil()) 209 210 expectedTranslations := []common.I18nStringInfo{} 211 err = json.Unmarshal(expectedBytes, &expectedTranslations) 212 Ω(err).Should(BeNil()) 213 214 actualTranslations := []common.I18nStringInfo{} 215 err = json.Unmarshal(actualBytes, &actualTranslations) 216 Ω(err).Should(BeNil()) 217 218 Ω(len(actualTranslations)).Should(Equal(len(expectedTranslations))) 219 for idx := range actualTranslations { 220 Ω(actualTranslations[idx].ID).Should(Equal(expectedTranslations[idx].ID)) 221 Ω(actualTranslations[idx].Translation).Should(Equal(expectedTranslations[idx].Translation)) 222 } 223 }) 224 }) 225 }) 226 }) 227 })