github.com/supabase/cli@v1.168.1/internal/utils/release_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/go-errors/errors"
     9  	"github.com/google/go-github/v62/github"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/supabase/cli/internal/testing/apitest"
    12  	"gopkg.in/h2non/gock.v1"
    13  )
    14  
    15  func TestLatestRelease(t *testing.T) {
    16  	t.Run("fetches latest release", func(t *testing.T) {
    17  		// Setup api mock
    18  		defer gock.OffAll()
    19  		gock.New("https://api.github.com").
    20  			Get("/repos/supabase/cli/releases/latest").
    21  			Reply(http.StatusOK).
    22  			JSON(github.RepositoryRelease{TagName: Ptr("v2")})
    23  		// Run test
    24  		version, err := GetLatestRelease(context.Background())
    25  		// Check error
    26  		assert.NoError(t, err)
    27  		assert.Equal(t, version, "v2")
    28  		assert.Empty(t, apitest.ListUnmatchedRequests())
    29  	})
    30  
    31  	t.Run("ignores missing version", func(t *testing.T) {
    32  		// Setup api mock
    33  		defer gock.OffAll()
    34  		gock.New("https://api.github.com").
    35  			Get("/repos/supabase/cli/releases/latest").
    36  			Reply(http.StatusOK).
    37  			JSON(github.RepositoryRelease{})
    38  		// Run test
    39  		version, err := GetLatestRelease(context.Background())
    40  		// Check error
    41  		assert.NoError(t, err)
    42  		assert.Empty(t, version)
    43  		assert.Empty(t, apitest.ListUnmatchedRequests())
    44  	})
    45  
    46  	t.Run("throws error on network error", func(t *testing.T) {
    47  		errNetwork := errors.New("network error")
    48  		// Setup api mock
    49  		defer gock.OffAll()
    50  		gock.New("https://api.github.com").
    51  			Get("/repos/supabase/cli/releases/latest").
    52  			ReplyError(errNetwork)
    53  		// Run test
    54  		version, err := GetLatestRelease(context.Background())
    55  		// Check error
    56  		assert.ErrorIs(t, err, errNetwork)
    57  		assert.Empty(t, version)
    58  		assert.Empty(t, apitest.ListUnmatchedRequests())
    59  	})
    60  }