github.com/jepp2078/gqlgen@v0.7.2/codegen/templates/import_test.go (about) 1 package templates 2 3 import ( 4 "os" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 ) 9 10 func TestImports(t *testing.T) { 11 wd, err := os.Getwd() 12 require.NoError(t, err) 13 14 aBar := "github.com/99designs/gqlgen/codegen/templates/testdata/a/bar" 15 bBar := "github.com/99designs/gqlgen/codegen/templates/testdata/b/bar" 16 mismatch := "github.com/99designs/gqlgen/codegen/templates/testdata/pkg_mismatch" 17 18 t.Run("multiple lookups is ok", func(t *testing.T) { 19 a := Imports{destDir: wd} 20 21 require.Equal(t, "bar", a.Lookup(aBar)) 22 require.Equal(t, "bar", a.Lookup(aBar)) 23 }) 24 25 t.Run("duplicates are decollisioned", func(t *testing.T) { 26 a := Imports{destDir: wd} 27 28 require.Equal(t, "bar", a.Lookup(aBar)) 29 require.Equal(t, "bar1", a.Lookup(bBar)) 30 31 t.Run("additionial calls get decollisioned name", func(t *testing.T) { 32 require.Equal(t, "bar1", a.Lookup(bBar)) 33 }) 34 }) 35 36 t.Run("package name defined in code will be used", func(t *testing.T) { 37 a := Imports{destDir: wd} 38 39 require.Equal(t, "turtles", a.Lookup(mismatch)) 40 }) 41 42 t.Run("string printing for import block", func(t *testing.T) { 43 a := Imports{destDir: wd} 44 a.Lookup(aBar) 45 a.Lookup(bBar) 46 a.Lookup(mismatch) 47 48 require.Equal( 49 t, 50 `"github.com/99designs/gqlgen/codegen/templates/testdata/a/bar" 51 bar1 "github.com/99designs/gqlgen/codegen/templates/testdata/b/bar" 52 "github.com/99designs/gqlgen/codegen/templates/testdata/pkg_mismatch"`, 53 a.String(), 54 ) 55 }) 56 57 t.Run("reserved collisions on path will panic", func(t *testing.T) { 58 a := Imports{destDir: wd} 59 60 a.Reserve(aBar) 61 62 require.Panics(t, func() { 63 a.Reserve(aBar) 64 }) 65 }) 66 67 t.Run("reserved collisions on alias will panic", func(t *testing.T) { 68 a := Imports{destDir: wd} 69 70 a.Reserve(aBar) 71 72 require.Panics(t, func() { 73 a.Reserve(bBar) 74 }) 75 }) 76 77 t.Run("aliased imports will not collide", func(t *testing.T) { 78 a := Imports{destDir: wd} 79 80 a.Reserve(aBar, "abar") 81 a.Reserve(bBar, "bbar") 82 83 require.Equal(t, `abar "github.com/99designs/gqlgen/codegen/templates/testdata/a/bar" 84 bbar "github.com/99designs/gqlgen/codegen/templates/testdata/b/bar"`, a.String()) 85 }) 86 }