From 17e1598d4e797b53c78248dcf6f2968a83315106 Mon Sep 17 00:00:00 2001 From: Lee ByeongJun Date: Sun, 22 Oct 2023 04:47:03 +0900 Subject: [PATCH 01/14] notJoon:remove-deprecated-io/ioutil --- gnovm/pkg/gnolang/nodes.go | 3 +-- gnovm/pkg/gnolang/precompile.go | 3 +-- gnovm/pkg/gnomod/gnomod.go | 5 ++--- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/gnovm/pkg/gnolang/nodes.go b/gnovm/pkg/gnolang/nodes.go index 46308fb3a02..2a9e0b51a97 100644 --- a/gnovm/pkg/gnolang/nodes.go +++ b/gnovm/pkg/gnolang/nodes.go @@ -4,7 +4,6 @@ import ( "fmt" "go/parser" "go/token" - "io/ioutil" "os" "path/filepath" "reflect" @@ -1095,7 +1094,7 @@ func PackageNameFromFileBody(name, body string) Name { // NOTE: panics if package name is invalid. func ReadMemPackage(dir string, pkgPath string) *std.MemPackage { - files, err := ioutil.ReadDir(dir) + files, err := os.ReadDir(dir) if err != nil { panic(err) } diff --git a/gnovm/pkg/gnolang/precompile.go b/gnovm/pkg/gnolang/precompile.go index f7a10d5589a..c3116f25800 100644 --- a/gnovm/pkg/gnolang/precompile.go +++ b/gnovm/pkg/gnolang/precompile.go @@ -7,7 +7,6 @@ import ( "go/format" "go/parser" "go/token" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -112,7 +111,7 @@ func GetPrecompileFilenameAndTags(gnoFilePath string) (targetFilename, tags stri func PrecompileAndCheckMempkg(mempkg *std.MemPackage) error { gofmt := "gofmt" - tmpDir, err := ioutil.TempDir("", mempkg.Name) + tmpDir, err := os.MkdirTemp("", mempkg.Name) if err != nil { return err } diff --git a/gnovm/pkg/gnomod/gnomod.go b/gnovm/pkg/gnomod/gnomod.go index 7bb51d6558a..3c224bafb87 100644 --- a/gnovm/pkg/gnomod/gnomod.go +++ b/gnovm/pkg/gnomod/gnomod.go @@ -3,7 +3,6 @@ package gnomod import ( "errors" "fmt" - "io/ioutil" "os" "path/filepath" "strings" @@ -172,9 +171,9 @@ func CreateGnoModFile(rootDir, modPath string) error { if modPath == "" { // Check .gno files for package name // and use it as modPath - files, err := ioutil.ReadDir(rootDir) + files, err := os.ReadDir(rootDir) if err != nil { - fmt.Errorf("read dir %q: %w", rootDir, err) + return fmt.Errorf("read dir %q: %w", rootDir, err) } var pkgName gnolang.Name From 92646bb1d3f1ac93dabc42224500fd2027396307 Mon Sep 17 00:00:00 2001 From: Lee ByeongJun Date: Tue, 24 Oct 2023 15:54:24 +0900 Subject: [PATCH 02/14] replace ioutil package in tm2 --- tm2/pkg/amino/genproto/genproto.go | 5 +--- tm2/pkg/autofile/autofile_test.go | 7 +++--- tm2/pkg/bft/config/toml.go | 3 +-- tm2/pkg/bft/consensus/common_test.go | 7 +++--- tm2/pkg/bft/consensus/replay_test.go | 3 +-- tm2/pkg/bft/privval/file_test.go | 25 +++++++++---------- tm2/pkg/bft/privval/socket_listeners_test.go | 3 +-- tm2/pkg/bft/rpc/client/main_test.go | 3 +-- tm2/pkg/bft/rpc/lib/client/http_client.go | 8 +++--- tm2/pkg/bft/rpc/lib/server/handlers.go | 4 +-- tm2/pkg/bft/rpc/lib/server/handlers_test.go | 10 ++++---- .../bft/rpc/lib/server/http_server_test.go | 5 ++-- tm2/pkg/bft/types/genesis_test.go | 3 +-- tm2/pkg/bft/types/part_set_test.go | 4 +-- tm2/pkg/db/fsdb.go | 4 +-- tm2/pkg/iavl/tree_dotgraph_test.go | 4 +-- tm2/pkg/log/tm_logger_test.go | 6 ++--- tm2/pkg/os/tempfile_test.go | 3 +-- tm2/pkg/p2p/upnp/upnp.go | 6 ++--- 19 files changed, 50 insertions(+), 63 deletions(-) diff --git a/tm2/pkg/amino/genproto/genproto.go b/tm2/pkg/amino/genproto/genproto.go index 8af73b2690a..4f7154e058c 100644 --- a/tm2/pkg/amino/genproto/genproto.go +++ b/tm2/pkg/amino/genproto/genproto.go @@ -6,7 +6,6 @@ import ( "bytes" "errors" "fmt" - "io/ioutil" "os" "os/exec" "path" @@ -120,7 +119,6 @@ func (p3c *P3Context) GetP3ImportPath(p3type P3Type, implicit bool) string { func (p3c *P3Context) GenerateProto3MessagePartial(p3doc *P3Doc, rt reflect.Type) (p3msg P3Message) { if p3doc.PackageName == "" { panic(fmt.Sprintf("cannot generate message partials in the root package \"\".")) - return } if rt.Kind() == reflect.Ptr { panic("pointers not yet supported. if you meant pointer-preferred (for decoding), pass in rt.Elem()") @@ -220,7 +218,6 @@ func (p3c *P3Context) GenerateProto3MessagePartial(p3doc *P3Doc, rt reflect.Type func (p3c *P3Context) GenerateProto3ListPartial(p3doc *P3Doc, nl NList) (p3msg P3Message) { if p3doc.PackageName == "" { panic(fmt.Sprintf("cannot generate message partials in the root package \"\".")) - return } ep3 := nl.ElemP3Type() @@ -496,7 +493,7 @@ func RunProtoc(pkg *amino.Package, protosDir string) { } } // First generate output to a temp dir. - tempDir, err := ioutil.TempDir("", "amino-genproto") + tempDir, err := os.MkdirTemp("", "amino-genproto") if err != nil { return } diff --git a/tm2/pkg/autofile/autofile_test.go b/tm2/pkg/autofile/autofile_test.go index d50bdca3ce0..d631e0ed265 100644 --- a/tm2/pkg/autofile/autofile_test.go +++ b/tm2/pkg/autofile/autofile_test.go @@ -1,7 +1,6 @@ package autofile import ( - "io/ioutil" "os" "syscall" "testing" @@ -14,7 +13,7 @@ import ( func TestSIGHUP(t *testing.T) { // First, create an AutoFile writing to a tempfile dir - file, err := ioutil.TempFile("", "sighup_test") + file, err := os.CreateTemp("", "sighup_test") require.NoError(t, err) err = file.Close() require.NoError(t, err) @@ -60,7 +59,7 @@ func TestSIGHUP(t *testing.T) { // // Manually modify file permissions, close, and reopen using autofile: // // We expect the file permissions to be changed back to the intended perms. // func TestOpenAutoFilePerms(t *testing.T) { -// file, err := ioutil.TempFile("", "permission_test") +// file, err := os.CreateTemp("", "permission_test") // require.NoError(t, err) // err = file.Close() // require.NoError(t, err) @@ -86,7 +85,7 @@ func TestSIGHUP(t *testing.T) { func TestAutoFileSize(t *testing.T) { // First, create an AutoFile writing to a tempfile dir - f, err := ioutil.TempFile("", "sighup_test") + f, err := os.CreateTemp("", "sighup_test") require.NoError(t, err) err = f.Close() require.NoError(t, err) diff --git a/tm2/pkg/bft/config/toml.go b/tm2/pkg/bft/config/toml.go index fdaa1295342..1599bc78968 100644 --- a/tm2/pkg/bft/config/toml.go +++ b/tm2/pkg/bft/config/toml.go @@ -3,7 +3,6 @@ package config import ( "bytes" "fmt" - "io/ioutil" "os" "path/filepath" "text/template" @@ -308,7 +307,7 @@ func ResetTestRoot(testName string) *Config { func ResetTestRootWithChainID(testName string, chainID string) *Config { // create a unique, concurrency-safe test directory under os.TempDir() - rootDir, err := ioutil.TempDir("", fmt.Sprintf("%s-%s_", chainID, testName)) + rootDir, err := os.MkdirTemp("", fmt.Sprintf("%s-%s_", chainID, testName)) if err != nil { panic(err) } diff --git a/tm2/pkg/bft/consensus/common_test.go b/tm2/pkg/bft/consensus/common_test.go index 7424305c00a..19c96756624 100644 --- a/tm2/pkg/bft/consensus/common_test.go +++ b/tm2/pkg/bft/consensus/common_test.go @@ -3,7 +3,6 @@ package consensus import ( "bytes" "fmt" - "io/ioutil" "os" "path" "path/filepath" @@ -662,11 +661,11 @@ func randConsensusNetWithPeers(nValidators, nPeers int, testName string, tickerF if i < nValidators { privVal = privVals[i] } else { - tempKeyFile, err := ioutil.TempFile("", "priv_validator_key_") + tempKeyFile, err := os.CreateTemp("", "priv_validator_key_") if err != nil { panic(err) } - tempStateFile, err := ioutil.TempFile("", "priv_validator_state_") + tempStateFile, err := os.CreateTemp("", "priv_validator_state_") if err != nil { panic(err) } @@ -795,7 +794,7 @@ func newCounter() abci.Application { } func newPersistentKVStore() abci.Application { - dir, err := ioutil.TempDir("", "persistent-kvstore") + dir, err := os.MkdirTemp("", "persistent-kvstore") if err != nil { panic(err) } diff --git a/tm2/pkg/bft/consensus/replay_test.go b/tm2/pkg/bft/consensus/replay_test.go index 3174207ef8d..556689ec3f4 100644 --- a/tm2/pkg/bft/consensus/replay_test.go +++ b/tm2/pkg/bft/consensus/replay_test.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "os" "path/filepath" "runtime" @@ -604,7 +603,7 @@ func TestMockProxyApp(t *testing.T) { } func tempWALWithData(data []byte) string { - walFile, err := ioutil.TempFile("", "wal") + walFile, err := os.CreateTemp("", "wal") if err != nil { panic(fmt.Sprintf("failed to create temp WAL file: %v", err)) } diff --git a/tm2/pkg/bft/privval/file_test.go b/tm2/pkg/bft/privval/file_test.go index 306db4177e5..36979e19ea3 100644 --- a/tm2/pkg/bft/privval/file_test.go +++ b/tm2/pkg/bft/privval/file_test.go @@ -3,7 +3,6 @@ package privval import ( "encoding/base64" "fmt" - "io/ioutil" "os" "testing" "time" @@ -20,9 +19,9 @@ import ( func TestGenLoadValidator(t *testing.T) { assert := assert.New(t) - tempKeyFile, err := ioutil.TempFile("", "priv_validator_key_") + tempKeyFile, err := os.CreateTemp("", "priv_validator_key_") require.Nil(t, err) - tempStateFile, err := ioutil.TempFile("", "priv_validator_state_") + tempStateFile, err := os.CreateTemp("", "priv_validator_state_") require.Nil(t, err) privVal := GenFilePV(tempKeyFile.Name(), tempStateFile.Name()) @@ -38,9 +37,9 @@ func TestGenLoadValidator(t *testing.T) { } func TestResetValidator(t *testing.T) { - tempKeyFile, err := ioutil.TempFile("", "priv_validator_key_") + tempKeyFile, err := os.CreateTemp("", "priv_validator_key_") require.Nil(t, err) - tempStateFile, err := ioutil.TempFile("", "priv_validator_state_") + tempStateFile, err := os.CreateTemp("", "priv_validator_state_") require.Nil(t, err) privVal := GenFilePV(tempKeyFile.Name(), tempStateFile.Name()) @@ -68,9 +67,9 @@ func TestResetValidator(t *testing.T) { func TestLoadOrGenValidator(t *testing.T) { assert := assert.New(t) - tempKeyFile, err := ioutil.TempFile("", "priv_validator_key_") + tempKeyFile, err := os.CreateTemp("", "priv_validator_key_") require.Nil(t, err) - tempStateFile, err := ioutil.TempFile("", "priv_validator_state_") + tempStateFile, err := os.CreateTemp("", "priv_validator_state_") require.Nil(t, err) tempKeyFilePath := tempKeyFile.Name() @@ -160,9 +159,9 @@ func TestUnmarshalValidatorKey(t *testing.T) { func TestSignVote(t *testing.T) { assert := assert.New(t) - tempKeyFile, err := ioutil.TempFile("", "priv_validator_key_") + tempKeyFile, err := os.CreateTemp("", "priv_validator_key_") require.Nil(t, err) - tempStateFile, err := ioutil.TempFile("", "priv_validator_state_") + tempStateFile, err := os.CreateTemp("", "priv_validator_state_") require.Nil(t, err) privVal := GenFilePV(tempKeyFile.Name(), tempStateFile.Name()) @@ -206,9 +205,9 @@ func TestSignVote(t *testing.T) { func TestSignProposal(t *testing.T) { assert := assert.New(t) - tempKeyFile, err := ioutil.TempFile("", "priv_validator_key_") + tempKeyFile, err := os.CreateTemp("", "priv_validator_key_") require.Nil(t, err) - tempStateFile, err := ioutil.TempFile("", "priv_validator_state_") + tempStateFile, err := os.CreateTemp("", "priv_validator_state_") require.Nil(t, err) privVal := GenFilePV(tempKeyFile.Name(), tempStateFile.Name()) @@ -248,9 +247,9 @@ func TestSignProposal(t *testing.T) { } func TestDifferByTimestamp(t *testing.T) { - tempKeyFile, err := ioutil.TempFile("", "priv_validator_key_") + tempKeyFile, err := os.CreateTemp("", "priv_validator_key_") require.Nil(t, err) - tempStateFile, err := ioutil.TempFile("", "priv_validator_state_") + tempStateFile, err := os.CreateTemp("", "priv_validator_state_") require.Nil(t, err) privVal := GenFilePV(tempKeyFile.Name(), tempStateFile.Name()) diff --git a/tm2/pkg/bft/privval/socket_listeners_test.go b/tm2/pkg/bft/privval/socket_listeners_test.go index a3630903197..6ca7863d1fd 100644 --- a/tm2/pkg/bft/privval/socket_listeners_test.go +++ b/tm2/pkg/bft/privval/socket_listeners_test.go @@ -1,7 +1,6 @@ package privval import ( - "io/ioutil" "net" "os" "testing" @@ -29,7 +28,7 @@ type listenerTestCase struct { // testUnixAddr will attempt to obtain a platform-independent temporary file // name for a Unix socket func testUnixAddr() (string, error) { - f, err := ioutil.TempFile("", "tendermint-privval-test-*") + f, err := os.CreateTemp("", "tendermint-privval-test-*") if err != nil { return "", err } diff --git a/tm2/pkg/bft/rpc/client/main_test.go b/tm2/pkg/bft/rpc/client/main_test.go index 58dd9538412..759104a3029 100644 --- a/tm2/pkg/bft/rpc/client/main_test.go +++ b/tm2/pkg/bft/rpc/client/main_test.go @@ -1,7 +1,6 @@ package client_test import ( - "io/ioutil" "os" "testing" @@ -14,7 +13,7 @@ var node *nm.Node func TestMain(m *testing.M) { // start a tendermint node (and kvstore) in the background to test against - dir, err := ioutil.TempDir("/tmp", "rpc-client-test") + dir, err := os.MkdirTemp("/tmp", "rpc-client-test") if err != nil { panic(err) } diff --git a/tm2/pkg/bft/rpc/lib/client/http_client.go b/tm2/pkg/bft/rpc/lib/client/http_client.go index 5a9da9ec052..c02d029f27a 100644 --- a/tm2/pkg/bft/rpc/lib/client/http_client.go +++ b/tm2/pkg/bft/rpc/lib/client/http_client.go @@ -4,7 +4,7 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" + "io" "net" "net/http" "net/url" @@ -201,7 +201,7 @@ func (c *JSONRPCClient) Call(method string, params map[string]interface{}, resul return nil, errors.New("server at '%s' returned %s", c.address, httpResponse.Status) } - responseBytes, err := ioutil.ReadAll(httpResponse.Body) + responseBytes, err := io.ReadAll(httpResponse.Body) if err != nil { return nil, err } @@ -238,7 +238,7 @@ func (c *JSONRPCClient) sendBatch(requests []*jsonRPCBufferedRequest) ([]interfa return nil, errors.New("server at '%s' returned %s", c.address, httpResponse.Status) } - responseBytes, err := ioutil.ReadAll(httpResponse.Body) + responseBytes, err := io.ReadAll(httpResponse.Body) if err != nil { return nil, err } @@ -332,7 +332,7 @@ func (c *URIClient) Call(method string, params map[string]interface{}, result in return nil, errors.New("server at '%s' returned %s", c.address, resp.Status) } - responseBytes, err := ioutil.ReadAll(resp.Body) + responseBytes, err := io.ReadAll(resp.Body) if err != nil { return nil, err } diff --git a/tm2/pkg/bft/rpc/lib/server/handlers.go b/tm2/pkg/bft/rpc/lib/server/handlers.go index aa3fadd4d18..40543e5f465 100644 --- a/tm2/pkg/bft/rpc/lib/server/handlers.go +++ b/tm2/pkg/bft/rpc/lib/server/handlers.go @@ -6,7 +6,7 @@ import ( "encoding/hex" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "reflect" "runtime/debug" @@ -101,7 +101,7 @@ func funcReturnTypes(f interface{}) []reflect.Type { // jsonrpc calls grab the given method's function info and runs reflect.Call func makeJSONRPCHandler(funcMap map[string]*RPCFunc, logger log.Logger) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { WriteRPCResponseHTTP(w, types.RPCInvalidRequestError(types.JSONRPCStringID(""), errors.Wrap(err, "error reading request body"))) return diff --git a/tm2/pkg/bft/rpc/lib/server/handlers_test.go b/tm2/pkg/bft/rpc/lib/server/handlers_test.go index 8bf2cc0f7f1..b7a9445a82d 100644 --- a/tm2/pkg/bft/rpc/lib/server/handlers_test.go +++ b/tm2/pkg/bft/rpc/lib/server/handlers_test.go @@ -3,7 +3,7 @@ package rpcserver_test import ( "bytes" "encoding/json" - "io/ioutil" + "io" "net/http" "net/http/httptest" "strings" @@ -70,7 +70,7 @@ func TestRPCParams(t *testing.T) { res := rec.Result() // Always expecting back a JSONRPCResponse assert.True(t, statusOK(res.StatusCode), "#%d: should always return 2XX", i) - blob, err := ioutil.ReadAll(res.Body) + blob, err := io.ReadAll(res.Body) if err != nil { t.Errorf("#%d: err reading body: %v", i, err) continue @@ -118,7 +118,7 @@ func TestJSONRPCID(t *testing.T) { res := rec.Result() // Always expecting back a JSONRPCResponse assert.True(t, statusOK(res.StatusCode), "#%d: should always return 2XX", i) - blob, err := ioutil.ReadAll(res.Body) + blob, err := io.ReadAll(res.Body) if err != nil { t.Errorf("#%d: err reading body: %v", i, err) continue @@ -147,7 +147,7 @@ func TestRPCNotification(t *testing.T) { // Always expecting back a JSONRPCResponse require.True(t, statusOK(res.StatusCode), "should always return 2XX") - blob, err := ioutil.ReadAll(res.Body) + blob, err := io.ReadAll(res.Body) require.Nil(t, err, "reading from the body should not give back an error") require.Equal(t, len(blob), 0, "a notification SHOULD NOT be responded to by the server") } @@ -182,7 +182,7 @@ func TestRPCNotificationInBatch(t *testing.T) { res := rec.Result() // Always expecting back a JSONRPCResponse assert.True(t, statusOK(res.StatusCode), "#%d: should always return 2XX", i) - blob, err := ioutil.ReadAll(res.Body) + blob, err := io.ReadAll(res.Body) if err != nil { t.Errorf("#%d: err reading body: %v", i, err) continue diff --git a/tm2/pkg/bft/rpc/lib/server/http_server_test.go b/tm2/pkg/bft/rpc/lib/server/http_server_test.go index 6753a339981..aaf817e6d85 100644 --- a/tm2/pkg/bft/rpc/lib/server/http_server_test.go +++ b/tm2/pkg/bft/rpc/lib/server/http_server_test.go @@ -5,7 +5,6 @@ import ( "crypto/tls" "fmt" "io" - "io/ioutil" "net" "net/http" "net/http/httptest" @@ -58,7 +57,7 @@ func TestMaxOpenConnections(t *testing.T) { return } defer r.Body.Close() - io.Copy(ioutil.Discard, r.Body) + io.Copy(io.Discard, r.Body) }() } wg.Wait() @@ -91,7 +90,7 @@ func TestStartHTTPAndTLSServer(t *testing.T) { defer res.Body.Close() assert.Equal(t, http.StatusOK, res.StatusCode) - body, err := ioutil.ReadAll(res.Body) + body, err := io.ReadAll(res.Body) require.NoError(t, err) assert.Equal(t, []byte("some body"), body) } diff --git a/tm2/pkg/bft/types/genesis_test.go b/tm2/pkg/bft/types/genesis_test.go index c8886f9bf0a..5d4c7630857 100644 --- a/tm2/pkg/bft/types/genesis_test.go +++ b/tm2/pkg/bft/types/genesis_test.go @@ -1,7 +1,6 @@ package types import ( - "io/ioutil" "os" "testing" @@ -87,7 +86,7 @@ func TestGenesisGood(t *testing.T) { } func TestGenesisSaveAs(t *testing.T) { - tmpfile, err := ioutil.TempFile("", "genesis") + tmpfile, err := os.CreateTemp("", "genesis") require.NoError(t, err) defer os.Remove(tmpfile.Name()) diff --git a/tm2/pkg/bft/types/part_set_test.go b/tm2/pkg/bft/types/part_set_test.go index 2e05daed849..c4e30c89372 100644 --- a/tm2/pkg/bft/types/part_set_test.go +++ b/tm2/pkg/bft/types/part_set_test.go @@ -1,7 +1,7 @@ package types import ( - "io/ioutil" + "io" "testing" "github.com/stretchr/testify/assert" @@ -54,7 +54,7 @@ func TestBasicPartSet(t *testing.T) { // Reconstruct data, assert that they are equal. data2Reader := partSet2.GetReader() - data2, err := ioutil.ReadAll(data2Reader) + data2, err := io.ReadAll(data2Reader) require.NoError(t, err) assert.Equal(t, data, data2) diff --git a/tm2/pkg/db/fsdb.go b/tm2/pkg/db/fsdb.go index aa8cea83889..e11cd6d4ce5 100644 --- a/tm2/pkg/db/fsdb.go +++ b/tm2/pkg/db/fsdb.go @@ -2,7 +2,7 @@ package db import ( "fmt" - "io/ioutil" + "io" "net/url" "os" "path/filepath" @@ -189,7 +189,7 @@ func read(path string) ([]byte, error) { } defer f.Close() - d, err := ioutil.ReadAll(f) + d, err := io.ReadAll(f) if err != nil { return nil, err } diff --git a/tm2/pkg/iavl/tree_dotgraph_test.go b/tm2/pkg/iavl/tree_dotgraph_test.go index 29be03ca241..f3c2786cda3 100644 --- a/tm2/pkg/iavl/tree_dotgraph_test.go +++ b/tm2/pkg/iavl/tree_dotgraph_test.go @@ -1,7 +1,7 @@ package iavl import ( - "io/ioutil" + "io" "testing" db "github.com/gnolang/gno/tm2/pkg/db" @@ -15,5 +15,5 @@ func TestWriteDOTGraph(t *testing.T) { key := []byte{ikey} tree.Set(key, key) } - WriteDOTGraph(ioutil.Discard, tree.ImmutableTree, []PathToLeaf{}) + WriteDOTGraph(io.Discard, tree.ImmutableTree, []PathToLeaf{}) } diff --git a/tm2/pkg/log/tm_logger_test.go b/tm2/pkg/log/tm_logger_test.go index a6d56bb8feb..81614c5ea21 100644 --- a/tm2/pkg/log/tm_logger_test.go +++ b/tm2/pkg/log/tm_logger_test.go @@ -2,7 +2,7 @@ package log_test import ( "bytes" - "io/ioutil" + "io" "strings" "testing" @@ -21,11 +21,11 @@ func TestLoggerLogsItsErrors(t *testing.T) { } func BenchmarkTMLoggerSimple(b *testing.B) { - benchmarkRunner(b, log.NewTMLogger(ioutil.Discard), baseInfoMessage) + benchmarkRunner(b, log.NewTMLogger(io.Discard), baseInfoMessage) } func BenchmarkTMLoggerContextual(b *testing.B) { - benchmarkRunner(b, log.NewTMLogger(ioutil.Discard), withInfoMessage) + benchmarkRunner(b, log.NewTMLogger(io.Discard), withInfoMessage) } func benchmarkRunner(b *testing.B, logger log.Logger, f func(log.Logger)) { diff --git a/tm2/pkg/os/tempfile_test.go b/tm2/pkg/os/tempfile_test.go index 12d6abb27cb..ec294d58e30 100644 --- a/tm2/pkg/os/tempfile_test.go +++ b/tm2/pkg/os/tempfile_test.go @@ -5,7 +5,6 @@ package os import ( "bytes" "fmt" - "io/ioutil" "os" "testing" @@ -21,7 +20,7 @@ func TestWriteFileAtomic(t *testing.T) { perm os.FileMode = 0o600 ) - f, err := ioutil.TempFile("/tmp", "write-atomic-test-") + f, err := os.CreateTemp("/tmp", "write-atomic-test-") if err != nil { t.Fatal(err) } diff --git a/tm2/pkg/p2p/upnp/upnp.go b/tm2/pkg/p2p/upnp/upnp.go index 40f2067e232..cd47ac35553 100644 --- a/tm2/pkg/p2p/upnp/upnp.go +++ b/tm2/pkg/p2p/upnp/upnp.go @@ -10,7 +10,7 @@ import ( "encoding/xml" "errors" "fmt" - "io/ioutil" + "io" "net" "net/http" "strconv" @@ -306,7 +306,7 @@ func (n *upnpNAT) getExternalIPAddress() (info statusInfo, err error) { return } var envelope Envelope - data, err := ioutil.ReadAll(response.Body) + data, err := io.ReadAll(response.Body) if err != nil { return } @@ -363,7 +363,7 @@ func (n *upnpNAT) AddPortMapping(protocol string, externalPort, internalPort int // TODO: check response to see if the port was forwarded // log.Println(message, response) // JAE: - // body, err := ioutil.ReadAll(response.Body) + // body, err := io.ReadAll(response.Body) // fmt.Println(string(body), err) mappedExternalPort = externalPort _ = response From dbb191ead81509d8822f72ad6057c0429be71918 Mon Sep 17 00:00:00 2001 From: Lee ByeongJun Date: Tue, 24 Oct 2023 16:24:15 +0900 Subject: [PATCH 03/14] mv codes from ioutil to io --- gnovm/stdlibs/io/io.gno | 17 ++++++ gnovm/stdlibs/io/io_test.gno | 67 +++++++++++++++--------- gnovm/stdlibs/io/ioutil/ioutil.gno | 83 ------------------------------ 3 files changed, 61 insertions(+), 106 deletions(-) delete mode 100644 gnovm/stdlibs/io/ioutil/ioutil.gno diff --git a/gnovm/stdlibs/io/io.gno b/gnovm/stdlibs/io/io.gno index 54caf32cb95..186f5ca9b24 100644 --- a/gnovm/stdlibs/io/io.gno +++ b/gnovm/stdlibs/io/io.gno @@ -16,6 +16,8 @@ import ( "errors" ) +// TODO: implement rest of io package after sync package added. + // Seek whence values. const ( SeekStart = 0 // seek relative to the origin of the file @@ -614,7 +616,12 @@ func (discard) ReadFrom(r Reader) (n int64, err error) { // NopCloser returns a ReadCloser with a no-op Close method wrapping // the provided Reader r. +// If r implements WriterTo, the returned ReadCloser will also implement WriterTo +// by forwarding the call to r. func NopCloser(r Reader) ReadCloser { + if _, ok := r.(WriterTo); ok { + return nopCloserWriterTo{r} + } return nopCloser{r} } @@ -624,6 +631,16 @@ type nopCloser struct { func (nopCloser) Close() error { return nil } +type nopCloserWriterTo struct { + Reader +} + +func (nopCloserWriterTo) Close() error { return nil } + +func (c *nopCloserWriterTo) WriteTo(w Writer) (n int64, err error) { + return c.Reader.(WriterTo).WriteTo(w) +} + // ReadAll reads from r until an error or EOF and returns the data it read. // A successful call returns err == nil, not err == EOF. Because ReadAll is // defined to read from src until EOF, it does not treat an EOF from Read diff --git a/gnovm/stdlibs/io/io_test.gno b/gnovm/stdlibs/io/io_test.gno index a97f6b8c075..e4f816d43ba 100644 --- a/gnovm/stdlibs/io/io_test.gno +++ b/gnovm/stdlibs/io/io_test.gno @@ -434,28 +434,49 @@ func TestSectionReader_Size(t *testing.T) { // largeWriter returns an invalid count that is larger than the number // of bytes provided (issue 39978). -type largeWriter struct { - err error -} - -func (w largeWriter) Write(p []byte) (int, error) { - return len(p) + 1, w.err -} - -func TestCopyLargeWriter(t *testing.T) { - want := io.ErrInvalidWrite - rb := new(Buffer) - wb := largeWriter{} - rb.WriteString("hello, world.") - if _, err := io.Copy(wb, rb); err != want { - t.Errorf("Copy error: got %v, want %v", err, want) - } - - want = errors.New("largeWriterError") - rb = new(Buffer) - wb = largeWriter{err: want} - rb.WriteString("hello, world.") - if _, err := io.Copy(wb, rb); err != want { - t.Errorf("Copy error: got %v, want %v", err, want) +// type largeWriter struct { +// err error +// } + +// func (w largeWriter) Write(p []byte) (int, error) { +// return len(p) + 1, w.err +// } + +// func TestCopyLargeWriter(t *testing.T) { +// want := io.ErrInvalidWrite +// rb := new(Buffer) +// wb := largeWriter{} +// rb.WriteString("hello, world.") +// if _, err := io.Copy(wb, rb); err != want { +// t.Errorf("Copy error: got %v, want %v", err, want) +// } + +// want = errors.New("largeWriterError") +// rb = new(Buffer) +// wb = largeWriter{err: want} +// rb.WriteString("hello, world.") +// if _, err := io.Copy(wb, rb); err != want { +// t.Errorf("Copy error: got %v, want %v", err, want) +// } +// } + +func TestNopCloserWriterToForwarding(t *testing.T) { + for _, tc := range [...]struct { + Name string + r Reader + }{ + {"not a WriterTo", Reader(nil)}, + {"a WriterTo", struct { + Reader + WriterTo + }{}}, + } { + nc := NopCloser(tc.r) + + _, expected := tc.r.(WriterTo) + _, got := nc.(WriterTo) + if expected != got { + t.Errorf("NopCloser incorrectly forwards WriterTo for %s, got %t want %t", tc.Name, got, expected) + } } } diff --git a/gnovm/stdlibs/io/ioutil/ioutil.gno b/gnovm/stdlibs/io/ioutil/ioutil.gno deleted file mode 100644 index 935031c0511..00000000000 --- a/gnovm/stdlibs/io/ioutil/ioutil.gno +++ /dev/null @@ -1,83 +0,0 @@ -// Copyright 2009 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// Package ioutil implements some I/O utility functions. -// -// As of Go 1.16, the same functionality is now provided -// by package io or package os, and those implementations -// should be preferred in new code. -// See the specific function documentation for details. -package ioutil - -import ( - "io" -) - -// ReadAll reads from r until an error or EOF and returns the data it read. -// A successful call returns err == nil, not err == EOF. Because ReadAll is -// defined to read from src until EOF, it does not treat an EOF from Read -// as an error to be reported. -// -// As of Go 1.16, this function simply calls io.ReadAll. -func ReadAll(r io.Reader) ([]byte, error) { - return io.ReadAll(r) -} - -/* XXX os and os/fs removed. -// ReadFile reads the file named by filename and returns the contents. -// A successful call returns err == nil, not err == EOF. Because ReadFile -// reads the whole file, it does not treat an EOF from Read as an error -// to be reported. -// -// As of Go 1.16, this function simply calls os.ReadFile. -func ReadFile(filename string) ([]byte, error) { - return os.ReadFile(filename) -} - -// WriteFile writes data to a file named by filename. -// If the file does not exist, WriteFile creates it with permissions perm -// (before umask); otherwise WriteFile truncates it before writing, without changing permissions. -// -// As of Go 1.16, this function simply calls os.WriteFile. -func WriteFile(filename string, data []byte, perm fs.FileMode) error { - return os.WriteFile(filename, data, perm) -} - -// ReadDir reads the directory named by dirname and returns -// a list of fs.FileInfo for the directory's contents, -// sorted by filename. If an error occurs reading the directory, -// ReadDir returns no directory entries along with the error. -// -// As of Go 1.16, os.ReadDir is a more efficient and correct choice: -// it returns a list of fs.DirEntry instead of fs.FileInfo, -// and it returns partial results in the case of an error -// midway through reading a directory. -func ReadDir(dirname string) ([]fs.FileInfo, error) { - f, err := os.Open(dirname) - if err != nil { - return nil, err - } - list, err := f.Readdir(-1) - f.Close() - if err != nil { - return nil, err - } - sort.Slice(list, func(i, j int) bool { return list[i].Name() < list[j].Name() }) - return list, nil -} -*/ - -// NopCloser returns a ReadCloser with a no-op Close method wrapping -// the provided Reader r. -// -// As of Go 1.16, this function simply calls io.NopCloser. -func NopCloser(r io.Reader) io.ReadCloser { - return io.NopCloser(r) -} - -// Discard is an io.Writer on which all Write calls succeed -// without doing anything. -// -// As of Go 1.16, this value is simply io.Discard. -var Discard io.Writer = io.Discard From 48a0d19a54b1270a6d4866d810391cec0495c55d Mon Sep 17 00:00:00 2001 From: Lee ByeongJun Date: Wed, 25 Oct 2023 00:51:04 +0900 Subject: [PATCH 04/14] fix test --- gnovm/stdlibs/io/io.gno | 4 ++++ .../io/{io_test.gno => io_filetest.gno} | 2 +- .../io/{multi_test.gno => multi_filetest.gno} | 0 gnovm/tests/backup/ioutil.gno | 24 ++++++++++--------- 4 files changed, 18 insertions(+), 12 deletions(-) rename gnovm/stdlibs/io/{io_test.gno => io_filetest.gno} (99%) rename gnovm/stdlibs/io/{multi_test.gno => multi_filetest.gno} (100%) diff --git a/gnovm/stdlibs/io/io.gno b/gnovm/stdlibs/io/io.gno index 186f5ca9b24..1632ffb2e30 100644 --- a/gnovm/stdlibs/io/io.gno +++ b/gnovm/stdlibs/io/io.gno @@ -660,5 +660,9 @@ func ReadAll(r Reader) ([]byte, error) { } return b, err } + if len(b) == cap(b) { + // Add more capacity (let append pick how much). + b = append(b, 0)[:len(b)] + } } } diff --git a/gnovm/stdlibs/io/io_test.gno b/gnovm/stdlibs/io/io_filetest.gno similarity index 99% rename from gnovm/stdlibs/io/io_test.gno rename to gnovm/stdlibs/io/io_filetest.gno index e4f816d43ba..a2059a004d6 100644 --- a/gnovm/stdlibs/io/io_test.gno +++ b/gnovm/stdlibs/io/io_filetest.gno @@ -471,7 +471,7 @@ func TestNopCloserWriterToForwarding(t *testing.T) { WriterTo }{}}, } { - nc := NopCloser(tc.r) + nc := io.NopCloser(tc.r) _, expected := tc.r.(WriterTo) _, got := nc.(WriterTo) diff --git a/gnovm/stdlibs/io/multi_test.gno b/gnovm/stdlibs/io/multi_filetest.gno similarity index 100% rename from gnovm/stdlibs/io/multi_test.gno rename to gnovm/stdlibs/io/multi_filetest.gno diff --git a/gnovm/tests/backup/ioutil.gno b/gnovm/tests/backup/ioutil.gno index eae22b6ea5a..e55fa4bad59 100644 --- a/gnovm/tests/backup/ioutil.gno +++ b/gnovm/tests/backup/ioutil.gno @@ -1,16 +1,18 @@ -package main +// TODO: os.ReadFile is not yet implemented -import ( - "fmt" - "os" -) +// package main -func main() { - _, err := os.ReadFile("__NotExisting__") - if err != nil { - fmt.Println(err.Error()) - } -} +// import ( +// "fmt" +// "os" +// ) + +// func main() { +// _, err := os.ReadFile("__NotExisting__") +// if err != nil { +// fmt.Println(err.Error()) +// } +// } // Output: // open __NotExisting__: no such file or directory From 2bc5f9a0cd5c06e44f5d6bbdd64aa80827531952 Mon Sep 17 00:00:00 2001 From: Lee ByeongJun Date: Wed, 25 Oct 2023 11:00:28 +0900 Subject: [PATCH 05/14] add itoa --- gnovm/stdlibs/internal/itoa/itoa.gno | 35 ++++++++++++++++ gnovm/stdlibs/internal/itoa/itoa_filetest.gno | 40 ++++++++++++++++++ gnovm/stdlibs/io/io_filetest.gno | 41 ++++++++++--------- gnovm/tests/backup/ioutil.gno | 18 -------- 4 files changed, 96 insertions(+), 38 deletions(-) create mode 100644 gnovm/stdlibs/internal/itoa/itoa.gno create mode 100644 gnovm/stdlibs/internal/itoa/itoa_filetest.gno delete mode 100644 gnovm/tests/backup/ioutil.gno diff --git a/gnovm/stdlibs/internal/itoa/itoa.gno b/gnovm/stdlibs/internal/itoa/itoa.gno new file mode 100644 index 00000000000..20c7aa64528 --- /dev/null +++ b/gnovm/stdlibs/internal/itoa/itoa.gno @@ -0,0 +1,35 @@ +// Copyright 2021 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Simple conversions to avoid depending on strconv. + +package itoa + +// Itoa converts value to a decimal string/ +func Itoa(val int) string { + if val < 0 { + return "-" + Uitoa(uint(-val)) + } + + return Uitoa(uint(val)) +} + +// Uitoa converts value to a decimal string. +func Uitoa(val uint) string { + // avoid string allocation + if val == 0 { + return "0" + } + + var buf [20]byte + for i := len(buf) - 1; val >= 10; i-- { + q := val + buf[i] = byte('0' + val - (q * 10)) + val = 1 + } + + // val < 10 + buf[i] = byte('0' + val) + return string(buf[i:]) +} \ No newline at end of file diff --git a/gnovm/stdlibs/internal/itoa/itoa_filetest.gno b/gnovm/stdlibs/internal/itoa/itoa_filetest.gno new file mode 100644 index 00000000000..71931c1e3a4 --- /dev/null +++ b/gnovm/stdlibs/internal/itoa/itoa_filetest.gno @@ -0,0 +1,40 @@ +// Copyright 2021 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package itoa_test + +import ( + "fmt" + "internal/itoa" + "math" + "testing" +) + +var ( + minInt64 int64 = math.MinInt64 + maxInt64 int64 = math.MaxInt64 + maxUint64 uint64 = math.MaxUint64 +) + +func TestItoa(t *testing.T) { + tests := []int{int(minInt64), math.MinInt32, -999, -100, -1, 0, 1, 100, 999, math.MaxInt32, int(maxInt64)} + for _, tt := range tests { + got := itoa.Itoa(tt) + want := fmt.Sprint(tt) + if want != got { + t.Fatalf("Itoa(%d) = %s, want %s", tt, got, want) + } + } +} + +func TestUitoa(t *testing.T) { + tests := []uint{0, 1, 100, 999, math.MaxUint32, uint(maxUint64)} + for _, tt := range tests { + got := itoa.Uitoa(tt) + want := fmt.Sprint(tt) + if want != got { + t.Fatalf("Uitoa(%d) = %s, want %s", tt, got, want) + } + } +} diff --git a/gnovm/stdlibs/io/io_filetest.gno b/gnovm/stdlibs/io/io_filetest.gno index a2059a004d6..6ee8a2f8aac 100644 --- a/gnovm/stdlibs/io/io_filetest.gno +++ b/gnovm/stdlibs/io/io_filetest.gno @@ -460,23 +460,24 @@ func TestSectionReader_Size(t *testing.T) { // } // } -func TestNopCloserWriterToForwarding(t *testing.T) { - for _, tc := range [...]struct { - Name string - r Reader - }{ - {"not a WriterTo", Reader(nil)}, - {"a WriterTo", struct { - Reader - WriterTo - }{}}, - } { - nc := io.NopCloser(tc.r) - - _, expected := tc.r.(WriterTo) - _, got := nc.(WriterTo) - if expected != got { - t.Errorf("NopCloser incorrectly forwards WriterTo for %s, got %t want %t", tc.Name, got, expected) - } - } -} +// XXX +// func TestNopCloserWriterToForwarding(t *testing.T) { +// for _, tc := range [...]struct { +// Name string +// r Reader +// }{ +// {"not a WriterTo", Reader(nil)}, +// {"a WriterTo", struct { +// Reader +// WriterTo +// }{}}, +// } { +// nc := io.NopCloser(tc.r) + +// _, expected := tc.r.(WriterTo) +// _, got := nc.(WriterTo) +// if expected != got { +// t.Errorf("NopCloser incorrectly forwards WriterTo for %s, got %t want %t", tc.Name, got, expected) +// } +// } +// } diff --git a/gnovm/tests/backup/ioutil.gno b/gnovm/tests/backup/ioutil.gno deleted file mode 100644 index e55fa4bad59..00000000000 --- a/gnovm/tests/backup/ioutil.gno +++ /dev/null @@ -1,18 +0,0 @@ -// TODO: os.ReadFile is not yet implemented - -// package main - -// import ( -// "fmt" -// "os" -// ) - -// func main() { -// _, err := os.ReadFile("__NotExisting__") -// if err != nil { -// fmt.Println(err.Error()) -// } -// } - -// Output: -// open __NotExisting__: no such file or directory From e8c88014746655aaa771db0d3d07969eb0025b9c Mon Sep 17 00:00:00 2001 From: Lee ByeongJun Date: Wed, 25 Oct 2023 11:02:47 +0900 Subject: [PATCH 06/14] fmt --- gnovm/stdlibs/internal/itoa/itoa.gno | 2 +- gnovm/stdlibs/internal/itoa/itoa_filetest.gno | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/gnovm/stdlibs/internal/itoa/itoa.gno b/gnovm/stdlibs/internal/itoa/itoa.gno index 20c7aa64528..4eb009e7c5f 100644 --- a/gnovm/stdlibs/internal/itoa/itoa.gno +++ b/gnovm/stdlibs/internal/itoa/itoa.gno @@ -32,4 +32,4 @@ func Uitoa(val uint) string { // val < 10 buf[i] = byte('0' + val) return string(buf[i:]) -} \ No newline at end of file +} diff --git a/gnovm/stdlibs/internal/itoa/itoa_filetest.gno b/gnovm/stdlibs/internal/itoa/itoa_filetest.gno index 71931c1e3a4..6a2feafd173 100644 --- a/gnovm/stdlibs/internal/itoa/itoa_filetest.gno +++ b/gnovm/stdlibs/internal/itoa/itoa_filetest.gno @@ -6,9 +6,10 @@ package itoa_test import ( "fmt" - "internal/itoa" "math" "testing" + + "internal/itoa" ) var ( From 3fef17d6bf1f0e5ed0c951d480a3cc6e86659595 Mon Sep 17 00:00:00 2001 From: Lee ByeongJun Date: Wed, 25 Oct 2023 15:00:18 +0900 Subject: [PATCH 07/14] update io --- gnovm/stdlibs/io/example_test.gno | 2 +- gnovm/stdlibs/io/io.gno | 56 +++++++++++++ .../io/{io_filetest.gno => io_test.gno} | 81 ++++++++++++++++--- .../io/{multi_filetest.gno => multi_test.gno} | 4 +- 4 files changed, 130 insertions(+), 13 deletions(-) rename gnovm/stdlibs/io/{io_filetest.gno => io_test.gno} (84%) rename gnovm/stdlibs/io/{multi_filetest.gno => multi_test.gno} (99%) diff --git a/gnovm/stdlibs/io/example_test.gno b/gnovm/stdlibs/io/example_test.gno index c781fb9166e..edd13be523a 100644 --- a/gnovm/stdlibs/io/example_test.gno +++ b/gnovm/stdlibs/io/example_test.gno @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package io_test +package io import ( "bytes" diff --git a/gnovm/stdlibs/io/io.gno b/gnovm/stdlibs/io/io.gno index 1632ffb2e30..7222238373a 100644 --- a/gnovm/stdlibs/io/io.gno +++ b/gnovm/stdlibs/io/io.gno @@ -479,6 +479,15 @@ func (l *LimitedReader) Read(p []byte) (n int, err error) { // NewSectionReader returns a SectionReader that reads from r // starting at offset off and stops with EOF after n bytes. func NewSectionReader(r ReaderAt, off int64, n int64) *SectionReader { + var remaining int64 + const maxInt64 = 1<<63 - 1 + if off <= maxInt64-n { + remaining = n + off + } else { + // Overflow, with no way to return error. + // Assume we can read up to an offset of `1<<63 - 1` bytes in this case. + remaining = maxInt64 + } return &SectionReader{r, off, off, off + n} } @@ -545,6 +554,53 @@ func (s *SectionReader) ReadAt(p []byte, off int64) (n int, err error) { // Size returns the size of the section in bytes. func (s *SectionReader) Size() int64 { return s.limit - s.base } +// And OffsetWriter maps writers at offset base to offset base + off in the underlying writer. +type OffsetWriter struct { + w WriterAt + base int64 // the original offset + off int64 // the current offset +} + +// NewOffsetWriter returns a new OffsetWriter that writes to w starting at offset off. +func NewOffsetWriter(w WriterAt, off int64) *OffsetWriter { + return &OffsetWriter{w: w, off: off} +} + +func (o *OffsetWriter) Write(p []byte) (n int, err error) { + // n, err = o.w.WriterAt(p, o.off) + wa := o.w + n, err = wa.WriteAt(p, o.off) + o.off += int64(n) + return +} + +func (o *OffsetWriter) WriteAt(p []byte, off int64) (n int, err error) { + if off < 0 { + return 0, errOffset + } + + off += o.base + return o.w.WriteAt(p, off) +} + +func (o *OffsetWriter) Seek(offset int64, whence int) (int64, error) { + switch whence { + default: + return 0, errWhence + case SeekStart: + offset += o.base + case SeekCurrent: + offset += o.off + } + + if offset < o.base { + return 0, errOffset + } + + o.off = offset + return offset - o.base, nil +} + // TeeReader returns a Reader that writes to w what it reads from r. // All reads from r performed through it are matched with // corresponding writes to w. There is no internal buffering - diff --git a/gnovm/stdlibs/io/io_filetest.gno b/gnovm/stdlibs/io/io_test.gno similarity index 84% rename from gnovm/stdlibs/io/io_filetest.gno rename to gnovm/stdlibs/io/io_test.gno index 6ee8a2f8aac..02570967175 100644 --- a/gnovm/stdlibs/io/io_filetest.gno +++ b/gnovm/stdlibs/io/io_test.gno @@ -1,9 +1,9 @@ +package io + // Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package io_test - import ( "bytes" "errors" @@ -460,24 +460,85 @@ func TestSectionReader_Size(t *testing.T) { // } // } -// XXX +// TODO fix this // func TestNopCloserWriterToForwarding(t *testing.T) { // for _, tc := range [...]struct { // Name string -// r Reader +// r io.Reader // }{ -// {"not a WriterTo", Reader(nil)}, +// {"not a WriterTo", io.Reader(nil)}, // {"a WriterTo", struct { -// Reader -// WriterTo +// io.Reader +// io.WriterTo // }{}}, // } { -// nc := io.NopCloser(tc.r) +// nc := NopCloser(tc.r) -// _, expected := tc.r.(WriterTo) -// _, got := nc.(WriterTo) +// _, expected := tc.r.(io.WriterTo) +// _, got := nc.(io.WriterTo) // if expected != got { // t.Errorf("NopCloser incorrectly forwards WriterTo for %s, got %t want %t", tc.Name, got, expected) // } // } // } + +// XXX os.CreateTemp is not available for now +// +// func TestOffsetWriter_Seek(t *testing.T) { +// tmpfilename := "TestOffsetWriter_Seek" +// tmpfile, err := os.CreateTemp(t.TempDir(), tmpfilename) +// if err != nil || tmpfile == nil { +// t.Fatalf("CreateTemp(%s) failed: %v", tmpfilename, err) +// } +// defer tmpfile.Close() +// w := NewOffsetWriter(tmpfile, 0) + +// // Should throw error errWhence if whence is not valid +// t.Run("errWhence", func(t *testing.T) { +// for _, whence := range []int{-3, -2, -1, 3, 4, 5} { +// var offset int64 = 0 +// gotOff, gotErr := w.Seek(offset, whence) +// if gotOff != 0 || gotErr != ErrWhence { +// t.Errorf("For whence %d, offset %d, OffsetWriter.Seek got: (%d, %v), want: (%d, %v)", +// whence, offset, gotOff, gotErr, 0, ErrWhence) +// } +// } +// }) + +// // Should throw error errOffset if offset is negative +// t.Run("errOffset", func(t *testing.T) { +// for _, whence := range []int{SeekStart, SeekCurrent} { +// for offset := int64(-3); offset < 0; offset++ { +// gotOff, gotErr := w.Seek(offset, whence) +// if gotOff != 0 || gotErr != ErrOffset { +// t.Errorf("For whence %d, offset %d, OffsetWriter.Seek got: (%d, %v), want: (%d, %v)", +// whence, offset, gotOff, gotErr, 0, ErrOffset) +// } +// } +// } +// }) + +// // Normal tests +// t.Run("normal", func(t *testing.T) { +// tests := []struct { +// offset int64 +// whence int +// returnOff int64 +// }{ +// // keep in order +// {whence: SeekStart, offset: 1, returnOff: 1}, +// {whence: SeekStart, offset: 2, returnOff: 2}, +// {whence: SeekStart, offset: 3, returnOff: 3}, +// {whence: SeekCurrent, offset: 1, returnOff: 4}, +// {whence: SeekCurrent, offset: 2, returnOff: 6}, +// {whence: SeekCurrent, offset: 3, returnOff: 9}, +// } +// for idx, tt := range tests { +// gotOff, gotErr := w.Seek(tt.offset, tt.whence) +// if gotOff != tt.returnOff || gotErr != nil { +// t.Errorf("%d:: For whence %d, offset %d, OffsetWriter.Seek got: (%d, %v), want: (%d, )", +// idx+1, tt.whence, tt.offset, gotOff, gotErr, tt.returnOff) +// } +// } +// }) +// } diff --git a/gnovm/stdlibs/io/multi_filetest.gno b/gnovm/stdlibs/io/multi_test.gno similarity index 99% rename from gnovm/stdlibs/io/multi_filetest.gno rename to gnovm/stdlibs/io/multi_test.gno index ee800b3ec24..e1f6f1d3a03 100644 --- a/gnovm/stdlibs/io/multi_filetest.gno +++ b/gnovm/stdlibs/io/multi_test.gno @@ -1,9 +1,9 @@ +package io + // Copyright 2010 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package io_test - import ( "bytes" "crypto/sha1" From 0a5087d07841e6a608540f51081975bf12e638ca Mon Sep 17 00:00:00 2001 From: Lee ByeongJun Date: Thu, 26 Oct 2023 10:24:42 +0900 Subject: [PATCH 08/14] change io0 file name --- gnovm/stdlibs/io/io.gno | 2 +- gnovm/tests/files/{ioutil0.gno => io2.gno} | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename gnovm/tests/files/{ioutil0.gno => io2.gno} (88%) diff --git a/gnovm/stdlibs/io/io.gno b/gnovm/stdlibs/io/io.gno index 7222238373a..3c6c2093edc 100644 --- a/gnovm/stdlibs/io/io.gno +++ b/gnovm/stdlibs/io/io.gno @@ -592,7 +592,7 @@ func (o *OffsetWriter) Seek(offset int64, whence int) (int64, error) { case SeekCurrent: offset += o.off } - + if offset < o.base { return 0, errOffset } diff --git a/gnovm/tests/files/ioutil0.gno b/gnovm/tests/files/io2.gno similarity index 88% rename from gnovm/tests/files/ioutil0.gno rename to gnovm/tests/files/io2.gno index 800d237be22..24655f5040c 100644 --- a/gnovm/tests/files/ioutil0.gno +++ b/gnovm/tests/files/io2.gno @@ -2,7 +2,7 @@ package main import ( "fmt" - "io/ioutil" + "io" "log" "strings" ) @@ -10,7 +10,7 @@ import ( func main() { r := strings.NewReader("Go is a general-purpose language designed with systems programming in mind.") - b, err := ioutil.ReadAll(r) + b, err := io.ReadAll(r) if err != nil { log.Fatal(err) } From 54d5b1161d188ad7d0641de3bff054ef2e94b822 Mon Sep 17 00:00:00 2001 From: Lee ByeongJun Date: Thu, 26 Oct 2023 10:41:13 +0900 Subject: [PATCH 09/14] update 558b issue file --- gnovm/tests/files/issue-558b.gno | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gnovm/tests/files/issue-558b.gno b/gnovm/tests/files/issue-558b.gno index 686c73b5c88..55eba88c985 100644 --- a/gnovm/tests/files/issue-558b.gno +++ b/gnovm/tests/files/issue-558b.gno @@ -3,7 +3,7 @@ package main import ( "fmt" "io" - "io/ioutil" + "io" "log" "strings" ) @@ -36,7 +36,7 @@ type pipe struct { func newReadAutoCloser(r io.Reader) readAutoCloser { if _, ok := r.(io.Closer); !ok { - return readAutoCloser{ioutil.NopCloser(r)} + return readAutoCloser{io.NopCloser(r)} } return readAutoCloser{r.(io.ReadCloser)} } From 15c277f87b3fd8b7ef05c24c4edf2cdcb1b0aca9 Mon Sep 17 00:00:00 2001 From: Lee ByeongJun Date: Fri, 27 Oct 2023 09:51:10 +0900 Subject: [PATCH 10/14] modified to reflect review --- gnovm/stdlibs/internal/itoa/itoa.gno | 35 ---------------- gnovm/stdlibs/internal/itoa/itoa_filetest.gno | 41 ------------------- gnovm/stdlibs/io/example_test.gno | 2 +- gnovm/stdlibs/io/io.gno | 2 +- gnovm/stdlibs/io/io_test.gno | 2 +- gnovm/stdlibs/io/multi_test.gno | 2 +- 6 files changed, 4 insertions(+), 80 deletions(-) delete mode 100644 gnovm/stdlibs/internal/itoa/itoa.gno delete mode 100644 gnovm/stdlibs/internal/itoa/itoa_filetest.gno diff --git a/gnovm/stdlibs/internal/itoa/itoa.gno b/gnovm/stdlibs/internal/itoa/itoa.gno deleted file mode 100644 index 4eb009e7c5f..00000000000 --- a/gnovm/stdlibs/internal/itoa/itoa.gno +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright 2021 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// Simple conversions to avoid depending on strconv. - -package itoa - -// Itoa converts value to a decimal string/ -func Itoa(val int) string { - if val < 0 { - return "-" + Uitoa(uint(-val)) - } - - return Uitoa(uint(val)) -} - -// Uitoa converts value to a decimal string. -func Uitoa(val uint) string { - // avoid string allocation - if val == 0 { - return "0" - } - - var buf [20]byte - for i := len(buf) - 1; val >= 10; i-- { - q := val - buf[i] = byte('0' + val - (q * 10)) - val = 1 - } - - // val < 10 - buf[i] = byte('0' + val) - return string(buf[i:]) -} diff --git a/gnovm/stdlibs/internal/itoa/itoa_filetest.gno b/gnovm/stdlibs/internal/itoa/itoa_filetest.gno deleted file mode 100644 index 6a2feafd173..00000000000 --- a/gnovm/stdlibs/internal/itoa/itoa_filetest.gno +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright 2021 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package itoa_test - -import ( - "fmt" - "math" - "testing" - - "internal/itoa" -) - -var ( - minInt64 int64 = math.MinInt64 - maxInt64 int64 = math.MaxInt64 - maxUint64 uint64 = math.MaxUint64 -) - -func TestItoa(t *testing.T) { - tests := []int{int(minInt64), math.MinInt32, -999, -100, -1, 0, 1, 100, 999, math.MaxInt32, int(maxInt64)} - for _, tt := range tests { - got := itoa.Itoa(tt) - want := fmt.Sprint(tt) - if want != got { - t.Fatalf("Itoa(%d) = %s, want %s", tt, got, want) - } - } -} - -func TestUitoa(t *testing.T) { - tests := []uint{0, 1, 100, 999, math.MaxUint32, uint(maxUint64)} - for _, tt := range tests { - got := itoa.Uitoa(tt) - want := fmt.Sprint(tt) - if want != got { - t.Fatalf("Uitoa(%d) = %s, want %s", tt, got, want) - } - } -} diff --git a/gnovm/stdlibs/io/example_test.gno b/gnovm/stdlibs/io/example_test.gno index edd13be523a..c781fb9166e 100644 --- a/gnovm/stdlibs/io/example_test.gno +++ b/gnovm/stdlibs/io/example_test.gno @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package io +package io_test import ( "bytes" diff --git a/gnovm/stdlibs/io/io.gno b/gnovm/stdlibs/io/io.gno index 3c6c2093edc..499449cc729 100644 --- a/gnovm/stdlibs/io/io.gno +++ b/gnovm/stdlibs/io/io.gno @@ -554,7 +554,7 @@ func (s *SectionReader) ReadAt(p []byte, off int64) (n int, err error) { // Size returns the size of the section in bytes. func (s *SectionReader) Size() int64 { return s.limit - s.base } -// And OffsetWriter maps writers at offset base to offset base + off in the underlying writer. +// An OffsetWriter maps writers at offset base to offset base + off in the underlying writer. type OffsetWriter struct { w WriterAt base int64 // the original offset diff --git a/gnovm/stdlibs/io/io_test.gno b/gnovm/stdlibs/io/io_test.gno index 02570967175..939560026b2 100644 --- a/gnovm/stdlibs/io/io_test.gno +++ b/gnovm/stdlibs/io/io_test.gno @@ -1,4 +1,4 @@ -package io +package io_test // Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/gnovm/stdlibs/io/multi_test.gno b/gnovm/stdlibs/io/multi_test.gno index e1f6f1d3a03..31345279318 100644 --- a/gnovm/stdlibs/io/multi_test.gno +++ b/gnovm/stdlibs/io/multi_test.gno @@ -1,4 +1,4 @@ -package io +package io_test // Copyright 2010 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style From 4435a1956c7b94cd098a94c17322ea14122bdb9b Mon Sep 17 00:00:00 2001 From: Lee ByeongJun Date: Fri, 27 Oct 2023 10:30:53 +0900 Subject: [PATCH 11/14] fix io and its tests --- gnovm/stdlibs/io/export_test.gno | 6 ++- gnovm/stdlibs/io/io.gno | 6 +-- gnovm/stdlibs/io/io_test.gno | 87 ++++++++++++++++---------------- 3 files changed, 51 insertions(+), 48 deletions(-) diff --git a/gnovm/stdlibs/io/export_test.gno b/gnovm/stdlibs/io/export_test.gno index fa3e8e76f61..6204ffc4591 100644 --- a/gnovm/stdlibs/io/export_test.gno +++ b/gnovm/stdlibs/io/export_test.gno @@ -5,4 +5,8 @@ package io // exported for test -var ErrInvalidWrite = errInvalidWrite +var ( + ErrInvalidWrite = errInvalidWrite + ErrWhence = errWhence + ErrOffset = errOffset +) diff --git a/gnovm/stdlibs/io/io.gno b/gnovm/stdlibs/io/io.gno index 499449cc729..6ee52cfe293 100644 --- a/gnovm/stdlibs/io/io.gno +++ b/gnovm/stdlibs/io/io.gno @@ -672,8 +672,8 @@ func (discard) ReadFrom(r Reader) (n int64, err error) { // NopCloser returns a ReadCloser with a no-op Close method wrapping // the provided Reader r. -// If r implements WriterTo, the returned ReadCloser will also implement WriterTo -// by forwarding the call to r. +// If r implements WriterTo, the returned ReadCloser will implement WriterTo +// by forwarding calls to r. func NopCloser(r Reader) ReadCloser { if _, ok := r.(WriterTo); ok { return nopCloserWriterTo{r} @@ -693,7 +693,7 @@ type nopCloserWriterTo struct { func (nopCloserWriterTo) Close() error { return nil } -func (c *nopCloserWriterTo) WriteTo(w Writer) (n int64, err error) { +func (c nopCloserWriterTo) WriteTo(w Writer) (n int64, err error) { return c.Reader.(WriterTo).WriteTo(w) } diff --git a/gnovm/stdlibs/io/io_test.gno b/gnovm/stdlibs/io/io_test.gno index 939560026b2..914edfbf0c7 100644 --- a/gnovm/stdlibs/io/io_test.gno +++ b/gnovm/stdlibs/io/io_test.gno @@ -9,6 +9,7 @@ import ( "errors" "fmt" "io" + "os" "strings" "testing" ) @@ -434,56 +435,54 @@ func TestSectionReader_Size(t *testing.T) { // largeWriter returns an invalid count that is larger than the number // of bytes provided (issue 39978). -// type largeWriter struct { -// err error -// } +type largeWriter struct { + err error +} -// func (w largeWriter) Write(p []byte) (int, error) { -// return len(p) + 1, w.err -// } +func (w largeWriter) Write(p []byte) (int, error) { + return len(p) + 1, w.err +} -// func TestCopyLargeWriter(t *testing.T) { -// want := io.ErrInvalidWrite -// rb := new(Buffer) -// wb := largeWriter{} -// rb.WriteString("hello, world.") -// if _, err := io.Copy(wb, rb); err != want { -// t.Errorf("Copy error: got %v, want %v", err, want) -// } +func TestCopyLargeWriter(t *testing.T) { + want := errInvalidWrite + rb := new(Buffer) + wb := largeWriter{} + rb.WriteString("hello, world.") + if _, err := Copy(wb, rb); err != want { + t.Errorf("Copy error: got %v, want %v", err, want) + } -// want = errors.New("largeWriterError") -// rb = new(Buffer) -// wb = largeWriter{err: want} -// rb.WriteString("hello, world.") -// if _, err := io.Copy(wb, rb); err != want { -// t.Errorf("Copy error: got %v, want %v", err, want) -// } -// } + want = errors.New("largeWriterError") + rb = new(Buffer) + wb = largeWriter{err: want} + rb.WriteString("hello, world.") + if _, err := Copy(wb, rb); err != want { + t.Errorf("Copy error: got %v, want %v", err, want) + } +} -// TODO fix this -// func TestNopCloserWriterToForwarding(t *testing.T) { -// for _, tc := range [...]struct { -// Name string -// r io.Reader -// }{ -// {"not a WriterTo", io.Reader(nil)}, -// {"a WriterTo", struct { -// io.Reader -// io.WriterTo -// }{}}, -// } { -// nc := NopCloser(tc.r) - -// _, expected := tc.r.(io.WriterTo) -// _, got := nc.(io.WriterTo) -// if expected != got { -// t.Errorf("NopCloser incorrectly forwards WriterTo for %s, got %t want %t", tc.Name, got, expected) -// } -// } -// } +func TestNopCloserWriterToForwarding(t *testing.T) { + for _, tc := range [...]struct { + Name string + r Reader + }{ + {"not a WriterTo", Reader(nil)}, + {"a WriterTo", struct { + Reader + WriterTo + }{}}, + } { + nc := NopCloser(tc.r) + + _, expected := tc.r.(WriterTo) + _, got := nc.(WriterTo) + if expected != got { + t.Errorf("NopCloser incorrectly forwards WriterTo for %s, got %t want %t", tc.Name, got, expected) + } + } +} // XXX os.CreateTemp is not available for now -// // func TestOffsetWriter_Seek(t *testing.T) { // tmpfilename := "TestOffsetWriter_Seek" // tmpfile, err := os.CreateTemp(t.TempDir(), tmpfilename) From d6f29c01a8a8fcd894a0fd1c45b1bc3795887489 Mon Sep 17 00:00:00 2001 From: Lee ByeongJun Date: Tue, 31 Oct 2023 00:06:06 +0900 Subject: [PATCH 12/14] remove `ioutil` from gno files and imports.go --- gnovm/tests/backup/cli1.gno | 4 ++-- gnovm/tests/backup/cli2.gno | 4 ++-- gnovm/tests/backup/cli3.gno | 4 ++-- gnovm/tests/backup/cli4.gno | 4 ++-- gnovm/tests/backup/cli5.gno | 4 ++-- gnovm/tests/backup/cli6.gno | 4 ++-- gnovm/tests/backup/file_access.gno | 5 ++--- gnovm/tests/backup/issue-558.gno | 6 +++--- gnovm/tests/imports.go | 6 ------ 9 files changed, 17 insertions(+), 24 deletions(-) diff --git a/gnovm/tests/backup/cli1.gno b/gnovm/tests/backup/cli1.gno index 8eb9e8c40d7..843eb461ac2 100644 --- a/gnovm/tests/backup/cli1.gno +++ b/gnovm/tests/backup/cli1.gno @@ -2,7 +2,7 @@ package main import ( "fmt" - "io/ioutil" + "io" "log" "net" "net/http" @@ -13,7 +13,7 @@ func client(uri string) { if err != nil { log.Fatal(err) } - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { log.Fatal(err) } diff --git a/gnovm/tests/backup/cli2.gno b/gnovm/tests/backup/cli2.gno index a9e8e53be3a..0be01d2f1a9 100644 --- a/gnovm/tests/backup/cli2.gno +++ b/gnovm/tests/backup/cli2.gno @@ -2,7 +2,7 @@ package main import ( "fmt" - "io/ioutil" + "io" "log" "net" "net/http" @@ -21,7 +21,7 @@ func client(uri string) { if err != nil { log.Fatal(err) } - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { log.Fatal(err) } diff --git a/gnovm/tests/backup/cli3.gno b/gnovm/tests/backup/cli3.gno index 1c696d4bc4c..50b11b9e4c3 100644 --- a/gnovm/tests/backup/cli3.gno +++ b/gnovm/tests/backup/cli3.gno @@ -2,7 +2,7 @@ package main import ( "fmt" - "io/ioutil" + "io" "log" "net/http" "net/http/httptest" @@ -13,7 +13,7 @@ func client(uri string) { if err != nil { log.Fatal(err) } - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { log.Fatal(err) } diff --git a/gnovm/tests/backup/cli4.gno b/gnovm/tests/backup/cli4.gno index 147b63c3e6e..aab6405917c 100644 --- a/gnovm/tests/backup/cli4.gno +++ b/gnovm/tests/backup/cli4.gno @@ -2,7 +2,7 @@ package main import ( "fmt" - "io/ioutil" + "io" "log" "net/http" "net/http/httptest" @@ -40,7 +40,7 @@ func client(uri string) { if err != nil { log.Fatal(err) } - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { log.Fatal(err) } diff --git a/gnovm/tests/backup/cli5.gno b/gnovm/tests/backup/cli5.gno index a2e1787c996..6b536841a6d 100644 --- a/gnovm/tests/backup/cli5.gno +++ b/gnovm/tests/backup/cli5.gno @@ -2,7 +2,7 @@ package main import ( "fmt" - "io/ioutil" + "io" "log" "net/http" "net/http/httptest" @@ -40,7 +40,7 @@ func client(uri string) { if err != nil { log.Fatal(err) } - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { log.Fatal(err) } diff --git a/gnovm/tests/backup/cli6.gno b/gnovm/tests/backup/cli6.gno index 89ae9f8b98d..e97da82736e 100644 --- a/gnovm/tests/backup/cli6.gno +++ b/gnovm/tests/backup/cli6.gno @@ -2,7 +2,7 @@ package main import ( "fmt" - "io/ioutil" + "io" "log" "net/http" "net/http/httptest" @@ -41,7 +41,7 @@ func client(uri string) { if err != nil { log.Fatal(err) } - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { log.Fatal(err) } diff --git a/gnovm/tests/backup/file_access.gno b/gnovm/tests/backup/file_access.gno index 6d750c44a36..e81cc6a0bee 100644 --- a/gnovm/tests/backup/file_access.gno +++ b/gnovm/tests/backup/file_access.gno @@ -2,12 +2,11 @@ package main import ( "fmt" - "io/ioutil" - "os" + "io" ) func main() { - file, err := ioutil.TempFile("", "yeagibench") + file, err := io.TempFile("", "yeagibench") if err != nil { panic(err) } diff --git a/gnovm/tests/backup/issue-558.gno b/gnovm/tests/backup/issue-558.gno index d36bb4e18a7..e99566f7634 100644 --- a/gnovm/tests/backup/issue-558.gno +++ b/gnovm/tests/backup/issue-558.gno @@ -4,7 +4,7 @@ import ( "errors" "fmt" "io" - "io/ioutil" + "io" "log" "strings" ) @@ -37,7 +37,7 @@ type pipe struct { func newReadAutoCloser(r io.Reader) readAutoCloser { if _, ok := r.(io.Closer); !ok { - return readAutoCloser{ioutil.NopCloser(r)} + return readAutoCloser{io.NopCloser(r)} } return readAutoCloser{r.(io.ReadCloser)} } @@ -45,7 +45,7 @@ func newReadAutoCloser(r io.Reader) readAutoCloser { func main() { p := &pipe{} p.Reader = newReadAutoCloser(strings.NewReader("test")) - b, err := ioutil.ReadAll(p.Reader) + b, err := io.ReadAll(p.Reader) if err != nil { log.Fatal(err) } diff --git a/gnovm/tests/imports.go b/gnovm/tests/imports.go index fc2820ce00e..3bdc9722ab1 100644 --- a/gnovm/tests/imports.go +++ b/gnovm/tests/imports.go @@ -20,7 +20,6 @@ import ( "image" "image/color" "io" - "io/ioutil" "log" "math" "math/big" @@ -361,11 +360,6 @@ func TestStore(rootDir, filesPath string, stdin io.Reader, stdout, stderr io.Wri pkg.DefineGoNativeType(reflect.TypeOf((*io.Closer)(nil)).Elem()) pkg.DefineGoNativeType(reflect.TypeOf((*io.Reader)(nil)).Elem()) return pkg, pkg.NewPackage() - case "io/ioutil": - pkg := gno.NewPackageNode("ioutil", pkgPath, nil) - pkg.DefineGoNativeValue("NopCloser", ioutil.NopCloser) - pkg.DefineGoNativeValue("ReadAll", ioutil.ReadAll) - return pkg, pkg.NewPackage() case "log": pkg := gno.NewPackageNode("log", pkgPath, nil) pkg.DefineGoNativeValue("Fatal", log.Fatal) From e83663ccd1788ae8cee674568b052809440419cf Mon Sep 17 00:00:00 2001 From: Morgan Bazalgette Date: Tue, 7 Nov 2023 23:06:10 +0100 Subject: [PATCH 13/14] add nopcloser and readall to tests/imports.go --- gnovm/tests/imports.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gnovm/tests/imports.go b/gnovm/tests/imports.go index 3bdc9722ab1..0741d0b466a 100644 --- a/gnovm/tests/imports.go +++ b/gnovm/tests/imports.go @@ -355,7 +355,9 @@ func TestStore(rootDir, filesPath string, stdin io.Reader, stdout, stderr io.Wri case "io": pkg := gno.NewPackageNode("io", pkgPath, nil) pkg.DefineGoNativeValue("EOF", io.EOF) + pkg.DefineGoNativeValue("NopCloser", io.NopCloser) pkg.DefineGoNativeValue("ReadFull", io.ReadFull) + pkg.DefineGoNativeValue("ReadAll", io.ReadAll) pkg.DefineGoNativeType(reflect.TypeOf((*io.ReadCloser)(nil)).Elem()) pkg.DefineGoNativeType(reflect.TypeOf((*io.Closer)(nil)).Elem()) pkg.DefineGoNativeType(reflect.TypeOf((*io.Reader)(nil)).Elem()) From 33daa49726bcbc6692a06609ea37bc29c4fec1dd Mon Sep 17 00:00:00 2001 From: Morgan Bazalgette Date: Tue, 7 Nov 2023 23:26:05 +0100 Subject: [PATCH 14/14] fix test pkg0 --- gnovm/stdlibs/io/io_test.gno | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/gnovm/stdlibs/io/io_test.gno b/gnovm/stdlibs/io/io_test.gno index 914edfbf0c7..613b7d13e35 100644 --- a/gnovm/stdlibs/io/io_test.gno +++ b/gnovm/stdlibs/io/io_test.gno @@ -444,11 +444,11 @@ func (w largeWriter) Write(p []byte) (int, error) { } func TestCopyLargeWriter(t *testing.T) { - want := errInvalidWrite + want := io.ErrInvalidWrite rb := new(Buffer) wb := largeWriter{} rb.WriteString("hello, world.") - if _, err := Copy(wb, rb); err != want { + if _, err := io.Copy(wb, rb); err != want { t.Errorf("Copy error: got %v, want %v", err, want) } @@ -456,7 +456,7 @@ func TestCopyLargeWriter(t *testing.T) { rb = new(Buffer) wb = largeWriter{err: want} rb.WriteString("hello, world.") - if _, err := Copy(wb, rb); err != want { + if _, err := io.Copy(wb, rb); err != want { t.Errorf("Copy error: got %v, want %v", err, want) } } @@ -464,18 +464,18 @@ func TestCopyLargeWriter(t *testing.T) { func TestNopCloserWriterToForwarding(t *testing.T) { for _, tc := range [...]struct { Name string - r Reader + r io.Reader }{ - {"not a WriterTo", Reader(nil)}, + {"not a WriterTo", io.Reader(nil)}, {"a WriterTo", struct { - Reader - WriterTo + io.Reader + io.WriterTo }{}}, } { - nc := NopCloser(tc.r) + nc := io.NopCloser(tc.r) - _, expected := tc.r.(WriterTo) - _, got := nc.(WriterTo) + _, expected := tc.r.(io.WriterTo) + _, got := nc.(io.WriterTo) if expected != got { t.Errorf("NopCloser incorrectly forwards WriterTo for %s, got %t want %t", tc.Name, got, expected) }