github.com/abemedia/appcast@v0.4.0/source/file/file_test.go (about)

     1  package file_test
     2  
     3  import (
     4  	"net/url"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/abemedia/appcast/internal/test"
    10  	"github.com/abemedia/appcast/source/file"
    11  )
    12  
    13  func TestFile(t *testing.T) {
    14  	tests := []struct {
    15  		name string
    16  		url  string
    17  	}{
    18  		{"FileURL", ""},
    19  		{"CustomURL", "http://dl.example.com"},
    20  	}
    21  
    22  	for _, testCase := range tests {
    23  		t.Run(testCase.name, func(t *testing.T) {
    24  			path := t.TempDir()
    25  
    26  			s, err := file.New(file.Config{Path: path, URL: testCase.url})
    27  			if err != nil {
    28  				t.Fatal(err)
    29  			}
    30  
    31  			baseURL := testCase.url
    32  			if baseURL == "" {
    33  				baseURL, _ = url.JoinPath("file:///", filepath.ToSlash(path))
    34  			}
    35  
    36  			test.Source(t, s, func(version, asset string) string {
    37  				return baseURL + "/" + version + "/" + asset
    38  			})
    39  		})
    40  	}
    41  
    42  	t.Run("New_ResolvePathError", func(t *testing.T) {
    43  		wd, _ := os.Getwd()
    44  		defer os.Chdir(wd)
    45  
    46  		dir := t.TempDir()
    47  		os.Chdir(dir)
    48  		os.Remove(dir)
    49  
    50  		_, err := file.New(file.Config{})
    51  		if err == nil {
    52  			t.Fatal("should error")
    53  		}
    54  	})
    55  }