This repository has been archived by the owner on Dec 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpull_target_test.go
142 lines (125 loc) · 3.68 KB
/
pull_target_test.go
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"strings"
"testing"
"github.com/phrase/phraseapp-go/phraseapp"
)
func getBaseTarget() *Target {
target := &Target{
File: "./tests/<locale_code>.yml",
ProjectID: "project-id",
AccessToken: "access-token",
FileFormat: "yml",
Params: new(PullParams),
RemoteLocales: getBaseLocales(),
}
return target
}
func TestTargetFields(t *testing.T) {
target := getBaseTarget()
if target.File != "./tests/<locale_code>.yml" {
t.Errorf("Expected File to be %s and not %s", "./tests/<locale_code>.yml", target.File)
}
if target.AccessToken != "access-token" {
t.Errorf("Expected AccesToken to be %s and not %s", "access-token", target.AccessToken)
}
if target.ProjectID != "project-id" {
t.Errorf("Expected ProjectID to be %s and not %s", "project-id", target.ProjectID)
}
if target.FileFormat != "yml" {
t.Errorf("Expected FileFormat to be %s and not %s", "yml", target.FileFormat)
}
}
func TestPreconditions(t *testing.T) {
target := &Target{
File: "./tests/en.yml",
ProjectID: "project-id",
AccessToken: "access-token",
FileFormat: "yml",
Params: new(PullParams),
RemoteLocales: []*phraseapp.Locale{
{
Code: "en",
ID: "en-locale-id",
Name: "english",
},
{
Code: "de",
ID: "de-locale-id",
Name: "german",
},
},
}
// no information given
expect := "Could not find any locale information."
err := target.CheckPreconditions()
if err == nil {
t.Errorf("Expected to fail for pattern %q. Did not fail.", target.File)
} else if !strings.Contains(err.Error(), expect) {
t.Errorf("Expected to fail with %q got %q", expect, err)
}
// placeholder used
target.File = "some/path/<locale_code>.yml"
err = target.CheckPreconditions()
if err != nil {
t.Errorf("Should not have failed for pattern %q. Error was: %q", target.File, err)
}
// locale_id set
target.File = "some/path/en.yml"
target.Params.LocaleID = "en"
err = target.CheckPreconditions()
if err != nil {
t.Errorf("Should not have failed for pattern %q. Error was: %q", target.File, err)
}
// locale_id and placeholder set
expect = "Found 'locale_id' in params and a (<locale_code|locale_name>) placeholder."
target.File = "some/path/<locale_code>.yml"
target.Params.LocaleID = "en"
err = target.CheckPreconditions()
if err == nil {
t.Errorf("Expected to fail for pattern %q. Did not fail.", target.File)
} else if !strings.Contains(err.Error(), expect) {
t.Errorf("Expected to fail with %q got %q", expect, err)
}
// tag in placeholder but no tag provided
target.Params.LocaleID = "en"
target.File = "some/<tag>/en.yml"
expect = "Using <tag> placeholder but no tags were provided."
err = target.CheckPreconditions()
if err == nil {
t.Errorf("Expected to fail for pattern %q. Did not fail.", target.File)
} else if !strings.Contains(err.Error(), expect) {
t.Errorf("Expected to fail with %q got %q", expect, err)
}
}
func TestPlaceholderPreconditions(t *testing.T) {
target := getBaseTarget()
for _, file := range []string{
"",
"no_extension",
"./<locale_code>/<locale_code>.yml",
"./**/**/en.yml",
"./**/*/*/en.yml",
"./**/*/en.yml",
"./en.yml",
"./**/*/<locale_name>/<locale_code>/<tag>.yml",
} {
target.File = file
if err := target.CheckPreconditions(); err == nil {
t.Errorf("CheckPrecondition did not fail for pattern: '%s'", file)
}
}
for _, file := range []string{
"./<tag>/<locale_code>.yml",
"./<locale_name>/<locale_code>/<tag>.yml",
} {
target.File = file
target.Params.Tag = sPt("any tag")
if err := target.CheckPreconditions(); err != nil {
t.Errorf("CheckPrecondition should not fail with: %s", err.Error())
}
}
}
func sPt(s string) *string {
return &s
}