<commit_before>package rewrite_package_test

import (
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"

	. "github.com/maximilien/i18n4go/integration/test_helpers"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("rewrite-package -f filename", func() {
	var (
		outputDir         string
		rootPath          string
		fixturesPath      string
		inputFilesPath    string
		expectedFilesPath string
	)

	AfterEach(func() {
		err := os.RemoveAll(outputDir)
		Ω(err).ShouldNot(HaveOccurred())
	})

	Context("no -o option passed, so input file is rewritten", func() {
		BeforeEach(func() {
			dir, err := os.Getwd()
			Ω(err).ShouldNot(HaveOccurred())
			rootPath = filepath.Join(dir, "..", "..")

			outputDir, err = ioutil.TempDir(rootPath, "gi18n_integration")
			Ω(err).ShouldNot(HaveOccurred())

			fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package")
			inputFilesPath = filepath.Join(fixturesPath, "f_option", "input_files")
			expectedFilesPath = filepath.Join(fixturesPath, "f_option", "expected_output")

			CopyFile(filepath.Join(inputFilesPath, "test.go"), filepath.Join(outputDir, "test.go"))

			session := Runi18n("-c", "rewrite-package",
				"-f", filepath.Join(outputDir, "test.go"),
				"--root-path", outputDir,
				"-v",
			)

			Ω(session.ExitCode()).Should(Equal(0))
		})

		It("overwrites the input file with T() wrappers around strings", func() {
			expectedOutputFile := filepath.Join(expectedFilesPath, "test.go")
			bytes, err := ioutil.ReadFile(expectedOutputFile)
			Ω(err).ShouldNot(HaveOccurred())

			expectedOutput := string(bytes)

			generatedOutputFile := filepath.Join(outputDir, "test.go")
			bytes, err = ioutil.ReadFile(generatedOutputFile)
			Ω(err).ShouldNot(HaveOccurred())

			actualOutput := string(bytes)

			Ω(actualOutput).Should(Equal(expectedOutput))
		})

		It("adds T func declaration and i18n init() func in the same directory as input file", func() {
			initFile := filepath.Join(outputDir, "i18n_init.go")
			expectedBytes, err := ioutil.ReadFile(initFile)
			Ω(err).ShouldNot(HaveOccurred())
			Ω(expectedBytes).ShouldNot(Equal(""))
		})
	})

	Context("all strings to rewrite are simple strings", func() {
		BeforeEach(func() {
			dir, err := os.Getwd()
			Ω(err).ShouldNot(HaveOccurred())
			rootPath = filepath.Join(dir, "..", "..")

			outputDir, err = ioutil.TempDir(rootPath, "gi18n_integration")
			Ω(err).ShouldNot(HaveOccurred())

			fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package")
			inputFilesPath = filepath.Join(fixturesPath, "f_option", "input_files")
			expectedFilesPath = filepath.Join(fixturesPath, "f_option", "expected_output")

			session := Runi18n("-c",
				"rewrite-package",
				"-f", filepath.Join(inputFilesPath, "test.go"),
				"-o", outputDir,
				"--root-path", rootPath,
				"-v",
			)

			Ω(session.ExitCode()).Should(Equal(0))
		})

		It("rewrites the input file with T() wrappers around strings", func() {
			expectedOutputFile := filepath.Join(expectedFilesPath, "test.go")
			bytes, err := ioutil.ReadFile(expectedOutputFile)
			Ω(err).ShouldNot(HaveOccurred())

			expectedOutput := string(bytes)

			generatedOutputFile := filepath.Join(outputDir, "test.go")
			bytes, err = ioutil.ReadFile(generatedOutputFile)
			Ω(err).ShouldNot(HaveOccurred())

			actualOutput := string(bytes)

			Ω(actualOutput).Should(Equal(expectedOutput))
		})

		It("adds T func declaration and i18n init() func", func() {
			initFile := filepath.Join(outputDir, "i18n_init.go")
			expectedBytes, err := ioutil.ReadFile(initFile)
			Ω(err).ShouldNot(HaveOccurred())
			expected := strings.TrimSpace(string(expectedBytes))

			expectedInitFile := filepath.Join(expectedFilesPath, "i18n_init.go")
			actualBytes, err := ioutil.ReadFile(expectedInitFile)
			Ω(err).ShouldNot(HaveOccurred())
			actual := strings.TrimSpace(string(actualBytes))

			Ω(actual).Should(Equal(expected))
		})
	})

	Context("Github issue#13: rewrites simple strings that are parameters to function call but not the first", func() {
		BeforeEach(func() {
			dir, err := os.Getwd()
			Ω(err).ShouldNot(HaveOccurred())
			rootPath = filepath.Join(dir, "..", "..")

			outputDir, err = ioutil.TempDir(rootPath, "gi18n_integration")
			Ω(err).ShouldNot(HaveOccurred())

			fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package")
			inputFilesPath = filepath.Join(fixturesPath, "f_option", "input_files")
			expectedFilesPath = filepath.Join(fixturesPath, "f_option", "expected_output")

			session := Runi18n("-c",
				"rewrite-package",
				"-f", filepath.Join(inputFilesPath, "issue13.go"),
				"-o", outputDir,
				"--root-path", rootPath,
				"-v",
			)

			Ω(session.ExitCode()).Should(Equal(0))
		})

		It("rewrites the input file with T() wrappers around strings in function call", func() {
			expectedOutputFile := filepath.Join(expectedFilesPath, "issue13.go")
			bytes, err := ioutil.ReadFile(expectedOutputFile)
			Ω(err).ShouldNot(HaveOccurred())

			expectedOutput := string(bytes)

			generatedOutputFile := filepath.Join(outputDir, "issue13.go")
			bytes, err = ioutil.ReadFile(generatedOutputFile)
			Ω(err).ShouldNot(HaveOccurred())

			actualOutput := string(bytes)

			Ω(actualOutput).Should(Equal(expectedOutput))
		})
	})

	Context("strings to rewrite contain complex templated strings", func() {
		BeforeEach(func() {
			dir, err := os.Getwd()
			Ω(err).ShouldNot(HaveOccurred())
			rootPath = filepath.Join(dir, "..", "..")

			outputDir, err = ioutil.TempDir(rootPath, "gi18n_integration")
			Ω(err).ShouldNot(HaveOccurred())

			fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package")
			inputFilesPath = filepath.Join(fixturesPath, "f_option", "input_files")
			expectedFilesPath = filepath.Join(fixturesPath, "f_option", "expected_output")

			session := Runi18n("-c",
				"rewrite-package",
				"-f", filepath.Join(inputFilesPath, "test_templated_strings.go"),
				"-o", filepath.Join(outputDir),
				"-v",
			)

			Ω(session.ExitCode()).Should(Equal(0))
		})

		It("rewrites the input file with T() wrappers around all (simple and templated) strings", func() {
			expectedOutputFile := filepath.Join(expectedFilesPath, "test_templated_strings.go")
			bytes, err := ioutil.ReadFile(expectedOutputFile)
			Ω(err).ShouldNot(HaveOccurred())

			expectedOutput := string(bytes)<commit_msg>removing failing test for issue #13 since moved to branch issue13<commit_after>package rewrite_package_test

import (
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"

	. "github.com/maximilien/i18n4go/integration/test_helpers"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("rewrite-package -f filename", func() {
	var (
		outputDir         string
		rootPath          string
		fixturesPath      string
		inputFilesPath    string
		expectedFilesPath string
	)

	AfterEach(func() {
		err := os.RemoveAll(outputDir)
		Ω(err).ShouldNot(HaveOccurred())
	})

	Context("no -o option passed, so input file is rewritten", func() {
		BeforeEach(func() {
			dir, err := os.Getwd()
			Ω(err).ShouldNot(HaveOccurred())
			rootPath = filepath.Join(dir, "..", "..")

			outputDir, err = ioutil.TempDir(rootPath, "gi18n_integration")
			Ω(err).ShouldNot(HaveOccurred())

			fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package")
			inputFilesPath = filepath.Join(fixturesPath, "f_option", "input_files")
			expectedFilesPath = filepath.Join(fixturesPath, "f_option", "expected_output")

			CopyFile(filepath.Join(inputFilesPath, "test.go"), filepath.Join(outputDir, "test.go"))

			session := Runi18n("-c", "rewrite-package",
				"-f", filepath.Join(outputDir, "test.go"),
				"--root-path", outputDir,
				"-v",
			)

			Ω(session.ExitCode()).Should(Equal(0))
		})

		It("overwrites the input file with T() wrappers around strings", func() {
			expectedOutputFile := filepath.Join(expectedFilesPath, "test.go")
			bytes, err := ioutil.ReadFile(expectedOutputFile)
			Ω(err).ShouldNot(HaveOccurred())

			expectedOutput := string(bytes)

			generatedOutputFile := filepath.Join(outputDir, "test.go")
			bytes, err = ioutil.ReadFile(generatedOutputFile)
			Ω(err).ShouldNot(HaveOccurred())

			actualOutput := string(bytes)

			Ω(actualOutput).Should(Equal(expectedOutput))
		})

		It("adds T func declaration and i18n init() func in the same directory as input file", func() {
			initFile := filepath.Join(outputDir, "i18n_init.go")
			expectedBytes, err := ioutil.ReadFile(initFile)
			Ω(err).ShouldNot(HaveOccurred())
			Ω(expectedBytes).ShouldNot(Equal(""))
		})
	})

	Context("all strings to rewrite are simple strings", func() {
		BeforeEach(func() {
			dir, err := os.Getwd()
			Ω(err).ShouldNot(HaveOccurred())
			rootPath = filepath.Join(dir, "..", "..")

			outputDir, err = ioutil.TempDir(rootPath, "gi18n_integration")
			Ω(err).ShouldNot(HaveOccurred())

			fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package")
			inputFilesPath = filepath.Join(fixturesPath, "f_option", "input_files")
			expectedFilesPath = filepath.Join(fixturesPath, "f_option", "expected_output")

			session := Runi18n("-c",
				"rewrite-package",
				"-f", filepath.Join(inputFilesPath, "test.go"),
				"-o", outputDir,
				"--root-path", rootPath,
				"-v",
			)

			Ω(session.ExitCode()).Should(Equal(0))
		})

		It("rewrites the input file with T() wrappers around strings", func() {
			expectedOutputFile := filepath.Join(expectedFilesPath, "test.go")
			bytes, err := ioutil.ReadFile(expectedOutputFile)
			Ω(err).ShouldNot(HaveOccurred())

			expectedOutput := string(bytes)

			generatedOutputFile := filepath.Join(outputDir, "test.go")
			bytes, err = ioutil.ReadFile(generatedOutputFile)
			Ω(err).ShouldNot(HaveOccurred())

			actualOutput := string(bytes)

			Ω(actualOutput).Should(Equal(expectedOutput))
		})

		It("adds T func declaration and i18n init() func", func() {
			initFile := filepath.Join(outputDir, "i18n_init.go")
			expectedBytes, err := ioutil.ReadFile(initFile)
			Ω(err).ShouldNot(HaveOccurred())
			expected := strings.TrimSpace(string(expectedBytes))

			expectedInitFile := filepath.Join(expectedFilesPath, "i18n_init.go")
			actualBytes, err := ioutil.ReadFile(expectedInitFile)
			Ω(err).ShouldNot(HaveOccurred())
			actual := strings.TrimSpace(string(actualBytes))

			Ω(actual).Should(Equal(expected))
		})
	})

	Context("strings to rewrite contain complex templated strings", func() {
		BeforeEach(func() {
			dir, err := os.Getwd()
			Ω(err).ShouldNot(HaveOccurred())
			rootPath = filepath.Join(dir, "..", "..")

			outputDir, err = ioutil.TempDir(rootPath, "gi18n_integration")
			Ω(err).ShouldNot(HaveOccurred())

			fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package")
			inputFilesPath = filepath.Join(fixturesPath, "f_option", "input_files")
			expectedFilesPath = filepath.Join(fixturesPath, "f_option", "expected_output")

			session := Runi18n("-c",
				"rewrite-package",
				"-f", filepath.Join(inputFilesPath, "test_templated_strings.go"),
				"-o", filepath.Join(outputDir),
				"-v",
			)

			Ω(session.ExitCode()).Should(Equal(0))
		})

		It("rewrites the input file with T() wrappers around all (simple and templated) strings", func() {
			expectedOutputFile := filepath.Join(expectedFilesPath, "test_templated_strings.go")
			bytes, err := ioutil.ReadFile(expectedOutputFile)
			Ω(err).ShouldNot(HaveOccurred())

			expectedOutput := string(bytes)
{'fraction_non_alphanumeric': 0.15968517708788807, 'fraction_numerical': 0.006383909051158723, 'mean_word_length': 4.538014527845037, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3860502', 'n_tokens_mistral': 4454, 'n_tokens_neox': 3846, 'n_words': 687}