gitlab.com/tilotech/tilores-cli@v0.1.2/cmd/init_test.go (about) 1 package cmd 2 3 import ( 4 "io/ioutil" 5 "os" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestInit(t *testing.T) { 13 cases := map[string]struct { 14 args []string 15 modulePathFlag string 16 expectFilesExist []string 17 expectFilesToContain map[string]string 18 }{ 19 "create project with path argument": { 20 args: []string{"foobar"}, 21 expectFilesExist: []string{ 22 "foobar/go.mod", 23 "foobar/foobar", 24 "foobar/gqlgen.yml", 25 "foobar/server.go", 26 "foobar/tilores-plugin-dispatcher", 27 }, 28 expectFilesToContain: map[string]string{ 29 "foobar/go.mod": "module foobar", 30 }, 31 }, 32 "create project without path argument with module path": { 33 args: []string{}, 34 modulePathFlag: "example.com/test/foopkg", 35 expectFilesExist: []string{ 36 "go.mod", 37 "foopkg", 38 "gqlgen.yml", 39 "server.go", 40 "tilores-plugin-dispatcher", 41 }, 42 expectFilesToContain: map[string]string{ 43 "go.mod": "module example.com/test/foopkg", 44 }, 45 }, 46 "create project without path argument or flags": { 47 args: []string{}, 48 expectFilesExist: []string{ 49 "go.mod", 50 "gqlgen.yml", 51 "server.go", 52 "tilores-plugin-dispatcher", 53 }, 54 }, 55 } 56 57 for name, c := range cases { 58 t.Run(name, func(t *testing.T) { 59 dir, err := createTempDir() 60 require.NoError(t, err) 61 defer os.RemoveAll(dir) 62 63 modulePath = c.modulePathFlag 64 65 err = initializeProject(c.args) 66 assert.NoError(t, err) 67 68 for _, file := range c.expectFilesExist { 69 assert.FileExists(t, dir+"/"+file) 70 } 71 72 for file, expectedPartialContent := range c.expectFilesToContain { 73 actualContent, err := ioutil.ReadFile(dir + "/" + file) 74 require.NoError(t, err) 75 assert.Contains(t, string(actualContent), expectedPartialContent) 76 } 77 }) 78 } 79 } 80 81 func createTempDir() (string, error) { 82 dir, err := os.MkdirTemp("", applicationNameLower) 83 if err != nil { 84 return "", err 85 } 86 87 err = os.Chdir(dir) 88 if err != nil { 89 return "", err 90 } 91 92 return dir, nil 93 }