git.sr.ht/~sircmpwn/gqlgen@v0.0.0-20200522192042-c84d29a1c940/internal/code/imports_test.go (about) 1 package code 2 3 import ( 4 "os" 5 "path/filepath" 6 "runtime" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestImportPathForDir(t *testing.T) { 14 wd, err := os.Getwd() 15 require.NoError(t, err) 16 17 assert.Equal(t, "git.sr.ht/~sircmpwn/gqlgen/internal/code", ImportPathForDir(wd)) 18 assert.Equal(t, "git.sr.ht/~sircmpwn/gqlgen/api", ImportPathForDir(filepath.Join(wd, "..", "..", "api"))) 19 20 // doesnt contain go code, but should still give a valid import path 21 assert.Equal(t, "git.sr.ht/~sircmpwn/gqlgen/docs", ImportPathForDir(filepath.Join(wd, "..", "..", "docs"))) 22 23 // directory does not exist 24 assert.Equal(t, "git.sr.ht/~sircmpwn/gqlgen/dos", ImportPathForDir(filepath.Join(wd, "..", "..", "dos"))) 25 26 if runtime.GOOS == "windows" { 27 assert.Equal(t, "", ImportPathForDir("C:/doesnotexist")) 28 } else { 29 assert.Equal(t, "", ImportPathForDir("/doesnotexist")) 30 } 31 } 32 33 func TestNameForDir(t *testing.T) { 34 wd, err := os.Getwd() 35 require.NoError(t, err) 36 37 assert.Equal(t, "tmp", NameForDir("/tmp")) 38 assert.Equal(t, "code", NameForDir(wd)) 39 assert.Equal(t, "internal", NameForDir(wd+"/..")) 40 assert.Equal(t, "main", NameForDir(wd+"/../..")) 41 }