github.com/CharukaK/i18n4go@v0.6.0/integration/extract_strings/s_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  	"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("extract-strings -s filePath", func() {
    30  	var (
    31  		outputPath            string
    32  		fixturesPath          string
    33  		inputFilesPath        string
    34  		expectedFilesPath     string
    35  		matchingGroupFilePath string
    36  	)
    37  
    38  	BeforeEach(func() {
    39  		_, err := os.Getwd()
    40  		Ω(err).ShouldNot(HaveOccurred())
    41  
    42  		outputPath, err = ioutil.TempDir("", "i18n4go4go")
    43  		Ω(err).ToNot(HaveOccurred())
    44  
    45  		fixturesPath = filepath.Join("..", "..", "test_fixtures", "extract_strings")
    46  		inputFilesPath = filepath.Join(fixturesPath, "s_option", "input_files", "app", "app.go")
    47  		expectedFilesPath = filepath.Join(fixturesPath, "s_option", "expected_output")
    48  		matchingGroupFilePath = filepath.Join(fixturesPath, "s_option", "input_files", "matching_group.json")
    49  	})
    50  
    51  	AfterEach(func() {
    52  		os.RemoveAll(outputPath)
    53  	})
    54  
    55  	Context("Using legacy commands", func() {
    56  		Context("When i18n4go4go is run with the -s flag", func() {
    57  			BeforeEach(func() {
    58  				session := Runi18n("-c", "extract-strings", "-v", "-f", inputFilesPath, "-o", outputPath, "-s", matchingGroupFilePath)
    59  
    60  				Ω(session.ExitCode()).Should(Equal(0))
    61  			})
    62  
    63  			It("use the regexFile to parse substring", func() {
    64  				expectedFilePath := filepath.Join(expectedFilesPath, "app.go.en.json")
    65  				actualFilePath := filepath.Join(outputPath, "app.go.en.json")
    66  
    67  				expectedBytes, err := ioutil.ReadFile(expectedFilePath)
    68  				Ω(err).Should(BeNil())
    69  				Ω(expectedBytes).ShouldNot(BeNil())
    70  
    71  				actualBytes, err := ioutil.ReadFile(actualFilePath)
    72  				Ω(err).Should(BeNil())
    73  				Ω(actualBytes).ShouldNot(BeNil())
    74  
    75  				var translations []common.I18nStringInfo
    76  				err = json.Unmarshal(actualBytes, &translations)
    77  				Ω(err).Should(BeNil())
    78  
    79  				Ω(translations).To(ContainElement(common.I18nStringInfo{
    80  					ID:          "a string",
    81  					Translation: "a string",
    82  				}))
    83  
    84  				Ω(translations).To(ContainElement(common.I18nStringInfo{
    85  					ID:          "show the app details",
    86  					Translation: "show the app details",
    87  				}))
    88  			})
    89  		})
    90  
    91  	})
    92  
    93  	Context("Using cobra commands", func() {
    94  		Context("When i18n4go4go is run with the -s flag", func() {
    95  			BeforeEach(func() {
    96  				session := Runi18n("extract-strings", "-v", "-f", inputFilesPath, "-o", outputPath, "-s", matchingGroupFilePath)
    97  
    98  				Ω(session.ExitCode()).Should(Equal(0))
    99  			})
   100  
   101  			It("use the regexFile to parse substring", func() {
   102  				expectedFilePath := filepath.Join(expectedFilesPath, "app.go.en.json")
   103  				actualFilePath := filepath.Join(outputPath, "app.go.en.json")
   104  
   105  				expectedBytes, err := ioutil.ReadFile(expectedFilePath)
   106  				Ω(err).Should(BeNil())
   107  				Ω(expectedBytes).ShouldNot(BeNil())
   108  
   109  				actualBytes, err := ioutil.ReadFile(actualFilePath)
   110  				Ω(err).Should(BeNil())
   111  				Ω(actualBytes).ShouldNot(BeNil())
   112  
   113  				var translations []common.I18nStringInfo
   114  				err = json.Unmarshal(actualBytes, &translations)
   115  				Ω(err).Should(BeNil())
   116  
   117  				Ω(translations).To(ContainElement(common.I18nStringInfo{
   118  					ID:          "a string",
   119  					Translation: "a string",
   120  				}))
   121  
   122  				Ω(translations).To(ContainElement(common.I18nStringInfo{
   123  					ID:          "show the app details",
   124  					Translation: "show the app details",
   125  				}))
   126  			})
   127  		})
   128  
   129  	})
   130  })