github.com/corylanou/buffalo@v0.8.0/generators/helpers_test.go (about)

     1  package generators
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestAppendRoute(t *testing.T) {
    13  	r := require.New(t)
    14  
    15  	tmpDir := os.TempDir()
    16  	packagePath := filepath.Join(tmpDir, "src", "sample")
    17  	os.MkdirAll(packagePath, 0755)
    18  	os.Chdir(packagePath)
    19  
    20  	const shortAppFileExample = `package actions
    21  
    22  import (
    23  	"os"
    24  
    25  	"github.com/gobuffalo/buffalo"
    26  	"github.com/gobuffalo/buffalo/examples/json-crud/models"
    27  	"github.com/gobuffalo/buffalo/middleware"
    28  	"github.com/markbates/going/defaults"
    29  )
    30  
    31  var ENV = defaults.String(os.Getenv("GO_ENV"), "development")
    32  var app *buffalo.App
    33  func App() *buffalo.App {
    34  	if app == nil {
    35  		app = buffalo.Automatic(buffalo.Options{
    36  			Env: ENV,
    37  		})
    38  
    39  		app.Use(middleware.SetContentType("application/json"))
    40  		app.Use(middleware.PopTransaction(models.DB))
    41  
    42  		app.Use(findUserMW)
    43  		app.GET("/users", UsersList)
    44  	}
    45  
    46  	return app
    47  }`
    48  
    49  	ioutil.WriteFile(filepath.Join(packagePath, "actions", "app.go"), []byte(shortAppFileExample), 0755)
    50  
    51  	AddRoute("GET", "/new/route", "UserCoolHandler")
    52  
    53  	contentAfter, _ := ioutil.ReadFile(filepath.Join(packagePath, "actions", "app.go"))
    54  	r.Equal(`package actions
    55  
    56  import (
    57  	"os"
    58  
    59  	"github.com/gobuffalo/buffalo"
    60  	"github.com/gobuffalo/buffalo/examples/json-crud/models"
    61  	"github.com/gobuffalo/buffalo/middleware"
    62  	"github.com/markbates/going/defaults"
    63  )
    64  
    65  var ENV = defaults.String(os.Getenv("GO_ENV"), "development")
    66  var app *buffalo.App
    67  func App() *buffalo.App {
    68  	if app == nil {
    69  		app = buffalo.Automatic(buffalo.Options{
    70  			Env: ENV,
    71  		})
    72  
    73  		app.Use(middleware.SetContentType("application/json"))
    74  		app.Use(middleware.PopTransaction(models.DB))
    75  
    76  		app.Use(findUserMW)
    77  		app.GET("/users", UsersList)
    78  		app.GET("/new/route", UserCoolHandler)
    79  	}
    80  
    81  	return app
    82  }`, string(contentAfter))
    83  
    84  }