@@ -101,10 +101,25 @@ func TestUpgrade(t *testing.T) {
101101 }
102102 assertUpgraded (t , tmpDir , "0.2.0" )
103103 })
104+ t .Run ("should error if no assets found" , func (t * testing.T ) {
105+ httpmock .Activate (t )
106+ httpmock .RegisterResponder ("GET" , "https://api.github.com/repos/loilo-inc/canarycage/releases" ,
107+ httpmock .NewJsonResponderOrPanic (200 , []* github.RepositoryRelease {
108+ {
109+ TagName : github .String ("0.2.0" ),
110+ Assets : []* github.ReleaseAsset {
111+ {Name : github .String ("some_other_file.txt" )},
112+ },
113+ },
114+ }))
115+ u := NewUpgrader (logDI , & cageapp.UpgradeCmdInput {
116+ CurrentVersion : "0.1.0" ,
117+ })
118+ err := u .Upgrade (t .Context ())
119+ assert .EqualError (t , err , "failed to find assets for version 0.2.0" )
120+ })
104121 t .Run ("parse checksum error" , func (t * testing.T ) {
105- httpmock .Activate ()
106- defer httpmock .DeactivateAndReset ()
107-
122+ httpmock .Activate (t )
108123 httpmock .RegisterResponder ("GET" , "https://api.github.com/repos/loilo-inc/canarycage/releases" ,
109124 httpmock .NewJsonResponderOrPanic (200 , makeReleases ("0.1.0" , "0.2.0" )))
110125 httpmock .RegisterResponder ("GET" , "https://localhost/0.2.0/canarycage_0.2.0_checksums.txt" ,
@@ -115,6 +130,47 @@ func TestUpgrade(t *testing.T) {
115130 err := u .Upgrade (t .Context ())
116131 assert .EqualError (t , err , "invalid checksum line: invalid" )
117132 })
133+ }
134+
135+ func Test_findAssets (t * testing.T ) {
136+ t .Run ("should return assets" , func (t * testing.T ) {
137+ release := & github.RepositoryRelease {
138+ TagName : github .String ("0.2.0" ),
139+ Assets : []* github.ReleaseAsset {
140+ makeAsset ("0.2.0" , "canarycage_0.2.0_checksums.txt" ),
141+ makeAsset ("0.2.0" , binaryAssetName ),
142+ },
143+ }
144+ checksumAsset , binaryAsset , err := findAssets (release )
145+ assert .NoError (t , err )
146+ assert .Equal (t , "canarycage_0.2.0_checksums.txt" , checksumAsset .GetName ())
147+ assert .Equal (t , binaryAssetName , binaryAsset .GetName ())
148+ })
149+ t .Run ("should trim v from tag name" , func (t * testing.T ) {
150+ release := & github.RepositoryRelease {
151+ TagName : github .String ("v0.2.0" ),
152+ Assets : []* github.ReleaseAsset {
153+ makeAsset ("v0.2.0" , "canarycage_0.2.0_checksums.txt" ),
154+ makeAsset ("v0.2.0" , binaryAssetName ),
155+ },
156+ }
157+ checksumAsset , binaryAsset , err := findAssets (release )
158+ assert .NoError (t , err )
159+ assert .Equal (t , "canarycage_0.2.0_checksums.txt" , checksumAsset .GetName ())
160+ assert .Equal (t , binaryAssetName , binaryAsset .GetName ())
161+ })
162+ t .Run ("should return error if assets not found" , func (t * testing.T ) {
163+ release := & github.RepositoryRelease {
164+ TagName : github .String ("0.2.0" ),
165+ Assets : []* github.ReleaseAsset {
166+ makeAsset ("0.2.0" , "some_other_file.txt" ),
167+ },
168+ }
169+ checksumAsset , binaryAsset , err := findAssets (release )
170+ assert .Nil (t , checksumAsset )
171+ assert .Nil (t , binaryAsset )
172+ assert .EqualError (t , err , "failed to find assets for version 0.2.0" )
173+ })
118174
119175}
120176
@@ -144,8 +200,7 @@ func Test_findLatestRelease(t *testing.T) {
144200 assert .EqualError (t , err , "no releases found" )
145201 })
146202 t .Run ("should return error if ListReleases failed" , func (t * testing.T ) {
147- httpmock .Activate ()
148- defer httpmock .DeactivateAndReset ()
203+ httpmock .Activate (t )
149204 httpmock .RegisterResponder ("GET" , "https://api.github.com/repos/loilo-inc/canarycage/releases" ,
150205 httpmock .NewErrorResponder (fmt .Errorf ("error" )))
151206 release , err := findLatestRelease (t .Context (), false )
@@ -182,10 +237,7 @@ func makeReleases(tags ...string) []*github.RepositoryRelease {
182237func registerResponses (
183238 t * testing.T ,
184239 candidates ... string ) {
185-
186- httpmock .Activate ()
187- t .Cleanup (httpmock .DeactivateAndReset )
188-
240+ httpmock .Activate (t )
189241 respond := func (req * http.Request ) (* http.Response , error ) {
190242 f , err := os .Open ("testdata" + req .URL .Path )
191243 if err != nil {
0 commit comments