github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/gnovm/pkg/gnomod/gnomod_test.go (about)

     1  package gnomod
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/gnolang/gno/tm2/pkg/testutils"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestCreateGnoModFile(t *testing.T) {
    14  	t.Parallel()
    15  
    16  	for _, tc := range []struct {
    17  		desc             string
    18  		in               []struct{ filename, content string }
    19  		inModPath        string
    20  		out              string
    21  		errShouldContain string
    22  	}{
    23  		{
    24  			desc:      "empty directory",
    25  			inModPath: "gno.land/p/demo/foo",
    26  			out:       "module gno.land/p/demo/foo\n",
    27  		},
    28  		{
    29  			desc:             "empty directory (without modPath)",
    30  			errShouldContain: "cannot determine package name",
    31  		},
    32  		{
    33  			desc:             "invalid modPath 1",
    34  			inModPath:        " ",
    35  			errShouldContain: "malformed import path",
    36  		},
    37  		{
    38  			desc:             "invalid modPath 2",
    39  			inModPath:        "\"",
    40  			errShouldContain: "malformed import path",
    41  		},
    42  		{
    43  			desc: "valid package",
    44  			in: []struct{ filename, content string }{
    45  				{
    46  					"foo.gno",
    47  					`package foo`,
    48  				},
    49  			},
    50  			inModPath: "gno.land/p/demo/foo",
    51  			out:       "module gno.land/p/demo/foo\n",
    52  		},
    53  		{
    54  			desc: "valid package (without modPath)",
    55  			in: []struct{ filename, content string }{
    56  				{
    57  					"foo.gno",
    58  					`package foo`,
    59  				},
    60  			},
    61  			out: "module foo\n",
    62  		},
    63  		{
    64  			desc: "ambigious package names",
    65  			in: []struct{ filename, content string }{
    66  				{
    67  					"foo.gno",
    68  					`package foo`,
    69  				},
    70  				{
    71  					"bar.gno",
    72  					`package bar`,
    73  				},
    74  			},
    75  			inModPath: "gno.land/p/demo/foo",
    76  			out:       "module gno.land/p/demo/foo\n",
    77  		},
    78  		{
    79  			desc: "ambigious package names (without modPath)",
    80  			in: []struct{ filename, content string }{
    81  				{
    82  					"foo.gno",
    83  					`package foo`,
    84  				},
    85  				{
    86  					"bar.gno",
    87  					`package bar`,
    88  				},
    89  			},
    90  			errShouldContain: "package name mismatch:",
    91  		},
    92  		{
    93  			desc: "valid package with gno.mod file",
    94  			in: []struct{ filename, content string }{
    95  				{
    96  					"foo.gno",
    97  					`package foo`,
    98  				},
    99  				{
   100  					"gno.mod",
   101  					`module gno.land/p/demo/foo`,
   102  				},
   103  			},
   104  			inModPath:        "gno.land/p/demo/foo",
   105  			errShouldContain: "gno.mod file already exists",
   106  		},
   107  		{
   108  			desc: "valid package with gno.mod file (without modPath)",
   109  			in: []struct{ filename, content string }{
   110  				{
   111  					"foo.gno",
   112  					`package foo`,
   113  				},
   114  				{
   115  					"gno.mod",
   116  					`module gno.land/p/demo/foo`,
   117  				},
   118  			},
   119  			errShouldContain: "gno.mod file already exists",
   120  		},
   121  	} {
   122  		tc := tc
   123  		t.Run(tc.desc, func(t *testing.T) {
   124  			t.Parallel()
   125  
   126  			// Create test dir
   127  			dirPath, cleanUpFn := testutils.NewTestCaseDir(t)
   128  			require.NotNil(t, dirPath)
   129  			defer cleanUpFn()
   130  
   131  			// Create files
   132  			for _, f := range tc.in {
   133  				err := os.WriteFile(filepath.Join(dirPath, f.filename), []byte(f.content), 0o644)
   134  				require.NoError(t, err)
   135  			}
   136  
   137  			err := CreateGnoModFile(dirPath, tc.inModPath)
   138  			if tc.errShouldContain != "" {
   139  				assert.Error(t, err)
   140  				assert.Contains(t, err.Error(), tc.errShouldContain)
   141  				return
   142  			}
   143  			assert.NoError(t, err)
   144  
   145  			// Verify gno.mod file
   146  			bz, err := os.ReadFile(filepath.Join(dirPath, "gno.mod"))
   147  			assert.NoError(t, err)
   148  			assert.Equal(t, tc.out, string(bz))
   149  		})
   150  	}
   151  }