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

     1  package fix
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"regexp"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/gobuffalo/genny/v2/gentest"
    11  	"github.com/gobuffalo/meta"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func Test_Deprecations_mainGo(t *testing.T) {
    16  	r := require.New(t)
    17  
    18  	tt := []struct {
    19  		Name     string
    20  		warnings []string
    21  	}{
    22  		{
    23  			Name:     "buffalo0_11",
    24  			warnings: []string{"app.Start has been removed in v0.11.0. Use app.Serve Instead. [main.go]"},
    25  		},
    26  		{
    27  			Name:     "buffaloPre0_18api",
    28  			warnings: []string{},
    29  		},
    30  		{
    31  			Name:     "buffaloPre0_18web",
    32  			warnings: []string{},
    33  		},
    34  	}
    35  
    36  	for _, tc := range tt {
    37  		t.Run(tc.Name, func(t *testing.T) {
    38  			run := gentest.NewRunner()
    39  
    40  			err := run.Disk.AddFS(os.DirFS(filepath.Join("_fixtures", tc.Name)))
    41  			r.NoError(err)
    42  
    43  			opts := &Options{
    44  				App: meta.Named("coke", "."),
    45  			}
    46  			run.WithRun(MoveMain(opts)) // structural test should go first
    47  			g := DeprecationsCheck(opts)
    48  			run.WithRun(g)
    49  
    50  			r.NoError(run.Run())
    51  
    52  			results := run.Results()
    53  			_, err = results.Find("cmd/app/main.go")
    54  			r.NoError(err)
    55  
    56  			r.ElementsMatch(opts.warnings, tc.warnings)
    57  		})
    58  	}
    59  }
    60  
    61  func Test_Deprecations_ReplacePackr(t *testing.T) {
    62  	r := require.New(t)
    63  
    64  	tt := []struct {
    65  		Name        string
    66  		contains    map[string][]string
    67  		notContains map[string][]string
    68  	}{
    69  		{
    70  			Name: "buffaloPre0_18api",
    71  			contains: map[string][]string{
    72  				"actions/app.go": {
    73  					"\"coke/locales\"",
    74  					"i18n.New(locales.FS(), \"en-US\")",
    75  				},
    76  			},
    77  			notContains: map[string][]string{
    78  				"actions/app.go": {
    79  					"packr.New",
    80  				},
    81  				"actions/render.go": {
    82  					"packr.New",
    83  				},
    84  			},
    85  		},
    86  		{
    87  			Name: "buffaloPre0_18web",
    88  			contains: map[string][]string{
    89  				"actions/app.go": {
    90  					"\"coke/locales\"",
    91  					"\"coke/public\"",
    92  					"\"net/http\"",
    93  					"app.ServeFiles(\"/\", http.FS(public.FS()))",
    94  					"i18n.New(locales.FS(), \"en-US\")",
    95  				},
    96  				"actions/render.go": {
    97  					"\"coke/public\"",
    98  					"\"coke/templates\"",
    99  					"AssetsFS: public.FS()",
   100  					"TemplatesFS: templates.FS()",
   101  				},
   102  			},
   103  			notContains: map[string][]string{
   104  				"actions/render.go": {
   105  					"packr.New",
   106  				},
   107  			},
   108  		},
   109  	}
   110  
   111  	for _, tc := range tt {
   112  		t.Run(tc.Name, func(t *testing.T) {
   113  			run := gentest.NewRunner()
   114  
   115  			err := run.Disk.AddFS(os.DirFS(filepath.Join("_fixtures", tc.Name)))
   116  			r.NoError(err)
   117  
   118  			opts := &Options{
   119  				App: meta.Named("coke", "."),
   120  			}
   121  			run.WithRun(MoveMain(opts)) // structural test should go first
   122  			g := DeprecationsCheck(opts)
   123  			run.WithRun(g)
   124  
   125  			r.NoError(run.Run())
   126  			results := run.Results()
   127  
   128  			clean := func(s string) string {
   129  				s = strings.TrimSpace(s)
   130  				s = strings.ReplaceAll(s, "\n", "")
   131  				s = strings.ReplaceAll(s, "\t", "")
   132  				s = strings.ReplaceAll(s, "\r", "")
   133  
   134  				spaces := regexp.MustCompile(`\s+`)
   135  				return spaces.ReplaceAllString(s, " ")
   136  			}
   137  
   138  			for file, contains := range tc.contains {
   139  				f, err := results.Find(file)
   140  				r.NoError(err)
   141  
   142  				for _, c := range contains {
   143  					r.Contains(clean(f.String()), clean(c))
   144  				}
   145  			}
   146  
   147  			for file, notContains := range tc.notContains {
   148  				f, err := results.Find(file)
   149  				r.NoError(err)
   150  
   151  				for _, c := range notContains {
   152  					r.NotContains(clean(f.String()), clean(c))
   153  				}
   154  			}
   155  		})
   156  	}
   157  }
   158  
   159  func Test_Deprecations_ReplaceSuite(t *testing.T) {
   160  	r := require.New(t)
   161  
   162  	tt := []struct {
   163  		Name        string
   164  		contains    map[string][]string
   165  		notContains map[string][]string
   166  	}{
   167  		{
   168  			Name: "buffaloPre0_18api",
   169  			contains: map[string][]string{
   170  				"actions/actions_test.go": {
   171  					"\"os\"",
   172  					"suite.NewActionWithFixtures(App(), os.DirFS(\"../fixtures\"))",
   173  				},
   174  				"models/models_test.go": {
   175  					"\"os\"",
   176  					"suite.NewModelWithFixtures(os.DirFS(\"../fixtures\"))",
   177  				},
   178  			},
   179  			notContains: map[string][]string{
   180  				"actions/actions_test.go": {
   181  					"packr.New",
   182  				},
   183  				"models/models_test.go": {
   184  					"packr.New",
   185  				},
   186  			},
   187  		},
   188  		{
   189  			Name: "buffaloPre0_18web",
   190  			contains: map[string][]string{
   191  				"actions/actions_test.go": {
   192  					"suite.NewActionWithFixtures(App(), os.DirFS(\"../fixtures\"))",
   193  				},
   194  				"models/models_test.go": {
   195  					"suite.NewModelWithFixtures(os.DirFS(\"../fixtures\"))",
   196  				},
   197  			},
   198  			notContains: map[string][]string{
   199  				"actions/actions_test.go": {
   200  					"packr.New",
   201  				},
   202  				"models/models_test.go": {
   203  					"packr.New",
   204  				},
   205  			},
   206  		},
   207  	}
   208  
   209  	for _, tc := range tt {
   210  		t.Run(tc.Name, func(t *testing.T) {
   211  			run := gentest.NewRunner()
   212  
   213  			err := run.Disk.AddFS(os.DirFS(filepath.Join("_fixtures", tc.Name)))
   214  			r.NoError(err)
   215  
   216  			opts := &Options{
   217  				App: meta.Named("coke", "."),
   218  			}
   219  			run.WithRun(MoveMain(opts)) // structural test should go first
   220  			g := DeprecationsCheck(opts)
   221  			run.WithRun(g)
   222  
   223  			r.NoError(run.Run())
   224  			results := run.Results()
   225  
   226  			clean := func(s string) string {
   227  				s = strings.TrimSpace(s)
   228  				s = strings.ReplaceAll(s, "\n", "")
   229  				s = strings.ReplaceAll(s, "\t", "")
   230  				s = strings.ReplaceAll(s, "\r", "")
   231  
   232  				spaces := regexp.MustCompile(`\s+`)
   233  				return spaces.ReplaceAllString(s, " ")
   234  			}
   235  
   236  			for file, contains := range tc.contains {
   237  				f, err := results.Find(file)
   238  				r.NoError(err)
   239  
   240  				for _, c := range contains {
   241  					r.Contains(clean(f.String()), clean(c))
   242  				}
   243  			}
   244  
   245  			for file, notContains := range tc.notContains {
   246  				f, err := results.Find(file)
   247  				r.NoError(err)
   248  
   249  				for _, c := range notContains {
   250  					r.NotContains(clean(f.String()), clean(c))
   251  				}
   252  			}
   253  		})
   254  	}
   255  }