...

Source file src/gotest.tools/v3/assert/cmd/gty-migrate-from-testify/migrate_test.go

Documentation: gotest.tools/v3/assert/cmd/gty-migrate-from-testify

     1  package main
     2  
     3  import (
     4  	"go/token"
     5  	"testing"
     6  
     7  	"golang.org/x/tools/go/packages"
     8  	"gotest.tools/v3/assert"
     9  	"gotest.tools/v3/assert/cmp"
    10  	"gotest.tools/v3/env"
    11  	"gotest.tools/v3/fs"
    12  	"gotest.tools/v3/icmd"
    13  )
    14  
    15  func TestMigrateFileReplacesTestingT(t *testing.T) {
    16  	source := `
    17  package foo
    18  
    19  import (
    20  	"testing"
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func TestSomething(t *testing.T) {
    26  	a := assert.TestingT(t)
    27  	b := require.TestingT(t)
    28  	c := require.TestingT(t)
    29  	if a == b {}
    30  	_ = c
    31  }
    32  
    33  func do(t require.TestingT) {}
    34  `
    35  	migration := newMigrationFromSource(t, source)
    36  	migrateFile(migration)
    37  
    38  	expected := `package foo
    39  
    40  import (
    41  	"testing"
    42  
    43  	"gotest.tools/v3/assert"
    44  )
    45  
    46  func TestSomething(t *testing.T) {
    47  	a := assert.TestingT(t)
    48  	b := assert.TestingT(t)
    49  	c := assert.TestingT(t)
    50  	if a == b {
    51  	}
    52  	_ = c
    53  }
    54  
    55  func do(t assert.TestingT) {}
    56  `
    57  	actual, err := formatFile(migration)
    58  	assert.NilError(t, err)
    59  	assert.Assert(t, cmp.Equal(expected, string(actual)))
    60  }
    61  
    62  func newMigrationFromSource(t *testing.T, source string) migration {
    63  	t.Helper()
    64  	goMod := `module example.com/foo
    65  
    66  require  github.com/stretchr/testify v1.7.1
    67  `
    68  
    69  	dir := fs.NewDir(t, t.Name(),
    70  		fs.WithFile("foo.go", source),
    71  		fs.WithFile("go.mod", goMod))
    72  	fileset := token.NewFileSet()
    73  
    74  	env.ChangeWorkingDir(t, dir.Path())
    75  	icmd.RunCommand("go", "mod", "tidy").Assert(t, icmd.Success)
    76  
    77  	opts := options{pkgs: []string{"./..."}}
    78  	pkgs, err := loadPackages(opts, fileset)
    79  	assert.NilError(t, err)
    80  	packages.PrintErrors(pkgs)
    81  
    82  	pkg := pkgs[0]
    83  	assert.Assert(t, !pkg.IllTyped)
    84  
    85  	return migration{
    86  		file:        pkg.Syntax[0],
    87  		fileset:     fileset,
    88  		importNames: newImportNames(pkg.Syntax[0].Imports, opts),
    89  		pkgInfo:     pkg.TypesInfo,
    90  	}
    91  }
    92  
    93  func TestMigrateFileWithNamedCmpPackage(t *testing.T) {
    94  	source := `
    95  package foo
    96  
    97  import (
    98  	"testing"
    99  	"github.com/stretchr/testify/assert"
   100  )
   101  
   102  func TestSomething(t *testing.T) {
   103  	assert.Equal(t, "a", "b")
   104  }
   105  `
   106  	migration := newMigrationFromSource(t, source)
   107  	migration.importNames.cmp = "is"
   108  	migrateFile(migration)
   109  
   110  	expected := `package foo
   111  
   112  import (
   113  	"testing"
   114  
   115  	"gotest.tools/v3/assert"
   116  	is "gotest.tools/v3/assert/cmp"
   117  )
   118  
   119  func TestSomething(t *testing.T) {
   120  	assert.Check(t, is.Equal("a", "b"))
   121  }
   122  `
   123  	actual, err := formatFile(migration)
   124  	assert.NilError(t, err)
   125  	assert.Assert(t, cmp.Equal(expected, string(actual)))
   126  }
   127  
   128  func TestMigrateFileWithCommentsOnAssert(t *testing.T) {
   129  	source := `
   130  package foo
   131  
   132  import (
   133  	"testing"
   134  	"github.com/stretchr/testify/assert"
   135  )
   136  
   137  func TestSomething(t *testing.T) {
   138  	// This is going to fail
   139  	assert.Equal(t, "a", "b")
   140  }
   141  `
   142  	migration := newMigrationFromSource(t, source)
   143  	migrateFile(migration)
   144  
   145  	expected := `package foo
   146  
   147  import (
   148  	"testing"
   149  
   150  	"gotest.tools/v3/assert"
   151  	"gotest.tools/v3/assert/cmp"
   152  )
   153  
   154  func TestSomething(t *testing.T) {
   155  	// This is going to fail
   156  	assert.Check(t, cmp.Equal("a", "b"))
   157  }
   158  `
   159  	actual, err := formatFile(migration)
   160  	assert.NilError(t, err)
   161  	assert.Assert(t, cmp.Equal(expected, string(actual)))
   162  }
   163  
   164  func TestMigrateFileConvertNilToNilError(t *testing.T) {
   165  	source := `
   166  package foo
   167  
   168  import (
   169  	"testing"
   170  	"github.com/stretchr/testify/require"
   171  	"github.com/stretchr/testify/assert"
   172  )
   173  
   174  func TestSomething(t *testing.T) {
   175  	var err error
   176  	assert.Nil(t, err)
   177  	require.Nil(t, err)
   178  }
   179  `
   180  	migration := newMigrationFromSource(t, source)
   181  	migrateFile(migration)
   182  
   183  	expected := `package foo
   184  
   185  import (
   186  	"testing"
   187  
   188  	"gotest.tools/v3/assert"
   189  )
   190  
   191  func TestSomething(t *testing.T) {
   192  	var err error
   193  	assert.Check(t, err)
   194  	assert.NilError(t, err)
   195  }
   196  `
   197  	actual, err := formatFile(migration)
   198  	assert.NilError(t, err)
   199  	assert.Assert(t, cmp.Equal(expected, string(actual)))
   200  }
   201  
   202  func TestMigrateFileConvertAssertNew(t *testing.T) {
   203  	source := `
   204  package foo
   205  
   206  import (
   207  	"testing"
   208  	"github.com/stretchr/testify/assert"
   209  	"github.com/stretchr/testify/require"
   210  )
   211  
   212  func TestSomething(t *testing.T) {
   213  	is := assert.New(t)
   214  	is.Equal("one", "two")
   215  	is.NotEqual("one", "two")
   216  
   217  	assert := require.New(t)
   218  	assert.Equal("one", "two")
   219  	assert.NotEqual("one", "two")
   220  }
   221  
   222  func TestOtherName(z *testing.T) {
   223  	is := require.New(z)
   224  	is.Equal("one", "two")
   225  }
   226  
   227  `
   228  	migration := newMigrationFromSource(t, source)
   229  	migrateFile(migration)
   230  
   231  	expected := `package foo
   232  
   233  import (
   234  	"testing"
   235  
   236  	"gotest.tools/v3/assert"
   237  	"gotest.tools/v3/assert/cmp"
   238  )
   239  
   240  func TestSomething(t *testing.T) {
   241  
   242  	assert.Check(t, cmp.Equal("one", "two"))
   243  	assert.Check(t, "one" != "two")
   244  
   245  	assert.Equal(t, "one", "two")
   246  	assert.Assert(t, "one" != "two")
   247  }
   248  
   249  func TestOtherName(z *testing.T) {
   250  
   251  	assert.Equal(z, "one", "two")
   252  }
   253  `
   254  	actual, err := formatFile(migration)
   255  	assert.NilError(t, err)
   256  	assert.Assert(t, cmp.Equal(expected, string(actual)))
   257  }
   258  
   259  func TestMigrateFileWithExtraArgs(t *testing.T) {
   260  	source := `
   261  package foo
   262  
   263  import (
   264  	"testing"
   265  	"github.com/stretchr/testify/assert"
   266  	"github.com/stretchr/testify/require"
   267  )
   268  
   269  func TestSomething(t *testing.T) {
   270  	var err error
   271  	assert.Error(t, err, "this is a comment")
   272  	require.ErrorContains(t, err, "this in the error")
   273  	assert.Empty(t, nil, "more comment")
   274  	require.Equal(t, []string{}, []string{}, "because")
   275  }
   276  `
   277  	migration := newMigrationFromSource(t, source)
   278  	migration.importNames.cmp = "is"
   279  	migrateFile(migration)
   280  
   281  	expected := `package foo
   282  
   283  import (
   284  	"testing"
   285  
   286  	"gotest.tools/v3/assert"
   287  	is "gotest.tools/v3/assert/cmp"
   288  )
   289  
   290  func TestSomething(t *testing.T) {
   291  	var err error
   292  	assert.Check(t, is.ErrorContains(err, ""), "this is a comment")
   293  	assert.ErrorContains(t, err, "this in the error")
   294  	assert.Check(t, is.Len(nil, 0), "more comment")
   295  	assert.Assert(t, is.DeepEqual([]string{}, []string{}), "because")
   296  }
   297  `
   298  	actual, err := formatFile(migration)
   299  	assert.NilError(t, err)
   300  	assert.Assert(t, cmp.Equal(expected, string(actual)))
   301  }
   302  
   303  func TestMigrate_AssertAlreadyImported(t *testing.T) {
   304  	source := `
   305  package foo
   306  
   307  import (
   308  	"testing"
   309  	"github.com/stretchr/testify/require"
   310  	"gotest.tools/v3/assert"
   311  )
   312  
   313  func TestSomething(t *testing.T) {
   314  	var err error
   315  	assert.Error(t, err, "this is the error")
   316  	require.Equal(t, []string{}, []string{}, "because")
   317  }
   318  `
   319  	migration := newMigrationFromSource(t, source)
   320  	migration.importNames.cmp = "is"
   321  	migrateFile(migration)
   322  
   323  	expected := `package foo
   324  
   325  import (
   326  	"testing"
   327  
   328  	"gotest.tools/v3/assert"
   329  	is "gotest.tools/v3/assert/cmp"
   330  )
   331  
   332  func TestSomething(t *testing.T) {
   333  	var err error
   334  	assert.Error(t, err, "this is the error")
   335  	assert.Assert(t, is.DeepEqual([]string{}, []string{}), "because")
   336  }
   337  `
   338  	actual, err := formatFile(migration)
   339  	assert.NilError(t, err)
   340  	assert.Assert(t, cmp.Equal(expected, string(actual)))
   341  }
   342  
   343  func TestMigrate_AssertAlreadyImportedWithAlias(t *testing.T) {
   344  	source := `
   345  package foo
   346  
   347  import (
   348  	"testing"
   349  	"github.com/stretchr/testify/require"
   350  	gtya "gotest.tools/v3/assert"
   351  )
   352  
   353  func TestSomething(t *testing.T) {
   354  	var err error
   355  	gtya.Error(t, err, "this is the error")
   356  	require.Equal(t, []string{}, []string{}, "because")
   357  }
   358  `
   359  	migration := newMigrationFromSource(t, source)
   360  	migration.importNames.cmp = "is"
   361  	migrateFile(migration)
   362  
   363  	expected := `package foo
   364  
   365  import (
   366  	"testing"
   367  
   368  	gtya "gotest.tools/v3/assert"
   369  	is "gotest.tools/v3/assert/cmp"
   370  )
   371  
   372  func TestSomething(t *testing.T) {
   373  	var err error
   374  	gtya.Error(t, err, "this is the error")
   375  	gtya.Assert(t, is.DeepEqual([]string{}, []string{}), "because")
   376  }
   377  `
   378  	actual, err := formatFile(migration)
   379  	assert.NilError(t, err)
   380  	assert.Assert(t, cmp.Equal(expected, string(actual)))
   381  }
   382  

View as plain text