github.com/fortexxx/gqlgen@v0.10.3-0.20191216030626-ca5ea8b21ead/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, "github.com/99designs/gqlgen/internal/code", ImportPathForDir(wd))
    18  	assert.Equal(t, "github.com/99designs/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, "github.com/99designs/gqlgen/docs", ImportPathForDir(filepath.Join(wd, "..", "..", "docs")))
    22  
    23  	// directory does not exist
    24  	assert.Equal(t, "github.com/99designs/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 TestNameForPackage(t *testing.T) {
    34  	assert.Equal(t, "api", NameForPackage("github.com/99designs/gqlgen/api"))
    35  
    36  	// does not contain go code, should still give a valid name
    37  	assert.Equal(t, "docs", NameForPackage("github.com/99designs/gqlgen/docs"))
    38  	assert.Equal(t, "github_com", NameForPackage("github.com"))
    39  }
    40  
    41  func TestNameForDir(t *testing.T) {
    42  	wd, err := os.Getwd()
    43  	require.NoError(t, err)
    44  
    45  	assert.Equal(t, "tmp", NameForDir("/tmp"))
    46  	assert.Equal(t, "code", NameForDir(wd))
    47  	assert.Equal(t, "internal", NameForDir(wd+"/.."))
    48  	assert.Equal(t, "main", NameForDir(wd+"/../.."))
    49  }