github.com/abdfnx/gh-api@v0.0.0-20210414084727-f5432eec23b8/internal/update/update_test.go (about)

     1  package update
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"log"
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/abdfnx/gh-api/api"
    11  	"github.com/abdfnx/gh-api/pkg/httpmock"
    12  )
    13  
    14  func TestCheckForUpdate(t *testing.T) {
    15  	scenarios := []struct {
    16  		Name           string
    17  		CurrentVersion string
    18  		LatestVersion  string
    19  		LatestURL      string
    20  		ExpectsResult  bool
    21  	}{
    22  		{
    23  			Name:           "latest is newer",
    24  			CurrentVersion: "v0.0.1",
    25  			LatestVersion:  "v1.0.0",
    26  			LatestURL:      "https://www.spacejam.com/archive/spacejam/movie/jam.htm",
    27  			ExpectsResult:  true,
    28  		},
    29  		{
    30  			Name:           "current is prerelease",
    31  			CurrentVersion: "v1.0.0-pre.1",
    32  			LatestVersion:  "v1.0.0",
    33  			LatestURL:      "https://www.spacejam.com/archive/spacejam/movie/jam.htm",
    34  			ExpectsResult:  true,
    35  		},
    36  		{
    37  			Name:           "current is built from source",
    38  			CurrentVersion: "v1.2.3-123-gdeadbeef",
    39  			LatestVersion:  "v1.2.3",
    40  			LatestURL:      "https://www.spacejam.com/archive/spacejam/movie/jam.htm",
    41  			ExpectsResult:  false,
    42  		},
    43  		{
    44  			Name:           "current is built from source after a prerelease",
    45  			CurrentVersion: "v1.2.3-rc.1-123-gdeadbeef",
    46  			LatestVersion:  "v1.2.3",
    47  			LatestURL:      "https://www.spacejam.com/archive/spacejam/movie/jam.htm",
    48  			ExpectsResult:  true,
    49  		},
    50  		{
    51  			Name:           "latest is newer than version build from source",
    52  			CurrentVersion: "v1.2.3-123-gdeadbeef",
    53  			LatestVersion:  "v1.2.4",
    54  			LatestURL:      "https://www.spacejam.com/archive/spacejam/movie/jam.htm",
    55  			ExpectsResult:  true,
    56  		},
    57  		{
    58  			Name:           "latest is current",
    59  			CurrentVersion: "v1.0.0",
    60  			LatestVersion:  "v1.0.0",
    61  			LatestURL:      "https://www.spacejam.com/archive/spacejam/movie/jam.htm",
    62  			ExpectsResult:  false,
    63  		},
    64  		{
    65  			Name:           "latest is older",
    66  			CurrentVersion: "v0.10.0-pre.1",
    67  			LatestVersion:  "v0.9.0",
    68  			LatestURL:      "https://www.spacejam.com/archive/spacejam/movie/jam.htm",
    69  			ExpectsResult:  false,
    70  		},
    71  	}
    72  
    73  	for _, s := range scenarios {
    74  		t.Run(s.Name, func(t *testing.T) {
    75  			http := &httpmock.Registry{}
    76  			client := api.NewClient(api.ReplaceTripper(http))
    77  
    78  			http.Register(
    79  				httpmock.REST("GET", "repos/OWNER/REPO/releases/latest"),
    80  				httpmock.StringResponse(fmt.Sprintf(`{
    81  					"tag_name": "%s",
    82  					"html_url": "%s"
    83  				}`, s.LatestVersion, s.LatestURL)),
    84  			)
    85  
    86  			rel, err := CheckForUpdate(client, tempFilePath(), "OWNER/REPO", s.CurrentVersion)
    87  			if err != nil {
    88  				t.Fatal(err)
    89  			}
    90  
    91  			if len(http.Requests) != 1 {
    92  				t.Fatalf("expected 1 HTTP request, got %d", len(http.Requests))
    93  			}
    94  			requestPath := http.Requests[0].URL.Path
    95  			if requestPath != "/repos/OWNER/REPO/releases/latest" {
    96  				t.Errorf("HTTP path: %q", requestPath)
    97  			}
    98  
    99  			if !s.ExpectsResult {
   100  				if rel != nil {
   101  					t.Fatal("expected no new release")
   102  				}
   103  				return
   104  			}
   105  			if rel == nil {
   106  				t.Fatal("expected to report new release")
   107  			}
   108  
   109  			if rel.Version != s.LatestVersion {
   110  				t.Errorf("Version: %q", rel.Version)
   111  			}
   112  			if rel.URL != s.LatestURL {
   113  				t.Errorf("URL: %q", rel.URL)
   114  			}
   115  		})
   116  	}
   117  }
   118  
   119  func tempFilePath() string {
   120  	file, err := ioutil.TempFile("", "")
   121  	if err != nil {
   122  		log.Fatal(err)
   123  	}
   124  	os.Remove(file.Name())
   125  	return file.Name()
   126  }