github.com/weiwenhao/getter@v1.30.1/module_test.go (about)

     1  package getter
     2  
     3  import (
     4  	"io/ioutil"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"net/url"
     8  	"os"
     9  	"path/filepath"
    10  	"reflect"
    11  	"testing"
    12  
    13  	urlhelper "github.com/weiwenhao/getter/helper/url"
    14  )
    15  
    16  const fixtureDir = "./testdata"
    17  
    18  func tempDir(t *testing.T) string {
    19  	dir, err := ioutil.TempDir("", "tf")
    20  	if err != nil {
    21  		t.Fatalf("err: %s", err)
    22  	}
    23  	if err := os.RemoveAll(dir); err != nil {
    24  		t.Fatalf("err: %s", err)
    25  	}
    26  
    27  	return dir
    28  }
    29  
    30  func tempTestFile(t *testing.T) string {
    31  	dir := tempDir(t)
    32  	return filepath.Join(dir, "foo")
    33  }
    34  
    35  func testModule(n string) string {
    36  	p := filepath.Join(fixtureDir, n)
    37  	p, err := filepath.Abs(p)
    38  	if err != nil {
    39  		panic(err)
    40  	}
    41  	return fmtFileURL(p)
    42  }
    43  func httpTestModule(n string) *httptest.Server {
    44  	p := filepath.Join(fixtureDir, n)
    45  	p, err := filepath.Abs(p)
    46  	if err != nil {
    47  		panic(err)
    48  	}
    49  
    50  	return httptest.NewServer(http.FileServer(http.Dir(p)))
    51  }
    52  
    53  func testModuleURL(n string) *url.URL {
    54  	n, subDir := SourceDirSubdir(n)
    55  	u, err := urlhelper.Parse(testModule(n))
    56  	if err != nil {
    57  		panic(err)
    58  	}
    59  	if subDir != "" {
    60  		u.Path += "//" + subDir
    61  		u.RawPath = u.Path
    62  	}
    63  
    64  	return u
    65  }
    66  
    67  func testURL(s string) *url.URL {
    68  	u, err := urlhelper.Parse(s)
    69  	if err != nil {
    70  		panic(err)
    71  	}
    72  
    73  	return u
    74  }
    75  
    76  func testStorage(t *testing.T) Storage {
    77  	return &FolderStorage{StorageDir: tempDir(t)}
    78  }
    79  
    80  func assertContents(t *testing.T, path string, contents string) {
    81  	data, err := ioutil.ReadFile(path)
    82  	if err != nil {
    83  		t.Fatalf("err: %s", err)
    84  	}
    85  
    86  	if !reflect.DeepEqual(data, []byte(contents)) {
    87  		t.Fatalf("bad. expected:\n\n%s\n\nGot:\n\n%s", contents, string(data))
    88  	}
    89  }