github.com/wawandco/ox@v0.13.6-0.20230809142027-913b3d837f2a/plugins/tools/ox/action/initializer_test.go (about)

     1  package action
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/wawandco/ox/plugins/base/new"
    11  )
    12  
    13  func TestInitializer(t *testing.T) {
    14  	t.Run("CompleteArgs", func(t *testing.T) {
    15  		root := t.TempDir()
    16  		err := os.Chdir(root)
    17  		if err != nil {
    18  			t.Error("could not change to temp directory")
    19  		}
    20  
    21  		err = os.MkdirAll(filepath.Join(root, "myapp"), 0777)
    22  		if err != nil {
    23  			t.Error("could not change to temp directory")
    24  		}
    25  
    26  		i := Initializer{}
    27  		ctx := context.Background()
    28  		options := new.Options{
    29  			Module: "oosss/myapp",
    30  			Folder: filepath.Join(root, "myapp"),
    31  		}
    32  
    33  		err = i.Initialize(ctx, options)
    34  		if err != nil {
    35  			t.Fatalf("error should be nil, got %v", err)
    36  		}
    37  
    38  		tcases := []struct {
    39  			path    string
    40  			content []string
    41  		}{
    42  			{
    43  				path: filepath.Join(root, "myapp", "app", "actions", "actions.go"),
    44  				content: []string{
    45  					`r = render.Engine`,
    46  					`"oosss/myapp/app/render"`,
    47  				},
    48  			},
    49  
    50  			{
    51  				path: filepath.Join(root, "myapp", "app", "actions", "actions_test.go"),
    52  				content: []string{
    53  					`package actions_test`,
    54  					`"oosss/myapp/app"`,
    55  					`func Test_ActionSuite(t *testing.T) {`,
    56  				},
    57  			},
    58  		}
    59  
    60  		for _, tcase := range tcases {
    61  			bm, err := os.ReadFile(tcase.path)
    62  			if err != nil {
    63  				t.Fatal("should have created the file:", tcase.path)
    64  			}
    65  
    66  			for _, cnt := range tcase.content {
    67  				if !bytes.Contains(bm, []byte(cnt)) {
    68  					t.Errorf("%v does not contain `%v`", tcase.path, cnt)
    69  				}
    70  			}
    71  		}
    72  
    73  	})
    74  }