github.com/comcast/canticle@v0.0.0-20161108184242-c53cface56e8/canticles/util_test.go (about)

     1  package canticles
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  )
     9  
    10  func TestEnvGoPath(t *testing.T) {
    11  	wd, err := os.Getwd()
    12  	if err != nil {
    13  		t.Fatalf("Could not get wd %s", err.Error())
    14  	}
    15  	defer os.Chdir(wd)
    16  	path, err := EnvGoPath()
    17  	if err != nil {
    18  		t.Errorf("Error fetching obviously good path gopath %s", err.Error())
    19  	}
    20  	if path == "" {
    21  		t.Errorf("Error fetching obviously good path got empty string")
    22  	}
    23  	testHome, err := ioutil.TempDir("", "cant-test-src")
    24  	if err != nil {
    25  		t.Fatalf("Error creating tempdir: %s", err.Error())
    26  	}
    27  	defer os.RemoveAll(testHome)
    28  
    29  	// on OSX, /tmp and /var are symlinked to /private/tmp and /private/var so resolve wd before continuing
    30  	os.Chdir(testHome)
    31  	testHome, err = os.Getwd()
    32  	os.Chdir(wd)
    33  	if err != nil {
    34  		t.Fatalf("Error creating tempdir: %s", err.Error())
    35  	}
    36  
    37  	if err := os.MkdirAll(filepath.Join(testHome, "src", "github.com", "comcast", "cant"), 0755); err != nil {
    38  		t.Fatalf("Error creating tempdir sub folders: %s", err.Error())
    39  	}
    40  	if err := os.Chdir(testHome); err != nil {
    41  		t.Fatalf("Could not chdir to created tmpdir: %s", err.Error())
    42  	}
    43  
    44  	if err := os.Setenv("GOPATH", testHome); err != nil {
    45  		t.Fatalf("Could not set gopath: %s", err.Error())
    46  	}
    47  	path, err = EnvGoPath()
    48  	if err != nil {
    49  		t.Errorf("Expected no error when getting envgopath with a valid gopath, got %s", err.Error())
    50  	}
    51  	if path != testHome {
    52  		t.Errorf("Expected path %s with a valid gopath, got %s", testHome, path)
    53  	}
    54  
    55  	if err := os.Setenv("GOPATH", ""); err != nil {
    56  		t.Fatalf("Could not set gopath: %s", err.Error())
    57  	}
    58  	if err := os.Chdir(filepath.Join(testHome, "src")); err != nil {
    59  		t.Fatalf("Could not chdir to created tmpdir: %s", err.Error())
    60  	}
    61  	path, err = EnvGoPath()
    62  	if err != nil {
    63  		t.Errorf("Expected no error when getting envgopath in a GB workspace, got %s", err.Error())
    64  	}
    65  	if path != testHome {
    66  		t.Errorf("Expected path %s with a valid GB workspace, got %s", testHome, path)
    67  	}
    68  
    69  	// Create a "nested" gb workspace
    70  	if err := os.MkdirAll(filepath.Join(testHome, "src", "nested", "src", "github.com", "comcast", "cant"), 0755); err != nil {
    71  		t.Fatalf("Error creating tempdir sub folders: %s", err.Error())
    72  	}
    73  	wspace := filepath.Join(testHome, "src", "nested")
    74  	if err := os.Chdir(filepath.Join(wspace, "src")); err != nil {
    75  		t.Fatalf("Could not chdir to created tmpdir: %s", err.Error())
    76  	}
    77  	path, err = EnvGoPath()
    78  	if err != nil {
    79  		t.Errorf("Expected no error when getting envgopath in a GB workspace, got %s", err.Error())
    80  	}
    81  	if path != wspace {
    82  		t.Errorf("Expected path %s with a valid GB workspace, got %s", testHome, path)
    83  	}
    84  
    85  	// Try an invalid spot
    86  	if err := os.Chdir(os.TempDir()); err != nil {
    87  		t.Fatalf("Could not chdir to created tmpdir: %s", err.Error())
    88  	}
    89  	path, err = EnvGoPath()
    90  	if err == nil {
    91  		t.Errorf("Expected an error when getting envgopath in an valid workspace, got")
    92  	}
    93  }