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

     1  package rewrite_package_test
     2  
     3  import (
     4  	. "github.com/Liam-Williams/i18n4go/integration/test_helpers"
     5  	. "github.com/onsi/ginkgo"
     6  	. "github.com/onsi/gomega"
     7  	"io/ioutil"
     8  	"os"
     9  	"path/filepath"
    10  	"strings"
    11  )
    12  
    13  var _ = Describe("rewrite-package -d dirname -r", func() {
    14  	var (
    15  		outputDir         string
    16  		rootPath          string
    17  		fixturesPath      string
    18  		inputFilesPath    string
    19  		expectedFilesPath string
    20  	)
    21  	AfterEach(func() {
    22  		err := os.RemoveAll(outputDir)
    23  		Ω(err).ShouldNot(HaveOccurred())
    24  	})
    25  
    26  	Context("rewrite all templated and interpolated strings", func() {
    27  		BeforeEach(func() {
    28  			dir, err := os.Getwd()
    29  			Ω(err).ShouldNot(HaveOccurred())
    30  
    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, "f_option", "input_files")
    38  			expectedFilesPath = filepath.Join(fixturesPath, "f_option", "expected_output")
    39  
    40  			session := Runi18n("-c",
    41  				"rewrite-package",
    42  				"-d", inputFilesPath,
    43  				"-o", outputDir,
    44  				"--ignore-regexp", "^[.]\\w+.go$", //Ignoring .*.go files, otherwise it defaults to ignoring *test*.go
    45  				"--root-path", rootPath,
    46  				"-r",
    47  				"-v",
    48  			)
    49  
    50  			Ω(session.ExitCode()).Should(Equal(0))
    51  		})
    52  
    53  		It("adds T() callExprs wrapping string literals", func() {
    54  			expectedOutputFile := filepath.Join(expectedFilesPath, "test.go")
    55  			bytes, err := ioutil.ReadFile(expectedOutputFile)
    56  			Ω(err).ShouldNot(HaveOccurred())
    57  
    58  			expectedOutput := string(bytes)
    59  
    60  			generatedOutputFile := filepath.Join(outputDir, "test.go")
    61  			bytes, err = ioutil.ReadFile(generatedOutputFile)
    62  			Ω(err).ShouldNot(HaveOccurred())
    63  
    64  			actualOutput := string(bytes)
    65  			Ω(actualOutput).Should(Equal(expectedOutput))
    66  		})
    67  
    68  		It("recurses to files in nested dirs", func() {
    69  			expectedOutputFile := filepath.Join(expectedFilesPath, "nested_dir", "test.go")
    70  			bytes, err := ioutil.ReadFile(expectedOutputFile)
    71  			Ω(err).ShouldNot(HaveOccurred())
    72  
    73  			expectedOutput := string(bytes)
    74  
    75  			generatedOutputFile := filepath.Join(outputDir, "nested_dir", "test.go")
    76  			bytes, err = ioutil.ReadFile(generatedOutputFile)
    77  			Ω(err).ShouldNot(HaveOccurred())
    78  
    79  			actualOutput := string(bytes)
    80  			Ω(actualOutput).Should(Equal(expectedOutput))
    81  		})
    82  
    83  		It("adds a i18n_init.go per package", func() {
    84  			initFile := filepath.Join(outputDir, "i18n_init.go")
    85  			expectedBytes, err := ioutil.ReadFile(initFile)
    86  			Ω(err).ShouldNot(HaveOccurred())
    87  			expected := strings.TrimSpace(string(expectedBytes))
    88  
    89  			expectedInitFile := filepath.Join(expectedFilesPath, "i18n_init.go")
    90  			actualBytes, err := ioutil.ReadFile(expectedInitFile)
    91  			Ω(err).ShouldNot(HaveOccurred())
    92  			actual := strings.TrimSpace(string(actualBytes))
    93  
    94  			Ω(actual).Should(Equal(expected))
    95  
    96  			initFile = filepath.Join(outputDir, "nested_dir", "i18n_init.go")
    97  			expectedBytes, err = ioutil.ReadFile(initFile)
    98  			Ω(err).ShouldNot(HaveOccurred())
    99  			expected = strings.TrimSpace(string(expectedBytes))
   100  
   101  			expectedInitFile = filepath.Join(expectedFilesPath, "nested_dir", "i18n_init.go")
   102  			actualBytes, err = ioutil.ReadFile(expectedInitFile)
   103  			Ω(err).ShouldNot(HaveOccurred())
   104  			actual = strings.TrimSpace(string(actualBytes))
   105  
   106  			Ω(actual).Should(Equal(expected))
   107  		})
   108  
   109  		It("does not translate test files", func() {
   110  			_, doesFileExistErr := os.Stat(filepath.Join(outputDir, "a_really_bad_test.go"))
   111  			Ω(os.IsNotExist(doesFileExistErr)).Should(BeTrue())
   112  		})
   113  	})
   114  
   115  	Context("rewrite only templated and interpolated strings from --i18n-strings-filename", func() {
   116  		BeforeEach(func() {
   117  			dir, err := os.Getwd()
   118  			Ω(err).ShouldNot(HaveOccurred())
   119  
   120  			rootPath = filepath.Join(dir, "..", "..")
   121  
   122  			outputDir, err = ioutil.TempDir(rootPath, "i18n4go_integration")
   123  			Ω(err).ShouldNot(HaveOccurred())
   124  
   125  			fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package")
   126  			inputFilesPath = filepath.Join(fixturesPath, "d_option", "input_files")
   127  			expectedFilesPath = filepath.Join(fixturesPath, "d_option", "expected_output")
   128  
   129  			err = os.MkdirAll(filepath.Join(outputDir, "doption"), 0777)
   130  			Ω(err).ShouldNot(HaveOccurred())
   131  
   132  			CopyFile(filepath.Join(expectedFilesPath, "doption", "_test.go.en.json"), filepath.Join(outputDir, "doption", "test.go.en.json"))
   133  
   134  			session := Runi18n("-c",
   135  				"rewrite-package",
   136  				"-d", inputFilesPath,
   137  				"-o", outputDir,
   138  				"--ignore-regexp", "^[.]\\w+.go$", //Ignoring .*.go files, otherwise it defaults to ignoring *test*.go
   139  				"-v",
   140  			)
   141  
   142  			Ω(session.ExitCode()).Should(Equal(0))
   143  		})
   144  
   145  		It("adds T() callExprs wrapping string literals", func() {
   146  			expectedOutputFile := filepath.Join(expectedFilesPath, "test.go")
   147  			generatedOutputFile := filepath.Join(outputDir, "test.go")
   148  			CompareExpectedOutputToGeneratedOutput(expectedOutputFile, generatedOutputFile)
   149  		})
   150  	})
   151  
   152  	Context("rewrite only templated and interpolated strings from --i18n-strings-filename with multiple files", func() {
   153  		BeforeEach(func() {
   154  			dir, err := os.Getwd()
   155  			Ω(err).ShouldNot(HaveOccurred())
   156  
   157  			rootPath = filepath.Join(dir, "..", "..")
   158  
   159  			outputDir, err = ioutil.TempDir(rootPath, "i18n4go_integration")
   160  			Ω(err).ShouldNot(HaveOccurred())
   161  
   162  			fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package")
   163  			inputFilesPath = filepath.Join(fixturesPath, "d_option", "input_files")
   164  			expectedFilesPath = filepath.Join(fixturesPath, "d_option", "expected_output")
   165  
   166  			err = os.MkdirAll(filepath.Join(outputDir, "doption"), 0777)
   167  			Ω(err).ShouldNot(HaveOccurred())
   168  
   169  			CopyFile(filepath.Join(expectedFilesPath, "doption", "_en.all.json"), filepath.Join(outputDir, "doption", "en.all.json"))
   170  
   171  			session := Runi18n("-c",
   172  				"rewrite-package",
   173  				"-d", inputFilesPath,
   174  				"-o", outputDir,
   175  				"--ignore-regexp", "^[.]\\w+.go$", //Ignoring .*.go files, otherwise it defaults to ignoring *test*.go
   176  				"--i18n-strings-filename", filepath.Join(expectedFilesPath, "doption", "en.all.json"),
   177  				"-v",
   178  			)
   179  
   180  			Ω(session.ExitCode()).Should(Equal(0))
   181  		})
   182  
   183  		It("not adds T() callExprs wrapping string literals to test3.go since none are in JSON", func() {
   184  			expectedOutputFile := filepath.Join(expectedFilesPath, "test3.go")
   185  			generatedOutputFile := filepath.Join(outputDir, "test3.go")
   186  			CompareExpectedOutputToGeneratedOutput(expectedOutputFile, generatedOutputFile)
   187  		})
   188  	})
   189  
   190  	Context("rewrite only templated and interpolated strings from --i18n-strings-dirname", func() {
   191  		BeforeEach(func() {
   192  			dir, err := os.Getwd()
   193  			Ω(err).ShouldNot(HaveOccurred())
   194  
   195  			rootPath = filepath.Join(dir, "..", "..")
   196  
   197  			outputDir, err = ioutil.TempDir(rootPath, "i18n4go_integration")
   198  			Ω(err).ShouldNot(HaveOccurred())
   199  
   200  			fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package")
   201  			inputFilesPath = filepath.Join(fixturesPath, "d_option", "input_files")
   202  			expectedFilesPath = filepath.Join(fixturesPath, "d_option", "expected_output")
   203  
   204  			err = os.MkdirAll(filepath.Join(outputDir, "doption"), 0777)
   205  			Ω(err).ShouldNot(HaveOccurred())
   206  
   207  			CopyFile(filepath.Join(expectedFilesPath, "doption", "_test.go.en.json"), filepath.Join(outputDir, "doption", "test.go.en.json"))
   208  			CopyFile(filepath.Join(expectedFilesPath, "doption", "_test2.go.en.json"), filepath.Join(outputDir, "doption", "test2.go.en.json"))
   209  
   210  			session := Runi18n("-c",
   211  				"rewrite-package",
   212  				"-d", inputFilesPath,
   213  				"-o", outputDir,
   214  				"--ignore-regexp", "^[.]\\w+.go$", //Ignoring .*.go files, otherwise it defaults to ignoring *test*.go
   215  				"-v",
   216  			)
   217  
   218  			Ω(session.ExitCode()).Should(Equal(0))
   219  		})
   220  
   221  		It("adds T() callExprs wrapping string literals for all sources in the directory", func() {
   222  			expectedOutputFile := filepath.Join(expectedFilesPath, "test.go")
   223  			generatedOutputFile := filepath.Join(outputDir, "test.go")
   224  			CompareExpectedOutputToGeneratedOutput(expectedOutputFile, generatedOutputFile)
   225  
   226  			expectedOutputFile = filepath.Join(expectedFilesPath, "test2.go")
   227  			generatedOutputFile = filepath.Join(outputDir, "test2.go")
   228  			CompareExpectedOutputToGeneratedOutput(expectedOutputFile, generatedOutputFile)
   229  		})
   230  	})
   231  })