github.com/everlongproject/i18n4go@v0.2.7-0.20201028180611-670cbaceaa6b/integration/test_helpers/helpers.go (about)

     1  package test_helpers
     2  
     3  import (
     4  	"bufio"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io"
     8  	"io/ioutil"
     9  	"os"
    10  	"os/exec"
    11  	"path/filepath"
    12  	"reflect"
    13  	"strings"
    14  
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  	. "github.com/onsi/gomega/gexec"
    18  )
    19  
    20  func CompareExpectedToGeneratedPo(expectedFilePath string, generatedFilePath string) {
    21  	expectedTranslation := ReadPo(expectedFilePath)
    22  	generatedTranslation := ReadPo(generatedFilePath)
    23  
    24  	Ω(reflect.DeepEqual(expectedTranslation, generatedTranslation)).Should(BeTrue())
    25  }
    26  
    27  func CompareExpectedToGeneratedTraslationJson(expectedFilePath string, generatedFilePath string) {
    28  	expectedTranslation := ReadJson(expectedFilePath)
    29  	generatedTranslation := ReadJson(generatedFilePath)
    30  
    31  	jsonExpected, _ := json.Marshal(expectedTranslation)
    32  	jsonGenerated, _ := json.Marshal(generatedTranslation)
    33  
    34  	Ω(reflect.DeepEqual(expectedTranslation, generatedTranslation)).Should(BeTrue(),
    35  		fmt.Sprintf("Expected\n%v\nto equal\n%v\n", string(jsonGenerated), string(jsonExpected)))
    36  }
    37  
    38  func CompareExpectedToGeneratedExtendedJson(expectedFilePath string, generatedFilePath string) {
    39  	expectedTranslation := ReadJsonExtended(expectedFilePath)
    40  	generatedTranslation := ReadJsonExtended(generatedFilePath)
    41  
    42  	Ω(reflect.DeepEqual(expectedTranslation, generatedTranslation)).Should(BeTrue(), fmt.Sprintf("expected extracted json %s to exactly match %s", expectedFilePath, generatedFilePath))
    43  }
    44  
    45  func GetFilePath(input_dir string, fileName string) string {
    46  	return filepath.Join(os.Getenv("PWD"), input_dir, fileName)
    47  }
    48  
    49  func RemoveAllFiles(args ...string) {
    50  	for _, arg := range args {
    51  		os.Remove(arg)
    52  	}
    53  }
    54  
    55  func Runi18n(args ...string) *Session {
    56  	session := RunCommand(I18n4goExec, args...)
    57  	return session
    58  }
    59  
    60  func RunCommand(cmd string, args ...string) *Session {
    61  	command := exec.Command(cmd, args...)
    62  	session, err := Start(command, GinkgoWriter, GinkgoWriter)
    63  	Ω(err).ShouldNot(HaveOccurred())
    64  	session.Wait()
    65  	return session
    66  }
    67  
    68  func ReadPo(fileName string) map[string]string {
    69  	file, _ := os.Open(fileName)
    70  	r := bufio.NewReader(file)
    71  
    72  	myMap := make(map[string]string)
    73  	for rawLine, _, err := r.ReadLine(); err != io.EOF; rawLine, _, err = r.ReadLine() {
    74  		if err != nil {
    75  			Fail(fmt.Sprintf("Error: %v", err))
    76  		}
    77  
    78  		line := string(rawLine)
    79  		if strings.HasPrefix(line, "msgid") {
    80  			rawLine, _, err = r.ReadLine()
    81  			if err != nil {
    82  				Fail(fmt.Sprintf("Error: %v", err))
    83  			}
    84  
    85  			myMap[line] = string(rawLine)
    86  		}
    87  	}
    88  
    89  	return myMap
    90  }
    91  
    92  func ReadJson(fileName string) map[string]string {
    93  	fileByte, err := ioutil.ReadFile(fileName)
    94  	if err != nil {
    95  		Fail("Cannot open json file:" + fileName)
    96  	}
    97  
    98  	var b interface{}
    99  
   100  	if err := json.Unmarshal(fileByte, &b); err != nil {
   101  		Fail(fmt.Sprintf("Cannot unmarshal: %v", err))
   102  	}
   103  
   104  	myMap := make(map[string]string)
   105  
   106  	for _, value := range b.([]interface{}) {
   107  		valueMap := value.(map[string]interface{})
   108  		myMap[valueMap["id"].(string)] = valueMap["translation"].(string)
   109  	}
   110  
   111  	return myMap
   112  }
   113  
   114  func ReadJsonExtended(fileName string) map[string]map[string]string {
   115  	fileByte, err := ioutil.ReadFile(fileName)
   116  	if err != nil {
   117  		Fail("Cannot open json file:" + fileName)
   118  	}
   119  
   120  	var b interface{}
   121  
   122  	if err := json.Unmarshal(fileByte, &b); err != nil {
   123  		Fail(fmt.Sprintf("Cannot unmarshal: %v", err))
   124  	}
   125  
   126  	myMap := make(map[string]map[string]string)
   127  
   128  	for _, value := range b.([]interface{}) {
   129  		valueMap := value.(map[string]interface{})
   130  
   131  		dataMap := make(map[string]string)
   132  
   133  		for key, val := range valueMap {
   134  			switch val.(type) {
   135  			case string:
   136  				dataMap[key] = val.(string)
   137  			case float64:
   138  				dataMap[key] = fmt.Sprintf("%v", int(val.(float64)))
   139  			default:
   140  				fmt.Println("We did something wrong", key)
   141  			}
   142  		}
   143  
   144  		myMap[valueMap["value"].(string)] = dataMap
   145  	}
   146  
   147  	return myMap
   148  }
   149  
   150  func CopyFile(srcFile, destFile string) {
   151  	content, err := ioutil.ReadFile(srcFile)
   152  	Ω(err).ShouldNot(HaveOccurred())
   153  	err = ioutil.WriteFile(destFile, content, 0644)
   154  	Ω(err).ShouldNot(HaveOccurred())
   155  }
   156  
   157  func CompareExpectedOutputToGeneratedOutput(expectedOutputFile, generatedOutputFile string) {
   158  	bytes, err := ioutil.ReadFile(expectedOutputFile)
   159  	Ω(err).ShouldNot(HaveOccurred())
   160  
   161  	expectedOutput := string(bytes)
   162  
   163  	bytes, err = ioutil.ReadFile(generatedOutputFile)
   164  	Ω(err).ShouldNot(HaveOccurred())
   165  
   166  	actualOutput := string(bytes)
   167  	Ω(actualOutput).Should(Equal(expectedOutput))
   168  }