github.com/CharukaK/i18n4go@v0.6.0/integration/extract_strings/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 extract_strings_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("extract-strings -f fileName", 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", "extract_strings", "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("compare generated and expected file", func() {
    45  			BeforeEach(func() {
    46  				session := Runi18n("-c", "extract-strings", "-v", "--po", "--meta", "-f", filepath.Join(inputFilesPath, "app.go"))
    47  				Ω(session.ExitCode()).Should(Equal(0))
    48  			})
    49  
    50  			AfterEach(func() {
    51  				RemoveAllFiles(
    52  					GetFilePath(inputFilesPath, "app.go.en.json"),
    53  					GetFilePath(inputFilesPath, "app.go.en.po"),
    54  					GetFilePath(inputFilesPath, "app.go.extracted.json"),
    55  				)
    56  			})
    57  
    58  			It("app.go.en.json", func() {
    59  				CompareExpectedToGeneratedTraslationJson(
    60  					GetFilePath(expectedFilesPath, "app.go.en.json"),
    61  					GetFilePath(inputFilesPath, "app.go.en.json"),
    62  				)
    63  			})
    64  
    65  			It("app.go.extracted.json", func() {
    66  				CompareExpectedToGeneratedExtendedJson(
    67  					GetFilePath(expectedFilesPath, "app.go.extracted.json"),
    68  					GetFilePath(inputFilesPath, "app.go.extracted.json"),
    69  				)
    70  			})
    71  
    72  			It("app.go.en.po", func() {
    73  				CompareExpectedToGeneratedPo(
    74  					GetFilePath(expectedFilesPath, "app.go.en.po"),
    75  					GetFilePath(inputFilesPath, "app.go.en.po"),
    76  				)
    77  			})
    78  		})
    79  
    80  		Context("GitHub issue #4: extracting some character as ascii code, e.g., > as \u003e", func() {
    81  			BeforeEach(func() {
    82  				session := Runi18n("-c", "extract-strings", "-v", "-f", filepath.Join(inputFilesPath, "issue4.go"))
    83  				Ω(session.ExitCode()).Should(Equal(0))
    84  			})
    85  
    86  			AfterEach(func() {
    87  				RemoveAllFiles(
    88  					GetFilePath(inputFilesPath, "issue4.go.en.json"),
    89  				)
    90  			})
    91  
    92  			It("issue4.go.en.json", func() {
    93  				CompareExpectedToGeneratedTraslationJson(
    94  					GetFilePath(expectedFilesPath, "issue4.go.en.json"),
    95  					GetFilePath(inputFilesPath, "issue4.go.en.json"),
    96  				)
    97  			})
    98  		})
    99  
   100  		Context("GitHub issue #16: Extract Strings should ignore string keys in maps", func() {
   101  			BeforeEach(func() {
   102  				session := Runi18n("-c", "extract-strings", "-v", "-f", filepath.Join(inputFilesPath, "issue16.go"))
   103  				Ω(session.ExitCode()).Should(Equal(0))
   104  			})
   105  
   106  			AfterEach(func() {
   107  				RemoveAllFiles(
   108  					GetFilePath(inputFilesPath, "issue16.go.en.json"),
   109  				)
   110  			})
   111  
   112  			It("issue16.go.en.json", func() {
   113  				CompareExpectedToGeneratedTraslationJson(
   114  					GetFilePath(expectedFilesPath, "issue16.go.en.json"),
   115  					GetFilePath(inputFilesPath, "issue16.go.en.json"),
   116  				)
   117  			})
   118  		})
   119  
   120  		Context("when the file specified has no strings at all", func() {
   121  			var (
   122  				OUTPUT_PATH string
   123  			)
   124  
   125  			BeforeEach(func() {
   126  				var err error
   127  				OUTPUT_PATH, err = ioutil.TempDir("", "i18n4go4go")
   128  				Ω(err).ShouldNot(HaveOccurred())
   129  
   130  				session := Runi18n("-c", "extract-strings", "-f", filepath.Join(inputFilesPath, "no_strings.go"), "-o", OUTPUT_PATH)
   131  				Ω(session.ExitCode()).Should(Equal(0))
   132  			})
   133  
   134  			It("does not generate any files", func() {
   135  				println(OUTPUT_PATH)
   136  				files, err := ioutil.ReadDir(OUTPUT_PATH)
   137  				Ω(err).ShouldNot(HaveOccurred())
   138  
   139  				Ω(files).Should(BeEmpty())
   140  			})
   141  		})
   142  
   143  		Context("GitHub issue #45: Extract Strings should extract strings string embedded inside a func, inside a func in a return", func() {
   144  			BeforeEach(func() {
   145  				session := Runi18n("-c", "extract-strings", "-v", "-f", filepath.Join(inputFilesPath, "issue45.go"))
   146  				Ω(session.ExitCode()).Should(Equal(0))
   147  			})
   148  
   149  			AfterEach(func() {
   150  				RemoveAllFiles(
   151  					GetFilePath(inputFilesPath, "issue45.go.en.json"),
   152  				)
   153  			})
   154  
   155  			It("generates issue45.go.en.json correctly", func() {
   156  				CompareExpectedToGeneratedTraslationJson(
   157  					GetFilePath(expectedFilesPath, "issue45.go.en.json"),
   158  					GetFilePath(inputFilesPath, "issue45.go.en.json"),
   159  				)
   160  			})
   161  		})
   162  
   163  	})
   164  
   165  	Context("Using cobra commands", func() {
   166  		Context("compare generated and expected file", func() {
   167  			BeforeEach(func() {
   168  				session := Runi18n("extract-strings", "-v", "--po", "--meta", "-f", filepath.Join(inputFilesPath, "app.go"))
   169  				Ω(session.ExitCode()).Should(Equal(0))
   170  			})
   171  
   172  			AfterEach(func() {
   173  				RemoveAllFiles(
   174  					GetFilePath(inputFilesPath, "app.go.en.json"),
   175  					GetFilePath(inputFilesPath, "app.go.en.po"),
   176  					GetFilePath(inputFilesPath, "app.go.extracted.json"),
   177  				)
   178  			})
   179  
   180  			It("app.go.en.json", func() {
   181  				CompareExpectedToGeneratedTraslationJson(
   182  					GetFilePath(expectedFilesPath, "app.go.en.json"),
   183  					GetFilePath(inputFilesPath, "app.go.en.json"),
   184  				)
   185  			})
   186  
   187  			It("app.go.extracted.json", func() {
   188  				CompareExpectedToGeneratedExtendedJson(
   189  					GetFilePath(expectedFilesPath, "app.go.extracted.json"),
   190  					GetFilePath(inputFilesPath, "app.go.extracted.json"),
   191  				)
   192  			})
   193  
   194  			It("app.go.en.po", func() {
   195  				CompareExpectedToGeneratedPo(
   196  					GetFilePath(expectedFilesPath, "app.go.en.po"),
   197  					GetFilePath(inputFilesPath, "app.go.en.po"),
   198  				)
   199  			})
   200  		})
   201  
   202  		Context("GitHub issue #4: extracting some character as ascii code, e.g., > as \u003e", func() {
   203  			BeforeEach(func() {
   204  				session := Runi18n("extract-strings", "-v", "-f", filepath.Join(inputFilesPath, "issue4.go"))
   205  				Ω(session.ExitCode()).Should(Equal(0))
   206  			})
   207  
   208  			AfterEach(func() {
   209  				RemoveAllFiles(
   210  					GetFilePath(inputFilesPath, "issue4.go.en.json"),
   211  				)
   212  			})
   213  
   214  			It("issue4.go.en.json", func() {
   215  				CompareExpectedToGeneratedTraslationJson(
   216  					GetFilePath(expectedFilesPath, "issue4.go.en.json"),
   217  					GetFilePath(inputFilesPath, "issue4.go.en.json"),
   218  				)
   219  			})
   220  		})
   221  
   222  		Context("GitHub issue #16: Extract Strings should ignore string keys in maps", func() {
   223  			BeforeEach(func() {
   224  				session := Runi18n("extract-strings", "-v", "-f", filepath.Join(inputFilesPath, "issue16.go"))
   225  				Ω(session.ExitCode()).Should(Equal(0))
   226  			})
   227  
   228  			AfterEach(func() {
   229  				RemoveAllFiles(
   230  					GetFilePath(inputFilesPath, "issue16.go.en.json"),
   231  				)
   232  			})
   233  
   234  			It("issue16.go.en.json", func() {
   235  				CompareExpectedToGeneratedTraslationJson(
   236  					GetFilePath(expectedFilesPath, "issue16.go.en.json"),
   237  					GetFilePath(inputFilesPath, "issue16.go.en.json"),
   238  				)
   239  			})
   240  		})
   241  
   242  		Context("when the file specified has no strings at all", func() {
   243  			var (
   244  				OUTPUT_PATH string
   245  			)
   246  
   247  			BeforeEach(func() {
   248  				var err error
   249  				OUTPUT_PATH, err = ioutil.TempDir("", "i18n4go4go")
   250  				Ω(err).ShouldNot(HaveOccurred())
   251  
   252  				session := Runi18n("extract-strings", "-f", filepath.Join(inputFilesPath, "no_strings.go"), "-o", OUTPUT_PATH)
   253  				Ω(session.ExitCode()).Should(Equal(0))
   254  			})
   255  
   256  			It("does not generate any files", func() {
   257  				println(OUTPUT_PATH)
   258  				files, err := ioutil.ReadDir(OUTPUT_PATH)
   259  				Ω(err).ShouldNot(HaveOccurred())
   260  
   261  				Ω(files).Should(BeEmpty())
   262  			})
   263  		})
   264  
   265  		Context("GitHub issue #45: Extract Strings should extract strings string embedded inside a func, inside a func in a return", func() {
   266  			BeforeEach(func() {
   267  				session := Runi18n("extract-strings", "-v", "-f", filepath.Join(inputFilesPath, "issue45.go"))
   268  				Ω(session.ExitCode()).Should(Equal(0))
   269  			})
   270  
   271  			AfterEach(func() {
   272  				RemoveAllFiles(
   273  					GetFilePath(inputFilesPath, "issue45.go.en.json"),
   274  				)
   275  			})
   276  
   277  			It("generates issue45.go.en.json correctly", func() {
   278  				CompareExpectedToGeneratedTraslationJson(
   279  					GetFilePath(expectedFilesPath, "issue45.go.en.json"),
   280  					GetFilePath(inputFilesPath, "issue45.go.en.json"),
   281  				)
   282  			})
   283  		})
   284  
   285  	})
   286  })