github.com/chaddy81/ponzu@v0.0.0-20200102001432-9bc41b703131/cmd/ponzu/new_test.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  )
     8  
     9  func TestNewName2Path(t *testing.T) {
    10  	savedGOPATH := os.Getenv("GOPATH")
    11  	defer os.Setenv("GOPATH", savedGOPATH)
    12  	pwd, err := os.Getwd()
    13  	if err != nil {
    14  		t.Fatalf("Could not determine current working directory: %s", err)
    15  	}
    16  
    17  	isNil := func(e error) bool { return e == nil }
    18  	isNonNil := func(e error) bool { return e != nil }
    19  
    20  	baseDir := filepath.Join(pwd, "test-fixtures", "new")
    21  
    22  	testTable := []struct {
    23  		gopath, wd, a,
    24  		wantP string
    25  		wantE func(e error) bool
    26  	}{{
    27  		gopath: baseDir,
    28  		wd:     filepath.Join("src", "existing"),
    29  		a:      ".",
    30  		wantP:  filepath.Join(pwd, "test-fixtures", "new", "src", "existing"),
    31  		wantE:  os.IsExist,
    32  	}, {
    33  		gopath: baseDir,
    34  		wd:     filepath.Join(""),
    35  		a:      "non-existing",
    36  		wantP:  filepath.Join(pwd, "test-fixtures", "new", "src", "non-existing"),
    37  		wantE:  isNil,
    38  	}, {
    39  		gopath: baseDir,
    40  		wd:     filepath.Join(""),
    41  		a:      ".",
    42  		wantP:  "",
    43  		wantE:  isNonNil,
    44  	}, {
    45  		gopath: baseDir,
    46  		wd:     "..",
    47  		a:      ".",
    48  		wantP:  "",
    49  		wantE:  isNonNil,
    50  	}}
    51  
    52  	for _, test := range testTable {
    53  		os.Setenv("GOPATH", test.gopath)
    54  		err = os.Chdir(filepath.Join(test.gopath, test.wd))
    55  		if err != nil {
    56  			t.Fatalf("could not setup base: %s", err)
    57  		}
    58  		got, gotE := name2path(test.a)
    59  		if got != test.wantP {
    60  			t.Errorf("got '%s', want: '%s'", got, test.wantP)
    61  		}
    62  		if !test.wantE(gotE) {
    63  			t.Errorf("got error '%s'", gotE)
    64  		}
    65  	}
    66  }