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

     1  package gnomod
     2  
     3  import (
     4  	"path/filepath"
     5  	"testing"
     6  
     7  	"github.com/gnolang/gno/tm2/pkg/testutils"
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestModuleDeprecated(t *testing.T) {
    13  	for _, tc := range []struct {
    14  		desc, in, expected string
    15  	}{
    16  		{
    17  			desc: "no_comment",
    18  			in:   `module m`,
    19  		},
    20  		{
    21  			desc: "other_comment",
    22  			in: `// yo
    23  			module m`,
    24  		},
    25  		{
    26  			desc: "deprecated_no_colon",
    27  			in: `//Deprecated
    28  			module m`,
    29  		},
    30  		{
    31  			desc: "deprecated_no_space",
    32  			in: `//Deprecated:blah
    33  			module m`,
    34  			expected: "blah",
    35  		},
    36  		{
    37  			desc: "deprecated_simple",
    38  			in: `// Deprecated: blah
    39  			module m`,
    40  			expected: "blah",
    41  		},
    42  		{
    43  			desc: "deprecated_lowercase",
    44  			in: `// deprecated: blah
    45  			module m`,
    46  		},
    47  		{
    48  			desc: "deprecated_multiline",
    49  			in: `// Deprecated: one
    50  			// two
    51  			module m`,
    52  			expected: "one\ntwo",
    53  		},
    54  		{
    55  			desc: "deprecated_mixed",
    56  			in: `// some other comment
    57  			// Deprecated: blah
    58  			module m`,
    59  		},
    60  		{
    61  			desc: "deprecated_middle",
    62  			in: `// module m is Deprecated: blah
    63  			module m`,
    64  		},
    65  		{
    66  			desc: "deprecated_multiple",
    67  			in: `// Deprecated: a
    68  			// Deprecated: b
    69  			module m`,
    70  			expected: "a\nDeprecated: b",
    71  		},
    72  		{
    73  			desc: "deprecated_paragraph",
    74  			in: `// Deprecated: a
    75  			// b
    76  			//
    77  			// c
    78  			module m`,
    79  			expected: "a\nb",
    80  		},
    81  		{
    82  			desc: "deprecated_paragraph_space",
    83  			in: `// Deprecated: the next line has a space
    84  			// 
    85  			// c
    86  			module m`,
    87  			expected: "the next line has a space",
    88  		},
    89  		{
    90  			desc:     "deprecated_suffix",
    91  			in:       `module m // Deprecated: blah`,
    92  			expected: "blah",
    93  		},
    94  		{
    95  			desc: `deprecated_mixed_suffix`,
    96  			in: `// some other comment
    97  			module m // Deprecated: blah`,
    98  		},
    99  		{
   100  			desc: "deprecated_mixed_suffix_paragraph",
   101  			in: `// some other comment
   102  			//
   103  			module m // Deprecated: blah`,
   104  			expected: "blah",
   105  		},
   106  		{
   107  			desc: "deprecated_block",
   108  			in: `// Deprecated: blah
   109  			module (
   110  				m
   111  			)`,
   112  			expected: "blah",
   113  		},
   114  	} {
   115  		t.Run(tc.desc, func(t *testing.T) {
   116  			f, err := Parse("in", []byte(tc.in))
   117  			assert.Nil(t, err)
   118  			assert.Equal(t, tc.expected, f.Module.Deprecated)
   119  		})
   120  	}
   121  }
   122  
   123  func TestParseDraft(t *testing.T) {
   124  	for _, tc := range []struct {
   125  		desc, in string
   126  		expected bool
   127  	}{
   128  		{
   129  			desc: "no_comment",
   130  			in:   `module m`,
   131  		},
   132  		{
   133  			desc: "other_comment",
   134  			in:   `// yo`,
   135  		},
   136  		{
   137  			desc:     "draft_no_space",
   138  			in:       `//Draft`,
   139  			expected: true,
   140  		},
   141  		{
   142  			desc:     "draft_simple",
   143  			in:       `// Draft`,
   144  			expected: true,
   145  		},
   146  		{
   147  			desc: "draft_lowercase",
   148  			in:   `// draft`,
   149  		},
   150  		{
   151  			desc: "draft_multiline",
   152  			in: `// Draft
   153  			// yo`,
   154  		},
   155  		{
   156  			desc: "draft_mixed",
   157  			in: `// some other comment
   158  			// Draft`,
   159  		},
   160  		{
   161  			desc: "draft_not_first_line",
   162  			in: `
   163  			// Draft`,
   164  		},
   165  	} {
   166  		t.Run(tc.desc, func(t *testing.T) {
   167  			f, err := Parse("in", []byte(tc.in))
   168  			assert.Nil(t, err)
   169  			assert.Equal(t, tc.expected, f.Draft)
   170  		})
   171  	}
   172  }
   173  
   174  func TestParseGnoMod(t *testing.T) {
   175  	pkgDir := "bar"
   176  	for _, tc := range []struct {
   177  		desc, modData, modPath, errShouldContain string
   178  	}{
   179  		{
   180  			desc:             "file not exists",
   181  			modData:          `module foo`,
   182  			modPath:          filepath.Join(pkgDir, "mod.gno"),
   183  			errShouldContain: "could not read gno.mod file:",
   184  		},
   185  		{
   186  			desc:             "file path is dir",
   187  			modData:          `module foo`,
   188  			modPath:          pkgDir,
   189  			errShouldContain: "is a directory",
   190  		},
   191  		{
   192  			desc:    "valid gno.mod file",
   193  			modData: `module foo`,
   194  			modPath: filepath.Join(pkgDir, "gno.mod"),
   195  		},
   196  		{
   197  			desc:             "error parsing gno.mod",
   198  			modData:          `module foo v0.0.0`,
   199  			modPath:          filepath.Join(pkgDir, "gno.mod"),
   200  			errShouldContain: "error parsing gno.mod file at",
   201  		},
   202  		{
   203  			desc:             "error validating gno.mod",
   204  			modData:          `require bar v0.0.0`,
   205  			modPath:          filepath.Join(pkgDir, "gno.mod"),
   206  			errShouldContain: "error validating gno.mod file at",
   207  		},
   208  	} {
   209  		t.Run(tc.desc, func(t *testing.T) {
   210  			// Create test dir
   211  			tempDir, cleanUpFn := testutils.NewTestCaseDir(t)
   212  			require.NotNil(t, tempDir)
   213  			defer cleanUpFn()
   214  
   215  			// Create gno package
   216  			createGnoModPkg(t, tempDir, pkgDir, tc.modData)
   217  
   218  			_, err := ParseGnoMod(filepath.Join(tempDir, tc.modPath))
   219  			if tc.errShouldContain != "" {
   220  				assert.ErrorContains(t, err, tc.errShouldContain)
   221  			} else {
   222  				assert.NoError(t, err)
   223  			}
   224  		})
   225  	}
   226  }