github.com/gnoswap-labs/gno@v0.0.4/gnovm/cmd/gno/mod_test.go (about) 1 package main 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestModApp(t *testing.T) { 13 tc := []testMainCase{ 14 { 15 args: []string{"mod"}, 16 errShouldBe: "flag: help requested", 17 }, 18 19 // test `gno mod download` 20 { 21 args: []string{"mod", "download"}, 22 testDir: "../../tests/integ/empty-dir", 23 simulateExternalRepo: true, 24 errShouldBe: "gno.mod not found", 25 }, 26 { 27 args: []string{"mod", "download"}, 28 testDir: "../../tests/integ/empty-gnomod", 29 simulateExternalRepo: true, 30 errShouldBe: "validate: requires module", 31 }, 32 { 33 args: []string{"mod", "download"}, 34 testDir: "../../tests/integ/invalid-module-name", 35 simulateExternalRepo: true, 36 errShouldContain: "usage: module module/path", 37 }, 38 { 39 args: []string{"mod", "download"}, 40 testDir: "../../tests/integ/minimalist-gnomod", 41 simulateExternalRepo: true, 42 }, 43 { 44 args: []string{"mod", "download"}, 45 testDir: "../../tests/integ/require-remote-module", 46 simulateExternalRepo: true, 47 }, 48 { 49 args: []string{"mod", "download"}, 50 testDir: "../../tests/integ/require-invalid-module", 51 simulateExternalRepo: true, 52 errShouldContain: "fetch: writepackage: querychain", 53 }, 54 { 55 args: []string{"mod", "download"}, 56 testDir: "../../tests/integ/invalid-module-version1", 57 simulateExternalRepo: true, 58 errShouldContain: "usage: require module/path v1.2.3", 59 }, 60 { 61 args: []string{"mod", "download"}, 62 testDir: "../../tests/integ/invalid-module-version2", 63 simulateExternalRepo: true, 64 errShouldContain: "invalid: must be of the form v1.2.3", 65 }, 66 { 67 args: []string{"mod", "download"}, 68 testDir: "../../tests/integ/replace-with-dir", 69 simulateExternalRepo: true, 70 }, 71 { 72 args: []string{"mod", "download"}, 73 testDir: "../../tests/integ/replace-with-module", 74 simulateExternalRepo: true, 75 }, 76 { 77 args: []string{"mod", "download"}, 78 testDir: "../../tests/integ/replace-with-invalid-module", 79 simulateExternalRepo: true, 80 errShouldContain: "fetch: writepackage: querychain", 81 }, 82 83 // test `gno mod init` with no module name 84 { 85 args: []string{"mod", "init"}, 86 testDir: "../../tests/integ/valid1", 87 simulateExternalRepo: true, 88 }, 89 { 90 args: []string{"mod", "init"}, 91 testDir: "../../tests/integ/empty-dir", 92 simulateExternalRepo: true, 93 errShouldBe: "create gno.mod file: cannot determine package name", 94 }, 95 { 96 args: []string{"mod", "init"}, 97 testDir: "../../tests/integ/empty-gno1", 98 simulateExternalRepo: true, 99 recoverShouldContain: "expected 'package', found 'EOF'", 100 }, 101 { 102 args: []string{"mod", "init"}, 103 testDir: "../../tests/integ/empty-gno2", 104 simulateExternalRepo: true, 105 recoverShouldContain: "expected 'package', found 'EOF'", 106 }, 107 { 108 args: []string{"mod", "init"}, 109 testDir: "../../tests/integ/empty-gno3", 110 simulateExternalRepo: true, 111 recoverShouldContain: "expected 'package', found 'EOF'", 112 }, 113 { 114 args: []string{"mod", "init"}, 115 testDir: "../../tests/integ/empty-gnomod", 116 simulateExternalRepo: true, 117 errShouldBe: "create gno.mod file: gno.mod file already exists", 118 }, 119 120 // test `gno mod init` with module name 121 { 122 args: []string{"mod", "init", "gno.land/p/demo/foo"}, 123 testDir: "../../tests/integ/empty-dir", 124 simulateExternalRepo: true, 125 }, 126 { 127 args: []string{"mod", "init", "gno.land/p/demo/foo"}, 128 testDir: "../../tests/integ/empty-gno1", 129 simulateExternalRepo: true, 130 }, 131 { 132 args: []string{"mod", "init", "gno.land/p/demo/foo"}, 133 testDir: "../../tests/integ/empty-gno2", 134 simulateExternalRepo: true, 135 }, 136 { 137 args: []string{"mod", "init", "gno.land/p/demo/foo"}, 138 testDir: "../../tests/integ/empty-gno3", 139 simulateExternalRepo: true, 140 }, 141 { 142 args: []string{"mod", "init", "gno.land/p/demo/foo"}, 143 testDir: "../../tests/integ/empty-gnomod", 144 simulateExternalRepo: true, 145 errShouldBe: "create gno.mod file: gno.mod file already exists", 146 }, 147 148 // test `gno mod tidy` 149 { 150 args: []string{"mod", "tidy", "arg1"}, 151 testDir: "../../tests/integ/minimalist-gnomod", 152 simulateExternalRepo: true, 153 errShouldContain: "flag: help requested", 154 }, 155 { 156 args: []string{"mod", "tidy"}, 157 testDir: "../../tests/integ/empty-dir", 158 simulateExternalRepo: true, 159 errShouldContain: "could not read gno.mod file", 160 }, 161 { 162 args: []string{"mod", "tidy"}, 163 testDir: "../../tests/integ/invalid-module-version1", 164 simulateExternalRepo: true, 165 errShouldContain: "error parsing gno.mod file at", 166 }, 167 { 168 args: []string{"mod", "tidy"}, 169 testDir: "../../tests/integ/minimalist-gnomod", 170 simulateExternalRepo: true, 171 }, 172 { 173 args: []string{"mod", "tidy"}, 174 testDir: "../../tests/integ/require-remote-module", 175 simulateExternalRepo: true, 176 }, 177 { 178 args: []string{"mod", "tidy"}, 179 testDir: "../../tests/integ/valid2", 180 simulateExternalRepo: true, 181 }, 182 { 183 args: []string{"mod", "tidy"}, 184 testDir: "../../tests/integ/invalid-gno-file", 185 simulateExternalRepo: true, 186 errShouldContain: "expected 'package', found packag", 187 }, 188 189 // test `gno mod why` 190 { 191 args: []string{"mod", "why"}, 192 testDir: "../../tests/integ/minimalist-gnomod", 193 simulateExternalRepo: true, 194 errShouldContain: "flag: help requested", 195 }, 196 { 197 args: []string{"mod", "why", "std"}, 198 testDir: "../../tests/integ/empty-dir", 199 simulateExternalRepo: true, 200 errShouldContain: "could not read gno.mod file", 201 }, 202 { 203 args: []string{"mod", "why", "std"}, 204 testDir: "../../tests/integ/invalid-module-version1", 205 simulateExternalRepo: true, 206 errShouldContain: "error parsing gno.mod file at", 207 }, 208 { 209 args: []string{"mod", "why", "std"}, 210 testDir: "../../tests/integ/invalid-gno-file", 211 simulateExternalRepo: true, 212 errShouldContain: "expected 'package', found packag", 213 }, 214 { 215 args: []string{"mod", "why", "std"}, 216 testDir: "../../tests/integ/minimalist-gnomod", 217 simulateExternalRepo: true, 218 stdoutShouldBe: `# std 219 (module minim does not need package std) 220 `, 221 }, 222 { 223 args: []string{"mod", "why", "std"}, 224 testDir: "../../tests/integ/require-remote-module", 225 simulateExternalRepo: true, 226 stdoutShouldBe: `# std 227 (module gno.land/tests/importavl does not need package std) 228 `, 229 }, 230 { 231 args: []string{"mod", "why", "std", "gno.land/p/demo/avl"}, 232 testDir: "../../tests/integ/valid2", 233 simulateExternalRepo: true, 234 stdoutShouldBe: `# std 235 (module gno.land/p/integ/valid does not need package std) 236 237 # gno.land/p/demo/avl 238 valid.gno 239 `, 240 }, 241 } 242 testMainCaseRun(t, tc) 243 } 244 245 func TestGetGnoImports(t *testing.T) { 246 workingDir, err := os.Getwd() 247 require.NoError(t, err) 248 249 // create external dir 250 tmpDir, cleanUpFn := createTmpDir(t) 251 defer cleanUpFn() 252 253 // cd to tmp directory 254 os.Chdir(tmpDir) 255 defer os.Chdir(workingDir) 256 257 files := []struct { 258 name, data string 259 }{ 260 { 261 name: "file1.gno", 262 data: ` 263 package tmp 264 265 import ( 266 "std" 267 268 "gno.land/p/demo/pkg1" 269 ) 270 `, 271 }, 272 { 273 name: "file2.gno", 274 data: ` 275 package tmp 276 277 import ( 278 "gno.land/p/demo/pkg1" 279 "gno.land/p/demo/pkg2" 280 ) 281 `, 282 }, 283 { 284 name: "file1_test.gno", 285 data: ` 286 package tmp 287 288 import ( 289 "testing" 290 291 "gno.land/p/demo/testpkg" 292 ) 293 `, 294 }, 295 { 296 name: "z_0_filetest.gno", 297 data: ` 298 package main 299 300 import ( 301 "gno.land/p/demo/filetestpkg" 302 ) 303 `, 304 }, 305 306 // subpkg files 307 { 308 name: filepath.Join("subtmp", "file1.gno"), 309 data: ` 310 package subtmp 311 312 import ( 313 "std" 314 315 "gno.land/p/demo/subpkg1" 316 ) 317 `, 318 }, 319 { 320 name: filepath.Join("subtmp", "file2.gno"), 321 data: ` 322 package subtmp 323 324 import ( 325 "gno.land/p/demo/subpkg1" 326 "gno.land/p/demo/subpkg2" 327 ) 328 `, 329 }, 330 } 331 332 // Expected list of imports 333 // - ignore subdirs 334 // - ignore duplicate 335 // - ignore *_filetest.gno 336 // - should be sorted 337 expected := []string{ 338 "gno.land/p/demo/pkg1", 339 "gno.land/p/demo/pkg2", 340 "gno.land/p/demo/testpkg", 341 } 342 343 // Create subpkg dir 344 err = os.Mkdir("subtmp", 0o700) 345 require.NoError(t, err) 346 347 // Create files 348 for _, f := range files { 349 err = os.WriteFile(f.name, []byte(f.data), 0o644) 350 require.NoError(t, err) 351 } 352 353 imports, err := getGnoPackageImports(tmpDir) 354 require.NoError(t, err) 355 356 require.Equal(t, len(expected), len(imports)) 357 for i := range imports { 358 assert.Equal(t, expected[i], imports[i]) 359 } 360 }