@@ -2,9 +2,11 @@ package txlib
2
2
3
3
import (
4
4
"fmt"
5
+ "io/ioutil"
5
6
"net/url"
6
7
"os"
7
8
"reflect"
9
+ "strings"
8
10
"testing"
9
11
10
12
"github.com/transifex/cli/internal/txlib/config"
@@ -82,3 +84,79 @@ func TestAddRemote(t *testing.T) {
82
84
t .Errorf ("Got request '%+v', expected '%+v'" , actual , expected )
83
85
}
84
86
}
87
+
88
+ func TestAddRemoteInvalidFileFormat (t * testing.T ) {
89
+ curDir , _ := os .Getwd ()
90
+ tempDir , _ := os .MkdirTemp ("" , "" )
91
+ defer os .RemoveAll (tempDir )
92
+ _ = os .Chdir (tempDir )
93
+ defer os .Chdir (curDir )
94
+
95
+ resourcesUrl := fmt .Sprintf (
96
+ "/resources?%s=%s" ,
97
+ url .QueryEscape ("filter[project]" ),
98
+ url .QueryEscape (projectId ),
99
+ )
100
+ i18nFormatsUrl := fmt .Sprintf (
101
+ "/i18n_formats?%s=%s" ,
102
+ url .QueryEscape ("filter[organization]" ),
103
+ url .QueryEscape ("o:orgslug" ),
104
+ )
105
+
106
+ // Capture stdout
107
+ oldStdout := os .Stdout
108
+ r , w , _ := os .Pipe ()
109
+ os .Stdout = w
110
+
111
+ mockData := jsonapi.MockData {
112
+ projectUrl : getProjectEndpoint (),
113
+ resourcesUrl : jsonapi .GetMockTextResponse (
114
+ `{"data": [{
115
+ "type": "resources",
116
+ "id": "o:orgslug:p:projslug:r:resslug",
117
+ "attributes": {"slug": "resslug"},
118
+ "relationships": {
119
+ "i18n_format": {"data": {"type": "i18n_formats", "id": "FILELESS"}}
120
+ }
121
+ }]}` ,
122
+ ),
123
+ i18nFormatsUrl : jsonapi .GetMockTextResponse (
124
+ `{"data": [{
125
+ "type": "i18n_formats",
126
+ "id": "PO",
127
+ "attributes": {"file_extensions": [".po"]}
128
+ }]}` ,
129
+ ),
130
+ }
131
+
132
+ api := jsonapi .GetTestConnection (mockData )
133
+ cfg := & config.Config {Local : & config.LocalConfig {}}
134
+
135
+ err := AddRemoteCommand (
136
+ cfg ,
137
+ & api ,
138
+ "https://app.transifex.com/orgslug/projslug/whatever/whatever/" ,
139
+ // Lets make the file filter a bit weird
140
+ "locale/<project_slug><project_slug>.<resource_slug>/<lang>.<ext>" ,
141
+ 50 ,
142
+ )
143
+ if err != nil {
144
+ t .Errorf ("%s" , err )
145
+ }
146
+
147
+ // Restore stdout
148
+ w .Close ()
149
+ os .Stdout = oldStdout
150
+ out , _ := ioutil .ReadAll (r )
151
+ r .Close ()
152
+
153
+ testSimpleGet (t , mockData , projectUrl )
154
+ testSimpleGet (t , mockData , resourcesUrl )
155
+ testSimpleGet (t , mockData , i18nFormatsUrl )
156
+
157
+ // Check if the expected error message was printed
158
+ expectedErrorMessage := "Resource o:orgslug:p:projslug:r:resslug skipped: Invalid file Format FILELESS"
159
+ if ! strings .Contains (string (out ), expectedErrorMessage ) {
160
+ t .Errorf ("Expected error message '%s' not found in printed output" , expectedErrorMessage )
161
+ }
162
+ }
0 commit comments