@@ -4,8 +4,10 @@ import (
4
4
"net/http/httptest"
5
5
"os"
6
6
"path/filepath"
7
+ "strings"
7
8
"testing"
8
9
10
+ "github.com/everdrone/grab/internal/config"
9
11
"github.com/everdrone/grab/internal/utils"
10
12
tu "github.com/everdrone/grab/testutils"
11
13
)
@@ -31,12 +33,14 @@ func TestGetCmd(t *testing.T) {
31
33
defer ts .Close ()
32
34
33
35
tests := []struct {
34
- Name string
35
- Args []string
36
- Config string
37
- ConfigPath string
38
- CheckFiles map [string ]string
39
- WantErr bool
36
+ Name string
37
+ Args []string
38
+ Config string
39
+ ConfigPath string
40
+ CheckFiles map [string ]string
41
+ WantErr bool
42
+ UseBadReleaseURL bool
43
+ WantUpdateMessage bool
40
44
}{
41
45
{
42
46
Name : "invalid config" ,
@@ -116,7 +120,7 @@ site "example" {
116
120
WantErr : true ,
117
121
},
118
122
{
119
- Name : "can download" ,
123
+ Name : "can download and get updates " ,
120
124
Args : []string {ts .URL + "/gallery/123/test" , "-s" },
121
125
Config : `
122
126
global {
@@ -138,11 +142,42 @@ site "example" {
138
142
filepath .Join (globalLocation , "example" , "b.jpg" ): "imageb" ,
139
143
filepath .Join (globalLocation , "example" , "c.jpg" ): "imagec" ,
140
144
},
141
- ConfigPath : filepath .Join (root , "grab.hcl" ),
142
- WantErr : false ,
145
+ ConfigPath : filepath .Join (root , "grab.hcl" ),
146
+ WantErr : false ,
147
+ WantUpdateMessage : true ,
148
+ },
149
+ {
150
+ Name : "can download without updates" ,
151
+ Args : []string {ts .URL + "/gallery/123/test" , "-s" },
152
+ Config : `
153
+ global {
154
+ location = "` + escapedGlobalLocation + `"
155
+ }
156
+
157
+ site "example" {
158
+ test = "http:\\/\\/127\\.0\\.0\\.1:\\d+"
159
+
160
+ asset "image" {
161
+ pattern = "<img src=\"([^\"]+/img/[^\"]+)"
162
+ capture = 1
163
+ find_all = true
164
+ }
165
+ }
166
+ ` ,
167
+ CheckFiles : map [string ]string {
168
+ filepath .Join (globalLocation , "example" , "a.jpg" ): "imagea" ,
169
+ filepath .Join (globalLocation , "example" , "b.jpg" ): "imageb" ,
170
+ filepath .Join (globalLocation , "example" , "c.jpg" ): "imagec" ,
171
+ },
172
+ ConfigPath : filepath .Join (root , "grab.hcl" ),
173
+ WantErr : false ,
174
+ UseBadReleaseURL : true ,
175
+ WantUpdateMessage : false ,
143
176
},
144
177
}
145
178
179
+ updateMsg := "\n \n A new release of grab is available: " + config .Version + " → 987.654.321\n https://github.com/everdrone/grab/releases/latest\n \n "
180
+
146
181
for _ , tt := range tests {
147
182
t .Run (tt .Name , func (tc * testing.T ) {
148
183
// reset fs
@@ -152,10 +187,16 @@ site "example" {
152
187
utils .AFS .MkdirAll (globalLocation , os .ModePerm )
153
188
utils .AFS .WriteFile (tt .ConfigPath , []byte (tt .Config ), os .ModePerm )
154
189
155
- c , _ , _ , err := tu .ExecuteCommandErr (RootCmd , append ([]string {"get" }, tt .Args ... )... )
190
+ // set releases url
191
+ config .LatestReleaseURL = ts .URL + "/releases"
192
+ if tt .UseBadReleaseURL {
193
+ config .LatestReleaseURL = ts .URL + "/bad_releases"
194
+ }
195
+
196
+ c , out , _ , err := tu .ExecuteCommandErr (RootCmd , append ([]string {"get" }, tt .Args ... )... )
156
197
157
- if c .Name () != "get" {
158
- tc .Fatalf ("got: %s, want: 'find " , c .Name ())
198
+ if c .Name () != GetCmd . Name () {
199
+ tc .Fatalf ("got: %s, want: %s " , c . Name (), GetCmd .Name ())
159
200
}
160
201
161
202
if tt .CheckFiles != nil {
@@ -166,6 +207,16 @@ site "example" {
166
207
}
167
208
}
168
209
210
+ if tt .WantUpdateMessage {
211
+ if ! strings .Contains (out , updateMsg ) {
212
+ tc .Errorf ("got: %s, want: %s" , out , updateMsg )
213
+ }
214
+ } else {
215
+ if strings .Contains (out , updateMsg ) {
216
+ tc .Errorf ("got: %s, want: %s" , out , updateMsg )
217
+ }
218
+ }
219
+
169
220
if (err != nil ) != tt .WantErr {
170
221
tc .Errorf ("got: %v, want: %v" , err , tt .WantErr )
171
222
}
0 commit comments