github.com/scaleway/scaleway-cli@v1.11.1/pkg/cli/main_test.go (about)

     1  package cli
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"os"
     7  	"os/exec"
     8  	"testing"
     9  
    10  	"github.com/scaleway/scaleway-cli/pkg/commands"
    11  	. "github.com/smartystreets/goconvey/convey"
    12  )
    13  
    14  func testHelpOutput(out string, err string) {
    15  	// headers & footers
    16  	So(out, ShouldContainSubstring, "Usage: scw [OPTIONS] COMMAND [arg...]")
    17  	So(out, ShouldContainSubstring, "Interact with Scaleway from the command line.")
    18  	So(out, ShouldContainSubstring, "Run 'scw COMMAND --help' for more information on a command.")
    19  
    20  	// options
    21  	So(out, ShouldContainSubstring, "Options:")
    22  	for _, option := range publicOptions {
    23  		So(out, ShouldContainSubstring, " "+option)
    24  	}
    25  
    26  	// public commands
    27  	So(out, ShouldContainSubstring, "Commands:")
    28  	for _, command := range publicCommands {
    29  		So(out, ShouldContainSubstring, "    "+command)
    30  	}
    31  
    32  	// secret commands
    33  	for _, command := range secretCommands {
    34  		So(out, ShouldNotContainSubstring, "    "+command)
    35  	}
    36  
    37  	// :lipstick:
    38  	/*
    39  		for _, line := range strings.Split(out, "\n") {
    40  			So(line, shouldFitInTerminal)
    41  		}
    42  	*/
    43  
    44  	// FIXME: count amount of options/commands, and panic if amount is different
    45  
    46  	// Testing stderr
    47  	So(err, ShouldEqual, "")
    48  }
    49  
    50  func testHelpCommandOutput(command string, out string, err string) {
    51  	// Header
    52  	So(out, ShouldContainSubstring, fmt.Sprintf("Usage: scw %s", command))
    53  	// FIXME: test description
    54  	// FIXME: test parameters
    55  
    56  	// Options
    57  	So(out, ShouldContainSubstring, "Options:")
    58  	So(out, ShouldContainSubstring, " -h, --help=false")
    59  	// FIXME: test other options
    60  
    61  	// Examples
    62  	// FIXME: todo
    63  	//So(out, ShouldContainSubstring, "Examples:")
    64  
    65  	// :lipstick:
    66  	/*
    67  		for _, line := range strings.Split(out, "\n") {
    68  			So(line, shouldFitInTerminal)
    69  		}
    70  	*/
    71  
    72  }
    73  
    74  func TestHelp(t *testing.T) {
    75  	Convey("Testing golang' `start(\"help\", ...)`", t, func() {
    76  		Convey("start(\"help\")", func() {
    77  			stdout := bytes.Buffer{}
    78  			stderr := bytes.Buffer{}
    79  			streams := commands.Streams{
    80  				Stdin:  os.Stdin,
    81  				Stdout: &stdout,
    82  				Stderr: &stderr,
    83  			}
    84  			ec, err := Start([]string{"help"}, &streams)
    85  
    86  			So(ec, ShouldEqual, 0)
    87  			So(err, ShouldBeNil)
    88  			testHelpOutput(stdout.String(), stderr.String())
    89  		})
    90  
    91  		cmds := append(publicCommands, secretCommands...)
    92  		for _, command := range cmds {
    93  			// FIXME: test 'start(COMMAND, "--help")'
    94  			if command == "help" {
    95  				continue
    96  			}
    97  
    98  			Convey(fmt.Sprintf("start(\"help\", \"%s\")", command), func() {
    99  				stdout := bytes.Buffer{}
   100  				stderr := bytes.Buffer{}
   101  				streams := commands.Streams{
   102  					Stdin:  os.Stdin,
   103  					Stdout: &stdout,
   104  					Stderr: &stderr,
   105  				}
   106  				ec, err := Start([]string{"help", command}, &streams)
   107  
   108  				So(ec, ShouldEqual, 1)
   109  				So(err, ShouldBeNil)
   110  				testHelpCommandOutput(command, stdout.String(), stderr.String())
   111  
   112  				// secondary help usage
   113  				// FIXME: should check for 'scw login' first
   114  				/*
   115  					if command != "help" {
   116  						// FIXME: this should works for "help" too
   117  						secondaryStdout := bytes.Buffer{}
   118  						secondaryStderr := bytes.Buffer{}
   119  						secondaryStreams := commands.Streams{
   120  							Stdin:  os.Stdin,
   121  							Stdout: &secondaryStdout,
   122  							Stderr: &secondaryStderr,
   123  						}
   124  						secondEc, secondErr := Start([]string{command, "--help"}, &secondaryStreams)
   125  						So(ec, ShouldEqual, secondEc)
   126  						//So(outStdout, ShouldEqual, secondOut)
   127  						So(fmt.Sprintf("%v", err), ShouldEqual, fmt.Sprintf("%v", secondErr))
   128  					}
   129  				*/
   130  
   131  			})
   132  		}
   133  	})
   134  
   135  	Convey("Testing shell' `scw help`", t, func() {
   136  		Convey("scw help", func() {
   137  			cmd := exec.Command(scwcli, "help")
   138  			out, ec, err := runCommandWithOutput(cmd)
   139  			stderr := "" // FIXME: get real stderr
   140  
   141  			// exit code
   142  			So(ec, ShouldEqual, 0)
   143  			So(err, ShouldBeNil)
   144  
   145  			// streams
   146  			testHelpOutput(out, stderr)
   147  		})
   148  
   149  		cmds := append(publicCommands, secretCommands...)
   150  		for _, command := range cmds {
   151  			// FIXME: test 'scw COMMAND --help'
   152  
   153  			Convey(fmt.Sprintf("scw help %s", command), func() {
   154  				cmd := exec.Command(scwcli, "help", command)
   155  				out, ec, err := runCommandWithOutput(cmd)
   156  				stderr := "" // FIXME: get real stderr
   157  
   158  				// exit code
   159  				So(ec, ShouldEqual, 1)
   160  				So(fmt.Sprintf("%s", err), ShouldEqual, "exit status 1")
   161  
   162  				// streams
   163  				testHelpCommandOutput(command, out, stderr)
   164  
   165  				// secondary help usage
   166  				// FIXME: should check for 'scw login' first
   167  				/*
   168  					if command != "help" {
   169  						// FIXME: this should works for "help" too
   170  						secondCmd := exec.Command(scwcli, command, "--help")
   171  						secondOut, secondEc, secondErr := runCommandWithOutput(secondCmd)
   172  						So(out, ShouldEqual, secondOut)
   173  						So(ec, ShouldEqual, secondEc)
   174  						So(fmt.Sprintf("%v", err), ShouldEqual, fmt.Sprintf("%v", secondErr))
   175  					}
   176  				*/
   177  			})
   178  		}
   179  		Convey("Unknown command", func() {
   180  			cmd := exec.Command(scwcli, "boogie")
   181  			out, ec, err := runCommandWithOutput(cmd)
   182  			So(out, ShouldContainSubstring, "scw: unknown subcommand boogie")
   183  			So(ec, ShouldEqual, 1)
   184  			So(fmt.Sprintf("%s", err), ShouldEqual, "exit status 1")
   185  		})
   186  	})
   187  }