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 TestExtractModuleName(t *testing.T) {
14 wd, err := os.Getwd()
15 require.NoError(t, err)
16
17 modDir := filepath.Join(wd, "..", "..", "testdata")
18 content, err := os.ReadFile(filepath.Join(modDir, "gomod-with-leading-comments.mod"))
19 require.NoError(t, err)
20 assert.Equal(t, "github.com/99designs/gqlgen", extractModuleName(content))
21 }
22
23 func TestImportPathForDir(t *testing.T) {
24 wd, err := os.Getwd()
25
26 require.NoError(t, err)
27
28 assert.Equal(t, "github.com/99designs/gqlgen/internal/code", ImportPathForDir(wd))
29
30 assert.Equal(t, "github.com/99designs/gqlgen/internal/code", ImportPathForDir(wd))
31 assert.Equal(t, "github.com/99designs/gqlgen/api", ImportPathForDir(filepath.Join(wd, "..", "..", "api")))
32
33
34 assert.Equal(t, "github.com/99designs/gqlgen/docs", ImportPathForDir(filepath.Join(wd, "..", "..", "docs")))
35
36
37 assert.Equal(t, "github.com/99designs/gqlgen/dos", ImportPathForDir(filepath.Join(wd, "..", "..", "dos")))
38
39
40 assert.Equal(t, "", ImportPathForDir(filepath.Join(wd, "..", "..", "..")))
41
42 if runtime.GOOS == "windows" {
43 assert.Equal(t, "", ImportPathForDir("C:/doesnotexist"))
44 } else {
45 assert.Equal(t, "", ImportPathForDir("/doesnotexist"))
46 }
47 }
48
49 func TestNameForDir(t *testing.T) {
50 wd, err := os.Getwd()
51 require.NoError(t, err)
52
53 assert.Equal(t, "tmp", NameForDir("/tmp"))
54 assert.Equal(t, "code", NameForDir(wd))
55 assert.Equal(t, "docs", NameForDir(wd+"/../../docs"))
56 assert.Equal(t, "main", NameForDir(wd+"/../.."))
57 }
58
View as plain text