-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
129 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newSyncL1Cmd(endBlock *uint64, run func(*cobra.Command, []string) error) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "syncl1 <end-block-number> [flags]", | ||
Short: "sync the state from L1 using Juno's database format.", | ||
Args: cobra.ExactArgs(1), | ||
PreRunE: func(_ *cobra.Command, args []string) error { | ||
var err error | ||
*endBlock, err = strconv.ParseUint(args[0], 10, 64) | ||
if err != nil { | ||
return fmt.Errorf("parse end block number: %v", err) | ||
} | ||
return nil | ||
}, | ||
RunE: run, | ||
} | ||
return cmd | ||
} |
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,54 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewSyncL1Cmd(t *testing.T) { | ||
tests := map[string]struct { | ||
args []string | ||
want int // if negative, expect error | ||
}{ | ||
"no args": { | ||
args: []string{}, | ||
want: -1, | ||
}, | ||
"single empty arg": { | ||
args: []string{""}, | ||
want: -1, | ||
}, | ||
"single negative arg": { | ||
args: []string{"-1"}, | ||
want: -1, | ||
}, | ||
"two args": { | ||
args: []string{"1", "1"}, | ||
want: -1, | ||
}, | ||
"zero": { | ||
args: []string{"0"}, | ||
want: 0, | ||
}, | ||
"large number": { | ||
args: []string{"1232882"}, | ||
want: 1232882, | ||
}, | ||
} | ||
|
||
for description, test := range tests { | ||
t.Run(description, func(t *testing.T) { | ||
var endBlock uint64 | ||
cmd := newSyncL1Cmd(&endBlock, func(_ *cobra.Command, _ []string) error { return nil }) | ||
cmd.SetArgs(test.args) | ||
err := cmd.Execute() | ||
if test.want < 0 { | ||
require.Error(t, err) | ||
return | ||
} | ||
require.Equal(t, uint64(test.want), endBlock) | ||
}) | ||
} | ||
} |