github.com/dan-leo/glide@v0.13.4-0.20210218130343-57e8627e3947/path/path_test.go (about)

     1  package path
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"runtime"
     7  	"testing"
     8  )
     9  
    10  const testdata = "../testdata/path"
    11  
    12  func TestGlideWD(t *testing.T) {
    13  	wd := filepath.Join(testdata, "a/b/c")
    14  	found, err := GlideWD(wd)
    15  	if err != nil {
    16  		t.Errorf("Failed to get Glide directory: %s", err)
    17  	}
    18  
    19  	if found != filepath.Join(testdata, "a") {
    20  		t.Errorf("Expected %s to match %s", found, filepath.Join(wd, "a"))
    21  	}
    22  
    23  	// This should fail
    24  	wd = "/No/Such/Dir"
    25  	found, err = GlideWD(wd)
    26  	if err == nil {
    27  		t.Errorf("Expected to get an error on a non-existent directory, not %s", found)
    28  	}
    29  
    30  }
    31  
    32  func TestVendor(t *testing.T) {
    33  	td, err := filepath.Abs(testdata)
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	wd, _ := os.Getwd()
    38  
    39  	os.Chdir(filepath.Join(td, "a", "b", "c"))
    40  	res, err := Vendor()
    41  	if err != nil {
    42  		t.Errorf("Failed to resolve vendor directory: %s", err)
    43  	}
    44  	expect := filepath.Join(td, "a", "vendor")
    45  	if res != expect {
    46  		t.Errorf("Failed to find vendor: expected %s got %s", expect, res)
    47  	}
    48  
    49  	os.Chdir(filepath.Join(td, "x", "y", "z"))
    50  	res, err = Vendor()
    51  	if err != nil {
    52  		t.Errorf("Failed to resolve vendor directory: %s", err)
    53  	}
    54  
    55  	// Windows symlinks are different than *nix and they can be inconsistent.
    56  	// The current testing only works for *nix testing and windows doesn't follow
    57  	// the symlinks. If this is a vendor.lnk file in windows this won't work for
    58  	// the go toolchain. If this is a windows link you need access to create one
    59  	// which isn't consistent.
    60  	// If there is a better way would love to know.
    61  	if runtime.GOOS == "windows" {
    62  		expect = filepath.Join(td, "x", "vendor")
    63  	} else {
    64  		expect = filepath.Join(td, "x", "symlinked_vendor")
    65  	}
    66  	if res != expect {
    67  		t.Errorf("Failed to find vendor: expected %s got %s", expect, res)
    68  	}
    69  
    70  	os.Chdir(wd)
    71  }
    72  func TestGlide(t *testing.T) {
    73  	wd, _ := os.Getwd()
    74  	td, err := filepath.Abs(testdata)
    75  	if err != nil {
    76  		t.Fatal(err)
    77  	}
    78  	os.Chdir(filepath.Join(td, "a/b/c"))
    79  	res, err := Glide()
    80  	if err != nil {
    81  		t.Errorf("Failed to resolve vendor directory: %s", err)
    82  	}
    83  	expect := filepath.Join(td, "a", "glide.yaml")
    84  	if res != expect {
    85  		t.Errorf("Failed to find vendor: expected %s got %s", expect, res)
    86  	}
    87  	os.Chdir(wd)
    88  }
    89  
    90  func TestCustomRemoveAll(t *testing.T) {
    91  	td, err := filepath.Abs(testdata)
    92  	if err != nil {
    93  		t.Fatal(err)
    94  	}
    95  	// test that deleting a non-existent directory does not throw an error
    96  	err = CustomRemoveAll(filepath.Join(td, "directory/doesnt/exist"))
    97  	if err != nil {
    98  		t.Errorf("Failed when removing non-existent directory %s", err)
    99  	}
   100  	// test that deleting a path with spaces does not throw an error
   101  	spaceyPath := filepath.Join(td, "10942384 12341234 12343214 324134132323")
   102  	err = os.MkdirAll(spaceyPath, 0777)
   103  	if err != nil {
   104  		t.Fatalf("Failed to make test directory %s", err)
   105  	}
   106  	err = CustomRemoveAll(spaceyPath)
   107  	if err != nil {
   108  		t.Errorf("Errored incorrectly when deleting a path with spaces %s", err)
   109  	}
   110  	if _, err = os.Stat(spaceyPath); !os.IsNotExist(err) {
   111  		t.Errorf("Failed to successfully delete a path with spaces")
   112  	}
   113  }