summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/nicksnyder/go-i18n/goi18n/merge_command_test.go
blob: 37e46518ba8c6b305445018761ab0a5178344afc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main

import (
	"bytes"
	"io/ioutil"
	"os"
	"testing"
)

func TestMergeExecuteJSON(t *testing.T) {
	files := []string{
		"testdata/input/en-us.one.json",
		"testdata/input/en-us.two.json",
		"testdata/input/fr-fr.json",
		"testdata/input/ar-ar.one.json",
		"testdata/input/ar-ar.two.json",
	}
	testMergeExecute(t, files)
}

func TestMergeExecuteYAML(t *testing.T) {
	files := []string{
		"testdata/input/yaml/en-us.one.yaml",
		"testdata/input/yaml/en-us.two.json",
		"testdata/input/yaml/fr-fr.json",
		"testdata/input/yaml/ar-ar.one.json",
		"testdata/input/yaml/ar-ar.two.json",
	}
	testMergeExecute(t, files)
}

func testMergeExecute(t *testing.T, files []string) {
	resetDir(t, "testdata/output")

	mc := &mergeCommand{
		translationFiles: files,
		sourceLanguage:   "en-us",
		outdir:           "testdata/output",
		format:           "json",
	}
	if err := mc.execute(); err != nil {
		t.Fatal(err)
	}

	expectEqualFiles(t, "testdata/output/en-us.all.json", "testdata/expected/en-us.all.json")
	expectEqualFiles(t, "testdata/output/ar-ar.all.json", "testdata/expected/ar-ar.all.json")
	expectEqualFiles(t, "testdata/output/fr-fr.all.json", "testdata/expected/fr-fr.all.json")
	expectEqualFiles(t, "testdata/output/en-us.untranslated.json", "testdata/expected/en-us.untranslated.json")
	expectEqualFiles(t, "testdata/output/ar-ar.untranslated.json", "testdata/expected/ar-ar.untranslated.json")
	expectEqualFiles(t, "testdata/output/fr-fr.untranslated.json", "testdata/expected/fr-fr.untranslated.json")
}

func resetDir(t *testing.T, dir string) {
	if err := os.RemoveAll(dir); err != nil {
		t.Fatal(err)
	}
	if err := os.Mkdir(dir, 0777); err != nil {
		t.Fatal(err)
	}
}

func expectEqualFiles(t *testing.T, expectedName, actualName string) {
	actual, err := ioutil.ReadFile(actualName)
	if err != nil {
		t.Fatal(err)
	}
	expected, err := ioutil.ReadFile(expectedName)
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(actual, expected) {
		t.Fatalf("contents of files did not match: %s, %s", expectedName, actualName)
	}
}