github.com/JarrahG/buffalocli@v0.0.0-20230801092127-b85bfd5d395a/internal/cmd/new/new_test.go (about) 1 //go:build integration 2 // +build integration 3 4 package new 5 6 import ( 7 "errors" 8 "os" 9 "path/filepath" 10 "testing" 11 12 "github.com/JarrahG/buffalocli/internal/testhelpers" 13 "github.com/stretchr/testify/require" 14 ) 15 16 func TestNew(t *testing.T) { 17 r := require.New(t) 18 r.NoError(testhelpers.EnsureBuffaloCMD(t)) 19 20 tt := []struct { 21 name string 22 args []string 23 check func(*require.Assertions, string, error) 24 }{ 25 { 26 name: "no application name", 27 args: []string{"new"}, 28 check: func(r *require.Assertions, out string, err error) { 29 r.Error(err) 30 r.Contains(out, "you must enter a name for your new application") 31 }, 32 }, 33 { 34 name: "skip docker", 35 args: []string{"new", "nodocker", "--api", "--skip-docker", "-f", "--vcs", "none"}, 36 check: func(r *require.Assertions, out string, err error) { 37 r.NoError(err) 38 r.NoFileExists(filepath.Join("nodocker", "Dockerfile")) 39 }, 40 }, 41 42 { 43 name: "docker there", 44 args: []string{"new", "wdocker", "--api", "-f", "--vcs", "none"}, 45 check: func(r *require.Assertions, out string, err error) { 46 r.NoError(err) 47 r.FileExists(filepath.Join("wdocker", "Dockerfile")) 48 }, 49 }, 50 51 { 52 name: "invalid db type", 53 args: []string{"new", "api", "--api", "-f", "--db-type", "a"}, 54 check: func(r *require.Assertions, out string, err error) { 55 r.Error(err) 56 r.Contains(out, `unknown dialect`) 57 }, 58 }, 59 60 { 61 name: "forbidden application name", 62 args: []string{"new", "buffalo", "-f", "--api"}, 63 check: func(r *require.Assertions, out string, err error) { 64 r.Error(err) 65 r.Contains(out, `name buffalo is not allowed, try a different application name`) 66 }, 67 }, 68 } 69 70 for _, tc := range tt { 71 t.Run(tc.name, func(t *testing.T) { 72 testhelpers.RunWithinTempFolder(t, func(t *testing.T) { 73 r := require.New(t) 74 out, err := testhelpers.RunBuffaloCMD(t, tc.args) 75 tc.check(r, out, err) 76 }) 77 }) 78 } 79 } 80 81 func TestNewDryRun(t *testing.T) { 82 r := require.New(t) 83 r.NoError(testhelpers.EnsureBuffaloCMD(t)) 84 85 testhelpers.RunWithinTempFolder(t, func(t *testing.T) { 86 r := require.New(t) 87 out, err := testhelpers.RunBuffaloCMD(t, []string{"new", "app", "--api", "-f", "--vcs", "none"}) 88 r.Contains(out, "Congratulations", "out: %s", out) 89 r.NoError(err) 90 r.DirExists("app") 91 out, err = testhelpers.RunBuffaloCMD(t, []string{"new", "app", "--api", "-f", "-d", "--vcs", "none"}) 92 r.Contains(out, "Congratulations", "out: %s", out) 93 r.NoError(err) 94 r.DirExists("app", "dryrun should not destroy anything but...") 95 }) 96 } 97 98 func TestNewAppAPIContent(t *testing.T) { 99 r := require.New(t) 100 r.NoError(testhelpers.EnsureBuffaloCMD(t)) 101 102 tt := []struct { 103 path string 104 check func(*require.Assertions, string, bool) 105 }{ 106 { 107 path: filepath.Join("apicontent", "actions", "app.go"), 108 check: func(r *require.Assertions, content string, exists bool) { 109 r.True(exists) 110 r.Contains(string(content), "app.Use(contenttype.Set(\"application/json\"))") 111 }, 112 }, 113 114 { 115 path: filepath.Join("apicontent", "actions", "home.go"), 116 check: func(r *require.Assertions, content string, exists bool) { 117 r.True(exists) 118 119 r.Contains(string(content), "r.JSON") 120 r.NotContains(string(content), "r.HTML") 121 }, 122 }, 123 124 { 125 path: filepath.Join("apicontent", "actions", "render.go"), 126 check: func(r *require.Assertions, content string, exists bool) { 127 r.True(exists) 128 129 r.Contains(string(content), "DefaultContentType: \"application/json\",") 130 r.NotContains(string(content), "HTMLLayout") 131 r.NotContains(string(content), "TemplatesBox") 132 r.NotContains(string(content), "Helpers") 133 }, 134 }, 135 136 { 137 path: filepath.Join("apicontent", "templates"), 138 check: func(r *require.Assertions, content string, exists bool) { 139 r.False(exists) 140 }, 141 }, 142 143 { 144 path: filepath.Join("apicontent", "assets"), 145 check: func(r *require.Assertions, content string, exists bool) { 146 r.False(exists) 147 }, 148 }, 149 150 { 151 path: filepath.Join("apicontent", "public"), 152 check: func(r *require.Assertions, content string, exists bool) { 153 r.False(exists) 154 }, 155 }, 156 } 157 158 testhelpers.RunWithinTempFolder(t, func(t *testing.T) { 159 r := require.New(t) 160 _, err := testhelpers.RunBuffaloCMD(t, []string{"new", "apicontent", "--api", "-f", "--vcs", "none"}) 161 r.NoError(err) 162 163 for _, tc := range tt { 164 t.Run(tc.path, func(t *testing.T) { 165 r := require.New(t) 166 exists := true 167 168 b, err := os.ReadFile(tc.path) 169 if err != nil && errors.Is(err, os.ErrNotExist) { 170 exists = false 171 } 172 173 tc.check(r, string(b), exists) 174 }) 175 } 176 }) 177 } 178 179 func TestNewAppTravis(t *testing.T) { 180 r := require.New(t) 181 r.NoError(testhelpers.EnsureBuffaloCMD(t)) 182 183 tt := []struct { 184 path string 185 check func(*require.Assertions, string, bool) 186 }{ 187 { 188 path: filepath.Join("apitravis", ".travis.yml"), 189 check: func(r *require.Assertions, content string, exists bool) { 190 r.True(exists) 191 r.Contains(string(content), "language: go") 192 r.Contains(string(content), "1.11.x") 193 r.Contains(string(content), "go_import_path:") 194 }, 195 }, 196 197 { 198 path: filepath.Join("apitravis", "database.yml"), 199 check: func(r *require.Assertions, content string, exists bool) { 200 r.True(exists) 201 r.Contains(string(content), "dialect: \"sqlite3\"") 202 r.Contains(string(content), "development:") 203 r.Contains(string(content), "production:") 204 r.Contains(string(content), "test:") 205 }, 206 }, 207 } 208 209 testhelpers.RunWithinTempFolder(t, func(t *testing.T) { 210 out, err := testhelpers.RunBuffaloCMD(t, []string{"new", "apitravis", "--api", "-f", "--vcs", "none", "--ci-provider", "travis", "--db-type", "sqlite3"}) 211 t.Log(out) 212 r.NoError(err) 213 214 r := require.New(t) 215 for _, tc := range tt { 216 t.Run(tc.path, func(t *testing.T) { 217 b, err := os.ReadFile(tc.path) 218 219 exists := true 220 if err != nil && errors.Is(err, os.ErrNotExist) { 221 exists = false 222 } 223 224 tc.check(r, string(b), exists) 225 }) 226 } 227 }) 228 }