github.com/robgonnella/ardi/v2@v2.4.5-0.20230102052001-11a49de978c3/commands/help_test.go (about)

     1  package commands_test
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"os"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/robgonnella/ardi/v2/commands"
    11  	"github.com/spf13/cobra"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestHelpCommand(t *testing.T) {
    16  	t.Run("should wrap lines to 80 char", func(st *testing.T) {
    17  		originalOut := os.Stdout
    18  		r, w, _ := os.Pipe()
    19  
    20  		st.Cleanup(func() {
    21  			os.Stdout = originalOut
    22  			w.Close()
    23  			r.Close()
    24  		})
    25  
    26  		os.Stdout = w
    27  		cmd := &cobra.Command{
    28  			Use:     "somecmd [args]",
    29  			Short:   "Longer than 80char Longer than 80char Longer than 80char Longer than 80char Longer than 80char Longer than 80char Longer than 80char Longer than 80char",
    30  			Long:    "\nLonger than 80char Longer than 80char Longer than 80char Longer than 80char Longer than 80char Longer than 80char Longer than 80char Longer than 80char Longer than 80char",
    31  			Aliases: []string{"many", "aliases", "so", "what", "about", "even", "more", "how", "can", "we", "get", "words", "to", "continue", "forever", "end", "ever", "without", "repeating"},
    32  			Run:     func(cmd *cobra.Command, args []string) {},
    33  		}
    34  
    35  		commands.Help(cmd, []string{})
    36  		w.Close()
    37  
    38  		var buf bytes.Buffer
    39  		io.Copy(&buf, r)
    40  		r.Close()
    41  
    42  		split := strings.Split(buf.String(), "\n")
    43  		for _, line := range split {
    44  			assert.True(st, len(line) <= 80)
    45  		}
    46  	})
    47  }