github.com/maeglindeveloper/gqlgen@v0.13.1-0.20210413081235-57808b12a0a0/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  
    16  	require.NoError(t, err)
    17  
    18  	assert.Equal(t, "github.com/99designs/gqlgen/internal/code", ImportPathForDir(wd))
    19  	assert.Equal(t, "github.com/99designs/gqlgen/api", ImportPathForDir(filepath.Join(wd, "..", "..", "api")))
    20  
    21  	// doesnt contain go code, but should still give a valid import path
    22  	assert.Equal(t, "github.com/99designs/gqlgen/docs", ImportPathForDir(filepath.Join(wd, "..", "..", "docs")))
    23  
    24  	// directory does not exist
    25  	assert.Equal(t, "github.com/99designs/gqlgen/dos", ImportPathForDir(filepath.Join(wd, "..", "..", "dos")))
    26  
    27  	// out of module
    28  	assert.Equal(t, "", ImportPathForDir(filepath.Join(wd, "..", "..", "..")))
    29  
    30  	if runtime.GOOS == "windows" {
    31  		assert.Equal(t, "", ImportPathForDir("C:/doesnotexist"))
    32  	} else {
    33  		assert.Equal(t, "", ImportPathForDir("/doesnotexist"))
    34  	}
    35  }
    36  
    37  func TestNameForDir(t *testing.T) {
    38  	wd, err := os.Getwd()
    39  	require.NoError(t, err)
    40  
    41  	assert.Equal(t, "tmp", NameForDir("/tmp"))
    42  	assert.Equal(t, "code", NameForDir(wd))
    43  	assert.Equal(t, "internal", NameForDir(wd+"/.."))
    44  	assert.Equal(t, "main", NameForDir(wd+"/../.."))
    45  }