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