github.com/JarrahG/buffalocli@v0.0.0-20230801092127-b85bfd5d395a/internal/genny/fix/npm_test.go (about)

     1  package fix
     2  
     3  import (
     4  	"encoding/json"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/gobuffalo/genny/v2"
     9  	"github.com/gobuffalo/genny/v2/gentest"
    10  	"github.com/gobuffalo/meta"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func Test_AddPackageJSONScripts_AddScriptSection(t *testing.T) {
    15  	r := require.New(t)
    16  
    17  	opts := &Options{
    18  		App: meta.App{
    19  			Root:        ".",
    20  			WithWebpack: true,
    21  		},
    22  	}
    23  
    24  	run := gentest.NewRunner()
    25  	r.NoError(run.File(genny.NewFileS("package.json", "{}")))
    26  
    27  	run.WithRun(AddPackageJSONScripts(opts))
    28  	r.NoError(run.Run())
    29  
    30  	results := run.Results()
    31  	f := results.Files[0]
    32  	r.Equal("package.json", f.Name())
    33  
    34  	packageJSON := map[string]map[string]string{}
    35  	r.NoError(json.NewDecoder(f).Decode(&packageJSON))
    36  	r.EqualValues(defaultScripts, packageJSON["scripts"])
    37  }
    38  
    39  func Test_AddPackageJSONScripts_AddMissingScripts(t *testing.T) {
    40  	r := require.New(t)
    41  
    42  	opts := &Options{
    43  		App: meta.App{
    44  			Root:        ".",
    45  			WithWebpack: true,
    46  		},
    47  	}
    48  
    49  	tt := []struct {
    50  		Name    string
    51  		Scripts map[string]string
    52  	}{
    53  		{
    54  			Name:    "EmptyScripts",
    55  			Scripts: map[string]string{},
    56  		},
    57  		{
    58  			Name: "MissingDev",
    59  			Scripts: map[string]string{
    60  				"build": "echo 'hello'",
    61  				"start": "echo 'hello'",
    62  			},
    63  		},
    64  		{
    65  			Name: "MissingBuild",
    66  			Scripts: map[string]string{
    67  				"dev":   "echo 'hello'",
    68  				"start": "echo 'hello'",
    69  			},
    70  		},
    71  		{
    72  			Name: "NoMissingScripts",
    73  			Scripts: map[string]string{
    74  				"dev":   "echo 'hello'",
    75  				"build": "echo 'hello'",
    76  				"start": "echo 'hello'",
    77  			},
    78  		},
    79  	}
    80  
    81  	for _, tc := range tt {
    82  		t.Run(tc.Name, func(t *testing.T) {
    83  			run := gentest.NewRunner()
    84  
    85  			packageJSON := map[string]map[string]string{
    86  				"scripts": tc.Scripts,
    87  			}
    88  			f := genny.NewFile("package.json", nil)
    89  			r.NoError(json.NewEncoder(f).Encode(packageJSON))
    90  			r.NoError(run.File(f))
    91  
    92  			run.WithRun(AddPackageJSONScripts(opts))
    93  			r.NoError(run.Run())
    94  
    95  			results := run.Results()
    96  			f = results.Files[0]
    97  			r.Equal("package.json", f.Name())
    98  
    99  			r.NoError(json.NewDecoder(f).Decode(&packageJSON))
   100  
   101  			for k, v := range packageJSON["scripts"] {
   102  				if _, ok := tc.Scripts[k]; !ok {
   103  					r.Equalf(defaultScripts[k], v, "missing default script: %s", k)
   104  				} else {
   105  					r.Equalf(tc.Scripts[k], v, "script %s has been altered", k)
   106  				}
   107  			}
   108  		})
   109  	}
   110  }
   111  
   112  func Test_PackageJSONCheck_NoOverwriteExisting(t *testing.T) {
   113  	r := require.New(t)
   114  
   115  	opts := &Options{
   116  		App: meta.App{
   117  			Root:        ".",
   118  			WithWebpack: true,
   119  		},
   120  	}
   121  
   122  	run := gentest.NewRunner()
   123  	bb, err := defaultPackageJson(opts.App)
   124  	r.NoError(err)
   125  	fileContents := bb.String()
   126  	r.NoError(run.File(genny.NewFileS("package.json", fileContents)))
   127  
   128  	run.WithRun(PackageJSONCheck(opts))
   129  	r.NoError(run.Run())
   130  
   131  	results := run.Results()
   132  	f := results.Files[0]
   133  	r.Equal("package.json", f.Name())
   134  	r.Equal(fileContents, f.String())
   135  }
   136  
   137  func Test_PackageJSONCheck_UpdatingFileAndClearNodeModules(t *testing.T) {
   138  	r := require.New(t)
   139  
   140  	opts := &Options{
   141  		App: meta.App{
   142  			Root:        ".",
   143  			WithWebpack: true,
   144  		},
   145  		YesToAll: true,
   146  	}
   147  
   148  	bb, err := defaultPackageJson(opts.App)
   149  	r.NoError(err)
   150  	fileContents := bb.String()
   151  
   152  	tt := []struct {
   153  		Name     string
   154  		WithYarn bool
   155  		Command  string
   156  	}{
   157  		{
   158  			Name:     "WithYarn",
   159  			WithYarn: true,
   160  			Command:  "yarn install",
   161  		},
   162  		{
   163  			Name:     "WithoutYarn",
   164  			WithYarn: false,
   165  			Command:  "npm install",
   166  		},
   167  	}
   168  
   169  	for _, tc := range tt {
   170  		t.Run(tc.Name, func(t *testing.T) {
   171  			run := gentest.NewRunner()
   172  			r.NoError(run.File(genny.NewFileS("package.json", "{}")))
   173  			r.NoError(run.File(genny.NewFileS("node_modules/test.txt", "this file should be deleted")))
   174  
   175  			opts.App.WithYarn = tc.WithYarn
   176  			run.WithRun(PackageJSONCheck(opts))
   177  			r.NoError(run.Run())
   178  
   179  			results := run.Results()
   180  
   181  			r.Len(results.Files, 1, "node_modules has not been deleted")
   182  
   183  			f := results.Files[0]
   184  			r.Equal("package.json", f.Name())
   185  			r.Equal(fileContents, f.String())
   186  
   187  			r.Len(results.Commands, 1, "command has not been run")
   188  			c := results.Commands[0]
   189  			r.Equal(tc.Command, strings.Join(c.Args, " "))
   190  		})
   191  	}
   192  }