github.com/wawandco/oxpecker@v1.5.7-0.20210910201653-5958d4afdd89/tools/buffalo/app/initializer_test.go (about)

     1  package app
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  	"testing"
    10  
    11  	"github.com/wawandco/oxpecker/lifecycle/new"
    12  )
    13  
    14  func TestInitializer(t *testing.T) {
    15  	t.Run("CompleteArgs", func(t *testing.T) {
    16  		root := t.TempDir()
    17  		err := os.Chdir(root)
    18  		if err != nil {
    19  			t.Error("could not change to temp directory")
    20  		}
    21  
    22  		err = os.MkdirAll(filepath.Join(root, "myapp"), 0777)
    23  		if err != nil {
    24  			t.Error("could not change to temp directory")
    25  		}
    26  
    27  		i := Initializer{}
    28  		ctx := context.Background()
    29  		options := new.Options{
    30  			Name:   "myapp",
    31  			Module: "oosss/myapp",
    32  			Folder: filepath.Join(root, "myapp"),
    33  		}
    34  
    35  		err = i.Initialize(ctx, options)
    36  		if err != nil {
    37  			t.Fatalf("error should be nil, got %v", err)
    38  		}
    39  
    40  		bm, err := ioutil.ReadFile(filepath.Join(root, "myapp", "app", "app.go"))
    41  		if err != nil {
    42  			t.Fatal("should have created the file")
    43  		}
    44  
    45  		if !bytes.Contains(bm, []byte(`package app`)) {
    46  			t.Fatal("should contain package name")
    47  		}
    48  
    49  		if !bytes.Contains(bm, []byte(`New() *buffalo.App {`)) {
    50  			t.Fatal("should contain func signature")
    51  		}
    52  
    53  		bm, err = ioutil.ReadFile(filepath.Join(root, "myapp", "app", "routes.go"))
    54  		if err != nil {
    55  			t.Fatal("should have created the file")
    56  		}
    57  
    58  		if !bytes.Contains(bm, []byte(`package app`)) {
    59  			t.Fatal("should contain package name")
    60  		}
    61  
    62  		if !bytes.Contains(bm, []byte(`func setRoutes(root *buffalo.App) {`)) {
    63  			t.Fatal("should use contain func signature")
    64  		}
    65  
    66  	})
    67  
    68  }