github.com/xenophex/i18n4go@v0.2.7-0.20160907212557-40256cda157a/integration/rewrite_package/i18n_strings_filename_test.go (about)

     1  package rewrite_package_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	. "github.com/XenoPhex/i18n4go/integration/test_helpers"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  )
    12  
    13  var _ = Describe("rewrite-package --i18n-strings-filename some-file", func() {
    14  	var (
    15  		outputDir         string
    16  		rootPath          string
    17  		fixturesPath      string
    18  		inputFilesPath    string
    19  		expectedFilesPath string
    20  	)
    21  
    22  	AfterEach(func() {
    23  		err := os.RemoveAll(outputDir)
    24  		Ω(err).ShouldNot(HaveOccurred())
    25  	})
    26  
    27  	Context("input file only contains simple strings", func() {
    28  		BeforeEach(func() {
    29  			dir, err := os.Getwd()
    30  			Ω(err).ShouldNot(HaveOccurred())
    31  			rootPath = filepath.Join(dir, "..", "..")
    32  
    33  			outputDir, err = ioutil.TempDir(rootPath, "i18n4go_integration")
    34  			Ω(err).ShouldNot(HaveOccurred())
    35  
    36  			fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package")
    37  			inputFilesPath = filepath.Join(fixturesPath, "i18n_strings_filename_option", "input_files")
    38  			expectedFilesPath = filepath.Join(fixturesPath, "i18n_strings_filename_option", "expected_output")
    39  
    40  			session := Runi18n("-c",
    41  				"rewrite-package",
    42  				"-f", filepath.Join(inputFilesPath, "test.go"),
    43  				"-o", outputDir,
    44  				"--i18n-strings-filename", filepath.Join(inputFilesPath, "strings.json"),
    45  				"-v",
    46  			)
    47  
    48  			Ω(session.ExitCode()).Should(Equal(0))
    49  		})
    50  
    51  		It("rewrites the input file with T() wrappers around the strings specified in the --i18n-strings-filename flag", func() {
    52  			expectedOutputFile := filepath.Join(expectedFilesPath, "test.go")
    53  			bytes, err := ioutil.ReadFile(expectedOutputFile)
    54  			Ω(err).ShouldNot(HaveOccurred())
    55  
    56  			expectedOutput := string(bytes)
    57  
    58  			generatedOutputFile := filepath.Join(outputDir, "test.go")
    59  			bytes, err = ioutil.ReadFile(generatedOutputFile)
    60  			Ω(err).ShouldNot(HaveOccurred())
    61  
    62  			actualOutput := string(bytes)
    63  
    64  			Ω(actualOutput).Should(Equal(expectedOutput))
    65  		})
    66  	})
    67  
    68  	Context("input file contains some templated strings", func() {
    69  		BeforeEach(func() {
    70  			dir, err := os.Getwd()
    71  			Ω(err).ShouldNot(HaveOccurred())
    72  			rootPath = filepath.Join(dir, "..", "..")
    73  
    74  			outputDir, err = ioutil.TempDir(rootPath, "i18n4go_integration")
    75  			Ω(err).ShouldNot(HaveOccurred())
    76  
    77  			fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package")
    78  			inputFilesPath = filepath.Join(fixturesPath, "i18n_strings_filename_option", "input_files")
    79  			expectedFilesPath = filepath.Join(fixturesPath, "i18n_strings_filename_option", "expected_output")
    80  
    81  			session := Runi18n("-c",
    82  				"rewrite-package",
    83  				"-f", filepath.Join(inputFilesPath, "test_templated_strings.go"),
    84  				"-o", outputDir,
    85  				"--i18n-strings-filename", filepath.Join(inputFilesPath, "test_templated_strings.go.en.json"),
    86  				"-v",
    87  			)
    88  
    89  			Ω(session.ExitCode()).Should(Equal(0))
    90  		})
    91  
    92  		It("rewrites the input file with T() wrappers around the strings (templated and not) specified in the --i18n-strings-filename flag", func() {
    93  			expectedOutputFile := filepath.Join(expectedFilesPath, "test_templated_strings.go")
    94  			bytes, err := ioutil.ReadFile(expectedOutputFile)
    95  			Ω(err).ShouldNot(HaveOccurred())
    96  
    97  			expectedOutput := string(bytes)
    98  
    99  			generatedOutputFile := filepath.Join(outputDir, "test_templated_strings.go")
   100  			bytes, err = ioutil.ReadFile(generatedOutputFile)
   101  			Ω(err).ShouldNot(HaveOccurred())
   102  
   103  			actualOutput := string(bytes)
   104  
   105  			Ω(actualOutput).Should(Equal(expectedOutput))
   106  		})
   107  	})
   108  
   109  	Context("input file contains some interpolated strings", func() {
   110  		BeforeEach(func() {
   111  			dir, err := os.Getwd()
   112  			Ω(err).ShouldNot(HaveOccurred())
   113  			rootPath = filepath.Join(dir, "..", "..")
   114  
   115  			outputDir, err = ioutil.TempDir(rootPath, "i18n4go_integration")
   116  			Ω(err).ShouldNot(HaveOccurred())
   117  
   118  			fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package")
   119  			inputFilesPath = filepath.Join(fixturesPath, "i18n_strings_filename_option", "input_files")
   120  			expectedFilesPath = filepath.Join(fixturesPath, "i18n_strings_filename_option", "expected_output")
   121  
   122  			CopyFile(filepath.Join(inputFilesPath, "_test_interpolated_strings.go.en.json"), filepath.Join(inputFilesPath, "test_interpolated_strings.go.en.json"))
   123  
   124  			session := Runi18n("-c",
   125  				"rewrite-package",
   126  				"-f", filepath.Join(inputFilesPath, "test_interpolated_strings.go"),
   127  				"-o", outputDir,
   128  				"--i18n-strings-filename", filepath.Join(inputFilesPath, "test_interpolated_strings.go.en.json"),
   129  				"-v",
   130  			)
   131  
   132  			Ω(session.ExitCode()).Should(Equal(0))
   133  		})
   134  
   135  		AfterEach(func() {
   136  			CopyFile(filepath.Join(inputFilesPath, "_test_interpolated_strings.go.en.json"), filepath.Join(inputFilesPath, "test_interpolated_strings.go.en.json"))
   137  		})
   138  
   139  		It("converts interpolated strings to templated and rewrites the input file with T() wrappers around the strings (templated and not) specified in the --i18n-strings-filename flag", func() {
   140  			expectedOutputFile := filepath.Join(expectedFilesPath, "test_interpolated_strings.go")
   141  			bytes, err := ioutil.ReadFile(expectedOutputFile)
   142  			Ω(err).ShouldNot(HaveOccurred())
   143  
   144  			expectedOutput := string(bytes)
   145  
   146  			generatedOutputFile := filepath.Join(outputDir, "test_interpolated_strings.go")
   147  			bytes, err = ioutil.ReadFile(generatedOutputFile)
   148  			Ω(err).ShouldNot(HaveOccurred())
   149  
   150  			actualOutput := string(bytes)
   151  
   152  			Ω(actualOutput).Should(Equal(expectedOutput))
   153  		})
   154  
   155  		It("updates the i18n strings JSON file with the converted interpolated JSON strings", func() {
   156  			expectedOutputFile := filepath.Join(expectedFilesPath, "test_interpolated_strings.go.en.json")
   157  			generatedOutputFile := filepath.Join(inputFilesPath, "test_interpolated_strings.go.en.json")
   158  			CompareExpectedToGeneratedTraslationJson(expectedOutputFile, generatedOutputFile)
   159  		})
   160  	})
   161  })