github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/genny/actions/actions_test.go (about)

     1  package actions
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/gobuffalo/genny/v2"
     9  	"github.com/gobuffalo/genny/v2/gentest"
    10  	packr "github.com/gobuffalo/packr/v2"
    11  	"github.com/google/go-cmp/cmp"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func compare(a, b string) bool {
    16  	a = strings.TrimSpace(a)
    17  	a = strings.Replace(a, "\r", "", -1)
    18  	b = strings.TrimSpace(b)
    19  	b = strings.Replace(b, "\r", "", -1)
    20  	a = strings.TrimSpace(a)
    21  	b = strings.TrimSpace(b)
    22  	res := cmp.Equal(a, b)
    23  	if !res {
    24  		fmt.Println(cmp.Diff(a, b))
    25  	}
    26  	return res
    27  }
    28  
    29  func runner() *genny.Runner {
    30  	run := gentest.NewRunner()
    31  	run.Disk.AddBox(packr.New("actions/start/test", "../actions/_fixtures/inputs/clean"))
    32  	return run
    33  }
    34  
    35  func Test_New(t *testing.T) {
    36  	r := require.New(t)
    37  
    38  	g, err := New(&Options{
    39  		Name:    "user",
    40  		Actions: []string{"index"},
    41  	})
    42  	r.NoError(err)
    43  
    44  	run := runner()
    45  	run.With(g)
    46  
    47  	r.NoError(run.Run())
    48  
    49  	res := run.Results()
    50  
    51  	r.Len(res.Commands, 0)
    52  	// r.Len(res.Files, 4)
    53  
    54  	box := packr.New("genny/actions/Test_New", "../actions/_fixtures/outputs/clean")
    55  
    56  	files := []string{"actions/user.go.tmpl", "actions/app.go.tmpl", "actions/user_test.go.tmpl", "templates/user/index.plush.html"}
    57  
    58  	for _, s := range files {
    59  		x, err := box.FindString(s)
    60  		r.NoError(err)
    61  		f, err := res.Find(strings.TrimSuffix(s, ".tmpl"))
    62  		r.NoError(err)
    63  		fmt.Printf("\nfile %s", s)
    64  		r.True(compare(x, f.String()))
    65  	}
    66  }
    67  
    68  func Test_New_Multi(t *testing.T) {
    69  	r := require.New(t)
    70  
    71  	g, err := New(&Options{
    72  		Name:    "user",
    73  		Actions: []string{"show", "edit"},
    74  	})
    75  	r.NoError(err)
    76  
    77  	run := runner()
    78  	run.With(g)
    79  
    80  	err = run.Run()
    81  	r.NoError(err)
    82  
    83  	res := run.Results()
    84  
    85  	r.Len(res.Commands, 0)
    86  
    87  	box := packr.New("genny/actions/Test_New_Multi", "../actions/_fixtures/outputs/multi")
    88  
    89  	files := []string{"actions/user.go.tmpl", "actions/app.go.tmpl", "actions/user_test.go.tmpl", "templates/user/show.plush.html", "templates/user/edit.plush.html"}
    90  
    91  	for _, s := range files {
    92  		x, err := box.FindString(s)
    93  		r.NoError(err)
    94  		f, err := res.Find(strings.TrimSuffix(s, ".tmpl"))
    95  		r.NoError(err)
    96  		fmt.Printf("\nfile %s", f)
    97  		r.True(compare(x, f.String()))
    98  	}
    99  }
   100  
   101  func Test_New_Multi_Existing(t *testing.T) {
   102  	r := require.New(t)
   103  
   104  	g, err := New(&Options{
   105  		Name:    "user",
   106  		Actions: []string{"show", "edit"},
   107  	})
   108  	r.NoError(err)
   109  
   110  	run := gentest.NewRunner()
   111  	ins := packr.New("Test_New_Multi_Existing_input", "../actions/_fixtures/inputs/existing")
   112  	for _, n := range ins.List() {
   113  		x, err := ins.FindString(n)
   114  		r.NoError(err)
   115  		n = strings.TrimSuffix(n, ".tmpl")
   116  		run.Disk.Add(genny.NewFileS(n, x))
   117  	}
   118  	run.With(g)
   119  
   120  	err = run.Run()
   121  	r.NoError(err)
   122  
   123  	res := run.Results()
   124  
   125  	r.Len(res.Commands, 0)
   126  
   127  	box := packr.New("genny/actions/Test_New_Multi_Existing", "../actions/_fixtures/outputs/existing")
   128  
   129  	files := []string{"actions/user.go.tmpl", "actions/app.go.tmpl", "actions/user_test.go.tmpl", "templates/user/show.plush.html", "templates/user/edit.plush.html"}
   130  
   131  	for _, s := range files {
   132  		x, err := box.FindString(s)
   133  		r.NoError(err)
   134  		f, err := res.Find(strings.TrimSuffix(s, ".tmpl"))
   135  		r.NoError(err)
   136  		r.True(compare(x, f.String()))
   137  	}
   138  }
   139  
   140  func Test_New_SkipTemplates(t *testing.T) {
   141  	r := require.New(t)
   142  
   143  	g, err := New(&Options{
   144  		Name:          "user",
   145  		Actions:       []string{"index"},
   146  		SkipTemplates: true,
   147  	})
   148  	r.NoError(err)
   149  
   150  	run := runner()
   151  	run.With(g)
   152  
   153  	r.NoError(run.Run())
   154  
   155  	res := run.Results()
   156  
   157  	r.Len(res.Commands, 0)
   158  
   159  	files := []string{"templates/user/index.html"}
   160  
   161  	for _, s := range files {
   162  		_, err := res.Find(s)
   163  		r.Error(err)
   164  	}
   165  }