Skip to content

Commit

Permalink
Merge pull request #22 from initia-labs/feat/coverage-cmd
Browse files Browse the repository at this point in the history
feat: add coverage cmd
  • Loading branch information
beer-1 authored Dec 5, 2023
2 parents 4888e13 + 93e0345 commit 7c56f5e
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 46 deletions.
226 changes: 208 additions & 18 deletions cmd/initiad/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"github.com/initia-labs/initiavm/api"
"github.com/initia-labs/initiavm/types/compiler"
buildtypes "github.com/initia-labs/initiavm/types/compiler/build"
coveragetypes "github.com/initia-labs/initiavm/types/compiler/coverage"
docgentypes "github.com/initia-labs/initiavm/types/compiler/docgen"
provetypes "github.com/initia-labs/initiavm/types/compiler/prove"
testtypes "github.com/initia-labs/initiavm/types/compiler/test"
"github.com/pelletier/go-toml"
Expand Down Expand Up @@ -74,14 +76,25 @@ const (
flagVerbosity = "verbosity"
// verify options
flagVerify = "verify"
// coverage options
flagFunctions = "functions"
flagOutputCSV = "output-csv"
// docgen
flagIncludeImpl = "include-impl"
flagIncludeSpecs = "include-specs"
flagSpecsInlined = "specs-inlined"
flagIncludeDepDiagram = "include-dep-diagram"
flagCollapsedSections = "collapsed-sections"
flagLandingPageTemplate = "landing-page-template"
flagReferencesFile = "references-file"
)

const (
defaultPackagePath = "."
defaultInstallDir = "."
)

func moveCommand() *cobra.Command {
func MoveCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "move",
Short: "move subcommands",
Expand All @@ -98,8 +111,24 @@ func moveCommand() *cobra.Command {
moveDeployCmd(),
moveProveCmd(),
moveVerifyCmd(),
moveDocgenCmd(),
)

coverageCmd := &cobra.Command{
Use: "coverage",
Short: "coverage subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
coverageCmd.AddCommand(
moveCoverageSummaryCmd(),
moveCoverageSourceCmd(),
moveCoverageBytecodeCmd(),
)

cmd.AddCommand(coverageCmd)

//initiaapp.ModuleBasics.AddQueryCommands(cmd)
cmd.PersistentFlags().String(flags.FlagChainID, "", "The network chain ID")

Expand Down Expand Up @@ -162,6 +191,91 @@ func moveTestCmd() *cobra.Command {
return cmd
}

func moveCoverageSummaryCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "summary [flags]",
Short: "Display a coverage summary for all modules in this package",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
arg, err := getInitiaCompilerArgument(cmd)
if err != nil {
return err
}

config, err := getCoverageSummaryConfig(cmd)
if err != nil {
return err
}

_, err = api.CoverageSummary(*arg, *config)
if err != nil {
return err
}

return nil
},
}

addMoveBuildFlags(cmd)
addMoveCoverageSummaryFlags(cmd)

return cmd
}

func moveCoverageSourceCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "source [module-name] [flags]",
Short: "Display coverage information about the module against source code",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
arg, err := getInitiaCompilerArgument(cmd)
if err != nil {
return err
}

_, err = api.CoverageSource(*arg, coveragetypes.CoverageSourceConfig{
ModuleName: args[0],
})
if err != nil {
return err
}

return nil
},
}

addMoveBuildFlags(cmd)

return cmd
}

func moveCoverageBytecodeCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "bytecode [module-name] [flags]",
Short: "Display coverage information about the module against disassembled bytecode",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
arg, err := getInitiaCompilerArgument(cmd)
if err != nil {
return err
}

_, err = api.CoverageBytecode(*arg, coveragetypes.CoverageBytecodeConfig{
ModuleName: args[0],
})
if err != nil {
return err
}

return nil
},
}

addMoveBuildFlags(cmd)

return cmd
}

func moveNewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "new <NAME>",
Expand Down Expand Up @@ -384,6 +498,37 @@ func moveVerifyCmd() *cobra.Command {
return cmd
}

func moveDocgenCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "document [flags]",
Short: "Generate documents of a move package",
Long: "Generate documents of a move package. The provided path must specify the path of move package to generate docs",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
arg, err := getInitiaCompilerArgument(cmd)
if err != nil {
return err
}
dgc, err := getDocgenConfig(cmd)
if err != nil {
return err
}

_, err = api.Docgen(*arg, *dgc)
if err != nil {
return err
}

return nil
},
}

addMoveBuildFlags(cmd)
addMoveDocgenFlags(cmd)

return cmd
}

func addMoveBuildFlags(cmd *cobra.Command) {
cmd.Flags().StringP(flagPackagePath, flagPackagePathShorthand, defaultPackagePath, "Path to a package which the command should be run with respect to")
cmd.Flags().Bool(flagGenerateABI, false, "Generate ABIs for packages")
Expand All @@ -408,11 +553,13 @@ func addMoveTestFlags(cmd *cobra.Command) {
cmd.Flags().StringP(flagFilter, flagFilterShorthand, "", `A filter string to determine which unit tests to run. A unit test will be run only if it
contains this string in its fully qualified (<addr>::<module_name>::<fn_name>) name`)
cmd.Flags().Bool(flagReportStorageOnError, false, "Show the storage state at the end of execution of a failing test")
cmd.Flags().Uint64P(flagGasLimit, flagGasLimitShorthand, testtypes.DefaultGasLimit, "Bound the number of instructions that can be executed by any one test")
cmd.Flags().Bool(flagIgnoreCompileWarnings, false, "Ignore compiler's warning, and continue run tests")
cmd.Flags().BoolP(flagList, flagListShorthand, false, "List all tests")
cmd.Flags().BoolP(flagReportStatistics, flagReportStatisticsShorthand, false, "Report test statistics at the end of testing")
cmd.Flags().UintP(flagNumThreads, flagNumThreadsShorthand, testtypes.DefaultNumThreads, "Number of threads to use for running tests")
}

func addMoveCoverageSummaryFlags(cmd *cobra.Command) {
cmd.Flags().Bool(flagFunctions, true, "Whether function coverage summaries should be displayed")
cmd.Flags().Bool(flagOutputCSV, true, "Output CSV data of coverage")
}

func addMoveCleanFlags(cmd *cobra.Command) {
Expand All @@ -439,6 +586,16 @@ func addMoveProveFlags(cmd *cobra.Command) {
cmd.Flags().StringP(flagVerbosity, flagVerboseShorthand, "", "Verbosity level")
}

func addMoveDocgenFlags(cmd *cobra.Command) {
cmd.Flags().Bool(flagIncludeImpl, false, "Whether to include private declarations and implementations into the generated documentation.")
cmd.Flags().Bool(flagIncludeSpecs, false, "Whether to include specifications in the generated documentation.")
cmd.Flags().Bool(flagSpecsInlined, false, "Whether specifications should be put side-by-side with declarations or into a separate section.")
cmd.Flags().Bool(flagIncludeDepDiagram, false, "Whether to include a dependency diagram.")
cmd.Flags().Bool(flagCollapsedSections, false, "Whether details should be put into collapsed sections. This is not supported by all markdown, but the github dialect.")
cmd.Flags().String(flagLandingPageTemplate, "", "Package-relative path to an optional markdown template which is a used to create a landing page.")
cmd.Flags().String(flagReferencesFile, "", "Package-relative path to a file whose content is added to each generated markdown file.")
}

func getInitiaCompilerArgument(cmd *cobra.Command) (*compiler.InitiaCompilerArgument, error) {
bc, err := getBuildConfig(cmd)
if err != nil {
Expand Down Expand Up @@ -509,8 +666,6 @@ func getTestConfig(cmd *cobra.Command) (*testtypes.TestConfig, error) {
boolFlags[flagReportStatistics] = testtypes.WithReportStatistics()
boolFlags[flagReportStorageOnError] = testtypes.WithReportStorageOnError()
boolFlags[flagIgnoreCompileWarnings] = testtypes.WithIgnoreCompileWarnings()
boolFlags[flagList] = testtypes.WithList()
boolFlags[flagVerbose] = testtypes.WithVerboseTestConfig()

for fn, opt := range boolFlags {
flag, err := cmd.Flags().GetBool(fn)
Expand All @@ -530,24 +685,23 @@ func getTestConfig(cmd *cobra.Command) (*testtypes.TestConfig, error) {
options = append(options, testtypes.WithFilter(filter))
}

gasLimit, err := cmd.Flags().GetUint64(flagGasLimit)
tc := testtypes.NewTestConfig(options...)
return &tc, nil
}

func getCoverageSummaryConfig(cmd *cobra.Command) (*coveragetypes.CoverageSummaryConfig, error) {
functions, err := cmd.Flags().GetBool(flagFunctions)
if err != nil {
return nil, err
}
if filter != "" {
options = append(options, testtypes.WithGasLimit(gasLimit))
}

n, err := cmd.Flags().GetUint(flagNumThreads)
outputCSV, err := cmd.Flags().GetBool(flagOutputCSV)
if err != nil {
return nil, err
}
if filter != "" {
options = append(options, testtypes.WithNumThreads(n))
}

tc := testtypes.NewTestConfig(options...)
return &tc, nil
return &coveragetypes.CoverageSummaryConfig{
Functions: functions,
OutputCSV: outputCSV,
}, nil
}

func getProveConfig(cmd *cobra.Command) (*provetypes.ProveConfig, error) {
Expand Down Expand Up @@ -611,3 +765,39 @@ func getProveConfig(cmd *cobra.Command) (*provetypes.ProveConfig, error) {
pc := provetypes.NewProveConfig(options...)
return &pc, nil
}

func getDocgenConfig(cmd *cobra.Command) (*docgentypes.DocgenConfig, error) {
options := []func(*docgentypes.DocgenConfig){}

boolFlags := map[string]func(*docgentypes.DocgenConfig){}
boolFlags[flagIncludeImpl] = docgentypes.WithIncludeImpl()
boolFlags[flagIncludeSpecs] = docgentypes.WithIncludeSpecs()
boolFlags[flagSpecsInlined] = docgentypes.WithSpecsInlined()
boolFlags[flagIncludeDepDiagram] = docgentypes.WithIncludeDepDiagram()
boolFlags[flagCollapsedSections] = docgentypes.WithCollapsedSections()

for fn, opt := range boolFlags {
flag, err := cmd.Flags().GetBool(fn)
if err != nil {
return nil, err
}
if flag {
options = append(options, opt)
}
}

if landingPageTemplate, err := cmd.Flags().GetString(flagLandingPageTemplate); err != nil {
return nil, err
} else if landingPageTemplate != "" {
options = append(options, docgentypes.WithLandingPageTemplate(landingPageTemplate))
}

if referencesFile, err := cmd.Flags().GetString(flagReferencesFile); err != nil {
return nil, err
} else if referencesFile != "" {
options = append(options, docgentypes.WithReferencesFile(referencesFile))
}

dgc := docgentypes.NewDocgenConfig(options...)
return &dgc, nil
}
2 changes: 1 addition & 1 deletion cmd/initiad/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {
rootCmd.AddCommand(rosettaCmd.RosettaCommand(encodingConfig.InterfaceRegistry, encodingConfig.Marshaler))

// add move commands
rootCmd.AddCommand(moveCommand())
rootCmd.AddCommand(MoveCommand())
}

func addModuleInitFlags(startCmd *cobra.Command) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require (
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/initia-labs/OPinit v0.1.0-beta.2
// we also need to update `LIBINITIAVM_VERSION` of images/private/Dockerfile#5
github.com/initia-labs/initiavm v0.1.2-beta.3
github.com/initia-labs/initiavm v0.1.2-beta.4
github.com/novifinancial/serde-reflection/serde-generate/runtime/golang v0.0.0-20220519162058-e5cd3c3b3f3a
github.com/pelletier/go-toml v1.9.5
github.com/pkg/errors v0.9.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -784,8 +784,8 @@ github.com/initia-labs/OPinit v0.1.0-beta.2 h1:JTtgkPWg/11FrWqnNx6Wl/DBMafco++6o
github.com/initia-labs/OPinit v0.1.0-beta.2/go.mod h1:MPnFU2x4xzeT3L8K3lc0wkOl09o/oekEabPHIwWdqJg=
github.com/initia-labs/cosmos-sdk v0.47.7-0.20231123105913-ebb56e6fbd16 h1:QoF6V/9XvEuaThvP/jM0MvHlwVe3kn9/hvTPMXH/1ro=
github.com/initia-labs/cosmos-sdk v0.47.7-0.20231123105913-ebb56e6fbd16/go.mod h1:xTc1chW8HyUWCfrgGbjS5jNu9RzlPVrBNfbL9RmZUio=
github.com/initia-labs/initiavm v0.1.2-beta.3 h1:+KTrSZIV3355I+KeRhX4LEex3M7enro0oWSjLZtzZGA=
github.com/initia-labs/initiavm v0.1.2-beta.3/go.mod h1:aQt4lImZWF9xj7Fm978n0IoeDdKWXTg1CSq6O4WoSD8=
github.com/initia-labs/initiavm v0.1.2-beta.4 h1:RTBJ2hl8DSwLTi7rWx15beslfaqLm/bnEzaEFxsuGZ8=
github.com/initia-labs/initiavm v0.1.2-beta.4/go.mod h1:aQt4lImZWF9xj7Fm978n0IoeDdKWXTg1CSq6O4WoSD8=
github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc=
github.com/jedisct1/go-minisign v0.0.0-20190909160543-45766022959e/go.mod h1:G1CVv03EnqU1wYL2dFwXxW2An0az9JTl/ZsqXQeBlkU=
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
Expand Down
2 changes: 1 addition & 1 deletion images/private/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM golang:1.19.1-alpine3.16 AS go-builder
#ARG arch=x86_64

# See https://github.com/initia-labs/initiavm/releases
ARG LIBINITIAVM_VERSION=v0.1.2-beta.3
ARG LIBINITIAVM_VERSION=v0.1.2-beta.4

# this comes from standard alpine nightly file
# https://github.com/rust-lang/docker-rust-nightly/blob/master/alpine3.12/Dockerfile
Expand Down
4 changes: 2 additions & 2 deletions x/move/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ Example of args: address:0x1 bool:true u8:0 string:hello vector<u32>:a,b,c,d
Example:
$ %s query move view \
%s1lwjmdnks33xwnmfayc64ycprww49n33mtm92ne \
BasicCoin \
getBalance \
ManagedCoin \
get_balance \
--type-args '0x1::native_uinit::Coin 0x1::native_uusdc::Coin' \
--args 'u8:0 address:0x1 string:"hello world"'
`, version.AppName, bech32PrefixAccAddr,
Expand Down
4 changes: 2 additions & 2 deletions x/move/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ Example of args: address:0x1 bool:true u8:0 string:hello vector<u32>:a,b,c,d
Example:
$ %s tx move execute \
%s1lwjmdnks33xwnmfayc64ycprww49n33mtm92ne \
BasicCoin \
getBalance \
ManagedCoin \
mint_to \
--type-args '0x1::native_uinit::Coin 0x1::native_uusdc::Coin' \
--args 'u8:0 address:0x1 string:"hello world"'
`, version.AppName, bech32PrefixAccAddr,
Expand Down
Loading

0 comments on commit 7c56f5e

Please sign in to comment.