Skip to content

Commit

Permalink
enable golangci-lint for test files as well
Browse files Browse the repository at this point in the history
  • Loading branch information
libotony committed Feb 1, 2024
1 parent 871427b commit 167d73f
Show file tree
Hide file tree
Showing 32 changed files with 76 additions and 167 deletions.
20 changes: 0 additions & 20 deletions .golangci.pull-request.yml

This file was deleted.

12 changes: 9 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Please refer to the official golangci-lint config documentation for more details:
# https://golangci-lint.run/usage/configuration/
# https://github.com/golangci/golangci-lint/blob/master/.golangci.reference.yml
run:
timeout: 10m
tests: true
# default is true. Enables skipping of directories:
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
skip-dirs-use-default: true

linters:
disable-all: true
Expand Down Expand Up @@ -28,9 +34,9 @@ linters:
# - makezero # false positives
# - nilerr # several intentional

run:
timeout: 10m
tests: false
linters-settings:
gofmt:
simplify: true

issues:
max-issues-per-linter: 1000
Expand Down
1 change: 0 additions & 1 deletion abi/abi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,5 @@ func TestABI(t *testing.T) {
assert.Nil(t, err)

assert.Equal(t, value, d)

}
}
74 changes: 36 additions & 38 deletions api/accounts/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package accounts_test
import (
"bytes"
"encoding/json"
"io/ioutil"
"io"
"math/big"
"net/http"
"net/http/httptest"
Expand All @@ -32,16 +32,16 @@ import (
"github.com/vechain/thor/v2/tx"
)

var sol = ` pragma solidity ^0.4.18;
contract Test {
uint8 value;
function add(uint8 a,uint8 b) public pure returns(uint8) {
return a+b;
}
function set(uint8 v) public {
value = v;
}
}`
// pragma solidity ^0.4.18;
// contract Test {
// uint8 value;
// function add(uint8 a,uint8 b) public pure returns(uint8) {
// return a+b;
// }
// function set(uint8 v) public {
// value = v;
// }
// }

var abiJSON = `[
{
Expand Down Expand Up @@ -111,32 +111,31 @@ func TestAccount(t *testing.T) {
}

func getAccount(t *testing.T) {
res, statusCode := httpGet(t, ts.URL+"/accounts/"+invalidAddr)
_, statusCode := httpGet(t, ts.URL+"/accounts/"+invalidAddr)
assert.Equal(t, http.StatusBadRequest, statusCode, "bad address")

res, statusCode = httpGet(t, ts.URL+"/accounts/"+addr.String()+"?revision="+invalidNumberRevision)
_, statusCode = httpGet(t, ts.URL+"/accounts/"+addr.String()+"?revision="+invalidNumberRevision)
assert.Equal(t, http.StatusBadRequest, statusCode, "bad revision")

//revision is optional defaut `best`
res, statusCode = httpGet(t, ts.URL+"/accounts/"+addr.String())
res, statusCode := httpGet(t, ts.URL+"/accounts/"+addr.String())
var acc accounts.Account
if err := json.Unmarshal(res, &acc); err != nil {
t.Fatal(err)
}
assert.Equal(t, math.HexOrDecimal256(*value), acc.Balance, "balance should be equal")
assert.Equal(t, http.StatusOK, statusCode, "OK")

}

func getCode(t *testing.T) {
res, statusCode := httpGet(t, ts.URL+"/accounts/"+invalidAddr+"/code")
_, statusCode := httpGet(t, ts.URL+"/accounts/"+invalidAddr+"/code")
assert.Equal(t, http.StatusBadRequest, statusCode, "bad address")

res, statusCode = httpGet(t, ts.URL+"/accounts/"+contractAddr.String()+"/code?revision="+invalidNumberRevision)
_, statusCode = httpGet(t, ts.URL+"/accounts/"+contractAddr.String()+"/code?revision="+invalidNumberRevision)
assert.Equal(t, http.StatusBadRequest, statusCode, "bad revision")

//revision is optional defaut `best`
res, statusCode = httpGet(t, ts.URL+"/accounts/"+contractAddr.String()+"/code")
res, statusCode := httpGet(t, ts.URL+"/accounts/"+contractAddr.String()+"/code")
var code map[string]string
if err := json.Unmarshal(res, &code); err != nil {
t.Fatal(err)
Expand All @@ -150,17 +149,17 @@ func getCode(t *testing.T) {
}

func getStorage(t *testing.T) {
res, statusCode := httpGet(t, ts.URL+"/accounts/"+invalidAddr+"/storage/"+storageKey.String())
_, statusCode := httpGet(t, ts.URL+"/accounts/"+invalidAddr+"/storage/"+storageKey.String())
assert.Equal(t, http.StatusBadRequest, statusCode, "bad address")

res, statusCode = httpGet(t, ts.URL+"/accounts/"+contractAddr.String()+"/storage/"+invalidBytes32)
_, statusCode = httpGet(t, ts.URL+"/accounts/"+contractAddr.String()+"/storage/"+invalidBytes32)
assert.Equal(t, http.StatusBadRequest, statusCode, "bad storage key")

res, statusCode = httpGet(t, ts.URL+"/accounts/"+contractAddr.String()+"/storage/"+storageKey.String()+"?revision="+invalidNumberRevision)
_, statusCode = httpGet(t, ts.URL+"/accounts/"+contractAddr.String()+"/storage/"+storageKey.String()+"?revision="+invalidNumberRevision)
assert.Equal(t, http.StatusBadRequest, statusCode, "bad revision")

//revision is optional defaut `best`
res, statusCode = httpGet(t, ts.URL+"/accounts/"+contractAddr.String()+"/storage/"+storageKey.String())
res, statusCode := httpGet(t, ts.URL+"/accounts/"+contractAddr.String()+"/storage/"+storageKey.String())
var value map[string]string
if err := json.Unmarshal(res, &value); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -190,9 +189,9 @@ func initAccountServer(t *testing.T) {
packTx(repo, stater, transaction, t)

method := "set"
abi, err := ABI.New([]byte(abiJSON))
abi, _ := ABI.New([]byte(abiJSON))
m, _ := abi.MethodByName(method)
input, err := m.EncodeInput(uint8(storageValue))
input, err := m.EncodeInput(storageValue)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -252,41 +251,40 @@ func deployContractWithCall(t *testing.T) {
Gas: 10000000,
Data: "abc",
}
res, statusCode := httpPost(t, ts.URL+"/accounts", badBody)
_, statusCode := httpPost(t, ts.URL+"/accounts", badBody)
assert.Equal(t, http.StatusBadRequest, statusCode, "bad data")

reqBody := &accounts.CallData{
Gas: 10000000,
Data: hexutil.Encode(bytecode),
}

res, statusCode = httpPost(t, ts.URL+"/accounts?revision="+invalidNumberRevision, reqBody)
_, statusCode = httpPost(t, ts.URL+"/accounts?revision="+invalidNumberRevision, reqBody)
assert.Equal(t, http.StatusBadRequest, statusCode, "bad revision")

//revision is optional defaut `best`
res, statusCode = httpPost(t, ts.URL+"/accounts", reqBody)
res, _ := httpPost(t, ts.URL+"/accounts", reqBody)
var output *accounts.CallResult
if err := json.Unmarshal(res, &output); err != nil {
t.Fatal(err)
}
assert.False(t, output.Reverted)

}

func callContract(t *testing.T) {
res, statusCode := httpPost(t, ts.URL+"/accounts/"+invalidAddr, nil)
_, statusCode := httpPost(t, ts.URL+"/accounts/"+invalidAddr, nil)
assert.Equal(t, http.StatusBadRequest, statusCode, "invalid address")

badBody := &accounts.CallData{
Data: "input",
}
res, statusCode = httpPost(t, ts.URL+"/accounts/"+contractAddr.String(), badBody)
_, statusCode = httpPost(t, ts.URL+"/accounts/"+contractAddr.String(), badBody)
assert.Equal(t, http.StatusBadRequest, statusCode, "invalid input data")

a := uint8(1)
b := uint8(2)
method := "add"
abi, err := ABI.New([]byte(abiJSON))
abi, _ := ABI.New([]byte(abiJSON))
m, _ := abi.MethodByName(method)
input, err := m.EncodeInput(a, b)
if err != nil {
Expand All @@ -295,7 +293,7 @@ func callContract(t *testing.T) {
reqBody := &accounts.CallData{
Data: hexutil.Encode(input),
}
res, statusCode = httpPost(t, ts.URL+"/accounts/"+contractAddr.String(), reqBody)
res, statusCode := httpPost(t, ts.URL+"/accounts/"+contractAddr.String(), reqBody)
var output *accounts.CallResult
if err = json.Unmarshal(res, &output); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -327,13 +325,13 @@ func batchCall(t *testing.T) {
Value: nil,
}},
}
res, statusCode := httpPost(t, ts.URL+"/accounts/*", badBody)
_, statusCode := httpPost(t, ts.URL+"/accounts/*", badBody)
assert.Equal(t, http.StatusBadRequest, statusCode, "invalid data")

a := uint8(1)
b := uint8(2)
method := "add"
abi, err := ABI.New([]byte(abiJSON))
abi, _ := ABI.New([]byte(abiJSON))
m, _ := abi.MethodByName(method)
input, err := m.EncodeInput(a, b)
if err != nil {
Expand All @@ -353,10 +351,10 @@ func batchCall(t *testing.T) {
}},
}

res, statusCode = httpPost(t, ts.URL+"/accounts/*?revision="+invalidNumberRevision, badBody)
_, statusCode = httpPost(t, ts.URL+"/accounts/*?revision="+invalidNumberRevision, badBody)
assert.Equal(t, http.StatusBadRequest, statusCode, "invalid revision")

res, statusCode = httpPost(t, ts.URL+"/accounts/*", reqBody)
res, statusCode := httpPost(t, ts.URL+"/accounts/*", reqBody)
var results accounts.BatchCallResults
if err = json.Unmarshal(res, &results); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -399,7 +397,7 @@ func httpPost(t *testing.T, url string, body interface{}) ([]byte, int) {
if err != nil {
t.Fatal(err)
}
r, err := ioutil.ReadAll(res.Body)
r, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Fatal(err)
Expand All @@ -412,7 +410,7 @@ func httpGet(t *testing.T, url string) ([]byte, int) {
if err != nil {
t.Fatal(err)
}
r, err := ioutil.ReadAll(res.Body)
r, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Fatal(err)
Expand Down
17 changes: 5 additions & 12 deletions api/blocks/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package blocks

import (
"encoding/json"
"io/ioutil"
"io"
"math/big"
"net/http"
"net/http/httptest"
Expand All @@ -28,11 +28,6 @@ import (
"github.com/vechain/thor/v2/tx"
)

const (
testAddress = "56e81f171bcc55a6ff8345e692c0f86e5b48e01a"
testPrivHex = "efa321f290811731036e5eccd373114e5186d9fe419081f5a607231279d5ef01"
)

var blk *block.Block
var ts *httptest.Server

Expand All @@ -43,13 +38,13 @@ func TestBlock(t *testing.T) {
initBlockServer(t)
defer ts.Close()
//invalid block id
res, statusCode := httpGet(t, ts.URL+"/blocks/"+invalidBytes32)
_, statusCode := httpGet(t, ts.URL+"/blocks/"+invalidBytes32)
assert.Equal(t, http.StatusBadRequest, statusCode)
//invalid block number
res, statusCode = httpGet(t, ts.URL+"/blocks/"+invalidNumberRevision)
_, statusCode = httpGet(t, ts.URL+"/blocks/"+invalidNumberRevision)
assert.Equal(t, http.StatusBadRequest, statusCode)

res, statusCode = httpGet(t, ts.URL+"/blocks/"+blk.Header().ID().String())
res, statusCode := httpGet(t, ts.URL+"/blocks/"+blk.Header().ID().String())
rb := new(JSONCollapsedBlock)
if err := json.Unmarshal(res, rb); err != nil {
t.Fatal(err)
Expand All @@ -70,7 +65,6 @@ func TestBlock(t *testing.T) {
}
checkBlock(t, blk, rb)
assert.Equal(t, http.StatusOK, statusCode)

}

func initBlockServer(t *testing.T) {
Expand Down Expand Up @@ -145,15 +139,14 @@ func checkBlock(t *testing.T, expBl *block.Block, actBl *JSONCollapsedBlock) {
for i, tx := range expBl.Transactions() {
assert.Equal(t, tx.ID(), actBl.Transactions[i], "txid should be equal")
}

}

func httpGet(t *testing.T, url string) ([]byte, int) {
res, err := http.Get(url)
if err != nil {
t.Fatal(err)
}
r, err := ioutil.ReadAll(res.Body)
r, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Fatal(err)
Expand Down
4 changes: 2 additions & 2 deletions api/node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package node_test

import (
"encoding/json"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -60,7 +60,7 @@ func httpGet(t *testing.T, url string) []byte {
if err != nil {
t.Fatal(err)
}
r, err := ioutil.ReadAll(res.Body)
r, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Fatal(err)
Expand Down
10 changes: 4 additions & 6 deletions api/transactions/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package transactions_test
import (
"bytes"
"encoding/json"
"io/ioutil"
"io"
"math/big"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -69,7 +69,7 @@ func getTxReceipt(t *testing.T) {
if err := json.Unmarshal(r, &receipt); err != nil {
t.Fatal(err)
}
assert.Equal(t, uint64(receipt.GasUsed), transaction.Gas(), "gas should be equal")
assert.Equal(t, receipt.GasUsed, transaction.Gas(), "gas should be equal")
}

func senTx(t *testing.T) {
Expand Down Expand Up @@ -111,7 +111,7 @@ func httpPost(t *testing.T, url string, obj interface{}) []byte {
if err != nil {
t.Fatal(err)
}
r, err := ioutil.ReadAll(res.Body)
r, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -172,7 +172,6 @@ func initTransactionServer(t *testing.T) {
router := mux.NewRouter()
transactions.New(repo, txpool.New(repo, stater, txpool.Options{Limit: 10000, LimitPerAccount: 16, MaxLifetime: 10 * time.Minute})).Mount(router, "/transactions")
ts = httptest.NewServer(router)

}

func checkTx(t *testing.T, expectedTx *tx.Transaction, actualTx *transactions.Transaction) {
Expand All @@ -189,15 +188,14 @@ func checkTx(t *testing.T, expectedTx *tx.Transaction, actualTx *transactions.Tr
assert.Equal(t, *c.Value(), big.Int(actualTx.Clauses[i].Value))
assert.Equal(t, c.To(), actualTx.Clauses[i].To)
}

}

func httpGet(t *testing.T, url string) []byte {
res, err := http.Get(url)
if err != nil {
t.Fatal(err)
}
r, err := ioutil.ReadAll(res.Body)
r, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Fatal(err)
Expand Down
Loading

0 comments on commit 167d73f

Please sign in to comment.