github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/gnovm/tests/package_test.go (about)

     1  package tests
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io/fs"
     7  	"log"
     8  	"os"
     9  	"path/filepath"
    10  	"sort"
    11  	"strings"
    12  	"testing"
    13  
    14  	"github.com/stretchr/testify/require"
    15  
    16  	gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
    17  )
    18  
    19  func TestPackages(t *testing.T) {
    20  	// find all packages with *_test.gno files.
    21  	rootDirs := []string{
    22  		filepath.Join("..", "stdlibs"),
    23  		filepath.Join("..", "..", "examples"),
    24  	}
    25  	testDirs := map[string]string{} // aggregate here, pkgPath -> dir
    26  	pkgPaths := []string{}
    27  	for _, rootDir := range rootDirs {
    28  		fileSystem := os.DirFS(rootDir)
    29  		fs.WalkDir(fileSystem, ".", func(path string, d fs.DirEntry, err error) error {
    30  			if err != nil {
    31  				log.Fatal(err)
    32  			}
    33  			if d.IsDir() {
    34  				return nil
    35  			}
    36  			if strings.HasSuffix(path, "_test.gno") {
    37  				dirPath := filepath.Dir(path)
    38  				if _, exists := testDirs[dirPath]; exists {
    39  					// already exists.
    40  				} else {
    41  					testDirs[dirPath] = filepath.Join(rootDir, dirPath)
    42  					pkgPaths = append(pkgPaths, dirPath)
    43  				}
    44  			}
    45  			return nil
    46  		})
    47  	}
    48  	// Sort pkgPaths for determinism.
    49  	sort.Strings(pkgPaths)
    50  	// For each package with testfiles (in testDirs), call Machine.TestMemPackage.
    51  	for _, pkgPath := range pkgPaths {
    52  		testDir := testDirs[pkgPath]
    53  		t.Run(pkgPath, func(t *testing.T) {
    54  			t.Skip("almost any new package is failing. Ignoring this test for now until we find a solution for this.")
    55  
    56  			if pkgPath == "gno.land/p/demo/avl" {
    57  				t.Skip("package failing")
    58  			}
    59  
    60  			if pkgPath == "gno.land/p/demo/flow" {
    61  				t.Skip("package failing")
    62  			}
    63  
    64  			if pkgPath == "gno.land/p/demo/grc/exts/vault" {
    65  				t.Skip("package failing")
    66  			}
    67  
    68  			if pkgPath == "gno.land/p/demo/grc/grc1155" {
    69  				t.Skip("package failing")
    70  			}
    71  
    72  			if pkgPath == "gno.land/p/demo/grc/grc20" {
    73  				t.Skip("package failing")
    74  			}
    75  
    76  			if pkgPath == "gno.land/p/demo/grc/grc721" {
    77  				t.Skip("package failing")
    78  			}
    79  
    80  			if pkgPath == "gno.land/p/demo/memeland" {
    81  				t.Skip("package failing")
    82  			}
    83  
    84  			if pkgPath == "gno.land/p/demo/ownable" {
    85  				t.Skip("package failing")
    86  			}
    87  
    88  			if pkgPath == "gno.land/p/demo/pausable" {
    89  				t.Skip("package failing")
    90  			}
    91  
    92  			if pkgPath == "gno.land/p/demo/rand" {
    93  				t.Skip("package failing")
    94  			}
    95  
    96  			if pkgPath == "gno.land/p/demo/tests" {
    97  				t.Skip("package failing")
    98  			}
    99  
   100  			if pkgPath == "gno.land/p/demo/todolist" {
   101  				t.Skip("package failing")
   102  			}
   103  
   104  			if pkgPath == "gno.land/r/demo/art/gnoface" {
   105  				t.Skip("package failing")
   106  			}
   107  
   108  			if pkgPath == "gno.land/r/demo/foo1155" {
   109  				t.Skip("package failing")
   110  			}
   111  
   112  			if pkgPath == "gno.land/r/demo/foo20" {
   113  				t.Skip("package failing")
   114  			}
   115  
   116  			if pkgPath == "gno.land/r/demo/keystore" {
   117  				t.Skip("package failing")
   118  			}
   119  
   120  			if pkgPath == "gno.land/r/demo/microblog" {
   121  				t.Skip("package failing")
   122  			}
   123  
   124  			if pkgPath == "gno.land/r/demo/tests" {
   125  				t.Skip("package failing")
   126  			}
   127  
   128  			if pkgPath == "gno.land/r/demo/todolist" {
   129  				t.Skip("package failing")
   130  			}
   131  
   132  			if pkgPath == "gno.land/r/demo/userbook" {
   133  				t.Skip("package failing")
   134  			}
   135  
   136  			if pkgPath == "gno.land/r/gnoland/blog" {
   137  				t.Skip("package failing")
   138  			}
   139  
   140  			if pkgPath == "gno.land/r/gnoland/faucet" {
   141  				t.Skip("package failing")
   142  			}
   143  
   144  			if pkgPath == "gno.land/r/x/manfred_outfmt" {
   145  				t.Skip("package failing")
   146  			}
   147  
   148  			if pkgPath == "gno.land/r/x/nir1218_evaluation_proposal" {
   149  				t.Skip("package failing")
   150  			}
   151  
   152  			if pkgPath == "gno.land/r/gnoland/ghverify" {
   153  				t.Skip("package failing")
   154  			}
   155  
   156  			runPackageTest(t, testDir, pkgPath)
   157  		})
   158  	}
   159  }
   160  
   161  func runPackageTest(t *testing.T, dir string, path string) {
   162  	t.Helper()
   163  
   164  	memPkg := gno.ReadMemPackage(dir, path)
   165  	require.False(t, memPkg.IsEmpty())
   166  
   167  	stdin := new(bytes.Buffer)
   168  	// stdout := new(bytes.Buffer)
   169  	stdout := os.Stdout
   170  	stderr := new(bytes.Buffer)
   171  	rootDir := filepath.Join("..", "..")
   172  	store := TestStore(rootDir, path, stdin, stdout, stderr, ImportModeStdlibsOnly)
   173  	store.SetLogStoreOps(true)
   174  	m := gno.NewMachineWithOptions(gno.MachineOptions{
   175  		PkgPath: "test",
   176  		Output:  stdout,
   177  		Store:   store,
   178  		Context: nil,
   179  	})
   180  	m.TestMemPackage(t, memPkg)
   181  
   182  	// Check that machine is empty.
   183  	err := m.CheckEmpty()
   184  	if err != nil {
   185  		t.Log("last state: \n", m.String())
   186  		panic(fmt.Sprintf("machine not empty after main: %v", err))
   187  	}
   188  }