github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/fly/ui/table_test.go (about)

     1  package ui_test
     2  
     3  import (
     4  	"io"
     5  	"runtime"
     6  	"sort"
     7  
     8  	"github.com/pf-qiu/concourse/v6/fly/pty"
     9  	. "github.com/pf-qiu/concourse/v6/fly/ui"
    10  	"github.com/fatih/color"
    11  
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  	"github.com/onsi/gomega/gbytes"
    15  )
    16  
    17  var _ = Describe("Table", func() {
    18  	var table Table
    19  
    20  	BeforeEach(func() {
    21  		table = Table{
    22  			Headers: TableRow{
    23  				{Contents: "column1", Color: color.New(color.Bold)},
    24  				{Contents: "column2", Color: color.New(color.Bold)},
    25  			},
    26  			Data: []TableRow{
    27  				{
    28  					{Contents: "r1c1"},
    29  					{Contents: "r1c2"},
    30  				},
    31  				{
    32  					{Contents: "r2c1"},
    33  					{Contents: "r2c2"},
    34  				},
    35  				{
    36  					{Contents: "r3c1"},
    37  					{Contents: "r3c2"},
    38  				},
    39  			},
    40  		}
    41  	})
    42  
    43  	Describe("Sort", func() {
    44  		Context("when rows are provided", func() {
    45  			BeforeEach(func() {
    46  				table = Table{
    47  					Headers: TableRow{
    48  						{Contents: "column1", Color: color.New(color.Bold)},
    49  						{Contents: "column2", Color: color.New(color.Bold)},
    50  					},
    51  					Data: []TableRow{
    52  						{
    53  							{Contents: "zzz"},
    54  							{Contents: "bbb"},
    55  						},
    56  						{
    57  							{Contents: "aaa"},
    58  							{Contents: "zzz"},
    59  						},
    60  						{
    61  							{Contents: "xxx"},
    62  							{Contents: "aaa"},
    63  						},
    64  					},
    65  				}
    66  			})
    67  
    68  			It("sorts them on the given column index", func() {
    69  				expectedOutput := "" +
    70  					"aaa  zzz\n" +
    71  					"xxx  aaa\n" +
    72  					"zzz  bbb\n"
    73  
    74  				buf := gbytes.NewBuffer()
    75  
    76  				sort.Sort(table.Data)
    77  
    78  				err := table.Render(buf, false)
    79  				Expect(err).ToNot(HaveOccurred())
    80  
    81  				Expect(string(buf.Contents())).To(Equal(expectedOutput))
    82  			})
    83  		})
    84  	})
    85  
    86  	Describe("Render", func() {
    87  		Context("when the render method is called without a TTY", func() {
    88  			It("prints the data with no headers", func() {
    89  				expectedOutput := "" +
    90  					"r1c1  r1c2\n" +
    91  					"r2c1  r2c2\n" +
    92  					"r3c1  r3c2\n"
    93  
    94  				buf := gbytes.NewBuffer()
    95  
    96  				err := table.Render(buf, false)
    97  				Expect(err).ToNot(HaveOccurred())
    98  
    99  				Expect(string(buf.Contents())).To(Equal(expectedOutput))
   100  			})
   101  		})
   102  
   103  		Context("when the render method is called without a TTY but with print headers flag", func() {
   104  			It("prints the headers and the data without color", func() {
   105  				buf := gbytes.NewBuffer()
   106  
   107  				err := table.Render(buf, true)
   108  				Expect(err).ToNot(HaveOccurred())
   109  
   110  				expectedOutput := "" +
   111  					"column1  column2\n" +
   112  					"r1c1     r1c2   \n" +
   113  					"r2c1     r2c2   \n" +
   114  					"r3c1     r3c2   \n"
   115  
   116  				Expect(string(buf.Contents())).To(Equal(expectedOutput))
   117  			})
   118  		})
   119  
   120  		Context("when the render method is called in a TTY", func() {
   121  			It("prints the headers and the data in color", func() {
   122  				if runtime.GOOS == "windows" {
   123  					Skip("these escape codes, and the pty stuff, don't apply to Windows")
   124  				}
   125  
   126  				pty, err := pty.Open()
   127  				Expect(err).NotTo(HaveOccurred())
   128  
   129  				defer pty.Close()
   130  
   131  				buf := gbytes.NewBuffer()
   132  
   133  				go io.Copy(buf, pty.PTYR)
   134  
   135  				err = table.Render(pty.TTYW, false)
   136  				Expect(err).ToNot(HaveOccurred())
   137  
   138  				expectedOutput := "" +
   139  					"\x1b[1mcolumn1\x1b[0m  \x1b[1mcolumn2\x1b[0m\r\n" +
   140  					"r1c1     r1c2   \r\n" +
   141  					"r2c1     r2c2   \r\n" +
   142  					"r3c1     r3c2   \r\n"
   143  
   144  				Eventually(buf.Contents).Should(Equal([]byte(expectedOutput)))
   145  			})
   146  		})
   147  	})
   148  })