github.com/CharukaK/i18n4go@v0.6.0/integration/test_helpers/helpers.go (about)

     1  // Copyright © 2015-2023 The Knative Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package test_helpers
    16  
    17  import (
    18  	"bufio"
    19  	"encoding/json"
    20  	"fmt"
    21  	"io"
    22  	"io/ioutil"
    23  	"os"
    24  	"os/exec"
    25  	"path/filepath"
    26  	"reflect"
    27  	"strings"
    28  
    29  	. "github.com/onsi/ginkgo"
    30  	. "github.com/onsi/gomega"
    31  	. "github.com/onsi/gomega/gexec"
    32  )
    33  
    34  func CompareExpectedToGeneratedPo(expectedFilePath string, generatedFilePath string) {
    35  	expectedTranslation := ReadPo(expectedFilePath)
    36  	generatedTranslation := ReadPo(generatedFilePath)
    37  
    38  	Ω(reflect.DeepEqual(expectedTranslation, generatedTranslation)).Should(BeTrue())
    39  }
    40  
    41  func CompareExpectedToGeneratedTraslationJson(expectedFilePath string, generatedFilePath string) {
    42  	expectedTranslation := ReadJson(expectedFilePath)
    43  	generatedTranslation := ReadJson(generatedFilePath)
    44  
    45  	jsonExpected, _ := json.Marshal(expectedTranslation)
    46  	jsonGenerated, _ := json.Marshal(generatedTranslation)
    47  
    48  	Ω(reflect.DeepEqual(expectedTranslation, generatedTranslation)).Should(BeTrue(),
    49  		fmt.Sprintf("Expected\n%v\nto equal\n%v\n", string(jsonGenerated), string(jsonExpected)))
    50  }
    51  
    52  func CompareExpectedToGeneratedExtendedJson(expectedFilePath string, generatedFilePath string) {
    53  	expectedTranslation := ReadJsonExtended(expectedFilePath)
    54  	generatedTranslation := ReadJsonExtended(generatedFilePath)
    55  
    56  	Ω(reflect.DeepEqual(expectedTranslation, generatedTranslation)).Should(BeTrue(), fmt.Sprintf("expected extracted json %s to exactly match %s", expectedFilePath, generatedFilePath))
    57  }
    58  
    59  func GetFilePath(input_dir string, fileName string) string {
    60  	return filepath.Join(os.Getenv("PWD"), input_dir, fileName)
    61  }
    62  
    63  func RemoveAllFiles(args ...string) {
    64  	for _, arg := range args {
    65  		os.Remove(arg)
    66  	}
    67  }
    68  
    69  func Runi18n(args ...string) *Session {
    70  	session := RunCommand(I18n4goExec, args...)
    71  	return session
    72  }
    73  
    74  func RunCommand(cmd string, args ...string) *Session {
    75  	command := exec.Command(cmd, args...)
    76  	session, err := Start(command, GinkgoWriter, GinkgoWriter)
    77  	Ω(err).ShouldNot(HaveOccurred())
    78  	session.Wait()
    79  	return session
    80  }
    81  
    82  func ReadPo(fileName string) map[string]string {
    83  	file, _ := os.Open(fileName)
    84  	r := bufio.NewReader(file)
    85  
    86  	myMap := make(map[string]string)
    87  	for rawLine, _, err := r.ReadLine(); err != io.EOF; rawLine, _, err = r.ReadLine() {
    88  		if err != nil {
    89  			Fail(fmt.Sprintf("Error: %v", err))
    90  		}
    91  
    92  		line := string(rawLine)
    93  		if strings.HasPrefix(line, "msgid") {
    94  			rawLine, _, err = r.ReadLine()
    95  			if err != nil {
    96  				Fail(fmt.Sprintf("Error: %v", err))
    97  			}
    98  
    99  			myMap[line] = string(rawLine)
   100  		}
   101  	}
   102  
   103  	return myMap
   104  }
   105  
   106  func ReadJson(fileName string) map[string]string {
   107  	fileByte, err := ioutil.ReadFile(fileName)
   108  	if err != nil {
   109  		Fail("Cannot open json file:" + fileName)
   110  	}
   111  
   112  	var b interface{}
   113  
   114  	if err := json.Unmarshal(fileByte, &b); err != nil {
   115  		Fail(fmt.Sprintf("Cannot unmarshal: %v", err))
   116  	}
   117  
   118  	myMap := make(map[string]string)
   119  
   120  	for _, value := range b.([]interface{}) {
   121  		valueMap := value.(map[string]interface{})
   122  		myMap[valueMap["id"].(string)] = valueMap["translation"].(string)
   123  	}
   124  
   125  	return myMap
   126  }
   127  
   128  func ReadJsonExtended(fileName string) map[string]map[string]string {
   129  	fileByte, err := ioutil.ReadFile(fileName)
   130  	if err != nil {
   131  		Fail("Cannot open json file:" + fileName)
   132  	}
   133  
   134  	var b interface{}
   135  
   136  	if err := json.Unmarshal(fileByte, &b); err != nil {
   137  		Fail(fmt.Sprintf("Cannot unmarshal: %v", err))
   138  	}
   139  
   140  	myMap := make(map[string]map[string]string)
   141  
   142  	for _, value := range b.([]interface{}) {
   143  		valueMap := value.(map[string]interface{})
   144  
   145  		dataMap := make(map[string]string)
   146  
   147  		for key, val := range valueMap {
   148  			switch val.(type) {
   149  			case string:
   150  				dataMap[key] = val.(string)
   151  			case float64:
   152  				dataMap[key] = fmt.Sprintf("%v", int(val.(float64)))
   153  			default:
   154  				fmt.Println("We did something wrong", key)
   155  			}
   156  		}
   157  
   158  		myMap[valueMap["value"].(string)] = dataMap
   159  	}
   160  
   161  	return myMap
   162  }
   163  
   164  func CopyFile(srcFile, destFile string) {
   165  	content, err := ioutil.ReadFile(srcFile)
   166  	Ω(err).ShouldNot(HaveOccurred())
   167  	err = ioutil.WriteFile(destFile, content, 0644)
   168  	Ω(err).ShouldNot(HaveOccurred())
   169  }
   170  
   171  func CompareExpectedOutputToGeneratedOutput(expectedOutputFile, generatedOutputFile string) {
   172  	bytes, err := ioutil.ReadFile(expectedOutputFile)
   173  	Ω(err).ShouldNot(HaveOccurred())
   174  
   175  	expectedOutput := string(bytes)
   176  
   177  	bytes, err = ioutil.ReadFile(generatedOutputFile)
   178  	Ω(err).ShouldNot(HaveOccurred())
   179  
   180  	actualOutput := string(bytes)
   181  	Ω(actualOutput).Should(Equal(expectedOutput))
   182  }