github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/gnovm/pkg/gnomod/file_test.go (about) 1 package gnomod 2 3 import ( 4 "bytes" 5 "log" 6 "os" 7 "path/filepath" 8 "testing" 9 10 "github.com/gnolang/gno/tm2/pkg/testutils" 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 "golang.org/x/mod/modfile" 14 "golang.org/x/mod/module" 15 ) 16 17 const testRemote string = "test3.gno.land:36657" 18 19 func TestFetchDeps(t *testing.T) { 20 for _, tc := range []struct { 21 desc string 22 modFile File 23 errorShouldContain string 24 requirements []string 25 stdOutContains []string 26 cachedStdOutContains []string 27 }{ 28 { 29 desc: "not_exists", 30 modFile: File{ 31 Module: &modfile.Module{ 32 Mod: module.Version{ 33 Path: "testFetchDeps", 34 }, 35 }, 36 Require: []*modfile.Require{ 37 { 38 Mod: module.Version{ 39 Path: "gno.land/p/demo/does_not_exists", 40 Version: "v0.0.0", 41 }, 42 }, 43 }, 44 }, 45 errorShouldContain: "querychain (gno.land/p/demo/does_not_exists)", 46 }, { 47 desc: "fetch_gno.land/p/demo/avl", 48 modFile: File{ 49 Module: &modfile.Module{ 50 Mod: module.Version{ 51 Path: "testFetchDeps", 52 }, 53 }, 54 Require: []*modfile.Require{ 55 { 56 Mod: module.Version{ 57 Path: "gno.land/p/demo/avl", 58 Version: "v0.0.0", 59 }, 60 }, 61 }, 62 }, 63 requirements: []string{"avl"}, 64 stdOutContains: []string{ 65 "fetching gno.land/p/demo/avl", 66 }, 67 cachedStdOutContains: []string{ 68 "cached gno.land/p/demo/avl", 69 }, 70 }, { 71 desc: "fetch_gno.land/p/demo/blog", 72 modFile: File{ 73 Module: &modfile.Module{ 74 Mod: module.Version{ 75 Path: "testFetchDeps", 76 }, 77 }, 78 Require: []*modfile.Require{ 79 { 80 Mod: module.Version{ 81 Path: "gno.land/p/demo/blog", 82 Version: "v0.0.0", 83 }, 84 }, 85 }, 86 }, 87 requirements: []string{"avl", "blog", "ufmt"}, 88 stdOutContains: []string{ 89 "fetching gno.land/p/demo/blog", 90 "fetching gno.land/p/demo/avl // indirect", 91 "fetching gno.land/p/demo/ufmt // indirect", 92 }, 93 cachedStdOutContains: []string{ 94 "cached gno.land/p/demo/blog", 95 }, 96 }, 97 } { 98 t.Run(tc.desc, func(t *testing.T) { 99 var buf bytes.Buffer 100 log.SetOutput(&buf) 101 defer func() { 102 log.SetOutput(os.Stderr) 103 }() 104 105 // Create test dir 106 dirPath, cleanUpFn := testutils.NewTestCaseDir(t) 107 assert.NotNil(t, dirPath) 108 defer cleanUpFn() 109 110 // Fetching dependencies 111 err := tc.modFile.FetchDeps(dirPath, testRemote, true) 112 if tc.errorShouldContain != "" { 113 require.ErrorContains(t, err, tc.errorShouldContain) 114 } else { 115 require.Nil(t, err) 116 117 // Read dir 118 entries, err := os.ReadDir(filepath.Join(dirPath, "gno.land", "p", "demo")) 119 require.Nil(t, err) 120 121 // Check dir entries 122 assert.Equal(t, len(tc.requirements), len(entries)) 123 for _, e := range entries { 124 assert.Contains(t, tc.requirements, e.Name()) 125 } 126 127 // Check logs 128 for _, c := range tc.stdOutContains { 129 assert.Contains(t, buf.String(), c) 130 } 131 132 buf.Reset() 133 134 // Try fetching again. Should be cached 135 tc.modFile.FetchDeps(dirPath, testRemote, true) 136 for _, c := range tc.cachedStdOutContains { 137 assert.Contains(t, buf.String(), c) 138 } 139 } 140 }) 141 } 142 }