11package cli_test
22
33import (
4+ "bytes"
5+ "context"
46 "fmt"
7+ "io"
8+ "os"
59 "testing"
610
11+ "github.com/spf13/cobra"
712 "github.com/stretchr/testify/suite"
813
914 "github.com/NibiruChain/nibiru/v2/app"
@@ -14,6 +19,14 @@ import (
1419 "github.com/NibiruChain/nibiru/v2/x/tokenfactory/types"
1520
1621 sdk "github.com/cosmos/cosmos-sdk/types"
22+
23+ rpcclientmock "github.com/cometbft/cometbft/rpc/client/mock"
24+ sdkclient "github.com/cosmos/cosmos-sdk/client"
25+ "github.com/cosmos/cosmos-sdk/crypto/keyring"
26+ svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
27+ sdktestutil "github.com/cosmos/cosmos-sdk/testutil"
28+ sdktestutilcli "github.com/cosmos/cosmos-sdk/testutil/cli"
29+ testutilmod "github.com/cosmos/cosmos-sdk/types/module/testutil"
1730)
1831
1932var (
@@ -30,6 +43,8 @@ type TestSuite struct {
3043}
3144
3245func TestIntegrationTestSuite (t * testing.T ) {
46+ suite .Run (t , new (CmdSuiteLite ))
47+
3348 testutil .RetrySuiteRunIfDbClosed (t , func () {
3449 suite .Run (t , new (TestSuite ))
3550 }, 2 )
@@ -196,3 +211,187 @@ func (s *TestSuite) TearDownSuite() {
196211 s .T ().Log ("tearing down integration test suite" )
197212 s .network .Cleanup ()
198213}
214+
215+ type CmdTestCase struct {
216+ name string
217+ args []string
218+ extraArgs []string
219+ wantErr string
220+ }
221+
222+ // Flags for broadcasting transactions
223+ func (s * CmdSuiteLite ) commonTxArgs () []string {
224+ return []string {
225+ "--yes=true" , // skip confirmation
226+ "--broadcast-mode=sync" ,
227+ "--fees=1unibi" ,
228+ "--chain-id=test-chain" ,
229+ }
230+ }
231+
232+ type CmdSuiteLite struct {
233+ suite.Suite
234+
235+ keyring keyring.Keyring
236+ testEncCfg testutilmod.TestEncodingConfig
237+
238+ testAcc sdktestutil.TestAccount
239+ }
240+
241+ func (s * CmdSuiteLite ) SetupSuite () {
242+ s .testEncCfg = testutilmod .TestEncodingConfig (app .MakeEncodingConfig ())
243+ s .keyring = keyring .NewInMemory (s .testEncCfg .Codec )
244+
245+ testAccs := sdktestutil .CreateKeyringAccounts (s .T (), s .keyring , 1 )
246+ s .testAcc = testAccs [0 ]
247+ }
248+
249+ func (s * CmdSuiteLite ) TestCmdSetDenomMetadata () {
250+ s .T ().Log (`Create a valid metadata file as "metadata.json"` )
251+ tempDir := s .T ().TempDir ()
252+ metadataFile , err := os .CreateTemp (tempDir , "metadata.json" )
253+ s .Require ().NoError (err )
254+ defer metadataFile .Close ()
255+
256+ _ , err = metadataFile .Write ([]byte (`
257+ {
258+ "description": "A short description of the token",
259+ "denom_units": [
260+ {
261+ "denom": "testdenom"
262+ },
263+ {
264+ "denom": "TEST",
265+ "exponent": 6
266+ }
267+ ],
268+ "base": "testdenom",
269+ "display": "TEST",
270+ "name": "Test Token",
271+ "symbol": "TEST"
272+ }` ),
273+ )
274+ s .Require ().NoError (err )
275+
276+ metadatFilePath := metadataFile .Name ()
277+
278+ testCases := []CmdTestCase {
279+ {
280+ name : "happy: set-denom-metadata" ,
281+ args : []string {
282+ "set-denom-metadata" ,
283+ metadatFilePath ,
284+ },
285+ extraArgs : []string {fmt .Sprintf ("--from=%s" , s .testAcc .Address )},
286+ wantErr : "" ,
287+ },
288+ {
289+ name : "happy: sudo-set-denom-metadata" ,
290+ args : []string {
291+ "sudo-set-denom-metadata" ,
292+ metadatFilePath ,
293+ },
294+ extraArgs : []string {fmt .Sprintf ("--from=%s" , s .testAcc .Address )},
295+ wantErr : "" ,
296+ },
297+ {
298+ name : "happy: template flag" ,
299+ args : []string {
300+ "set-denom-metadata" ,
301+ "args.json" ,
302+ "--template" ,
303+ },
304+ extraArgs : []string {},
305+ wantErr : "" ,
306+ },
307+ {
308+ name : "happy: template flag sudo" ,
309+ args : []string {
310+ "sudo-set-denom-metadata" ,
311+ "args.json" ,
312+ "--template" ,
313+ },
314+ extraArgs : []string {},
315+ wantErr : "" ,
316+ },
317+ {
318+ name : "sad: no FILE given" ,
319+ args : []string {
320+ "set-denom-metadata" ,
321+ },
322+ extraArgs : []string {fmt .Sprintf ("--from=%s" , s .testAcc .Address )},
323+ wantErr : "accepts 1 arg(s), received 0" ,
324+ },
325+ {
326+ name : "sad: file does not exist" ,
327+ args : []string {
328+ "set-denom-metadata" ,
329+ "not-a-file.json" ,
330+ },
331+ extraArgs : []string {fmt .Sprintf ("--from=%s" , s .testAcc .Address )},
332+ wantErr : "no such file or directory" ,
333+ },
334+ }
335+
336+ for _ , tc := range testCases {
337+ testOutput := new (bytes.Buffer )
338+ tc .RunTxCmd (
339+ s ,
340+ cli .NewTxCmd (),
341+ testOutput ,
342+ )
343+ }
344+ }
345+
346+ func (tc CmdTestCase ) NewCtx (s * CmdSuiteLite ) sdkclient.Context {
347+ return sdkclient.Context {}.
348+ WithKeyring (s .keyring ).
349+ WithTxConfig (s .testEncCfg .TxConfig ).
350+ WithCodec (s .testEncCfg .Codec ).
351+ WithClient (sdktestutilcli.MockTendermintRPC {Client : rpcclientmock.Client {}}).
352+ WithAccountRetriever (sdkclient.MockAccountRetriever {}).
353+ WithOutput (io .Discard ).
354+ WithChainID ("test-chain" )
355+ }
356+
357+ func (tc CmdTestCase ) RunTxCmd (s * CmdSuiteLite , txCmd * cobra.Command , output io.Writer ) {
358+ s .Run (tc .name , func () {
359+ ctx := svrcmd .CreateExecuteContext (context .Background ())
360+
361+ cmd := txCmd
362+ cmd .SetContext (ctx )
363+ cmd .SetOutput (output )
364+ args := append (tc .args , s .commonTxArgs ()... )
365+ cmd .SetArgs (append (args , tc .extraArgs ... ))
366+
367+ s .Require ().NoError (sdkclient .SetCmdClientContextHandler (tc .NewCtx (s ), cmd ))
368+
369+ err := cmd .Execute ()
370+ if tc .wantErr != "" {
371+ s .Require ().ErrorContains (err , tc .wantErr )
372+ return
373+ }
374+ s .Require ().NoError (err )
375+ })
376+ }
377+
378+ func (tc CmdTestCase ) RunQueryCmd (s * CmdSuiteLite , queryCmd * cobra.Command , output io.Writer ) {
379+ s .Run (tc .name , func () {
380+ ctx := svrcmd .CreateExecuteContext (context .Background ())
381+
382+ cmd := queryCmd
383+ cmd .SetContext (ctx )
384+ cmd .SetOutput (output )
385+ args := tc .args // don't append common tx args
386+ cmd .SetArgs (append (args , tc .extraArgs ... ))
387+
388+ s .Require ().NoError (sdkclient .SetCmdClientContextHandler (tc .NewCtx (s ), cmd ))
389+
390+ err := cmd .Execute ()
391+ if tc .wantErr != "" {
392+ s .Require ().ErrorContains (err , tc .wantErr )
393+ return
394+ }
395+ s .Require ().NoError (err )
396+ })
397+ }
0 commit comments