github.com/buth/gqlgen@v0.7.2/internal/gopath/gopath_test.go (about)

     1  package gopath
     2  
     3  import (
     4  	"go/build"
     5  	"runtime"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestContains(t *testing.T) {
    12  	origBuildContext := build.Default
    13  	defer func() { build.Default = origBuildContext }()
    14  
    15  	if runtime.GOOS == "windows" {
    16  		build.Default.GOPATH = `C:\go;C:\Users\user\go`
    17  
    18  		assert.True(t, Contains(`C:\go\src\github.com\vektah\gqlgen`))
    19  		assert.True(t, Contains(`C:\go\src\fpp`))
    20  		assert.True(t, Contains(`C:/go/src/github.com/vektah/gqlgen`))
    21  		assert.True(t, Contains(`C:\Users\user\go\src\foo`))
    22  		assert.False(t, Contains(`C:\tmp`))
    23  		assert.False(t, Contains(`C:\Users\user`))
    24  		assert.False(t, Contains(`C:\Users\another\go`))
    25  	} else {
    26  		build.Default.GOPATH = "/go:/home/user/go"
    27  
    28  		assert.True(t, Contains("/go/src/github.com/vektah/gqlgen"))
    29  		assert.True(t, Contains("/go/src/foo"))
    30  		assert.True(t, Contains("/home/user/go/src/foo"))
    31  		assert.False(t, Contains("/tmp"))
    32  		assert.False(t, Contains("/home/user"))
    33  		assert.False(t, Contains("/home/another/go"))
    34  	}
    35  }
    36  
    37  func TestDir2Package(t *testing.T) {
    38  	origBuildContext := build.Default
    39  	defer func() { build.Default = origBuildContext }()
    40  
    41  	if runtime.GOOS == "windows" {
    42  		build.Default.GOPATH = "C:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;C:/a/y;C:/b/"
    43  
    44  		assert.Equal(t, "foo/bar", MustDir2Import("C:/a/y/src/foo/bar"))
    45  		assert.Equal(t, "foo/bar", MustDir2Import(`C:\a\y\src\foo\bar`))
    46  		assert.Equal(t, "foo/bar", MustDir2Import("C:/b/src/foo/bar"))
    47  		assert.Equal(t, "foo/bar", MustDir2Import(`C:\b\src\foo\bar`))
    48  
    49  		assert.PanicsWithValue(t, NotFound, func() {
    50  			MustDir2Import("C:/tmp/foo")
    51  		})
    52  	} else {
    53  		build.Default.GOPATH = "/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:/a/y:/b/"
    54  
    55  		assert.Equal(t, "foo/bar", MustDir2Import("/a/y/src/foo/bar"))
    56  		assert.Equal(t, "foo/bar", MustDir2Import("/b/src/foo/bar"))
    57  
    58  		assert.PanicsWithValue(t, NotFound, func() {
    59  			MustDir2Import("/tmp/foo")
    60  		})
    61  	}
    62  }