github.com/abemedia/appcast@v0.4.0/target/github/github_test.go (about)

     1  package github_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"path"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/abemedia/appcast/internal/test"
    12  	"github.com/abemedia/appcast/target/github"
    13  	gh "github.com/google/go-github/github"
    14  	"golang.org/x/oauth2"
    15  )
    16  
    17  func TestGithub(t *testing.T) {
    18  	token, ok := os.LookupEnv("GITHUB_TOKEN")
    19  	if !ok {
    20  		t.Skip("Missing environment variable: GITHUB_TOKEN")
    21  	}
    22  
    23  	ctx := context.Background()
    24  	ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
    25  	client := gh.NewClient(oauth2.NewClient(ctx, ts))
    26  	user, _, err := client.Users.Get(ctx, "")
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  
    31  	owner := user.GetLogin()
    32  	repo := fmt.Sprintf("test_%d", time.Now().UnixNano())
    33  
    34  	_, _, err = client.Repositories.Create(ctx, "", &gh.Repository{Name: &repo})
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	t.Cleanup(func() {
    39  		_, err := client.Repositories.Delete(ctx, owner, repo)
    40  		if err != nil {
    41  			t.Fatal(err)
    42  		}
    43  	})
    44  
    45  	tgt, err := github.New(github.Config{Owner: owner, Repo: repo})
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  
    50  	test.Target(t, tgt, func(asset string) string {
    51  		return "https://raw.githubusercontent.com/" + path.Join(owner, repo, "main", asset)
    52  	}, test.WithDelay(time.Second))
    53  
    54  	_, err = github.New(github.Config{Owner: owner, Repo: repo, Branch: "foo"})
    55  	if err == nil {
    56  		t.Fatal("should fail for invalid branch")
    57  	}
    58  
    59  	_, err = github.New(github.Config{Owner: owner, Repo: "foo"})
    60  	if err == nil {
    61  		t.Fatal("should fail for invalid repo")
    62  	}
    63  }