github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/pkg/cmd/run/shared/shared_test.go (about)

     1  package shared
     2  
     3  import (
     4  	"net/http"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/cli/cli/api"
     9  	"github.com/cli/cli/internal/ghrepo"
    10  	"github.com/cli/cli/pkg/httpmock"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestPreciseAgo(t *testing.T) {
    15  	const form = "2006-Jan-02 15:04:05"
    16  	now, _ := time.Parse(form, "2021-Apr-12 14:00:00")
    17  
    18  	cases := map[string]string{
    19  		"2021-Apr-12 14:00:00": "0s ago",
    20  		"2021-Apr-12 13:59:30": "30s ago",
    21  		"2021-Apr-12 13:59:00": "1m0s ago",
    22  		"2021-Apr-12 13:30:15": "29m45s ago",
    23  		"2021-Apr-12 13:00:00": "1h0m0s ago",
    24  		"2021-Apr-12 02:30:45": "11h29m15s ago",
    25  		"2021-Apr-11 14:00:00": "24h0m0s ago",
    26  		"2021-Apr-01 14:00:00": "264h0m0s ago",
    27  		"2021-Mar-12 14:00:00": "Mar 12, 2021",
    28  	}
    29  
    30  	for createdAt, expected := range cases {
    31  		d, _ := time.Parse(form, createdAt)
    32  		got := preciseAgo(now, d)
    33  		if got != expected {
    34  			t.Errorf("expected %s but got %s for %s", expected, got, createdAt)
    35  		}
    36  	}
    37  }
    38  
    39  func TestGetAnnotations404(t *testing.T) {
    40  	reg := &httpmock.Registry{}
    41  	defer reg.Verify(t)
    42  
    43  	reg.Register(
    44  		httpmock.REST("GET", "repos/OWNER/REPO/check-runs/123456/annotations"),
    45  		httpmock.StatusStringResponse(404, "not found"))
    46  
    47  	httpClient := &http.Client{Transport: reg}
    48  	apiClient := api.NewClientFromHTTP(httpClient)
    49  	repo := ghrepo.New("OWNER", "REPO")
    50  
    51  	result, err := GetAnnotations(apiClient, repo, Job{ID: 123456, Name: "a job"})
    52  	assert.NoError(t, err)
    53  	assert.Equal(t, result, []Annotation{})
    54  }