github.com/xenophex/i18n4go@v0.2.7-0.20160907212557-40256cda157a/integration/fixup/fixup_test.go (about) 1 package fixup_test 2 3 import ( 4 "bufio" 5 "fmt" 6 "io" 7 "io/ioutil" 8 "os" 9 "os/exec" 10 "path/filepath" 11 "strings" 12 13 "github.com/XenoPhex/i18n4go/common" 14 15 . "github.com/XenoPhex/i18n4go/integration/test_helpers" 16 . "github.com/onsi/ginkgo" 17 . "github.com/onsi/gomega" 18 ) 19 20 var _ = Describe("fixup", func() { 21 var ( 22 fixturesPath string 23 cmd *exec.Cmd 24 stdinPipe io.WriteCloser 25 stdoutPipe io.ReadCloser 26 stdoutReader *bufio.Reader 27 curDir string 28 jsonFiles map[string][]byte 29 err error 30 ) 31 32 BeforeEach(func() { 33 curDir, err = os.Getwd() 34 if err != nil { 35 fmt.Println("Could not get working directory") 36 panic(err.Error()) 37 } 38 }) 39 40 AfterEach(func() { 41 // restore json files 42 for path, bytes := range jsonFiles { 43 err = ioutil.WriteFile(path, bytes, 0666) 44 if err != nil { 45 fmt.Println("Could not rewrite backup JSON files") 46 panic(err.Error()) 47 } 48 } 49 50 err = os.Chdir(curDir) 51 if err != nil { 52 fmt.Println("Could not change back to working directory") 53 panic(err.Error()) 54 } 55 }) 56 57 JustBeforeEach(func() { 58 err = os.Chdir(fixturesPath) 59 if err != nil { 60 fmt.Println("Could not change to fixtures directory") 61 panic(err.Error()) 62 } 63 64 // backup json files 65 jsonFiles, err = storeTranslationFiles(".") 66 if err != nil { 67 fmt.Println("Could not back up the JSON files.") 68 panic(err.Error()) 69 } 70 71 //session = Runi18n("-c", "fixup") 72 cmd = exec.Command(I18n4goExec, "-c", "fixup") 73 74 stdinPipe, err = cmd.StdinPipe() 75 if err != nil { 76 fmt.Println("Could not get the stdin pipe.") 77 panic(err.Error()) 78 } 79 80 stdoutPipe, err = cmd.StdoutPipe() 81 if err != nil { 82 fmt.Println("Could not get the stdout pipe.") 83 panic(err.Error()) 84 } 85 stdoutReader = bufio.NewReader(stdoutPipe) 86 87 _, err = cmd.StderrPipe() 88 if err != nil { 89 fmt.Println("Could not get the stderr pipe.") 90 panic(err.Error()) 91 } 92 93 err = cmd.Start() 94 if err != nil { 95 fmt.Println("Could not run fixup") 96 panic(err.Error()) 97 } 98 }) 99 100 Context("When there are no problems", func() { 101 BeforeEach(func() { 102 fixturesPath = filepath.Join("..", "..", "test_fixtures", "fixup", "allgood") 103 }) 104 105 It("returns 0 and prints a reassuring message", func() { 106 Ω(getNextOutputLine(stdoutReader)).Should(Equal("OK")) 107 108 exitCode := cmd.Wait() 109 Ω(exitCode).Should(BeNil()) 110 }) 111 112 Context("for adding", func() { 113 It("does not print empty add messages", func() { 114 Ω(getNextOutputLine(stdoutReader)).ShouldNot(ContainSubstring("Adding these strings")) 115 116 exitCode := cmd.Wait() 117 Ω(exitCode).Should(BeNil()) 118 }) 119 }) 120 121 Context("for deleting", func() { 122 It("does not print empty delete messages", func() { 123 Ω(getNextOutputLine(stdoutReader)).ShouldNot(ContainSubstring("Removing these strings")) 124 125 exitCode := cmd.Wait() 126 Ω(exitCode).Should(BeNil()) 127 }) 128 }) 129 130 Context("For updating", func() { 131 It("does not print empty new or update check message", func() { 132 Ω(getNextOutputLine(stdoutReader)).ShouldNot(ContainSubstring("new or updated string")) 133 134 exitCode := cmd.Wait() 135 Ω(exitCode).Should(BeNil()) 136 }) 137 }) 138 }) 139 140 Context("When there are brand new strings in the code that don't exist in en-us", func() { 141 BeforeEach(func() { 142 fixturesPath = filepath.Join("..", "..", "test_fixtures", "fixup", "notsogood", "add") 143 }) 144 145 It("adds strings to all the locales", func() { 146 Ω(getNextOutputLine(stdoutReader)).Should(ContainSubstring("Adding these strings")) 147 Ω(getNextOutputLine(stdoutReader)).Should(ContainSubstring("Heal the world")) 148 149 exitCode := cmd.Wait() 150 Ω(exitCode).Should(BeNil()) 151 152 file, err := ioutil.ReadFile(filepath.Join(".", "translations", "en-us.all.json")) 153 Ω(err).ShouldNot(HaveOccurred()) 154 Ω(file).Should(ContainSubstring("\"Heal the world\"")) 155 156 chineseFile, err := ioutil.ReadFile(filepath.Join(".", "translations", "zh_CN.all.json")) 157 Ω(err).ShouldNot(HaveOccurred()) 158 Ω(chineseFile).Should(ContainSubstring("\"Heal the world\"")) 159 }) 160 }) 161 162 Context("When there are old strings in the translations that don't exist in the code", func() { 163 BeforeEach(func() { 164 fixturesPath = filepath.Join("..", "..", "test_fixtures", "fixup", "notsogood", "delete") 165 }) 166 167 It("removes the strings from all the locales", func() { 168 Ω(getNextOutputLine(stdoutReader)).Should(ContainSubstring("Removing these strings")) 169 Ω(getNextOutputLine(stdoutReader)).Should(ContainSubstring("Heal the world")) 170 171 exitCode := cmd.Wait() 172 Ω(exitCode).Should(BeNil()) 173 174 file, err := ioutil.ReadFile(filepath.Join(".", "translations", "en-us.all.json")) 175 Ω(err).ShouldNot(HaveOccurred()) 176 Ω(file).ShouldNot(ContainSubstring("\"Heal the world\"")) 177 178 chineseFile, err := ioutil.ReadFile(filepath.Join(".", "translations", "zh_CN.all.json")) 179 Ω(err).ShouldNot(HaveOccurred()) 180 Ω(chineseFile).ShouldNot(ContainSubstring("\"Heal the world\"")) 181 }) 182 }) 183 184 Context("When a string has been updated or added in the code", func() { 185 BeforeEach(func() { 186 fixturesPath = filepath.Join("..", "..", "test_fixtures", "fixup", "notsogood", "update") 187 }) 188 189 Context("When the user says the translation was updated", func() { 190 JustBeforeEach(func() { 191 Ω(getNextOutputLine(stdoutReader)).Should(ContainSubstring("Is the string \"I like apples.\" a new or updated string? [new/upd]")) 192 stdinPipe.Write([]byte("upd\n")) 193 stdinPipe.Write([]byte("1\n")) 194 }) 195 }) 196 }) 197 198 Context("When a foreign language is missing an english translation", func() { 199 BeforeEach(func() { 200 fixturesPath = filepath.Join("..", "..", "test_fixtures", "fixup", "notsogood", "missing_foreign_key") 201 }) 202 203 It("adds the extra translation", func() { 204 cmd.Wait() 205 206 translations, err := common.LoadI18nStringInfos(filepath.Join(".", "translations", "zh_CN.all.json")) 207 Ω(err).ShouldNot(HaveOccurred()) 208 mappedTranslations, err := common.CreateI18nStringInfoMap(translations) 209 Ω(err).ShouldNot(HaveOccurred()) 210 Ω(mappedTranslations["I'm the extra key"]).Should(Equal( 211 common.I18nStringInfo{ID: "I'm the extra key", Translation: "I'm the extra key", Modified: false}, 212 )) 213 }) 214 }) 215 216 Context("When a foreign language has an extra key", func() { 217 BeforeEach(func() { 218 fixturesPath = filepath.Join("..", "..", "test_fixtures", "fixup", "notsogood", "extra_foreign_key") 219 }) 220 221 It("removes the extra translation", func() { 222 cmd.Wait() 223 224 translations, err := common.LoadI18nStringInfos(filepath.Join(".", "translations", "zh_CN.all.json")) 225 Ω(err).ShouldNot(HaveOccurred()) 226 mappedTranslations, err := common.CreateI18nStringInfoMap(translations) 227 Ω(err).ShouldNot(HaveOccurred()) 228 Ω(mappedTranslations["I'm the extra key"]).Should(Equal(common.I18nStringInfo{})) 229 }) 230 }) 231 }) 232 233 func storeTranslationFiles(dir string) (files map[string][]byte, err error) { 234 files = make(map[string][]byte) 235 contents, _ := ioutil.ReadDir(dir) 236 237 for _, fileInfo := range contents { 238 if !fileInfo.IsDir() { 239 name := fileInfo.Name() 240 241 if strings.HasSuffix(name, ".all.json") { 242 path := filepath.Join(dir, fileInfo.Name()) 243 files[path], err = ioutil.ReadFile(path) 244 245 if err != nil { 246 return nil, err 247 } 248 } 249 } else { 250 newFiles, err := storeTranslationFiles(filepath.Join(dir, fileInfo.Name())) 251 if err != nil { 252 return nil, err 253 } 254 255 for path, bytes := range newFiles { 256 files[path] = bytes 257 } 258 } 259 } 260 261 return 262 } 263 264 func getNextOutputLine(reader *bufio.Reader) string { 265 line, _, err := reader.ReadLine() 266 if err != nil { 267 panic(err) 268 } 269 270 return string(line) 271 }