Skip to content

Commit

Permalink
test(commands): setup simple integration framework
Browse files Browse the repository at this point in the history
* test(commands): setup simple integration framework

* chore(ci): enable codecov

* docs(readme): update badges

* chore(ci): add caching to circleci

* chore: fix linting errors

* address first round of cr

* chore(ci): add codecov config

* fix broken import

* fixup tests and lint after rebase
  • Loading branch information
whyrusleeping authored and dignifiedquire committed Feb 28, 2018
1 parent 1be8d3f commit 6506f51
Show file tree
Hide file tree
Showing 39 changed files with 907 additions and 226 deletions.
28 changes: 24 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,32 @@ version: 2
jobs:
build:
docker:
- image: circleci/golang:1.9
- image: circleci/golang:1.10

working_directory: /go/src/github.com/filecoin-project/go-filecoin
steps:
- checkout

- run: go run ./build/*.go deps
- run: go run ./build/*.go test -cover
- run: go run ./build/*.go lint
- restore_cache:
key: v1-go-deps-{{ arch }}-{{ checksum "build/main.go" }}-{{ checksum "package.json" }}

- run:
name: Install Dependencies
command: go run ./build/*.go deps

- save_cache:
key: v1-go-deps-{{ arch }}-{{ checksum "build/main.go" }}-{{ checksum "package.json" }}
paths:
- "/go/pkg"
- "/go/src/gx" # gx deps
- run:
name: Build
command: go run ./build/*.go build
- run:
name: Test
command: |
go run ./build/*.go test -covermode=atomic -coverprofile=coverage.txt &&
bash <(curl -s https://codecov.io/bash)
- run:
name: Lint
command: go run ./build/*.go lint
6 changes: 6 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
coverage:
range: 70..100
round: down
precision: 2

comment: false
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
go-filecoin
.task
*.coverprofile
*.coverprofile
*.out
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# go-filecoin

[![CircleCI](https://circleci.com/gh/filecoin-project/go-filecoin.svg?style=svg&circle-token=5a9d1cb48788b41d98bdfbc8b15298816ec71fea)](https://circleci.com/gh/filecoin-project/go-filecoin) [![Build status](https://ci.appveyor.com/api/projects/status/27ee44u7ets5je5m?svg=true)](https://ci.appveyor.com/project/FilecoinAdmin/go-filecoin)
[![codecov](https://codecov.io/gh/filecoin-project/go-filecoin/branch/master/graph/badge.svg?token=J5QWYWkgHT)](https://codecov.io/gh/filecoin-project/go-filecoin)
[![CircleCI](https://circleci.com/gh/filecoin-project/go-filecoin.svg?style=svg&circle-token=5a9d1cb48788b41d98bdfbc8b15298816ec71fea)](https://circleci.com/gh/filecoin-project/go-filecoin)

> Filecoin Implementation in Go
Expand Down
24 changes: 8 additions & 16 deletions commands/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var addrsNewCmd = &cmds.Command{
fcn := GetNode(env)
re.Emit(&addressResult{fcn.Wallet.NewAddress().String()}) // nolint: errcheck
},
Type: addressResult{},
Type: &addressResult{},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, a *addressResult) error {
_, err := fmt.Fprintln(w, a.Address)
Expand All @@ -52,22 +52,15 @@ var addrsNewCmd = &cmds.Command{
var addrsListCmd = &cmds.Command{
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) {
fcn := GetNode(env)
var out []addressResult
for _, a := range fcn.Wallet.GetAddresses() {
out = append(out, addressResult{a.String()})
re.Emit(&addressResult{a.String()}) // nolint: errcheck
}
re.Emit(out) // nolint: errcheck
},
Type: []addressResult{},
Type: &addressResult{},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, addrs []addressResult) error {
for _, a := range addrs {
_, err := fmt.Fprintln(w, a.Address)
if err != nil {
return err
}
}
return nil
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, addr *addressResult) error {
_, err := fmt.Fprintln(w, addr.Address)
return err
}),
},
}
Expand Down Expand Up @@ -104,11 +97,10 @@ var balanceCmd = &cmds.Command{

re.Emit(act.Balance) // nolint: errcheck
},
Type: big.Int{},
Type: &big.Int{},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, b *big.Int) error {
_, err := fmt.Fprintln(w, b.String())
return err
return PrintString(w, b)
}),
},
}
68 changes: 45 additions & 23 deletions commands/address_test.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,60 @@
package commands

import (
"fmt"
"strings"
"testing"

"github.com/filecoin-project/go-filecoin/types"
"github.com/stretchr/testify/assert"

"github.com/filecoin-project/go-filecoin/node"
"github.com/filecoin-project/go-filecoin/testhelpers"
"github.com/filecoin-project/go-filecoin/wallet"
)

func TestAddrsNew(t *testing.T) {
assert := assert.New(t)

nd := &node.Node{Wallet: wallet.New()}

out, err := testhelpers.RunCommand(addrsNewCmd, nil, nil, &Env{node: nd})
assert.NoError(err)

assert.NoError(out.HasLine(nd.Wallet.GetAddresses()[0].String()))
// makeAddr must be run inside a `withDaemon` context to have access to
// the default daemon.
func makeAddr(t *testing.T) string {
t.Helper()
outNew := runSuccess(t, "go-filecoin wallet addrs new")
addr := strings.Trim(outNew.ReadStdout(), "\n")
assert.NotEmpty(t, addr)
return addr
}

func TestAddrsList(t *testing.T) {
func TestAddrsNewAndList(t *testing.T) {
assert := assert.New(t)

nd := &node.Node{Wallet: wallet.New()}
a1 := nd.Wallet.NewAddress()
a2 := nd.Wallet.NewAddress()
a3 := nd.Wallet.NewAddress()
daemon := withDaemon(func() {
addrs := make([]string, 10)
for i := 0; i < 10; i++ {
addrs[i] = makeAddr(t)
}

outList := runSuccess(t, "go-filecoin wallet addrs list")
list := outList.ReadStdout()

for _, addr := range addrs {
assert.Contains(list, addr)
}
})
assert.NoError(daemon.Error)
assert.Equal(daemon.Code, 0)
}

out, err := testhelpers.RunCommand(addrsListCmd, nil, nil, &Env{node: nd})
assert.NoError(err)
func TestWalletBalance(t *testing.T) {
assert := assert.New(t)

assert.NoError(out.HasLine(a1.String()))
assert.NoError(out.HasLine(a2.String()))
assert.NoError(out.HasLine(a3.String()))
daemon := withDaemon(func() {
addr := makeAddr(t)

t.Log("[failure] not found")
balance := run(fmt.Sprintf("go-filecoin wallet balance %s", addr))
assert.Contains(balance.ReadStderr(), "not found")
assert.Equal(balance.Code, 1)
assert.Empty(balance.ReadStdout())

t.Log("[success] balance 100000")
balance = runSuccess(t, fmt.Sprintf("go-filecoin wallet balance %s", types.Address("filecoin")))
assert.Contains(balance.ReadStdout(), "100000")
})
assert.NoError(daemon.Error)
assert.Equal(daemon.Code, 0)
}
19 changes: 12 additions & 7 deletions commands/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import (
"github.com/filecoin-project/go-filecoin/node"
)

// exposed here, to be available during testing
var sigCh = make(chan os.Signal, 1)

var daemonCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Start a long-running daemon-process",
Expand All @@ -40,9 +43,9 @@ func daemonRun(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment)
return
}

fmt.Println("My peer ID is", fcn.Host.ID().Pretty())
re.Emit(fmt.Sprintf("My peer ID is %s", fcn.Host.ID().Pretty())) // nolint: errcheck
for _, a := range fcn.Host.Addrs() {
fmt.Println("Swarm listening on", a)
re.Emit(fmt.Sprintf("Swarm listening on: %s", a)) // nolint: errcheck
}

if err := runAPIAndWait(req.Context, fcn, api); err != nil {
Expand All @@ -66,20 +69,22 @@ func runAPIAndWait(ctx context.Context, node *node.Node, api string) error {

handler := cmdhttp.NewHandler(servenv, rootCmd, cfg)

sigc := make(chan os.Signal, 1)
signal.Notify(sigc, os.Interrupt)
defer signal.Stop(sigc)
signal.Notify(sigCh, os.Interrupt)
defer signal.Stop(sigCh)

apiserv := http.Server{
Addr: api,
Handler: handler,
}

go func() {
panic(http.ListenAndServe(api, handler))
err := apiserv.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
panic(err)
}
}()

<-sigc
<-sigCh
fmt.Println("Got interrupt, shutting down...")

// allow 5 seconds for clean shutdown. Ideally it would never take this long.
Expand Down
36 changes: 24 additions & 12 deletions commands/id_test.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
package commands

import (
"context"
"testing"

"github.com/stretchr/testify/assert"

"github.com/filecoin-project/go-filecoin/node"
"github.com/filecoin-project/go-filecoin/testhelpers"
)

func TestId(t *testing.T) {
assert := assert.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
nd, err := node.New(ctx)
assert.NoError(err)
defer nd.Stop()

out, err := testhelpers.RunCommand(idCmd, nil, nil, &Env{node: nd})
assert.NoError(err)
runWithDaemon("go-filecoin id", func(id *output) {
assert.NoError(id.Error)
assert.Equal(id.Code, 0)
assert.Empty(id.ReadStderr())

idContent := id.ReadStdout()
assert.Containsf(idContent, "/ip4/127.0.0.1/tcp/6000/ipfs/", "default addr")
assert.Contains(idContent, "ID")
})
}

func TestIdFormat(t *testing.T) {
assert := assert.New(t)

runWithDaemon("go-filecoin id --format=\"<id>\\t<aver>\\t<pver>\\t<pubkey>\\n<addrs>\"", func(id *output) {
assert.NoError(id.Error)
assert.Equal(id.Code, 0)
assert.Empty(id.ReadStderr())

assert.Contains(out.Raw, nd.Host.ID().Pretty())
idContent := id.ReadStdout()
assert.Contains(idContent, "\t")
assert.Contains(idContent, "\n")
assert.Containsf(idContent, "/ip4/127.0.0.1/tcp/6000/ipfs/", "default addr")
assert.NotContains(idContent, "ID")
})
}
8 changes: 1 addition & 7 deletions commands/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"

cmds "gx/ipfs/QmRv6ddf7gkiEgBs1LADv3vC1mkVGPZEfByoiiVybjE9Mc/go-ipfs-cmds"
cmdcli "gx/ipfs/QmRv6ddf7gkiEgBs1LADv3vC1mkVGPZEfByoiiVybjE9Mc/go-ipfs-cmds/cli"
cmdhttp "gx/ipfs/QmRv6ddf7gkiEgBs1LADv3vC1mkVGPZEfByoiiVybjE9Mc/go-ipfs-cmds/http"
cmdkit "gx/ipfs/QmceUdzxkimdYsgtX733uNgzf1DLHyBKN6ehGSp85ayppM/go-ipfs-cmdkit"
)
Expand Down Expand Up @@ -72,12 +71,7 @@ func init() {

// Run processes the arguments and stdin
func Run(args []string, stdin, stdout, stderr *os.File) (int, error) {
err := cmdcli.Run(context.Background(), rootCmd, args, stdin, stdout, stderr, buildEnv, makeExecutor)
if err != nil {
return 1, err
}

return 0, nil
return CliRun(context.Background(), rootCmd, args, stdin, stdout, stderr, buildEnv, makeExecutor)
}

func buildEnv(ctx context.Context, req *cmds.Request) (cmds.Environment, error) {
Expand Down
9 changes: 5 additions & 4 deletions commands/message.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package commands

import (
"fmt"
"io"
"math/big"

Expand Down Expand Up @@ -32,7 +31,10 @@ var sendMsgCmd = &cmds.Command{
return
}

val := req.Options["value"].(int)
val, ok := req.Options["value"].(int)
if !ok {
val = 0
}

from, _ := req.Options["from"].(string)
var fromAddr types.Address
Expand Down Expand Up @@ -69,8 +71,7 @@ var sendMsgCmd = &cmds.Command{
Type: cid.Cid{},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, c *cid.Cid) error {
_, err := fmt.Fprintln(w, c.String())
return err
return PrintString(w, c)
}),
},
}
Loading

0 comments on commit 6506f51

Please sign in to comment.