github.com/creativeprojects/go-selfupdate@v1.2.0/github_source_test.go (about)

     1  package selfupdate
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestGitHubTokenEnv(t *testing.T) {
    13  	token := os.Getenv("GITHUB_TOKEN")
    14  	if token == "" {
    15  		t.Skip("because $GITHUB_TOKEN is not set")
    16  	}
    17  
    18  	if _, err := NewGitHubSource(GitHubConfig{}); err != nil {
    19  		t.Error("Failed to initialize GitHub source with empty config")
    20  	}
    21  	if _, err := NewGitHubSource(GitHubConfig{APIToken: token}); err != nil {
    22  		t.Error("Failed to initialize GitHub source with API token config")
    23  	}
    24  }
    25  
    26  func TestGitHubTokenIsNotSet(t *testing.T) {
    27  	t.Setenv("GITHUB_TOKEN", "")
    28  
    29  	if _, err := NewGitHubSource(GitHubConfig{}); err != nil {
    30  		t.Error("Failed to initialize GitHub source with empty config")
    31  	}
    32  }
    33  
    34  func TestGitHubEnterpriseClientInvalidURL(t *testing.T) {
    35  	_, err := NewGitHubSource(GitHubConfig{APIToken: "my_token", EnterpriseBaseURL: ":this is not a URL"})
    36  	if err == nil {
    37  		t.Fatal("Invalid URL should raise an error")
    38  	}
    39  }
    40  
    41  func TestGitHubEnterpriseClientValidURL(t *testing.T) {
    42  	_, err := NewGitHubSource(GitHubConfig{APIToken: "my_token", EnterpriseBaseURL: "http://localhost"})
    43  	if err != nil {
    44  		t.Fatal("Failed to initialize GitHub source with valid URL")
    45  	}
    46  }
    47  
    48  func TestGitHubListReleasesContextCancelled(t *testing.T) {
    49  	source, err := NewGitHubSource(GitHubConfig{})
    50  	require.NoError(t, err)
    51  
    52  	ctx, cancelFn := context.WithCancel(context.Background())
    53  	cancelFn()
    54  
    55  	_, err = source.ListReleases(ctx, ParseSlug("creativeprojects/resticprofile"))
    56  	assert.ErrorIs(t, err, context.Canceled)
    57  }
    58  
    59  func TestGitHubDownloadReleaseAssetContextCancelled(t *testing.T) {
    60  	source, err := NewGitHubSource(GitHubConfig{})
    61  	require.NoError(t, err)
    62  
    63  	ctx, cancelFn := context.WithCancel(context.Background())
    64  	cancelFn()
    65  
    66  	_, err = source.DownloadReleaseAsset(ctx, &Release{repository: ParseSlug("creativeprojects/resticprofile")}, 11)
    67  	assert.ErrorIs(t, err, context.Canceled)
    68  }
    69  
    70  func TestGitHubDownloadReleaseAssetWithNilRelease(t *testing.T) {
    71  	source, err := NewGitHubSource(GitHubConfig{})
    72  	require.NoError(t, err)
    73  
    74  	_, err = source.DownloadReleaseAsset(context.Background(), nil, 11)
    75  	assert.ErrorIs(t, err, ErrInvalidRelease)
    76  }