forked from ignite/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(template): scaffold genesis validation tests (ignite#1489)
* Initialize test template * Add placeholder * Genesis test patch * map test case * Small fix * list genesis tests * Small fix * Template fixes * lint template * Lint * Small fix template * Small fix template * Valid case tests * Lint * docs: update changelog * Fix issue with ibc Co-authored-by: Danilo Pantani <[email protected]> Co-authored-by: Denis Fadeev <[email protected]>
- Loading branch information
1 parent
1b65dfb
commit 4a6aea6
Showing
13 changed files
with
316 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package scaffolder | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/gobuffalo/genny" | ||
"github.com/tendermint/starport/starport/pkg/placeholder" | ||
modulecreate "github.com/tendermint/starport/starport/templates/module/create" | ||
) | ||
|
||
// supportGenesisTests checks if types/genesis_test.go exists | ||
// appends the generator to create the file if it doesn't | ||
func supportGenesisTests( | ||
gens []*genny.Generator, | ||
appPath, | ||
appName, | ||
modulePath, | ||
moduleName string, | ||
) ([]*genny.Generator, error) { | ||
filepath, err := filepath.Abs(filepath.Join(appPath, "x", moduleName, "types", "genesis_test.go")) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if _, err := os.Stat(filepath); os.IsNotExist(err) { | ||
g, err := modulecreate.AddGenesisTest(appName, modulePath, moduleName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
gens = append(gens, g) | ||
} | ||
return gens, err | ||
} | ||
|
||
// supportMsgServer checks if the module supports the MsgServer convention | ||
// appends the generator to support it if it doesn't | ||
// https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-031-msg-service.md | ||
func supportMsgServer( | ||
gens []*genny.Generator, | ||
replacer placeholder.Replacer, | ||
appPath string, | ||
opts *modulecreate.MsgServerOptions, | ||
) ([]*genny.Generator, error) { | ||
// Check if convention used | ||
msgServerDefined, err := isMsgServerDefined(appPath, opts.ModuleName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if !msgServerDefined { | ||
// Patch the module to support the convention | ||
g, err := modulecreate.AddMsgServerConventionToLegacyModule(replacer, opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
gens = append(gens, g) | ||
} | ||
return gens, nil | ||
} | ||
|
||
// isMsgServerDefined checks if the module uses the MsgServer convention for transactions | ||
// this is checked by verifying the existence of the tx.proto file | ||
func isMsgServerDefined(appPath, moduleName string) (bool, error) { | ||
txProto, err := filepath.Abs(filepath.Join(appPath, "proto", moduleName, "tx.proto")) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
if _, err := os.Stat(txProto); os.IsNotExist(err) { | ||
return false, nil | ||
} | ||
return true, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package modulecreate | ||
|
||
import ( | ||
"github.com/gobuffalo/genny" | ||
"github.com/gobuffalo/plush" | ||
"github.com/gobuffalo/plushgen" | ||
) | ||
|
||
// AddGenesisTest returns the generator to generate genesis_test.go | ||
func AddGenesisTest(appName, modulePath, moduleName string) (*genny.Generator, error) { | ||
g := genny.New() | ||
if err := g.Box(genesisTestTemplate); err != nil { | ||
return g, err | ||
} | ||
ctx := plush.NewContext() | ||
ctx.Set("moduleName", moduleName) | ||
ctx.Set("modulePath", modulePath) | ||
ctx.Set("appName", appName) | ||
|
||
g.Transformer(plushgen.Transformer(ctx)) | ||
g.Transformer(genny.Replace("{{moduleName}}", moduleName)) | ||
return g, nil | ||
} |
39 changes: 39 additions & 0 deletions
39
starport/templates/module/create/genesistest/x/{{moduleName}}/types/genesis_test.go.plush
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package types_test | ||
|
||
import ( | ||
"testing" | ||
"github.com/stretchr/testify/require" | ||
"<%= modulePath %>/x/<%= moduleName %>/types" | ||
) | ||
|
||
func TestGenesisState_Validate(t *testing.T) { | ||
for _, tc := range []struct { | ||
desc string | ||
genState *types.GenesisState | ||
valid bool | ||
} { | ||
{ | ||
desc: "default is valid", | ||
genState: types.DefaultGenesis(), | ||
valid: true, | ||
}, | ||
{ | ||
desc: "valid genesis state", | ||
genState: &types.GenesisState{ | ||
// this line is used by starport scaffolding # types/genesis/validField | ||
}, | ||
valid: true, | ||
}, | ||
// this line is used by starport scaffolding # types/genesis/testcase | ||
} { | ||
tc := tc | ||
t.Run(tc.desc, func(t *testing.T) { | ||
err := tc.genState.Validate() | ||
if tc.valid { | ||
require.NoError(t, err) | ||
} else { | ||
require.Error(t, err) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.