github.com/Liam-Williams/i18n4go@v0.2.7-0.20201028180611-670cbaceaa6b/integration/rewrite_package/init_code_snippet_filename_test.go (about)

     1  package rewrite_package_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	. "github.com/Liam-Williams/i18n4go/integration/test_helpers"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  )
    13  
    14  var _ = Describe("rewrite-package [...] --init-code-snippet-filename some-file", func() {
    15  	var (
    16  		outputDir         string
    17  		rootPath          string
    18  		fixturesPath      string
    19  		inputFilesPath    string
    20  		expectedFilesPath string
    21  	)
    22  
    23  	AfterEach(func() {
    24  		err := os.RemoveAll(outputDir)
    25  		Ω(err).ShouldNot(HaveOccurred())
    26  	})
    27  
    28  	Context("invokes rewrite-package command and uses the default i18n init function", func() {
    29  		BeforeEach(func() {
    30  			dir, err := os.Getwd()
    31  			Ω(err).ShouldNot(HaveOccurred())
    32  			rootPath = filepath.Join(dir, "..", "..")
    33  
    34  			outputDir, err = ioutil.TempDir(rootPath, "i18n4go_integration")
    35  			Ω(err).ShouldNot(HaveOccurred())
    36  
    37  			fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package")
    38  			inputFilesPath = filepath.Join(fixturesPath, "init_code_snippet_filename", "input_files")
    39  			expectedFilesPath = filepath.Join(fixturesPath, "init_code_snippet_filename", "expected_output")
    40  
    41  			session := Runi18n("-c",
    42  				"rewrite-package",
    43  				"-f", filepath.Join(inputFilesPath, "issue14.go"),
    44  				"-o", outputDir,
    45  				"--root-path", rootPath,
    46  				"-v",
    47  			)
    48  
    49  			Ω(session.ExitCode()).Should(Equal(0))
    50  		})
    51  
    52  		It("rewrites the source go file wrapping strings with T() and generates a i18n_init.go using default", func() {
    53  			expectedOutputFile := filepath.Join(expectedFilesPath, "issue14.go")
    54  			bytes, err := ioutil.ReadFile(expectedOutputFile)
    55  			Ω(err).ShouldNot(HaveOccurred())
    56  
    57  			expectedOutput := string(bytes)
    58  
    59  			generatedOutputFile := filepath.Join(outputDir, "issue14.go")
    60  			bytes, err = ioutil.ReadFile(generatedOutputFile)
    61  			Ω(err).ShouldNot(HaveOccurred())
    62  
    63  			actualOutput := string(bytes)
    64  
    65  			Ω(actualOutput).Should(Equal(expectedOutput))
    66  
    67  			expectedOutputFile = filepath.Join(expectedFilesPath, "i18n_init_default.go")
    68  			bytes, err = ioutil.ReadFile(expectedOutputFile)
    69  			Ω(err).ShouldNot(HaveOccurred())
    70  
    71  			expectedOutput = strings.Trim(string(bytes), "\n")
    72  
    73  			generatedOutputFile = filepath.Join(outputDir, "i18n_init.go")
    74  			bytes, err = ioutil.ReadFile(generatedOutputFile)
    75  			Ω(err).ShouldNot(HaveOccurred())
    76  
    77  			actualOutput = string(bytes)
    78  
    79  			Ω(actualOutput).Should(Equal(expectedOutput))
    80  		})
    81  	})
    82  
    83  	Context("invokes rewrite-package command and uses the specified --init-code-snippet-filename", func() {
    84  		BeforeEach(func() {
    85  			dir, err := os.Getwd()
    86  			Ω(err).ShouldNot(HaveOccurred())
    87  			rootPath = filepath.Join(dir, "..", "..")
    88  
    89  			outputDir, err = ioutil.TempDir(rootPath, "i18n4go_integration")
    90  			Ω(err).ShouldNot(HaveOccurred())
    91  
    92  			fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package")
    93  			inputFilesPath = filepath.Join(fixturesPath, "init_code_snippet_filename", "input_files")
    94  			expectedFilesPath = filepath.Join(fixturesPath, "init_code_snippet_filename", "expected_output")
    95  
    96  			session := Runi18n("-c",
    97  				"rewrite-package",
    98  				"-f", filepath.Join(inputFilesPath, "issue14.go"),
    99  				"-o", outputDir,
   100  				"--init-code-snippet-filename", filepath.Join(inputFilesPath, "init_code_snippet.go.template"),
   101  				"--root-path", rootPath,
   102  				"-v",
   103  			)
   104  
   105  			Ω(session.ExitCode()).Should(Equal(0))
   106  		})
   107  
   108  		It("rewrites the source go file wrapping strings with T() and generates a i18n_init.go using teamplate file", func() {
   109  			expectedOutputFile := filepath.Join(expectedFilesPath, "issue14.go")
   110  			bytes, err := ioutil.ReadFile(expectedOutputFile)
   111  			Ω(err).ShouldNot(HaveOccurred())
   112  
   113  			expectedOutput := string(bytes)
   114  
   115  			generatedOutputFile := filepath.Join(outputDir, "issue14.go")
   116  			bytes, err = ioutil.ReadFile(generatedOutputFile)
   117  			Ω(err).ShouldNot(HaveOccurred())
   118  
   119  			actualOutput := string(bytes)
   120  
   121  			Ω(actualOutput).Should(Equal(expectedOutput))
   122  
   123  			expectedOutputFile = filepath.Join(expectedFilesPath, "i18n_init_from_template.go")
   124  			bytes, err = ioutil.ReadFile(expectedOutputFile)
   125  			Ω(err).ShouldNot(HaveOccurred())
   126  
   127  			expectedOutput = string(bytes)
   128  
   129  			generatedOutputFile = filepath.Join(outputDir, "i18n_init.go")
   130  			bytes, err = ioutil.ReadFile(generatedOutputFile)
   131  			Ω(err).ShouldNot(HaveOccurred())
   132  
   133  			actualOutput = string(bytes)
   134  
   135  			Ω(actualOutput).Should(Equal(expectedOutput))
   136  		})
   137  	})
   138  })