@@ -2,6 +2,11 @@ package modiff_test
2
2
3
3
//nolint:revive // test file
4
4
import (
5
+ "fmt"
6
+ "net/http"
7
+ "net/http/httptest"
8
+ "testing"
9
+
5
10
. "github.com/onsi/ginkgo/v2"
6
11
. "github.com/onsi/gomega"
7
12
"github.com/saschagrunert/go-modiff/pkg/modiff"
@@ -42,9 +47,10 @@ _Nothing has changed._
42
47
`
43
48
44
49
const (
45
- repo = "github.com/saschagrunert/go-modiff"
46
- from = "v0.10.0"
47
- to = "v0.11.0"
50
+ repo = "github.com/saschagrunert/go-modiff"
51
+ from = "v0.10.0"
52
+ to = "v0.11.0"
53
+ badRepo = "github.com/saschagrunert/go-modiff-invalid"
48
54
)
49
55
50
56
BeforeEach (func () {
@@ -120,4 +126,71 @@ _Nothing has changed._
120
126
Expect (err ).To (HaveOccurred ())
121
127
Expect (res ).To (BeEmpty ())
122
128
})
129
+
130
+ It ("should fail if the repository url is invalid" , func () {
131
+ // Given
132
+ config := modiff .NewConfig (badRepo , from , to , true , 1 )
133
+
134
+ // When
135
+ res , err := modiff .Run (config )
136
+
137
+ // Then
138
+ Expect (err ).To (HaveOccurred ())
139
+ Expect (res ).To (BeEmpty ())
140
+ })
123
141
})
142
+
143
+ func TestCheckURLValid (t * testing.T ) {
144
+ t .Parallel ()
145
+
146
+ tests := []struct {
147
+ name string
148
+ url string
149
+ expected bool
150
+ err error
151
+ client * http.Client
152
+ }{
153
+ {
154
+ name : "Valid URL" ,
155
+ url : "https://github.com/hashicorp/consul/compare/api/v1.18.0...api/v1.20.0" ,
156
+ expected : true ,
157
+ err : nil ,
158
+ },
159
+ {
160
+ name : "Invalid URL" ,
161
+ url : "https://github.com/hashicorp/consul/compare/v1.18.0...v1.20.0" ,
162
+ expected : false ,
163
+ err : nil ,
164
+ },
165
+ {
166
+ name : "Request Sending Error" ,
167
+ url : "invalid-url" ,
168
+ expected : false ,
169
+ err : fmt .Errorf ("error while sending request: " ),
170
+ },
171
+ }
172
+
173
+ for _ , tt := range tests {
174
+ tt := tt
175
+ t .Run (tt .name , func (t * testing.T ) {
176
+ t .Parallel ()
177
+ g := NewGomegaWithT (t )
178
+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
179
+ w .WriteHeader (http .StatusNotFound )
180
+ }))
181
+ defer server .Close ()
182
+
183
+ if tt .client == nil {
184
+ tt .client = & http.Client {}
185
+ }
186
+ valid , err := modiff .CheckURLValid (* tt .client , tt .url )
187
+ g .Expect (valid ).To (Equal (tt .expected ))
188
+ if tt .err != nil {
189
+ g .Expect (err ).To (HaveOccurred ())
190
+ g .Expect (err .Error ()).To (ContainSubstring (tt .err .Error ()))
191
+ } else {
192
+ g .Expect (err ).ToNot (HaveOccurred ())
193
+ }
194
+ })
195
+ }
196
+ }
0 commit comments