@@ -15,12 +15,12 @@ import (
1515 "github.com/arduino/go-updater/releaser"
1616)
1717
18- func TestPerformUpdate (t * testing.T ) {
18+ func TestApply (t * testing.T ) {
1919 tmpExec := CreateTmpExecutable (t , "successfulUpdate" , []byte {0xDE , 0xAD , 0xBE , 0xEF })
2020 defer tmpExec .cleanup ()
2121 client := CreateRelease (t , "2.0.0" , []byte {0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 })
2222
23- restartPath , err := checkForUpdates (tmpExec .targetPath , Version ("1.0.0" ), client )
23+ restartPath , err := apply (tmpExec .targetPath , releaser . Version ("1.0.0" ), client , DefaultUpgradeConfirmCb )
2424 require .NoError (t , err )
2525 require .NotEmpty (t , restartPath )
2626
@@ -29,21 +29,21 @@ func TestPerformUpdate(t *testing.T) {
2929 require .Equal (t , []byte {0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 }, data , "Updated binary content does not match expected content" )
3030}
3131
32- func TestNoUpdateRequired (t * testing.T ) {
32+ func TestApplyWithNoUpdate (t * testing.T ) {
3333 tmpExec := CreateTmpExecutable (t , "noUpdate" , []byte {0xDE , 0xAD , 0xBE , 0xEF })
3434 defer tmpExec .cleanup ()
3535 client := CreateRelease (t , "1.0.0" , []byte {0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 })
3636
37- result , err := checkForUpdates (tmpExec .targetPath , Version ("1.0.0" ), client )
37+ result , err := apply (tmpExec .targetPath , releaser . Version ("1.0.0" ), client , DefaultUpgradeConfirmCb )
3838 require .NoError (t , err )
3939 require .Equal (t , "" , result )
4040}
4141
42- func TestCleanUpOldFiles (t * testing.T ) {
42+ func TestApplyWillCleanUpFiles (t * testing.T ) {
4343 tmpExec := CreateTmpExecutable (t , "cleanUp" , []byte {0xDE , 0xAD , 0xBE , 0xEF })
4444 client := CreateRelease (t , "3.0.0" , []byte {0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 })
4545
46- result , err := checkForUpdates (tmpExec .targetPath , Version ("1.0.0" ), client )
46+ result , err := apply (tmpExec .targetPath , releaser . Version ("1.0.0" ), client , DefaultUpgradeConfirmCb )
4747 require .NoError (t , err )
4848 require .Equal (t , tmpExec .targetPath , result )
4949
@@ -66,6 +66,7 @@ type TmpExecutable struct {
6666}
6767
6868func CreateTmpExecutable (t * testing.T , binaryName string , content []byte ) TmpExecutable {
69+ // prefix the binary name with "test-" to put the folders in the .gitignore
6970 tmpDir := filepath .Join ("." , "test-" + binaryName )
7071 err := os .MkdirAll (tmpDir , 0755 )
7172 require .NoError (t , err )
@@ -85,7 +86,7 @@ func CreateTmpExecutable(t *testing.T, binaryName string, content []byte) TmpExe
8586 }
8687}
8788
88- func CreateRelease (t * testing.T , version Version , content []byte ) * releaser.Client {
89+ func CreateRelease (t * testing.T , version releaser. Version , content []byte ) * releaser.Client {
8990 tmpDir := t .TempDir ()
9091
9192 inputDir := filepath .Join (tmpDir , "input" )
@@ -96,7 +97,7 @@ func CreateRelease(t *testing.T, version Version, content []byte) *releaser.Clie
9697
9798 outputDir := filepath .Join (tmpDir , "output" )
9899
99- _ , err := releaser .CreateRelease (inputDir , releaser .NewPlatform ("linux" , "amd64" ), version . String () , outputDir )
100+ _ , err := releaser .CreateRelease (inputDir , releaser .NewPlatform ("linux" , "amd64" ), version , outputDir )
100101 require .NoError (t , err )
101102
102103 // check zip file exists and json manifest is created
@@ -140,6 +141,20 @@ func CreateRelease(t *testing.T, version Version, content []byte) *releaser.Clie
140141 return client
141142}
142143
144+ func CreateReleaseWithHTTPErrorResponse (t * testing.T , statusCode int ) * releaser.Client {
145+ return & releaser.Client {
146+ BaseURL : & url.URL {Scheme : "http" , Host : "example.com" },
147+ CmdName : "testcmd" ,
148+ HTTPClient : & mockHTTPClient {doFunc : func (req * http.Request ) (* http.Response , error ) {
149+ resp := & http.Response {
150+ StatusCode : statusCode ,
151+ Body : io .NopCloser (bytes .NewBufferString ("" )),
152+ }
153+ return resp , nil
154+ }},
155+ }
156+ }
157+
143158// mockHTTPClient implements releaser.HTTPDoer for testing.
144159type mockHTTPClient struct {
145160 doFunc func (req * http.Request ) (* http.Response , error )
0 commit comments