github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zcli/cli_test.go (about)

     1  package zcli
     2  
     3  import (
     4  	"flag"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/sohaha/zlsgo"
     9  )
    10  
    11  func TestCli(T *testing.T) {
    12  	oldOsExit := osExit
    13  	defer func() { osExit = oldOsExit }()
    14  	myExit := func(code int) {
    15  	}
    16  	osExit = myExit
    17  	t := zlsgo.NewTest(T)
    18  	// resetForTesting("-debug")
    19  	Logo = `
    20  ________  ____  .__   .__
    21  \___   /_/ ___\ |  |  |__|
    22   /    / \  \___ |  |  |  |
    23  /_____ \ \___  >|  |__|  |
    24        \/     \/ |____/|__|`
    25  	Version = "1.0.1"
    26  	Add("run", "run", &testCmd{})
    27  	Run(func() {
    28  		t.Log("Run", *globalDebug)
    29  	})
    30  }
    31  
    32  func TestCli2(T *testing.T) {
    33  	oldOsExit := osExit
    34  	defer func() { osExit = oldOsExit }()
    35  	myExit := func(code int) {
    36  	}
    37  	osExit = myExit
    38  	resetForTesting("-debug")
    39  	Run()
    40  }
    41  
    42  func TestVersion(T *testing.T) {
    43  	oldOsExit := osExit
    44  	defer func() { osExit = oldOsExit }()
    45  	myExit := func(code int) {
    46  	}
    47  	osExit = myExit
    48  	t := zlsgo.NewTest(T)
    49  	resetForTesting("-version")
    50  	Version = "1.0.1"
    51  	Run(func() {
    52  		current, b := Current()
    53  		t.Equal(nil, current)
    54  		t.EqualTrue(!b)
    55  		t.Log("Run", *globalDebug)
    56  	})
    57  }
    58  
    59  func TestCliOther(t *testing.T) {
    60  	testOther(t)
    61  }
    62  
    63  func TestCliCommand(t *testing.T) {
    64  	tt := zlsgo.NewTest(t)
    65  	oldOsExit := osExit
    66  	defer func() { osExit = oldOsExit }()
    67  	myExit := func(code int) {
    68  		t.Log("myExit:", code)
    69  	}
    70  	osExit = myExit
    71  	requiredFlags = []string{}
    72  	resetForTesting("test", "-flag1")
    73  	Add("test", "test", &testCmd{
    74  		tt: tt,
    75  	})
    76  	Run()
    77  	showFlags(flag.CommandLine)
    78  }
    79  
    80  func TestCliCommandErr(_ *testing.T) {
    81  	oldOsExit := osExit
    82  	defer func() { osExit = oldOsExit }()
    83  	myExit := func(code int) {
    84  	}
    85  	osExit = myExit
    86  	requiredFlags = []string{}
    87  	resetForTesting("test")
    88  	Add("test", "test", &testCmd{})
    89  	Run()
    90  }
    91  
    92  func TestCliCommandHelp(t *testing.T) {
    93  	expectedName := "gopher"
    94  	requiredFlags = []string{}
    95  	resetForTesting("testHelp", "-help")
    96  	matchingCmd := Add("testHelp", "test", &testCmd{})
    97  	expectedErrorHandling := flag.ExitOnError
    98  	expectedOutput := os.Stdout
    99  	parseSubcommand(flag.Args())
   100  	flag.CommandLine.Init(expectedName, expectedErrorHandling)
   101  	flag.CommandLine.SetOutput(expectedOutput)
   102  	showSubcommandUsage(flag.CommandLine, matchingCmd)
   103  	showFlags(flag.CommandLine)
   104  }
   105  
   106  func TestCliCommandHelp2(t *testing.T) {
   107  	oldOsExit := osExit
   108  	defer func() { osExit = oldOsExit }()
   109  	myExit := func(code int) {
   110  		t.Log("myExit:", code)
   111  	}
   112  	osExit = myExit
   113  	requiredFlags = []string{}
   114  	resetForTesting("test", "ddd", "-h")
   115  	Add("test", "test", &testCmd{})
   116  	Run()
   117  }
   118  
   119  func TestUnknown(_ *testing.T) {
   120  	oldOsExit := osExit
   121  	defer func() { osExit = oldOsExit }()
   122  	myExit := func(code int) {
   123  	}
   124  	osExit = myExit
   125  	resetForTesting("unknown")
   126  	Run()
   127  }
   128  
   129  func TestUnknown2(T *testing.T) {
   130  	t := zlsgo.NewTest(T)
   131  	oldOsExit := osExit
   132  	defer func() { osExit = oldOsExit }()
   133  
   134  	myExit := func(code int) {
   135  		t.Log(code)
   136  	}
   137  	osExit = myExit
   138  	SetUnknownCommand(func(name string) {
   139  		t.Log(name)
   140  	})
   141  	resetForTesting("unknown")
   142  	Run()
   143  }
   144  
   145  func TestInput(t *testing.T) {
   146  	Inputln("test:", false)
   147  }