github.com/JarrahG/buffalocli@v0.0.0-20230801092127-b85bfd5d395a/internal/genny/assets/webpack/webpack_test.go (about)

     1  package webpack
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/gobuffalo/genny/v2"
     8  	"github.com/gobuffalo/genny/v2/gentest"
     9  	"github.com/gobuffalo/meta"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func runner() *genny.Runner {
    14  	run := gentest.NewRunner()
    15  	run.Disk.Add(genny.NewFileS("templates/application.plush.html", layout))
    16  	run.LookPathFn = func(s string) (string, error) {
    17  		return s, nil
    18  	}
    19  	return run
    20  }
    21  
    22  func Test_Webpack_New(t *testing.T) {
    23  	r := require.New(t)
    24  
    25  	g, err := New(&Options{})
    26  	r.NoError(err)
    27  
    28  	run := runner()
    29  	r.NoError(run.With(g))
    30  	r.NoError(run.Run())
    31  
    32  	res := run.Results()
    33  	r.Len(res.Commands, 2)
    34  	c := res.Commands[0]
    35  	r.Equal("npm --version", strings.Join(c.Args, " "))
    36  	c = res.Commands[1]
    37  	r.Equal("npm install --no-progress --save", strings.Join(c.Args, " "))
    38  
    39  	files := []string{
    40  		".babelrc",
    41  		"assets/css/_buffalo.scss",
    42  		"assets/css/application.scss",
    43  		"assets/images/favicon.ico",
    44  		"assets/images/logo.svg",
    45  		"assets/js/application.js",
    46  		"package.json",
    47  		"postcss.config.js",
    48  		"public/assets/keep",
    49  		"templates/application.plush.html",
    50  		"webpack.config.js",
    51  	}
    52  	r.Len(res.Files, len(files))
    53  	for i, f := range res.Files {
    54  		r.Equal(files[i], f.Name())
    55  	}
    56  
    57  	f, err := res.Find("package.json")
    58  	r.NoError(err)
    59  	r.Contains(f.String(), `"bootstrap": "^5.`)
    60  }
    61  
    62  func Test_Webpack_New_WithYarn(t *testing.T) {
    63  	r := require.New(t)
    64  
    65  	g, err := New(&Options{
    66  		App: meta.App{WithYarn: true},
    67  	})
    68  	r.NoError(err)
    69  
    70  	run := runner()
    71  	r.NoError(run.With(g))
    72  	r.NoError(run.Run())
    73  
    74  	res := run.Results()
    75  	r.Len(res.Commands, 6)
    76  	r.Len(res.Files, 11) // see file list in Test_Webpack_New
    77  
    78  	versionCommand := res.Commands[0]
    79  	r.Equal("yarn --version", strings.Join(versionCommand.Args, " "))
    80  
    81  	berryCommand := res.Commands[1]
    82  	r.Equal("yarn set version berry", strings.Join(berryCommand.Args, " "))
    83  
    84  	installCommand := res.Commands[5]
    85  	r.Equal("yarn install", strings.Join(installCommand.Args, " "))
    86  }
    87  
    88  const layout = `<!DOCTYPE html>
    89  <html>
    90    <head>
    91      <title>Buffalo - Foo</title>
    92      <%= stylesheetTag("buffalo.css") %>
    93      <%= stylesheetTag("application.css") %>
    94    </head>
    95    <body>
    96    </body>
    97  </html>
    98  `