github.com/CharukaK/i18n4go@v0.6.0/integration/create_translations/f_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 create_translations_test 16 17 import ( 18 "path/filepath" 19 20 "os" 21 22 . "github.com/CharukaK/i18n4go/integration/test_helpers" 23 . "github.com/onsi/ginkgo" 24 . "github.com/onsi/gomega" 25 ) 26 27 var _ = Describe("create-translations -f fileName --languages \"[lang,?]+\"", func() { 28 var ( 29 fixturesPath string 30 inputFilesPath string 31 expectedFilesPath string 32 ) 33 34 BeforeEach(func() { 35 _, err := os.Getwd() 36 Ω(err).ShouldNot(HaveOccurred()) 37 38 fixturesPath = filepath.Join("..", "..", "test_fixtures", "create_translations", "f_option") 39 inputFilesPath = filepath.Join(fixturesPath, "input_files") 40 expectedFilesPath = filepath.Join(fixturesPath, "expected_output") 41 }) 42 43 Context("Using legacy commands", func() { 44 Context("when valid input file is provided", func() { 45 Context("and a single language is specified", func() { 46 BeforeEach(func() { 47 session := Runi18n("-c", "create-translations", "-v", "-f", filepath.Join(inputFilesPath, "quota.go.en.json"), "--languages", "\"fr\"", "-o", expectedFilesPath) 48 Ω(session.ExitCode()).Should(Equal(0)) 49 }) 50 51 AfterEach(func() { 52 RemoveAllFiles( 53 GetFilePath(expectedFilesPath, "quota.go.fr.json"), 54 ) 55 }) 56 57 It("creates the language file", func() { 58 fileInfo, err := os.Stat(GetFilePath(expectedFilesPath, "quota.go.fr.json")) 59 Ω(err).Should(BeNil()) 60 Ω(fileInfo.Name()).Should(Equal("quota.go.fr.json")) 61 }) 62 }) 63 64 Context("and multiple languages are specified", func() { 65 BeforeEach(func() { 66 session := Runi18n("-c", "create-translations", "-v", "-f", filepath.Join(inputFilesPath, "quota.go.en.json"), "--languages", "\"fr,de\"", "-o", expectedFilesPath) 67 Ω(session.ExitCode()).Should(Equal(0)) 68 }) 69 70 AfterEach(func() { 71 RemoveAllFiles( 72 GetFilePath(expectedFilesPath, "quota.go.fr.json"), 73 GetFilePath(expectedFilesPath, "quota.go.de.json"), 74 ) 75 }) 76 77 It("creates a language file for each language specified", func() { 78 fileInfo, err := os.Stat(GetFilePath(expectedFilesPath, "quota.go.fr.json")) 79 Ω(err).Should(BeNil()) 80 Ω(fileInfo.Name()).Should(Equal("quota.go.fr.json")) 81 82 fileInfo, err = os.Stat(GetFilePath(expectedFilesPath, "quota.go.de.json")) 83 Ω(err).Should(BeNil()) 84 Ω(fileInfo.Name()).Should(Equal("quota.go.de.json")) 85 }) 86 }) 87 88 }) 89 90 Context("when invalid input file is provided", func() { 91 Context("and file does not exist", func() { 92 BeforeEach(func() { 93 session := Runi18n("-c", "create-translations", "-v", "-f", filepath.Join(inputFilesPath, "quota.go.de.json"), "--languages", "\"fr\"", "-o", expectedFilesPath, "--source-language", "zh_TW") 94 Ω(session.ExitCode()).Should(Equal(1)) 95 }) 96 97 It("fails verification", func() { 98 _, err := os.Stat(GetFilePath(expectedFilesPath, "quota.go.fr.json")) 99 Ω(os.IsNotExist(err)).Should(Equal(true)) 100 }) 101 }) 102 103 Context("and file is empty", func() { 104 BeforeEach(func() { 105 session := Runi18n("-c", "create-translations", "-v", "-f", filepath.Join(inputFilesPath, "quota.go.ja.json"), "--languages", "\"fr\"", "-o", expectedFilesPath, "--source-language", "ja") 106 Ω(session.ExitCode()).Should(Equal(1)) 107 }) 108 109 It("fails verification", func() { 110 _, err := os.Stat(GetFilePath(expectedFilesPath, "quota.go.ja.json")) 111 Ω(os.IsNotExist(err)).Should(Equal(true)) 112 }) 113 }) 114 }) 115 116 }) 117 118 Context("Using cobra commands", func() { 119 Context("when valid input file is provided", func() { 120 Context("and a single language is specified", func() { 121 BeforeEach(func() { 122 session := Runi18n("create-translations", "-v", "-f", filepath.Join(inputFilesPath, "quota.go.en.json"), "--languages", "\"fr\"", "-o", expectedFilesPath) 123 Ω(session.ExitCode()).Should(Equal(0)) 124 }) 125 126 AfterEach(func() { 127 RemoveAllFiles( 128 GetFilePath(expectedFilesPath, "quota.go.fr.json"), 129 ) 130 }) 131 132 It("creates the language file", func() { 133 fileInfo, err := os.Stat(GetFilePath(expectedFilesPath, "quota.go.fr.json")) 134 Ω(err).Should(BeNil()) 135 Ω(fileInfo.Name()).Should(Equal("quota.go.fr.json")) 136 }) 137 }) 138 139 Context("and multiple languages are specified", func() { 140 BeforeEach(func() { 141 session := Runi18n("create-translations", "-v", "-f", filepath.Join(inputFilesPath, "quota.go.en.json"), "--languages", "\"fr,de\"", "-o", expectedFilesPath) 142 Ω(session.ExitCode()).Should(Equal(0)) 143 }) 144 145 AfterEach(func() { 146 RemoveAllFiles( 147 GetFilePath(expectedFilesPath, "quota.go.fr.json"), 148 GetFilePath(expectedFilesPath, "quota.go.de.json"), 149 ) 150 }) 151 152 It("creates a language file for each language specified", func() { 153 fileInfo, err := os.Stat(GetFilePath(expectedFilesPath, "quota.go.fr.json")) 154 Ω(err).Should(BeNil()) 155 Ω(fileInfo.Name()).Should(Equal("quota.go.fr.json")) 156 157 fileInfo, err = os.Stat(GetFilePath(expectedFilesPath, "quota.go.de.json")) 158 Ω(err).Should(BeNil()) 159 Ω(fileInfo.Name()).Should(Equal("quota.go.de.json")) 160 }) 161 }) 162 163 }) 164 165 Context("when invalid input file is provided", func() { 166 Context("and file does not exist", func() { 167 BeforeEach(func() { 168 session := Runi18n("create-translations", "-v", "-f", filepath.Join(inputFilesPath, "quota.go.de.json"), "--languages", "\"fr\"", "-o", expectedFilesPath, "--source-language", "zh_TW") 169 Ω(session.ExitCode()).Should(Equal(1)) 170 }) 171 172 It("fails verification", func() { 173 _, err := os.Stat(GetFilePath(expectedFilesPath, "quota.go.fr.json")) 174 Ω(os.IsNotExist(err)).Should(Equal(true)) 175 }) 176 }) 177 178 Context("and file is empty", func() { 179 BeforeEach(func() { 180 session := Runi18n("create-translations", "-v", "-f", filepath.Join(inputFilesPath, "quota.go.ja.json"), "--languages", "\"fr\"", "-o", expectedFilesPath, "--source-language", "ja") 181 Ω(session.ExitCode()).Should(Equal(1)) 182 }) 183 184 It("fails verification", func() { 185 _, err := os.Stat(GetFilePath(expectedFilesPath, "quota.go.ja.json")) 186 Ω(os.IsNotExist(err)).Should(Equal(true)) 187 }) 188 }) 189 }) 190 191 }) 192 })