github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/cmd/ledgerutil/main_test.go (about)

     1  /*
     2  Copyright hechain. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package main
     8  
     9  import (
    10  	"os/exec"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/onsi/gomega"
    15  	"github.com/onsi/gomega/gexec"
    16  )
    17  
    18  func TestArguments(t *testing.T) {
    19  	testCases := map[string]struct {
    20  		exitCode int
    21  		args     []string
    22  	}{
    23  		"ledger": {
    24  			exitCode: 0,
    25  			args:     []string{},
    26  		},
    27  		"ledger-help": {
    28  			exitCode: 0,
    29  			args:     []string{"--help"},
    30  		},
    31  		"compare-help": {
    32  			exitCode: 0,
    33  			args:     []string{"compare", "--help"},
    34  		},
    35  		"compare": {
    36  			exitCode: 1,
    37  			args:     []string{"compare"},
    38  		},
    39  		"one-snapshot": {
    40  			exitCode: 1,
    41  			args:     []string{"compare, snapshotDir1"},
    42  		},
    43  	}
    44  
    45  	// Build ledger binary
    46  	gt := gomega.NewWithT(t)
    47  	ledgerutil, err := gexec.Build("github.com/hechain20/hechain/cmd/ledgerutil")
    48  	gt.Expect(err).NotTo(gomega.HaveOccurred())
    49  	defer gexec.CleanupBuildArtifacts()
    50  
    51  	for testName, testCase := range testCases {
    52  		t.Run(testName, func(t *testing.T) {
    53  			cmd := exec.Command(ledgerutil, testCase.args...)
    54  			session, err := gexec.Start(cmd, nil, nil)
    55  			gt.Expect(err).NotTo(gomega.HaveOccurred())
    56  			gt.Eventually(session, 5*time.Second).Should(gexec.Exit(testCase.exitCode))
    57  		})
    58  	}
    59  }